Commit 540cc085 authored by Lucio Maciel's avatar Lucio Maciel

More retryDB

parent c3037e3e
...@@ -39,6 +39,7 @@ import chat.rocket.android.util.extension.getByteArray ...@@ -39,6 +39,7 @@ import chat.rocket.android.util.extension.getByteArray
import chat.rocket.android.util.extension.launchUI import chat.rocket.android.util.extension.launchUI
import chat.rocket.android.util.extensions.avatarUrl import chat.rocket.android.util.extensions.avatarUrl
import chat.rocket.android.util.extensions.exhaustive import chat.rocket.android.util.extensions.exhaustive
import chat.rocket.android.util.retryDB
import chat.rocket.android.util.retryIO import chat.rocket.android.util.retryIO
import chat.rocket.common.RocketChatException import chat.rocket.common.RocketChatException
import chat.rocket.common.model.RoomType import chat.rocket.common.model.RoomType
...@@ -138,7 +139,7 @@ class ChatRoomPresenter @Inject constructor( ...@@ -138,7 +139,7 @@ class ChatRoomPresenter @Inject constructor(
} finally { } finally {
// User has at least an 'owner' or 'moderator' role. // User has at least an 'owner' or 'moderator' role.
val userCanMod = isOwnerOrMod() val userCanMod = isOwnerOrMod()
val chatRoom = dbManager.getRoom(roomId) val chatRoom = retryDB("getRoom($roomId)") { dbManager.getRoom(roomId) }
val muted = chatRoom?.chatRoom?.muted ?: emptyList() val muted = chatRoom?.chatRoom?.muted ?: emptyList()
// Can post anyway if has the 'post-readonly' permission on server. // Can post anyway if has the 'post-readonly' permission on server.
val userCanPost = userCanMod || permissions.canPostToReadOnlyChannels() || val userCanPost = userCanMod || permissions.canPostToReadOnlyChannels() ||
...@@ -904,77 +905,81 @@ class ChatRoomPresenter @Inject constructor( ...@@ -904,77 +905,81 @@ class ChatRoomPresenter @Inject constructor(
// TODO: move this to new interactor or FetchChatRoomsInteractor? // TODO: move this to new interactor or FetchChatRoomsInteractor?
private suspend fun getChatRoomAsync(roomId: String): ChatRoom? = withContext(CommonPool) { private suspend fun getChatRoomAsync(roomId: String): ChatRoom? = withContext(CommonPool) {
return@withContext dbManager.chatRoomDao().get(roomId)?.let { retryDB("getRoom($roomId)") {
with(it.chatRoom) { dbManager.chatRoomDao().get(roomId)?.let {
ChatRoom( with(it.chatRoom) {
id = id, ChatRoom(
subscriptionId = subscriptionId, id = id,
type = roomTypeOf(type), subscriptionId = subscriptionId,
unread = unread, type = roomTypeOf(type),
broadcast = broadcast ?: false, unread = unread,
alert = alert, broadcast = broadcast ?: false,
fullName = fullname, alert = alert,
name = name, fullName = fullname,
favorite = favorite ?: false, name = name,
default = isDefault ?: false, favorite = favorite ?: false,
readonly = readonly, default = isDefault ?: false,
open = open, readonly = readonly,
lastMessage = null, open = open,
archived = false, lastMessage = null,
status = null, archived = false,
user = null, status = null,
userMentions = userMentions, user = null,
client = client, userMentions = userMentions,
announcement = null, client = client,
description = null, announcement = null,
groupMentions = groupMentions, description = null,
roles = null, groupMentions = groupMentions,
topic = null, roles = null,
lastSeen = this.lastSeen, topic = null,
timestamp = timestamp, lastSeen = this.lastSeen,
updatedAt = updatedAt timestamp = timestamp,
) updatedAt = updatedAt
)
}
} }
} }
} }
// TODO: move this to new interactor or FetchChatRoomsInteractor? // TODO: move this to new interactor or FetchChatRoomsInteractor?
private suspend fun getChatRoomsAsync(name: String? = null): List<ChatRoom> = withContext(CommonPool) { private suspend fun getChatRoomsAsync(name: String? = null): List<ChatRoom> = withContext(CommonPool) {
return@withContext dbManager.chatRoomDao().getAllSync().filter { retryDB("getAllSync()") {
if (name == null) { dbManager.chatRoomDao().getAllSync().filter {
return@filter true if (name == null) {
} return@filter true
it.chatRoom.name == name || it.chatRoom.fullname == name }
}.map { it.chatRoom.name == name || it.chatRoom.fullname == name
with(it.chatRoom) { }.map {
ChatRoom( with(it.chatRoom) {
id = id, ChatRoom(
subscriptionId = subscriptionId, id = id,
type = roomTypeOf(type), subscriptionId = subscriptionId,
unread = unread, type = roomTypeOf(type),
broadcast = broadcast ?: false, unread = unread,
alert = alert, broadcast = broadcast ?: false,
fullName = fullname, alert = alert,
name = name ?: "", fullName = fullname,
favorite = favorite ?: false, name = name ?: "",
default = isDefault ?: false, favorite = favorite ?: false,
readonly = readonly, default = isDefault ?: false,
open = open, readonly = readonly,
lastMessage = null, open = open,
archived = false, lastMessage = null,
status = null, archived = false,
user = null, status = null,
userMentions = userMentions, user = null,
client = client, userMentions = userMentions,
announcement = null, client = client,
description = null, announcement = null,
groupMentions = groupMentions, description = null,
roles = null, groupMentions = groupMentions,
topic = null, roles = null,
lastSeen = this.lastSeen, topic = null,
timestamp = timestamp, lastSeen = this.lastSeen,
updatedAt = updatedAt timestamp = timestamp,
) updatedAt = updatedAt
)
}
} }
} }
} }
......
...@@ -13,6 +13,7 @@ import chat.rocket.android.server.domain.useRealName ...@@ -13,6 +13,7 @@ import chat.rocket.android.server.domain.useRealName
import chat.rocket.android.server.domain.useSpecialCharsOnRoom import chat.rocket.android.server.domain.useSpecialCharsOnRoom
import chat.rocket.android.server.infraestructure.ConnectionManager import chat.rocket.android.server.infraestructure.ConnectionManager
import chat.rocket.android.util.extension.launchUI import chat.rocket.android.util.extension.launchUI
import chat.rocket.android.util.retryDB
import chat.rocket.android.util.retryIO import chat.rocket.android.util.retryIO
import chat.rocket.common.RocketChatException import chat.rocket.common.RocketChatException
import chat.rocket.common.model.RoomType import chat.rocket.common.model.RoomType
...@@ -45,7 +46,7 @@ class ChatRoomsPresenter @Inject constructor( ...@@ -45,7 +46,7 @@ class ChatRoomsPresenter @Inject constructor(
launchUI(strategy) { launchUI(strategy) {
view.showLoadingRoom(chatRoom.name) view.showLoadingRoom(chatRoom.name)
try { try {
val room = dbManager.getRoom(chatRoom.id) val room = retryDB("getRoom(${chatRoom.id}") { dbManager.getRoom(chatRoom.id) }
if (room != null) { if (room != null) {
loadChatRoom(room.chatRoom, true) loadChatRoom(room.chatRoom, true)
} else { } else {
......
...@@ -92,8 +92,8 @@ class DatabaseManager(val context: Application, val serverUrl: String) { ...@@ -92,8 +92,8 @@ class DatabaseManager(val context: Application, val serverUrl: String) {
} }
} }
fun logout() { suspend fun logout() {
database.clearAllTables() retryDB("clearAllTables") { database.clearAllTables() }
} }
suspend fun getRoom(id: String) = withContext(dbManagerContext) { suspend fun getRoom(id: String) = withContext(dbManagerContext) {
......
...@@ -5,6 +5,7 @@ import chat.rocket.android.core.lifecycle.CancelStrategy ...@@ -5,6 +5,7 @@ import chat.rocket.android.core.lifecycle.CancelStrategy
import chat.rocket.android.db.DatabaseManager import chat.rocket.android.db.DatabaseManager
import chat.rocket.android.server.infraestructure.RocketChatClientFactory import chat.rocket.android.server.infraestructure.RocketChatClientFactory
import chat.rocket.android.util.extension.launchUI import chat.rocket.android.util.extension.launchUI
import chat.rocket.android.util.retryDB
import chat.rocket.common.RocketChatException import chat.rocket.common.RocketChatException
import chat.rocket.common.model.roomTypeOf import chat.rocket.common.model.roomTypeOf
import chat.rocket.common.util.ifNull import chat.rocket.common.util.ifNull
...@@ -34,14 +35,16 @@ class FavoriteMessagesPresenter @Inject constructor( ...@@ -34,14 +35,16 @@ class FavoriteMessagesPresenter @Inject constructor(
launchUI(strategy) { launchUI(strategy) {
try { try {
view.showLoading() view.showLoading()
dbManager.getRoom(roomId)?.let { retryDB("getRoom($roomId)") {
val favoriteMessages = dbManager.getRoom(roomId)?.let {
client.getFavoriteMessages(roomId, roomTypeOf(it.chatRoom.type), offset) val favoriteMessages =
val messageList = mapper.map(favoriteMessages.result, asNotReversed = true) client.getFavoriteMessages(roomId, roomTypeOf(it.chatRoom.type), offset)
view.showFavoriteMessages(messageList) val messageList = mapper.map(favoriteMessages.result, asNotReversed = true)
offset += 1 * 30 view.showFavoriteMessages(messageList)
}.ifNull { offset += 1 * 30
Timber.e("Couldn't find a room with id: $roomId at current server.") }.ifNull {
Timber.e("Couldn't find a room with id: $roomId at current server.")
}
} }
} catch (exception: RocketChatException) { } catch (exception: RocketChatException) {
Timber.e(exception) Timber.e(exception)
......
...@@ -7,6 +7,7 @@ import chat.rocket.android.files.uimodel.FileUiModel ...@@ -7,6 +7,7 @@ import chat.rocket.android.files.uimodel.FileUiModel
import chat.rocket.android.files.uimodel.FileUiModelMapper import chat.rocket.android.files.uimodel.FileUiModelMapper
import chat.rocket.android.server.infraestructure.RocketChatClientFactory import chat.rocket.android.server.infraestructure.RocketChatClientFactory
import chat.rocket.android.util.extension.launchUI import chat.rocket.android.util.extension.launchUI
import chat.rocket.android.util.retryDB
import chat.rocket.common.RocketChatException import chat.rocket.common.RocketChatException
import chat.rocket.common.model.roomTypeOf import chat.rocket.common.model.roomTypeOf
import chat.rocket.common.util.ifNull import chat.rocket.common.util.ifNull
...@@ -36,13 +37,15 @@ class FilesPresenter @Inject constructor( ...@@ -36,13 +37,15 @@ class FilesPresenter @Inject constructor(
launchUI(strategy) { launchUI(strategy) {
try { try {
view.showLoading() view.showLoading()
dbManager.getRoom(roomId)?.let { retryDB("getRoom($roomId)") {
val files = client.getFiles(roomId, roomTypeOf(it.chatRoom.type), offset) dbManager.getRoom(roomId)?.let {
val filesUiModel = mapper.mapToUiModelList(files.result) val files = client.getFiles(roomId, roomTypeOf(it.chatRoom.type), offset)
view.showFiles(filesUiModel, files.total) val filesUiModel = mapper.mapToUiModelList(files.result)
offset += 1 * 30 view.showFiles(filesUiModel, files.total)
}.ifNull { offset += 1 * 30
Timber.e("Couldn't find a room with id: $roomId at current server.") }.ifNull {
Timber.e("Couldn't find a room with id: $roomId at current server.")
}
} }
} catch (exception: RocketChatException) { } catch (exception: RocketChatException) {
exception.message?.let { exception.message?.let {
......
...@@ -6,6 +6,7 @@ import chat.rocket.android.members.uimodel.MemberUiModel ...@@ -6,6 +6,7 @@ import chat.rocket.android.members.uimodel.MemberUiModel
import chat.rocket.android.members.uimodel.MemberUiModelMapper import chat.rocket.android.members.uimodel.MemberUiModelMapper
import chat.rocket.android.server.infraestructure.RocketChatClientFactory import chat.rocket.android.server.infraestructure.RocketChatClientFactory
import chat.rocket.android.util.extension.launchUI import chat.rocket.android.util.extension.launchUI
import chat.rocket.android.util.retryDB
import chat.rocket.common.RocketChatException import chat.rocket.common.RocketChatException
import chat.rocket.common.model.roomTypeOf import chat.rocket.common.model.roomTypeOf
import chat.rocket.common.util.ifNull import chat.rocket.common.util.ifNull
...@@ -36,14 +37,16 @@ class MembersPresenter @Inject constructor( ...@@ -36,14 +37,16 @@ class MembersPresenter @Inject constructor(
launchUI(strategy) { launchUI(strategy) {
try { try {
view.showLoading() view.showLoading()
dbManager.getRoom(roomId)?.let { retryDB("getRoom($roomId)") {
val members = dbManager.getRoom(roomId)?.let {
client.getMembers(roomId, roomTypeOf(it.chatRoom.type), offset, 60) val members =
val memberUiModels = mapper.mapToUiModelList(members.result) client.getMembers(roomId, roomTypeOf(it.chatRoom.type), offset, 60)
view.showMembers(memberUiModels, members.total) val memberUiModels = mapper.mapToUiModelList(members.result)
offset += 1 * 60L view.showMembers(memberUiModels, members.total)
}.ifNull { offset += 1 * 60L
Timber.e("Couldn't find a room with id: $roomId at current server.") }.ifNull {
Timber.e("Couldn't find a room with id: $roomId at current server.")
}
} }
} catch (exception: RocketChatException) { } catch (exception: RocketChatException) {
exception.message?.let { exception.message?.let {
......
...@@ -5,6 +5,7 @@ import chat.rocket.android.core.lifecycle.CancelStrategy ...@@ -5,6 +5,7 @@ import chat.rocket.android.core.lifecycle.CancelStrategy
import chat.rocket.android.db.DatabaseManager import chat.rocket.android.db.DatabaseManager
import chat.rocket.android.server.infraestructure.RocketChatClientFactory import chat.rocket.android.server.infraestructure.RocketChatClientFactory
import chat.rocket.android.util.extension.launchUI import chat.rocket.android.util.extension.launchUI
import chat.rocket.android.util.retryDB
import chat.rocket.common.RocketChatException import chat.rocket.common.RocketChatException
import chat.rocket.common.model.roomTypeOf import chat.rocket.common.model.roomTypeOf
import chat.rocket.common.util.ifNull import chat.rocket.common.util.ifNull
...@@ -34,14 +35,16 @@ class PinnedMessagesPresenter @Inject constructor( ...@@ -34,14 +35,16 @@ class PinnedMessagesPresenter @Inject constructor(
launchUI(strategy) { launchUI(strategy) {
try { try {
view.showLoading() view.showLoading()
dbManager.getRoom(roomId)?.let { retryDB("getRoom($roomId)") {
val pinnedMessages = dbManager.getRoom(roomId)?.let {
client.getPinnedMessages(roomId, roomTypeOf(it.chatRoom.type), offset) val pinnedMessages =
val messageList = mapper.map(pinnedMessages.result, asNotReversed = true) client.getPinnedMessages(roomId, roomTypeOf(it.chatRoom.type), offset)
view.showPinnedMessages(messageList) val messageList = mapper.map(pinnedMessages.result, asNotReversed = true)
offset += 1 * 30 view.showPinnedMessages(messageList)
}.ifNull { offset += 1 * 30
Timber.e("Couldn't find a room with id: $roomId at current server.") }.ifNull {
Timber.e("Couldn't find a room with id: $roomId at current server.")
}
} }
} catch (exception: RocketChatException) { } catch (exception: RocketChatException) {
Timber.e(exception) Timber.e(exception)
......
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