Commit ccec99f5 authored by Filipe de Lima Brito's avatar Filipe de Lima Brito

Fix coding issues.

parent 42c34407
...@@ -118,7 +118,7 @@ class ChatRoomPresenter @Inject constructor( ...@@ -118,7 +118,7 @@ class ChatRoomPresenter @Inject constructor(
private var chatRoomId: String? = null private var chatRoomId: String? = null
private lateinit var chatRoomType: String private lateinit var chatRoomType: String
private lateinit var chatRoomName: String private lateinit var chatRoomName: String
private var chatIsBroadcast: Boolean = false private var isBroadcast: Boolean = false
private var chatRoles = emptyList<ChatRoomRole>() private var chatRoles = emptyList<ChatRoomRole>()
private val stateChannel = Channel<State>() private val stateChannel = Channel<State>()
private var typingStatusSubscriptionId: String? = null private var typingStatusSubscriptionId: String? = null
...@@ -145,14 +145,16 @@ class ChatRoomPresenter @Inject constructor( ...@@ -145,14 +145,16 @@ class ChatRoomPresenter @Inject constructor(
// Can post anyway if has the 'post-readonly' permission on server. // Can post anyway if has the 'post-readonly' permission on server.
val room = dbManager.getRoom(roomId) val room = dbManager.getRoom(roomId)
room?.let { room?.let {
chatIsBroadcast = it.chatRoom.broadcast ?: false isBroadcast = it.chatRoom.broadcast ?: false
val roomUiModel = roomMapper.map(it, true) val roomUiModel = roomMapper.map(it, true)
launchUI(strategy) { launchUI(strategy) {
view.onRoomUpdated(roomUiModel = roomUiModel.copy( view.onRoomUpdated(
broadcast = chatIsBroadcast, roomUiModel = roomUiModel.copy(
broadcast = isBroadcast,
canModerate = canModerate, canModerate = canModerate,
writable = roomUiModel.writable || canModerate writable = roomUiModel.writable || canModerate
)) )
)
} }
} }
...@@ -188,14 +190,16 @@ class ChatRoomPresenter @Inject constructor( ...@@ -188,14 +190,16 @@ class ChatRoomPresenter @Inject constructor(
} }
} }
private suspend fun getChatRole() : Boolean { private suspend fun getChatRole(): Boolean {
var returnVal = false
try { try {
if (roomTypeOf(chatRoomType) !is RoomType.DirectMessage) { if (roomTypeOf(chatRoomType) !is RoomType.DirectMessage) {
chatRoles = withContext(Dispatchers.IO + strategy.jobs) { chatRoles = withContext(Dispatchers.IO + strategy.jobs) {
client.chatRoomRoles(roomType = roomTypeOf(chatRoomType), roomName = chatRoomName) client.chatRoomRoles(
roomType = roomTypeOf(chatRoomType),
roomName = chatRoomName
)
} }
returnVal = true return true
} else { } else {
chatRoles = emptyList() chatRoles = emptyList()
} }
...@@ -203,7 +207,7 @@ class ChatRoomPresenter @Inject constructor( ...@@ -203,7 +207,7 @@ class ChatRoomPresenter @Inject constructor(
Timber.e(ex) Timber.e(ex)
chatRoles = emptyList() chatRoles = emptyList()
} }
return returnVal return false
} }
private suspend fun subscribeRoomChanges() { private suspend fun subscribeRoomChanges() {
...@@ -212,7 +216,12 @@ class ChatRoomPresenter @Inject constructor( ...@@ -212,7 +216,12 @@ class ChatRoomPresenter @Inject constructor(
manager.addRoomChannel(it, roomChangesChannel) manager.addRoomChannel(it, roomChangesChannel)
for (room in roomChangesChannel) { for (room in roomChangesChannel) {
dbManager.getRoom(room.id)?.let { chatRoom -> dbManager.getRoom(room.id)?.let { chatRoom ->
view.onRoomUpdated(roomMapper.map(chatRoom = chatRoom, showLastMessage = true)) view.onRoomUpdated(
roomMapper.map(
chatRoom = chatRoom,
showLastMessage = true
)
)
} }
} }
} }
...@@ -248,7 +257,7 @@ class ChatRoomPresenter @Inject constructor( ...@@ -248,7 +257,7 @@ class ChatRoomPresenter @Inject constructor(
localMessages, RoomUiModel( localMessages, RoomUiModel(
roles = chatRoles, roles = chatRoles,
// FIXME: Why are we fixing isRoom attribute to true here? // FIXME: Why are we fixing isRoom attribute to true here?
isBroadcast = chatIsBroadcast, isRoom = true isBroadcast = isBroadcast, isRoom = true
) )
) )
lastMessageId = localMessages.firstOrNull()?.id lastMessageId = localMessages.firstOrNull()?.id
...@@ -309,7 +318,7 @@ class ChatRoomPresenter @Inject constructor( ...@@ -309,7 +318,7 @@ class ChatRoomPresenter @Inject constructor(
view.showMessages( view.showMessages(
mapper.map( mapper.map(
messages, messages,
RoomUiModel(roles = chatRoles, isBroadcast = chatIsBroadcast, isRoom = true) RoomUiModel(roles = chatRoles, isBroadcast = isBroadcast, isRoom = true)
), ),
clearDataSet clearDataSet
) )
...@@ -325,7 +334,7 @@ class ChatRoomPresenter @Inject constructor( ...@@ -325,7 +334,7 @@ class ChatRoomPresenter @Inject constructor(
view.showSearchedMessages( view.showSearchedMessages(
mapper.map( mapper.map(
messages, messages,
RoomUiModel(chatRoles, chatIsBroadcast, true) RoomUiModel(chatRoles, isBroadcast, true)
) )
) )
} catch (ex: Exception) { } catch (ex: Exception) {
...@@ -357,7 +366,11 @@ class ChatRoomPresenter @Inject constructor( ...@@ -357,7 +366,11 @@ class ChatRoomPresenter @Inject constructor(
timestamp = Instant.now().toEpochMilli(), timestamp = Instant.now().toEpochMilli(),
sender = SimpleUser(user?.id, user?.username ?: username, user?.name), sender = SimpleUser(user?.id, user?.username ?: username, user?.name),
attachments = null, attachments = null,
avatar = currentServer.avatarUrl(username!!, token?.userId, token?.authToken), avatar = currentServer.avatarUrl(
username!!,
token?.userId,
token?.authToken
),
channels = null, channels = null,
editedAt = null, editedAt = null,
editedBy = null, editedBy = null,
...@@ -379,7 +392,7 @@ class ChatRoomPresenter @Inject constructor( ...@@ -379,7 +392,7 @@ class ChatRoomPresenter @Inject constructor(
view.showNewMessage( view.showNewMessage(
mapper.map( mapper.map(
newMessage, newMessage,
RoomUiModel(roles = chatRoles, isBroadcast = chatIsBroadcast) RoomUiModel(roles = chatRoles, isBroadcast = isBroadcast)
), false ), false
) )
client.sendMessage(id, chatRoomId, text) client.sendMessage(id, chatRoomId, text)
...@@ -630,16 +643,21 @@ class ChatRoomPresenter @Inject constructor( ...@@ -630,16 +643,21 @@ class ChatRoomPresenter @Inject constructor(
Timber.d("History: $messages") Timber.d("History: $messages")
if (messages.result.isNotEmpty()) { if (messages.result.isNotEmpty()) {
val models = mapper.map(messages.result, RoomUiModel( val models = mapper.map(
messages.result, RoomUiModel(
roles = chatRoles, roles = chatRoles,
isBroadcast = chatIsBroadcast, isBroadcast = isBroadcast,
// FIXME: Why are we fixing isRoom attribute to true here? // FIXME: Why are we fixing isRoom attribute to true here?
isRoom = true isRoom = true
)) )
)
messagesRepository.saveAll(messages.result) messagesRepository.saveAll(messages.result)
//if success - saving last synced time //if success - saving last synced time
//assume that BE returns ordered messages, the first message is the latest one //assume that BE returns ordered messages, the first message is the latest one
messagesRepository.saveLastSyncDate(chatRoomId, messages.result.first().timestamp) messagesRepository.saveLastSyncDate(
chatRoomId,
messages.result.first().timestamp
)
launchUI(strategy) { launchUI(strategy) {
view.showNewMessage(models, true) view.showNewMessage(models, true)
...@@ -717,7 +735,7 @@ class ChatRoomPresenter @Inject constructor( ...@@ -717,7 +735,7 @@ class ChatRoomPresenter @Inject constructor(
quotedMessage = mapper.map( quotedMessage = mapper.map(
message, RoomUiModel( message, RoomUiModel(
roles = chatRoles, roles = chatRoles,
isBroadcast = chatIsBroadcast isBroadcast = isBroadcast
) )
).last().preview?.message ?: "" ).last().preview?.message ?: ""
) )
...@@ -844,7 +862,8 @@ class ChatRoomPresenter @Inject constructor( ...@@ -844,7 +862,8 @@ class ChatRoomPresenter @Inject constructor(
val sender = it.sender val sender = it.sender
val username = sender?.username ?: "" val username = sender?.username ?: ""
val name = sender?.name ?: "" val name = sender?.name ?: ""
val avatarUrl = currentServer.avatarUrl(username, token?.userId, token?.authToken) val avatarUrl =
currentServer.avatarUrl(username, token?.userId, token?.authToken)
val found = members.firstOrNull { member -> member.username == username } val found = members.firstOrNull { member -> member.username == username }
val status = if (found != null) found.status else UserStatus.Offline() val status = if (found != null) found.status else UserStatus.Offline()
val searchList = mutableListOf(username, name) val searchList = mutableListOf(username, name)
...@@ -865,7 +884,8 @@ class ChatRoomPresenter @Inject constructor( ...@@ -865,7 +884,8 @@ class ChatRoomPresenter @Inject constructor(
activeUsers.addAll(others.map { activeUsers.addAll(others.map {
val username = it.username ?: "" val username = it.username ?: ""
val name = it.name ?: "" val name = it.name ?: ""
val avatarUrl = currentServer.avatarUrl(username, token?.userId, token?.authToken) val avatarUrl =
currentServer.avatarUrl(username, token?.userId, token?.authToken)
val searchList = mutableListOf(username, name) val searchList = mutableListOf(username, name)
PeopleSuggestionUiModel( PeopleSuggestionUiModel(
avatarUrl, avatarUrl,
...@@ -997,7 +1017,8 @@ class ChatRoomPresenter @Inject constructor( ...@@ -997,7 +1017,8 @@ 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(Dispatchers.IO) { private suspend fun getChatRoomsAsync(name: String? = null): List<ChatRoom> =
withContext(Dispatchers.IO) {
retryDB("getAllSync()") { retryDB("getAllSync()") {
dbManager.chatRoomDao().getAllSync().filter { dbManager.chatRoomDao().getAllSync().filter {
if (name == null) { if (name == null) {
...@@ -1047,7 +1068,8 @@ class ChatRoomPresenter @Inject constructor( ...@@ -1047,7 +1068,8 @@ class ChatRoomPresenter @Inject constructor(
val canPost = permissions.canPostToReadOnlyChannels() val canPost = permissions.canPostToReadOnlyChannels()
dbManager.getRoom(chatRoomId)?.let { dbManager.getRoom(chatRoomId)?.let {
val roomUiModel = roomMapper.map(it, true).copy( val roomUiModel = roomMapper.map(it, true).copy(
writable = canPost) writable = canPost
)
view.onJoined(roomUiModel = roomUiModel) view.onJoined(roomUiModel = roomUiModel)
view.onRoomUpdated(roomUiModel = roomUiModel) view.onRoomUpdated(roomUiModel = roomUiModel)
} }
...@@ -1308,7 +1330,7 @@ class ChatRoomPresenter @Inject constructor( ...@@ -1308,7 +1330,7 @@ class ChatRoomPresenter @Inject constructor(
launchUI(strategy) { launchUI(strategy) {
val viewModelStreamedMessage = mapper.map( val viewModelStreamedMessage = mapper.map(
streamedMessage, RoomUiModel( streamedMessage, RoomUiModel(
roles = chatRoles, isBroadcast = chatIsBroadcast, isRoom = true roles = chatRoles, isBroadcast = isBroadcast, isRoom = true
) )
) )
val roomMessages = messagesRepository.getByRoomId(streamedMessage.roomId) val roomMessages = messagesRepository.getByRoomId(streamedMessage.roomId)
......
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