Commit bfe48550 authored by Lucio Maciel's avatar Lucio Maciel

Set the temporary status on reconnection

parent e7f17783
......@@ -5,6 +5,7 @@ import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import chat.rocket.android.server.domain.GetAccountInteractor
import chat.rocket.android.server.domain.GetCurrentServerInteractor
import chat.rocket.android.server.infraestructure.ConnectionManagerFactory
import chat.rocket.android.server.infraestructure.RocketChatClientFactory
import chat.rocket.common.RocketChatException
import chat.rocket.common.model.UserStatus
......@@ -15,8 +16,7 @@ import javax.inject.Inject
class AppLifecycleObserver @Inject constructor(
private val serverInteractor: GetCurrentServerInteractor,
private val factory: RocketChatClientFactory,
private val getAccountInteractor: GetAccountInteractor
private val factory: ConnectionManagerFactory
) : LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_START)
......@@ -31,14 +31,8 @@ class AppLifecycleObserver @Inject constructor(
private fun changeTemporaryStatus(userStatus: UserStatus) {
launch {
val currentServer = serverInteractor.get()
val account = currentServer?.let { getAccountInteractor.get(currentServer) }
val client = account?.let { factory.create(currentServer) }
try {
client?.setTemporaryStatus(userStatus)
} catch (exception: RocketChatException) {
Timber.e(exception)
serverInteractor.get()?.let { currentServer ->
factory.create(currentServer).setTemporaryStatus(userStatus)
}
}
}
......
......@@ -176,7 +176,7 @@ class MainPresenter @Inject constructor(
fun changeDefaultStatus(userStatus: UserStatus) {
launchUI(strategy) {
try {
client.setDefaultStatus(userStatus)
manager.setDefaultStatus(userStatus)
view.showUserStatus(userStatus)
} catch (ex: RocketChatException) {
ex.message?.let {
......
......@@ -5,7 +5,10 @@ import chat.rocket.android.db.DatabaseManager
import chat.rocket.android.infrastructure.LocalRepository
import chat.rocket.common.model.BaseRoom
import chat.rocket.common.model.User
import chat.rocket.common.model.UserStatus
import chat.rocket.core.RocketChatClient
import chat.rocket.core.internal.realtime.setDefaultStatus
import chat.rocket.core.internal.realtime.setTemporaryStatus
import chat.rocket.core.internal.realtime.socket.connect
import chat.rocket.core.internal.realtime.socket.disconnect
import chat.rocket.core.internal.realtime.socket.model.State
......@@ -49,6 +52,7 @@ class ConnectionManager(
private var roomsId: String? = null
private var userDataId: String? = null
private var activeUserId: String? = null
private var temporaryStatus: UserStatus? = null
private val activeUsersContext = newSingleThreadContext("activeUsersContext")
private val roomsContext = newSingleThreadContext("roomsContext")
......@@ -90,6 +94,10 @@ class ConnectionManager(
}
resubscribeRooms()
temporaryStatus?.let { status ->
client.setTemporaryStatus(status)
}
}
is State.Waiting -> {
Timber.d("Connection in: ${status.seconds}")
......@@ -176,6 +184,16 @@ class ConnectionManager(
}
}
fun setDefaultStatus(userStatus: UserStatus) {
temporaryStatus = null
client.setDefaultStatus(userStatus)
}
fun setTemporaryStatus(userStatus: UserStatus) {
temporaryStatus = userStatus
client.setTemporaryStatus(userStatus)
}
private fun resubscribeRooms() {
roomMessagesChannels.toList().map { (roomId, channel) ->
client.subscribeRoomMessages(roomId) { _, id ->
......@@ -190,6 +208,7 @@ class ConnectionManager(
client.removeStateChannel(statusChannel)
client.disconnect()
connectJob?.cancel()
temporaryStatus = null
}
fun addStatusChannel(channel: Channel<State>) = statusChannelList.add(channel)
......
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