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,7 +905,8 @@ class ChatRoomPresenter @Inject constructor( ...@@ -904,7 +905,8 @@ 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)") {
dbManager.chatRoomDao().get(roomId)?.let {
with(it.chatRoom) { with(it.chatRoom) {
ChatRoom( ChatRoom(
id = id, id = id,
...@@ -937,10 +939,12 @@ class ChatRoomPresenter @Inject constructor( ...@@ -937,10 +939,12 @@ class ChatRoomPresenter @Inject constructor(
} }
} }
} }
}
// 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()") {
dbManager.chatRoomDao().getAllSync().filter {
if (name == null) { if (name == null) {
return@filter true return@filter true
} }
...@@ -978,6 +982,7 @@ class ChatRoomPresenter @Inject constructor( ...@@ -978,6 +982,7 @@ class ChatRoomPresenter @Inject constructor(
} }
} }
} }
}
fun joinChat(chatRoomId: String) { fun joinChat(chatRoomId: String) {
launchUI(strategy) { launchUI(strategy) {
......
...@@ -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,6 +35,7 @@ class FavoriteMessagesPresenter @Inject constructor( ...@@ -34,6 +35,7 @@ class FavoriteMessagesPresenter @Inject constructor(
launchUI(strategy) { launchUI(strategy) {
try { try {
view.showLoading() view.showLoading()
retryDB("getRoom($roomId)") {
dbManager.getRoom(roomId)?.let { dbManager.getRoom(roomId)?.let {
val favoriteMessages = val favoriteMessages =
client.getFavoriteMessages(roomId, roomTypeOf(it.chatRoom.type), offset) client.getFavoriteMessages(roomId, roomTypeOf(it.chatRoom.type), offset)
...@@ -43,6 +45,7 @@ class FavoriteMessagesPresenter @Inject constructor( ...@@ -43,6 +45,7 @@ class FavoriteMessagesPresenter @Inject constructor(
}.ifNull { }.ifNull {
Timber.e("Couldn't find a room with id: $roomId at current server.") 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)
} finally { } finally {
......
...@@ -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,6 +37,7 @@ class FilesPresenter @Inject constructor( ...@@ -36,6 +37,7 @@ class FilesPresenter @Inject constructor(
launchUI(strategy) { launchUI(strategy) {
try { try {
view.showLoading() view.showLoading()
retryDB("getRoom($roomId)") {
dbManager.getRoom(roomId)?.let { dbManager.getRoom(roomId)?.let {
val files = client.getFiles(roomId, roomTypeOf(it.chatRoom.type), offset) val files = client.getFiles(roomId, roomTypeOf(it.chatRoom.type), offset)
val filesUiModel = mapper.mapToUiModelList(files.result) val filesUiModel = mapper.mapToUiModelList(files.result)
...@@ -44,6 +46,7 @@ class FilesPresenter @Inject constructor( ...@@ -44,6 +46,7 @@ class FilesPresenter @Inject constructor(
}.ifNull { }.ifNull {
Timber.e("Couldn't find a room with id: $roomId at current server.") 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 {
view.showMessage(it) view.showMessage(it)
......
...@@ -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,6 +37,7 @@ class MembersPresenter @Inject constructor( ...@@ -36,6 +37,7 @@ class MembersPresenter @Inject constructor(
launchUI(strategy) { launchUI(strategy) {
try { try {
view.showLoading() view.showLoading()
retryDB("getRoom($roomId)") {
dbManager.getRoom(roomId)?.let { dbManager.getRoom(roomId)?.let {
val members = val members =
client.getMembers(roomId, roomTypeOf(it.chatRoom.type), offset, 60) client.getMembers(roomId, roomTypeOf(it.chatRoom.type), offset, 60)
...@@ -45,6 +47,7 @@ class MembersPresenter @Inject constructor( ...@@ -45,6 +47,7 @@ class MembersPresenter @Inject constructor(
}.ifNull { }.ifNull {
Timber.e("Couldn't find a room with id: $roomId at current server.") 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 {
view.showMessage(it) view.showMessage(it)
......
...@@ -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,6 +35,7 @@ class PinnedMessagesPresenter @Inject constructor( ...@@ -34,6 +35,7 @@ class PinnedMessagesPresenter @Inject constructor(
launchUI(strategy) { launchUI(strategy) {
try { try {
view.showLoading() view.showLoading()
retryDB("getRoom($roomId)") {
dbManager.getRoom(roomId)?.let { dbManager.getRoom(roomId)?.let {
val pinnedMessages = val pinnedMessages =
client.getPinnedMessages(roomId, roomTypeOf(it.chatRoom.type), offset) client.getPinnedMessages(roomId, roomTypeOf(it.chatRoom.type), offset)
...@@ -43,6 +45,7 @@ class PinnedMessagesPresenter @Inject constructor( ...@@ -43,6 +45,7 @@ class PinnedMessagesPresenter @Inject constructor(
}.ifNull { }.ifNull {
Timber.e("Couldn't find a room with id: $roomId at current server.") 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)
} finally { } finally {
......
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