Commit 6f85fabe authored by Filipe de Lima Brito's avatar Filipe de Lima Brito

Send message using SDK

parent bee51600
......@@ -8,6 +8,7 @@ import chat.rocket.common.model.BaseRoom
import chat.rocket.common.util.ifNull
import chat.rocket.core.RocketChatClient
import chat.rocket.core.internal.rest.messages
import chat.rocket.core.internal.rest.sendMessage
import javax.inject.Inject
class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
......@@ -16,7 +17,7 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
factory: RocketChatClientFactory) {
private val client = factory.create(serverInteractor.get()!!)
fun messages(chatRoomId: String, chatRoomType: String, offset: Int = 0) {
fun loadMessages(chatRoomId: String, chatRoomType: String, offset: Int = 0) {
launchUI(strategy) {
view.showLoading()
try {
......@@ -33,4 +34,19 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
}
}
}
fun sendMessage(chatRoomId: String, text: String) {
launchUI(strategy) {
try {
val message = client.sendMessage(chatRoomId, text)
view.showSentMessage(message)
} catch (ex: Exception) {
ex.message?.let {
view.showMessage(it)
}.ifNull {
view.showGenericErrorMessage()
}
}
}
}
}
\ No newline at end of file
......@@ -13,4 +13,18 @@ interface ChatRoomView : LoadingView, MessageView {
* @param serverUrl The server URL.
*/
fun showMessages(dataSet: MutableList<Message>, serverUrl: String)
/**
* Send a message to a chat room.
*
* @param text The text to send.
*/
fun sendMessage(text: String)
/**
* Shows a (recent) message sent to a chat room.
* @param message The (recent) message sent to a chat room.
*/
fun showSentMessage(message: Message)
}
\ No newline at end of file
......@@ -35,6 +35,11 @@ class ChatRoomAdapter(private val context: Context,
notifyItemRangeInserted(previousDataSetSize, dataSet.size)
}
fun addItem(message: Message) {
dataSet.add(0, message)
notifyItemInserted(0)
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bind(message: Message) = with(itemView) {
......
......@@ -14,9 +14,11 @@ import chat.rocket.android.chatroom.presentation.ChatRoomView
import chat.rocket.android.helper.EndlessRecyclerViewScrollListener
import chat.rocket.android.util.inflate
import chat.rocket.android.util.setVisibility
import chat.rocket.android.util.textContent
import chat.rocket.core.model.Message
import dagger.android.support.AndroidSupportInjection
import kotlinx.android.synthetic.main.fragment_chat_rooms.*
import kotlinx.android.synthetic.main.fragment_chat_room.*
import kotlinx.android.synthetic.main.message_composer.*
import javax.inject.Inject
fun newInstance(chatRoomId: String, chatRoomName: String, chatRoomType: String, isChatRoomOpen: Boolean): Fragment {
......@@ -41,6 +43,7 @@ class ChatRoomFragment : Fragment(), ChatRoomView {
private lateinit var chatRoomName: String
private lateinit var chatRoomType: String
private var isChatRoomOpen: Boolean = true
private lateinit var adapter: ChatRoomAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
......@@ -61,28 +64,42 @@ class ChatRoomFragment : Fragment(), ChatRoomView {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
presenter.messages(chatRoomId, chatRoomType)
presenter.loadMessages(chatRoomId, chatRoomType)
setupComposer()
}
override fun showMessages(dataSet: MutableList<Message>, serverUrl: String) {
activity?.apply {
if (recycler_view.adapter == null) {
recycler_view.adapter = ChatRoomAdapter(this, dataSet, serverUrl)
adapter = ChatRoomAdapter(this, dataSet, serverUrl)
recycler_view.adapter = adapter
val linearLayoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, true)
recycler_view.layoutManager = linearLayoutManager
if (dataSet.size >= 30) {
recycler_view.addOnScrollListener(object : EndlessRecyclerViewScrollListener(linearLayoutManager) {
override fun onLoadMore(page: Int, totalItemsCount: Int, recyclerView: RecyclerView?) {
presenter.messages(chatRoomId, chatRoomType, page * 30)
presenter.loadMessages(chatRoomId, chatRoomType, page * 30)
}
})
}
} else {
(recycler_view.adapter as ChatRoomAdapter).addDataSet(dataSet)
adapter.addDataSet(dataSet)
}
}
}
override fun sendMessage(text: String) {
if (!text.isBlank()) {
presenter.sendMessage(chatRoomId, text)
}
}
override fun showSentMessage(message: Message) {
text_message.textContent = ""
adapter.addItem(message)
recycler_view.smoothScrollToPosition(0)
}
override fun showLoading() = view_loading.setVisibility(true)
override fun hideLoading() = view_loading.setVisibility(false)
......@@ -90,4 +107,13 @@ class ChatRoomFragment : Fragment(), ChatRoomView {
override fun showMessage(message: String) = Toast.makeText(activity, message, Toast.LENGTH_SHORT).show()
override fun showGenericErrorMessage() = showMessage(getString(R.string.msg_generic_error))
private fun setupComposer() {
if (isChatRoomOpen) {
text_send.setOnClickListener { sendMessage(text_message.textContent) }
} else {
text_room_is_read_only.setVisibility(true)
top_container.setVisibility(false)
}
}
}
\ No newline at end of file
......@@ -10,6 +10,17 @@
android:layout_height="1dp"
android:background="@color/colorDividerMessageComposer" />
<TextView
android:id="@+id/text_room_is_read_only"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="@color/white"
android:gravity="center"
android:text="@string/msg_this_room_is_read_only"
android:textColor="@color/black"
android:visibility="gone"
app:layout_constraintTop_toBottomOf="@+id/divider" />
<LinearLayout
android:id="@+id/top_container"
android:layout_width="match_parent"
......
......@@ -27,6 +27,7 @@
<string name="msg_invalid_2fa_code">Código 2FA inválido</string>
<string name="msg_yesterday">ontem</string>
<string name="msg_message">Messagem</string>
<string name="msg_this_room_is_read_only">Este chat é apenas de leitura</string>
<string name="msg_content_description_log_in_using_facebook">Fazer login através do Facebook</string>
<string name="msg_content_description_log_in_using_github">Fazer login através do Github</string>
<string name="msg_content_description_log_in_using_google">Fazer login através do Google</string>
......
......@@ -29,6 +29,7 @@
<string name="msg_more_than_ninety_nine_unread_messages" translatable="false">99+</string>
<string name="msg_yesterday">Yesterday</string>
<string name="msg_message">Message</string>
<string name="msg_this_room_is_read_only">This room is read only</string>
<string name="msg_content_description_log_in_using_facebook">Login using Facebook</string>
<string name="msg_content_description_log_in_using_github">Login using Github</string>
<string name="msg_content_description_log_in_using_google">Login using Google</string>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment