Commit 4b61077d authored by Lucio Maciel's avatar Lucio Maciel

Create an Operation queue for DB write operations

parent 62a28388
...@@ -174,7 +174,9 @@ class ChatRoomPresenter @Inject constructor( ...@@ -174,7 +174,9 @@ class ChatRoomPresenter @Inject constructor(
view.showLoading() view.showLoading()
try { try {
if (offset == 0L) { if (offset == 0L) {
val localMessages = messagesRepository.getByRoomId(chatRoomId) // FIXME - load just 50 messages from DB to speed up. We will reload from Network after that
// FIXME - We need to handle the pagination, first fetch from DB, then from network
val localMessages = messagesRepository.getRecentMessages(chatRoomId, 50)
val oldMessages = mapper.map( val oldMessages = mapper.map(
localMessages, RoomUiModel( localMessages, RoomUiModel(
roles = chatRoles, roles = chatRoles,
......
...@@ -24,6 +24,7 @@ import chat.rocket.common.model.roomTypeOf ...@@ -24,6 +24,7 @@ import chat.rocket.common.model.roomTypeOf
import chat.rocket.common.model.userStatusOf import chat.rocket.common.model.userStatusOf
import chat.rocket.core.model.Room import chat.rocket.core.model.Room
import chat.rocket.core.model.SpotlightResult import chat.rocket.core.model.SpotlightResult
import ru.noties.markwon.Markwon
class RoomUiModelMapper( class RoomUiModelMapper(
private val context: Application, private val context: Application,
...@@ -119,6 +120,8 @@ class RoomUiModelMapper( ...@@ -119,6 +120,8 @@ class RoomUiModelMapper(
type is RoomType.DirectMessage) } else { null } type is RoomType.DirectMessage) } else { null }
val open = open val open = open
val lastMessageMarkdown = lastMessage?.let { Markwon.markdown(context, it.toString()).toString() }
RoomUiModel( RoomUiModel(
id = id, id = id,
name = roomName, name = roomName,
...@@ -128,7 +131,7 @@ class RoomUiModelMapper( ...@@ -128,7 +131,7 @@ class RoomUiModelMapper(
date = timestamp, date = timestamp,
unread = unread, unread = unread,
alert = isUnread, alert = isUnread,
lastMessage = lastMessage, lastMessage = lastMessageMarkdown,
status = status, status = status,
username = if (type is RoomType.DirectMessage) name else null username = if (type is RoomType.DirectMessage) name else null
) )
......
...@@ -33,7 +33,7 @@ class RoomViewHolder(itemView: View, private val listener: (RoomUiModel) -> Unit ...@@ -33,7 +33,7 @@ class RoomViewHolder(itemView: View, private val listener: (RoomUiModel) -> Unit
if (room.lastMessage != null) { if (room.lastMessage != null) {
text_last_message.isVisible = true text_last_message.isVisible = true
text_last_message.text = Markwon.markdown(context, room.lastMessage.toString()).toString() text_last_message.text = room.lastMessage
} else { } else {
text_last_message.isGone = true text_last_message.isGone = true
} }
......
...@@ -19,17 +19,21 @@ class RoomsAdapter(private val listener: (RoomUiModel) -> Unit) : RecyclerView.A ...@@ -19,17 +19,21 @@ class RoomsAdapter(private val listener: (RoomUiModel) -> Unit) : RecyclerView.A
} }
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder<*> { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder<*> {
if (viewType == VIEW_TYPE_ROOM) { return when (viewType) {
VIEW_TYPE_ROOM -> {
val view = parent.inflate(R.layout.item_chat) val view = parent.inflate(R.layout.item_chat)
return RoomViewHolder(view, listener) RoomViewHolder(view, listener)
} else if (viewType == VIEW_TYPE_HEADER) { }
VIEW_TYPE_HEADER -> {
val view = parent.inflate(R.layout.item_chatroom_header) val view = parent.inflate(R.layout.item_chatroom_header)
return HeaderViewHolder(view) HeaderViewHolder(view)
} else if (viewType == VIEW_TYPE_LOADING) { }
VIEW_TYPE_LOADING -> {
val view = parent.inflate(R.layout.item_loading) val view = parent.inflate(R.layout.item_loading)
return LoadingViewHolder(view) LoadingViewHolder(view)
}
else -> throw IllegalStateException("View type must be either Room, Header or Loading")
} }
throw IllegalStateException("View type must be either Room, Header or Loading")
} }
override fun getItemCount() = values.size override fun getItemCount() = values.size
......
package chat.rocket.android.server.infraestructure package chat.rocket.android.server.infraestructure
import chat.rocket.android.db.DatabaseManager import chat.rocket.android.db.DatabaseManager
import chat.rocket.android.db.Operation
import chat.rocket.android.db.model.MessagesSync import chat.rocket.android.db.model.MessagesSync
import chat.rocket.android.server.domain.MessagesRepository import chat.rocket.android.server.domain.MessagesRepository
import chat.rocket.core.model.Message import chat.rocket.core.model.Message
...@@ -61,9 +62,7 @@ class DatabaseMessagesRepository( ...@@ -61,9 +62,7 @@ class DatabaseMessagesRepository(
} }
override suspend fun saveLastSyncDate(roomId: String, timeMillis: Long) { override suspend fun saveLastSyncDate(roomId: String, timeMillis: Long) {
withContext(dbManager.dbContext) { dbManager.sendOperation(Operation.SaveLastSync(MessagesSync(roomId, timeMillis)))
dbManager.messageDao().saveLastSync(MessagesSync(roomId, timeMillis))
}
} }
override suspend fun getLastSyncDate(roomId: String): Long? = withContext(CommonPool) { override suspend fun getLastSyncDate(roomId: String): Long? = withContext(CommonPool) {
......
package chat.rocket.android.util.extensions
val <T> T.exhaustive: T
get() = this
\ No newline at end of file
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