Commit 64069aa2 authored by kb0304's avatar kb0304

Not show last message on search icon click

parent 067edc43
......@@ -42,27 +42,28 @@ class RoomUiModelMapper(
userInteractor.get()
}
fun map(rooms: List<ChatRoom>, grouped: Boolean = false): List<ItemHolder<*>> {
fun map(rooms: List<ChatRoom>, grouped: Boolean = false, showLastMessage: Boolean = true): List<ItemHolder<*>> {
val list = ArrayList<ItemHolder<*>>(rooms.size + 4)
var lastType: String? = null
rooms.forEach { room ->
if (grouped && lastType != room.chatRoom.type) {
list.add(HeaderItemHolder(roomType(room.chatRoom.type)))
}
list.add(RoomItemHolder(map(room)))
list.add(RoomItemHolder(map(room, showLastMessage)))
lastType = room.chatRoom.type
}
return list
}
fun map(spotlight: SpotlightResult): List<ItemHolder<*>> {
fun map(spotlight: SpotlightResult, showLastMessage: Boolean = true): List<ItemHolder<*>> {
val list = ArrayList<ItemHolder<*>>(spotlight.users.size + spotlight.rooms.size)
spotlight.users.filterNot { it.username.isNullOrEmpty() }.forEach { user ->
list.add(RoomItemHolder(mapUser(user)))
}
spotlight.rooms.filterNot { it.name.isNullOrEmpty() }.forEach { room ->
list.add(RoomItemHolder(mapRoom(room)))
list.add(RoomItemHolder(mapRoom(room, showLastMessage)))
}
return list
......@@ -86,21 +87,21 @@ class RoomUiModelMapper(
}
}
private fun mapRoom(room: Room): RoomUiModel {
private fun mapRoom(room: Room, showLastMessage:Boolean = true): RoomUiModel {
return with(room) {
RoomUiModel(
id = id,
name = name!!,
type = type,
avatar = serverUrl.avatarUrl(name!!, isGroupOrChannel = true),
lastMessage = mapLastMessage(lastMessage?.sender?.id, lastMessage?.sender?.username,
lastMessage = if(showLastMessage) { mapLastMessage(lastMessage?.sender?.id, lastMessage?.sender?.username,
lastMessage?.sender?.name, lastMessage?.message,
isDirectMessage = type is RoomType.DirectMessage)
isDirectMessage = type is RoomType.DirectMessage)} else { null }
)
}
}
fun map(chatRoom: ChatRoom): RoomUiModel {
fun map(chatRoom: ChatRoom, showLastMessage:Boolean = true): RoomUiModel {
return with(chatRoom.chatRoom) {
val isUnread = alert || unread > 0
val type = roomTypeOf(type)
......@@ -113,9 +114,9 @@ class RoomUiModelMapper(
serverUrl.avatarUrl(name, isGroupOrChannel = true)
}
val unread = mapUnread(unread)
val lastMessage = mapLastMessage(lastMessageUserId, chatRoom.lastMessageUserName,
val lastMessage = if(showLastMessage) { mapLastMessage(lastMessageUserId, chatRoom.lastMessageUserName,
chatRoom.lastMessageUserFullName, lastMessageText, isUnread,
type is RoomType.DirectMessage)
type is RoomType.DirectMessage) } else { null }
val open = open
RoomUiModel(
......@@ -148,6 +149,7 @@ class RoomUiModelMapper(
private fun mapLastMessage(userId: String?, name: String?, fullName: String?, text: String?,
unread: Boolean = false,
isDirectMessage: Boolean = false): CharSequence? {
return if (!settings.showLastMessage()) {
null
} else if (name != null && text != null) {
......
......@@ -179,11 +179,14 @@ class ChatRoomsFragment : Fragment(), ChatRoomsView {
override fun onMenuItemActionCollapse(item: MenuItem): Boolean {
// Simply setting sortView to visible won't work, so we invalidate the options
// to recreate the entire menu...
viewModel.showLastMessage = true
activity?.invalidateOptionsMenu()
queryChatRoomsByName(null)
return true
}
override fun onMenuItemActionExpand(item: MenuItem): Boolean {
viewModel.showLastMessage = false
sortView?.isVisible = false
return true
}
......
......@@ -30,6 +30,7 @@ import timber.log.Timber
import java.security.InvalidParameterException
import kotlin.coroutines.experimental.coroutineContext
class ChatRoomsViewModel(
private val connectionManager: ConnectionManager,
private val interactor: FetchChatRoomsInteractor,
......@@ -41,9 +42,11 @@ class ChatRoomsViewModel(
private val runContext = newSingleThreadContext("chat-rooms-view-model")
private val client = connectionManager.client
private var loaded = false
var showLastMessage = true
fun getChatRooms(): LiveData<RoomsModel> {
return Transformations.switchMap(query) { query ->
return@switchMap if (query.isSearch()) {
this@ChatRoomsViewModel.query.wrap(runContext) { _, data: MutableLiveData<RoomsModel> ->
val string = (query as Query.Search).query
......@@ -53,11 +56,13 @@ class ChatRoomsViewModel(
// TODO - find a better way for cancellation checking
if (!coroutineContext.isActive) return@wrap
val rooms = repository.search(string).let { mapper.map(it) }
val rooms = repository.search(string).let { mapper.map(it, showLastMessage = this.showLastMessage) }
data.postValue(rooms.toMutableList() + LoadingItemHolder())
if (!coroutineContext.isActive) return@wrap
val spotlight = spotlight(query.query)?.let { mapper.map(it) }
val spotlight = spotlight(query.query)?.let { mapper.map(it, showLastMessage = this.showLastMessage) }
if (!coroutineContext.isActive) return@wrap
spotlight?.let {
......@@ -72,7 +77,7 @@ class ChatRoomsViewModel(
.distinct()
.transform(runContext) { rooms ->
val mappedRooms = rooms?.let {
mapper.map(rooms, query.isGrouped())
mapper.map(rooms, query.isGrouped(), this.showLastMessage)
}
if (loaded && mappedRooms?.isEmpty() == true) {
loadingState.postValue(LoadingState.Loaded(0))
......
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