Commit 576b2a20 authored by Filipe de Lima Brito's avatar Filipe de Lima Brito

Fix pinned messages and favorite messages layouts.

parent 234f1a7c
...@@ -243,7 +243,9 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR ...@@ -243,7 +243,9 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR
if (recycler_view.adapter == null) { if (recycler_view.adapter == null) {
adapter = ChatRoomAdapter( adapter = ChatRoomAdapter(
chatRoomType, chatRoomName, presenter, chatRoomType,
chatRoomName,
presenter,
reactionListener = this@ChatRoomFragment reactionListener = this@ChatRoomFragment
) )
recycler_view.adapter = adapter recycler_view.adapter = adapter
......
...@@ -65,70 +65,75 @@ class ViewModelMapper @Inject constructor( ...@@ -65,70 +65,75 @@ class ViewModelMapper @Inject constructor(
private val currentUsername: String? = localRepository.get(LocalRepository.CURRENT_USERNAME_KEY) private val currentUsername: String? = localRepository.get(LocalRepository.CURRENT_USERNAME_KEY)
private val secondaryTextColor = ContextCompat.getColor(context, R.color.colorSecondaryText) private val secondaryTextColor = ContextCompat.getColor(context, R.color.colorSecondaryText)
suspend fun map(message: Message, roomViewModel: RoomViewModel = RoomViewModel( suspend fun map(
roles = emptyList(), isBroadcast = true)): List<BaseViewModel<*>> { message: Message,
roomViewModel: RoomViewModel = RoomViewModel(roles = emptyList(), isBroadcast = true)
): List<BaseViewModel<*>> {
return translate(message, roomViewModel) return translate(message, roomViewModel)
} }
suspend fun map(messages: List<Message>, roomViewModel: RoomViewModel = RoomViewModel( suspend fun map(
roles = emptyList(), isBroadcast = true)): List<BaseViewModel<*>> = withContext(CommonPool) { messages: List<Message>,
val list = ArrayList<BaseViewModel<*>>(messages.size) roomViewModel: RoomViewModel = RoomViewModel(roles = emptyList(), isBroadcast = true)
): List<BaseViewModel<*>> =
withContext(CommonPool) {
val list = ArrayList<BaseViewModel<*>>(messages.size)
messages.forEach { messages.forEach { list.addAll(translate(it, roomViewModel)) }
list.addAll(translate(it, roomViewModel)) return@withContext list
} }
return@withContext list private suspend fun translate(
} message: Message,
roomViewModel: RoomViewModel
private suspend fun translate(message: Message, roomViewModel: RoomViewModel) ): List<BaseViewModel<*>> =
: List<BaseViewModel<*>> = withContext(CommonPool) { withContext(CommonPool) {
val list = ArrayList<BaseViewModel<*>>() val list = ArrayList<BaseViewModel<*>>()
message.urls?.forEach { message.urls?.forEach {
val url = mapUrl(message, it) val url = mapUrl(message, it)
url?.let { list.add(url) } url?.let { list.add(url) }
} }
message.attachments?.forEach { message.attachments?.forEach {
val attachment = mapAttachment(message, it) val attachment = mapAttachment(message, it)
attachment?.let { list.add(attachment) } attachment?.let { list.add(attachment) }
} }
mapMessage(message).let { mapMessage(message).let {
if (list.isNotEmpty()) { if (list.isNotEmpty()) {
it.preview = list.first().preview it.preview = list.first().preview
}
list.add(it)
} }
list.add(it)
}
for (i in list.size - 1 downTo 0) { for (i in list.size - 1 downTo 0) {
val next = if (i - 1 < 0) null else list[i - 1] val next = if (i - 1 < 0) null else list[i - 1]
list[i].nextDownStreamMessage = next list[i].nextDownStreamMessage = next
} }
if (isBroadcastReplyAvailable(roomViewModel, message)) { if (isBroadcastReplyAvailable(roomViewModel, message)) {
roomsInteractor.getById(currentServer, message.roomId)?.let { chatRoom -> roomsInteractor.getById(currentServer, message.roomId)?.let { chatRoom ->
val replyViewModel = mapMessageReply(message, chatRoom) val replyViewModel = mapMessageReply(message, chatRoom)
list.first().nextDownStreamMessage = replyViewModel list.first().nextDownStreamMessage = replyViewModel
list.add(0, replyViewModel) list.add(0, replyViewModel)
}
} }
}
return@withContext list return@withContext list
} }
private fun isBroadcastReplyAvailable(roomViewModel: RoomViewModel, message: Message): Boolean { private fun isBroadcastReplyAvailable(roomViewModel: RoomViewModel, message: Message): Boolean {
val senderUsername = message.sender?.username val senderUsername = message.sender?.username
return roomViewModel.isRoom && roomViewModel.isBroadcast && return roomViewModel.isRoom && roomViewModel.isBroadcast &&
!message.isSystemMessage() && !message.isSystemMessage() &&
senderUsername != currentUsername senderUsername != currentUsername
} }
private fun mapMessageReply(message: Message, chatRoom: ChatRoom): MessageReplyViewModel { private fun mapMessageReply(message: Message, chatRoom: ChatRoom): MessageReplyViewModel {
val name = message.sender?.name val name = message.sender?.name
val roomName = if (settings.useRealName() && name != null) name else message.sender?.username val roomName =
?: "" if (settings.useRealName() && name != null) name else message.sender?.username ?: ""
val permalink = messageHelper.createPermalink(message, chatRoom) val permalink = messageHelper.createPermalink(message, chatRoom)
return MessageReplyViewModel( return MessageReplyViewModel(
messageId = message.id, messageId = message.id,
......
...@@ -25,9 +25,9 @@ class FavoriteMessagesPresenter @Inject constructor( ...@@ -25,9 +25,9 @@ class FavoriteMessagesPresenter @Inject constructor(
private var offset: Int = 0 private var offset: Int = 0
/** /**
* Loads all favorite messages for room. the given room id. * Loads all favorite messages for the given room id.
* *
* @param roomId The id of the room to get its favorite messages. * @param roomId The id of the room to get favorite messages from.
*/ */
fun loadFavoriteMessages(roomId: String) { fun loadFavoriteMessages(roomId: String) {
launchUI(strategy) { launchUI(strategy) {
......
...@@ -69,10 +69,11 @@ class FavoriteMessagesFragment : Fragment(), FavoriteMessagesView { ...@@ -69,10 +69,11 @@ class FavoriteMessagesFragment : Fragment(), FavoriteMessagesView {
adapter = ChatRoomAdapter(enableActions = false) adapter = ChatRoomAdapter(enableActions = false)
recycler_view.adapter = adapter recycler_view.adapter = adapter
val linearLayoutManager = val linearLayoutManager =
LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false) LinearLayoutManager(context, LinearLayoutManager.VERTICAL, true)
linearLayoutManager.stackFromEnd = true
recycler_view.layoutManager = linearLayoutManager recycler_view.layoutManager = linearLayoutManager
recycler_view.itemAnimator = DefaultItemAnimator() recycler_view.itemAnimator = DefaultItemAnimator()
if (favoriteMessages.size > 10) { if (favoriteMessages.size >= 30) {
recycler_view.addOnScrollListener(object : recycler_view.addOnScrollListener(object :
EndlessRecyclerViewScrollListener(linearLayoutManager) { EndlessRecyclerViewScrollListener(linearLayoutManager) {
override fun onLoadMore( override fun onLoadMore(
......
...@@ -26,7 +26,7 @@ class PinnedMessagesPresenter @Inject constructor( ...@@ -26,7 +26,7 @@ class PinnedMessagesPresenter @Inject constructor(
private var offset: Int = 0 private var offset: Int = 0
/** /**
* Load all pinned messages for the given room id. * Loads all pinned messages for the given room id.
* *
* @param roomId The id of the room to get pinned messages from. * @param roomId The id of the room to get pinned messages from.
*/ */
...@@ -36,8 +36,7 @@ class PinnedMessagesPresenter @Inject constructor( ...@@ -36,8 +36,7 @@ class PinnedMessagesPresenter @Inject constructor(
view.showLoading() view.showLoading()
roomsInteractor.getById(serverUrl, roomId)?.let { roomsInteractor.getById(serverUrl, roomId)?.let {
val pinnedMessages = client.getPinnedMessages(roomId, it.type, offset) val pinnedMessages = client.getPinnedMessages(roomId, it.type, offset)
val messageList = val messageList = mapper.map(pinnedMessages.result)
mapper.map(pinnedMessages.result.filterNot { it.isSystemMessage() })
view.showPinnedMessages(messageList) view.showPinnedMessages(messageList)
offset += 1 * 30 offset += 1 * 30
}.ifNull { }.ifNull {
......
...@@ -71,10 +71,11 @@ class PinnedMessagesFragment : Fragment(), PinnedMessagesView { ...@@ -71,10 +71,11 @@ class PinnedMessagesFragment : Fragment(), PinnedMessagesView {
adapter = ChatRoomAdapter(enableActions = false) adapter = ChatRoomAdapter(enableActions = false)
recycler_view_pinned.adapter = adapter recycler_view_pinned.adapter = adapter
val linearLayoutManager = val linearLayoutManager =
LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false) LinearLayoutManager(context, LinearLayoutManager.VERTICAL, true)
linearLayoutManager.stackFromEnd = true
recycler_view_pinned.layoutManager = linearLayoutManager recycler_view_pinned.layoutManager = linearLayoutManager
recycler_view_pinned.itemAnimator = DefaultItemAnimator() recycler_view_pinned.itemAnimator = DefaultItemAnimator()
if (pinnedMessages.size > 10) { if (pinnedMessages.size >= 30) {
recycler_view_pinned.addOnScrollListener(object : recycler_view_pinned.addOnScrollListener(object :
EndlessRecyclerViewScrollListener(linearLayoutManager) { EndlessRecyclerViewScrollListener(linearLayoutManager) {
override fun onLoadMore( override fun onLoadMore(
......
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