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

Moving some activeUsers computing to proper interactors.

parent c96946ff
...@@ -40,6 +40,8 @@ class ChatRoomsPresenter @Inject constructor( ...@@ -40,6 +40,8 @@ class ChatRoomsPresenter @Inject constructor(
private val serverInteractor: GetCurrentServerInteractor, private val serverInteractor: GetCurrentServerInteractor,
private val getChatRoomsInteractor: GetChatRoomsInteractor, private val getChatRoomsInteractor: GetChatRoomsInteractor,
private val saveChatRoomsInteractor: SaveChatRoomsInteractor, private val saveChatRoomsInteractor: SaveChatRoomsInteractor,
private val saveActiveUsersInteractor: SaveActiveUsersInteractor,
private val getActiveUsersInteractor: GetActiveUsersInteractor,
private val refreshSettingsInteractor: RefreshSettingsInteractor, private val refreshSettingsInteractor: RefreshSettingsInteractor,
private val viewModelMapper: ViewModelMapper, private val viewModelMapper: ViewModelMapper,
private val jobSchedulerInteractor: JobSchedulerInteractor, private val jobSchedulerInteractor: JobSchedulerInteractor,
...@@ -54,7 +56,6 @@ class ChatRoomsPresenter @Inject constructor( ...@@ -54,7 +56,6 @@ class ChatRoomsPresenter @Inject constructor(
private val stateChannel = Channel<State>() private val stateChannel = Channel<State>()
private val subscriptionsChannel = Channel<StreamMessage<BaseRoom>>() private val subscriptionsChannel = Channel<StreamMessage<BaseRoom>>()
private val activeUserChannel = Channel<User>() private val activeUserChannel = Channel<User>()
private val activeUserList = mutableListOf<User>()
private var lastState = manager.state private var lastState = manager.state
fun loadChatRooms() { fun loadChatRooms() {
...@@ -126,7 +127,10 @@ class ChatRoomsPresenter @Inject constructor( ...@@ -126,7 +127,10 @@ class ChatRoomsPresenter @Inject constructor(
id = it.id, id = it.id,
type = RoomType.DIRECT_MESSAGE, type = RoomType.DIRECT_MESSAGE,
user = SimpleUser(username = it.username, name = it.name, id = null), user = SimpleUser(username = it.username, name = it.name, id = null),
status = getActiveUserByUsername(it.name!!)?.status, status = getActiveUsersInteractor.getActiveUserByUsername(
currentServer,
it.name!!
)?.status,
name = it.name ?: "", name = it.name ?: "",
fullName = it.name, fullName = it.name,
readonly = false, readonly = false,
...@@ -154,7 +158,10 @@ class ChatRoomsPresenter @Inject constructor( ...@@ -154,7 +158,10 @@ class ChatRoomsPresenter @Inject constructor(
id = it.id, id = it.id,
type = it.type, type = it.type,
user = it.user, user = it.user,
status = getActiveUserByUsername(it.name!!)?.status, status = getActiveUsersInteractor.getActiveUserByUsername(
currentServer,
it.name!!
)?.status,
name = it.name ?: "", name = it.name ?: "",
fullName = it.fullName, fullName = it.fullName,
readonly = it.readonly, readonly = it.readonly,
...@@ -181,7 +188,8 @@ class ChatRoomsPresenter @Inject constructor( ...@@ -181,7 +188,8 @@ class ChatRoomsPresenter @Inject constructor(
val sortedRooms = sortRooms(chatRooms) val sortedRooms = sortRooms(chatRooms)
Timber.d("Loaded rooms: ${sortedRooms.size}") Timber.d("Loaded rooms: ${sortedRooms.size}")
saveChatRoomsInteractor.save(currentServer, sortedRooms) saveChatRoomsInteractor.save(currentServer, sortedRooms)
return getChatRoomsWithPreviews(sortedRooms) val chatRoomsWithPreview = getChatRoomsWithPreviews(sortedRooms)
return getChatRoomWithStatus(chatRoomsWithPreview)
} }
fun updateSortedChatRooms() { fun updateSortedChatRooms() {
...@@ -233,6 +241,41 @@ class ChatRoomsPresenter @Inject constructor( ...@@ -233,6 +241,41 @@ class ChatRoomsPresenter @Inject constructor(
} }
} }
private fun getChatRoomWithStatus(chatRooms: List<ChatRoom>): List<ChatRoom> {
val chatRoomsList = mutableListOf<ChatRoom>()
chatRooms.forEach {
val newRoom = ChatRoom(
id = it.id,
type = it.type,
user = it.user,
status = getActiveUsersInteractor.getActiveUserByUsername(
currentServer,
it.name
)?.status,
name = it.name,
fullName = it.fullName,
readonly = it.readonly,
updatedAt = it.updatedAt,
timestamp = it.timestamp,
lastSeen = it.lastSeen,
topic = it.topic,
description = it.description,
announcement = it.announcement,
default = it.default,
favorite = it.favorite,
open = it.open,
alert = it.alert,
unread = it.unread,
userMenstions = it.userMenstions,
groupMentions = it.groupMentions,
lastMessage = it.lastMessage,
client = client
)
chatRoomsList.add(newRoom)
}
return chatRoomsList
}
private suspend fun getChatRoomsWithPreviews(chatRooms: List<ChatRoom>): List<ChatRoom> { private suspend fun getChatRoomsWithPreviews(chatRooms: List<ChatRoom>): List<ChatRoom> {
return chatRooms.map { return chatRooms.map {
if (it.lastMessage != null) { if (it.lastMessage != null) {
...@@ -347,7 +390,10 @@ class ChatRoomsPresenter @Inject constructor( ...@@ -347,7 +390,10 @@ class ChatRoomsPresenter @Inject constructor(
id = room.id, id = room.id,
type = room.type, type = room.type,
user = room.user ?: user, user = room.user ?: user,
status = getActiveUserByUsername(room.name!!)?.status, status = getActiveUsersInteractor.getActiveUserByUsername(
currentServer,
room.name!!
)?.status,
name = room.name ?: name, name = room.name ?: name,
fullName = room.fullName ?: fullName, fullName = room.fullName ?: fullName,
readonly = room.readonly, readonly = room.readonly,
...@@ -383,7 +429,10 @@ class ChatRoomsPresenter @Inject constructor( ...@@ -383,7 +429,10 @@ class ChatRoomsPresenter @Inject constructor(
id = subscription.roomId, id = subscription.roomId,
type = subscription.type, type = subscription.type,
user = subscription.user ?: user, user = subscription.user ?: user,
status = getActiveUserByUsername(subscription.name)?.status, status = getActiveUsersInteractor.getActiveUserByUsername(
currentServer,
subscription.name
)?.status,
name = subscription.name, name = subscription.name,
fullName = subscription.fullName ?: fullName, fullName = subscription.fullName ?: fullName,
readonly = subscription.readonly ?: readonly, readonly = subscription.readonly ?: readonly,
...@@ -429,45 +478,22 @@ class ChatRoomsPresenter @Inject constructor( ...@@ -429,45 +478,22 @@ class ChatRoomsPresenter @Inject constructor(
private fun processActiveUser(user: User) { private fun processActiveUser(user: User) {
// The first activeUsers stream contains all details of the users (username, UTC Offset, // The first activeUsers stream contains all details of the users (username, UTC Offset,
// etc.), so we add each user to our [activeUserList] because the following streams don't // etc.), so we add each user to our [saveActiveUsersInteractor] class because the following
// contain those details. // streams don't contain those details.
if (!activeUserList.any { user_ -> user_.id == user.id }) { if (!getActiveUsersInteractor.isActiveUserOnRepository(currentServer, user)) {
Timber.i("Got first active user stream for the user: $user") Timber.d("Got first active user stream for the user: $user")
activeUserList.add(user) saveActiveUsersInteractor.addActiveUser(currentServer, user)
} else { } else {
// After the first stream the next is about the active users updates. // After the first stream the next is about the active users updates.
Timber.i("Got update of active user stream for the user: $user") Timber.d("Got update of active user stream for the user: $user")
updateActiveUser(user) saveActiveUsersInteractor.updateActiveUser(currentServer, user)
} }
getActiveUserById(user.id)?.let { getActiveUsersInteractor.getActiveUserById(currentServer, user.id)?.let {
updateChatRoomWithUserStatus(it) updateChatRoomWithUserStatus(it)
} }
} }
private fun updateActiveUser(user: User) {
activeUserList.find { user_ -> user_.id == user.id }?.let {
val newUser = User(
id = user.id,
name = user.name ?: it.name,
username = user.username ?: it.username,
status = user.status ?: it.status,
emails = user.emails ?: it.emails,
utcOffset = user.utcOffset ?: it.utcOffset
)
activeUserList.remove(it)
activeUserList.add(newUser)
}
}
private fun getActiveUserById(id: String): User? {
return activeUserList.find { user -> user.id == id }
}
private fun getActiveUserByUsername(username: String): User? {
return activeUserList.find { user -> user.username == username }
}
private fun updateChatRoomWithUserStatus(user_: User) { private fun updateChatRoomWithUserStatus(user_: User) {
val username = user_.username val username = user_.username
val status = user_.status val status = user_.status
......
...@@ -5,6 +5,14 @@ import javax.inject.Inject ...@@ -5,6 +5,14 @@ import javax.inject.Inject
class GetActiveUsersInteractor @Inject constructor(private val repository: ActiveUsersRepository) { class GetActiveUsersInteractor @Inject constructor(private val repository: ActiveUsersRepository) {
fun isActiveUserOnRepository(url: String, user: User): Boolean {
return repository.get(url).any { user_ -> user_.id == user.id }
}
fun getAllActiveUsers(url: String): List<User> {
return repository.get(url)
}
fun getActiveUserById(url: String, id: String): User? { fun getActiveUserById(url: String, id: String): User? {
return repository.get(url).find { user -> user.id == id } return repository.get(url).find { user -> user.id == id }
} }
......
...@@ -3,7 +3,42 @@ package chat.rocket.android.server.domain ...@@ -3,7 +3,42 @@ package chat.rocket.android.server.domain
import chat.rocket.common.model.User import chat.rocket.common.model.User
import javax.inject.Inject import javax.inject.Inject
class SaveActiveUsersInteractor @Inject constructor(private val repository: ActiveUsersRepository) { class SaveActiveUsersInteractor @Inject constructor(
private val repository: ActiveUsersRepository,
private val getActiveUsersInteractor: GetActiveUsersInteractor
) {
fun save(url: String, activeUsers: List<User>) = repository.save(url, activeUsers) fun save(url: String, activeUsers: List<User>) {
repository.save(url, activeUsers)
}
fun addActiveUser(url: String, user: User) {
val activeUserList: MutableList<User> =
getActiveUsersInteractor.getAllActiveUsers(url).toMutableList()
synchronized(this) {
activeUserList.add(user)
}
save(url, activeUserList)
}
fun updateActiveUser(url: String, user: User) {
getActiveUsersInteractor.getActiveUserById(url, user.id)?.let {
val newUser = User(
id = user.id,
name = user.name ?: it.name,
username = user.username ?: it.username,
status = user.status ?: it.status,
emails = user.emails ?: it.emails,
utcOffset = user.utcOffset ?: it.utcOffset
)
val activeUserList: MutableList<User> =
getActiveUsersInteractor.getAllActiveUsers(url).toMutableList()
synchronized(this) {
activeUserList.removeAll { user_ -> user_.id == user.id }
}
activeUserList.add(newUser)
save(url, activeUserList)
}
}
} }
\ 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