Commit 00278aaf authored by Leonardo Aramaki's avatar Leonardo Aramaki

Honor use real name setting

parent b0d03bea
......@@ -2,6 +2,7 @@ package chat.rocket.android.chatroom.presentation
import chat.rocket.android.core.lifecycle.CancelStrategy
import chat.rocket.android.server.domain.GetCurrentServerInteractor
import chat.rocket.android.server.domain.GetSettingsInteractor
import chat.rocket.android.server.infraestructure.RocketChatClientFactory
import chat.rocket.android.util.launchUI
import chat.rocket.common.model.BaseRoom
......@@ -11,6 +12,7 @@ import chat.rocket.core.internal.realtime.unsubscibre
import chat.rocket.core.internal.rest.messages
import chat.rocket.core.internal.rest.sendMessage
import chat.rocket.core.model.Message
import chat.rocket.core.model.Value
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.launch
import timber.log.Timber
......@@ -18,11 +20,17 @@ import javax.inject.Inject
class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
private val strategy: CancelStrategy,
getSettingsInteractor: GetSettingsInteractor,
private val serverInteractor: GetCurrentServerInteractor,
factory: RocketChatClientFactory) {
private val client = factory.create(serverInteractor.get()!!)
private val roomMessages = ArrayList<Message>()
private var subId: String? = null
private var settings: Map<String, Value<Any>>? = null
init {
settings = getSettingsInteractor.get(serverInteractor.get()!!)
}
fun loadMessages(chatRoomId: String, chatRoomType: String, offset: Int = 0) {
launchUI(strategy) {
......@@ -32,7 +40,7 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
synchronized(roomMessages) {
roomMessages.addAll(messages)
}
view.showMessages(messages, serverInteractor.get()!!)
view.showMessages(messages, serverInteractor.get()!!, settings)
} catch (ex: Exception) {
ex.printStackTrace()
ex.message?.let {
......
......@@ -3,6 +3,7 @@ package chat.rocket.android.chatroom.presentation
import chat.rocket.android.core.behaviours.LoadingView
import chat.rocket.android.core.behaviours.MessageView
import chat.rocket.core.model.Message
import chat.rocket.core.model.Value
interface ChatRoomView : LoadingView, MessageView {
......@@ -11,8 +12,9 @@ interface ChatRoomView : LoadingView, MessageView {
*
* @param dataSet The data set to show.
* @param serverUrl The server URL.
* @param settings The server settings.
*/
fun showMessages(dataSet: List<Message>, serverUrl: String)
fun showMessages(dataSet: List<Message>, serverUrl: String, settings: Map<String, Value<Any>>?)
/**
* Send a message to a chat room.
......
......@@ -9,17 +9,20 @@ import android.widget.ImageView
import android.widget.TextView
import chat.rocket.android.R
import chat.rocket.android.helper.UrlHelper
import chat.rocket.android.server.domain.USE_REALNAME
import chat.rocket.android.util.inflate
import chat.rocket.android.util.setVisibility
import chat.rocket.android.util.textContent
import chat.rocket.common.util.ifNull
import chat.rocket.core.model.Message
import chat.rocket.core.model.Value
import com.facebook.drawee.view.SimpleDraweeView
import kotlinx.android.synthetic.main.avatar.view.*
import kotlinx.android.synthetic.main.item_message.view.*
class ChatRoomAdapter(private val context: Context,
private val serverUrl: String) : RecyclerView.Adapter<ChatRoomAdapter.ViewHolder>() {
private val serverUrl: String,
private val settings: Map<String, Value<Any>>?) : RecyclerView.Adapter<ChatRoomAdapter.ViewHolder>() {
init {
setHasStableIds(true)
......@@ -74,12 +77,20 @@ class ChatRoomAdapter(private val context: Context,
imageUnknownAvatar.setVisibility(true)
}
private fun bindUserName(message: Message, textView: TextView) = message.sender?.username.let {
textView.textContent = it.toString()
}.ifNull {
textView.textContent = context.getString(R.string.msg_unknown)
}
private fun bindUserName(message: Message, textView: TextView) {
val useRealName = settings?.get(USE_REALNAME)?.value as Boolean
val realName = message.sender?.name
val username = message.sender?.username
val senderName = if (useRealName) realName else username
senderName.let {
// TODO: Fallback to username if real name happens to be null. ATM this could happen if the
// present message is a system message. We should handle that on the SDK
textView.textContent = if (senderName == null) username.toString() else it.toString()
}.ifNull {
textView.textContent = context.getString(R.string.msg_unknown)
}
}
private fun bindTime(message: Message, textView: TextView) {
textView.textContent = DateTimeHelper.getTime(DateTimeHelper.getLocalDateTime(message.timestamp))
}
......
......@@ -17,6 +17,7 @@ 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 chat.rocket.core.model.Value
import dagger.android.support.AndroidSupportInjection
import kotlinx.android.synthetic.main.fragment_chat_room.*
import kotlinx.android.synthetic.main.message_composer.*
......@@ -76,10 +77,10 @@ class ChatRoomFragment : Fragment(), ChatRoomView {
super.onDestroyView()
}
override fun showMessages(dataSet: List<Message>, serverUrl: String) {
override fun showMessages(dataSet: List<Message>, serverUrl: String, settings: Map<String, Value<Any>>?) {
activity?.apply {
if (recycler_view.adapter == null) {
adapter = ChatRoomAdapter(this, serverUrl)
adapter = ChatRoomAdapter(this, serverUrl, settings)
recycler_view.adapter = adapter
val linearLayoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, true)
recycler_view.layoutManager = linearLayoutManager
......
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