BayernMessenger/vector/src/main/java/im/vector/riotredesign/features/home/room/list/RoomSummaryController.kt

131 lines
5.2 KiB
Kotlin
Raw Normal View History

2019-01-18 10:12:08 +00:00
/*
* Copyright 2019 New Vector Ltd
2019-01-18 10:12:08 +00:00
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
2019-01-18 10:12:08 +00:00
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
2019-01-18 10:12:08 +00:00
*/
package im.vector.riotredesign.features.home.room.list
import androidx.annotation.StringRes
2018-11-02 15:30:22 +00:00
import com.airbnb.epoxy.TypedEpoxyController
2019-06-04 10:02:34 +00:00
import im.vector.matrix.android.api.session.events.model.EventType
import im.vector.matrix.android.api.session.events.model.toModel
import im.vector.matrix.android.api.session.room.model.RoomSummary
2019-06-04 10:02:34 +00:00
import im.vector.matrix.android.api.session.room.model.message.MessageContent
2019-05-17 08:19:19 +00:00
import im.vector.riotredesign.core.extensions.localDateTime
import im.vector.riotredesign.core.resources.DateProvider
import im.vector.riotredesign.core.resources.StringProvider
import im.vector.riotredesign.features.home.room.detail.timeline.format.NoticeEventFormatter
2019-05-17 08:19:19 +00:00
import im.vector.riotredesign.features.home.room.detail.timeline.helper.TimelineDateFormatter
2019-05-17 08:19:19 +00:00
class RoomSummaryController(private val stringProvider: StringProvider,
private val eventFormatter: NoticeEventFormatter,
2019-05-17 08:19:19 +00:00
private val timelineDateFormatter: TimelineDateFormatter
) : TypedEpoxyController<RoomListViewState>() {
2018-10-30 17:22:29 +00:00
var callback: Callback? = null
2018-10-30 17:22:29 +00:00
override fun buildModels(viewState: RoomListViewState) {
2019-05-17 15:07:02 +00:00
val roomSummaries = viewState.asyncFilteredRooms()
roomSummaries?.forEach { (category, summaries) ->
if (summaries.isEmpty()) {
return@forEach
} else {
val isExpanded = viewState.isCategoryExpanded(category)
buildRoomCategory(viewState, summaries, category.titleRes, viewState.isCategoryExpanded(category)) {
callback?.onToggleRoomCategory(category)
}
if (isExpanded) {
buildRoomModels(summaries)
}
}
}
}
2019-03-13 17:36:57 +00:00
private fun buildRoomCategory(viewState: RoomListViewState,
summaries: List<RoomSummary>,
@StringRes titleRes: Int,
isExpanded: Boolean,
mutateExpandedState: () -> Unit) {
if (summaries.isEmpty()) {
return
}
//TODO should add some business logic later
2019-01-29 15:02:42 +00:00
val unreadCount = if (summaries.isEmpty()) {
0
} else {
summaries.map { it.notificationCount }.sumBy { i -> i }
2019-01-29 15:02:42 +00:00
}
val showHighlighted = summaries.any { it.highlightCount > 0 }
roomCategoryItem {
id(titleRes)
title(stringProvider.getString(titleRes).toUpperCase())
expanded(isExpanded)
unreadCount(unreadCount)
showHighlighted(showHighlighted)
listener {
mutateExpandedState()
setData(viewState)
}
}
2018-10-30 17:22:29 +00:00
}
private fun buildRoomModels(summaries: List<RoomSummary>) {
2018-11-02 15:30:22 +00:00
summaries.forEach { roomSummary ->
val unreadCount = roomSummary.notificationCount
val showHighlighted = roomSummary.highlightCount > 0
2019-05-17 08:19:19 +00:00
var lastMessageFormatted: CharSequence = ""
var lastMessageTime: CharSequence = ""
val lastMessage = roomSummary.lastMessage
if (lastMessage != null) {
val date = lastMessage.localDateTime()
val currentData = DateProvider.currentLocalDateTime()
val isSameDay = date.toLocalDate() == currentData.toLocalDate()
//TODO: get formatted
2019-06-04 10:02:34 +00:00
if (lastMessage.type == EventType.MESSAGE) {
val content = lastMessage.content?.toModel<MessageContent>()
lastMessageFormatted = content?.body ?: ""
} else {
lastMessageFormatted = lastMessage.type
}
2019-05-17 08:19:19 +00:00
lastMessageTime = if (isSameDay) {
timelineDateFormatter.formatMessageHour(date)
} else {
//TODO: change this
timelineDateFormatter.formatMessageDay(date)
}
}
roomSummaryItem {
id(roomSummary.roomId)
roomId(roomSummary.roomId)
2019-05-17 08:19:19 +00:00
lastEventTime(lastMessageTime)
lastFormattedEvent(lastMessageFormatted)
roomName(roomSummary.displayName)
avatarUrl(roomSummary.avatarUrl)
showHighlighted(showHighlighted)
unreadCount(unreadCount)
listener { callback?.onRoomSelected(roomSummary) }
}
}
}
interface Callback {
fun onToggleRoomCategory(roomCategory: RoomCategory)
fun onRoomSelected(room: RoomSummary)
}
}