Commit d0e879cd authored by Lucio Maciel's avatar Lucio Maciel

Hide hidden (open = false) rooms and clear status on new connection

parent b49598d9
...@@ -28,7 +28,6 @@ import chat.rocket.core.model.SpotlightResult ...@@ -28,7 +28,6 @@ import chat.rocket.core.model.SpotlightResult
class RoomUiModelMapper( class RoomUiModelMapper(
private val context: Application, private val context: Application,
private val settings: PublicSettings, private val settings: PublicSettings,
private val localRepository: LocalRepository,
private val userInteractor: GetCurrentUserInteractor, private val userInteractor: GetCurrentUserInteractor,
private val serverUrl: String private val serverUrl: String
) { ) {
...@@ -117,12 +116,14 @@ class RoomUiModelMapper( ...@@ -117,12 +116,14 @@ class RoomUiModelMapper(
val lastMessage = mapLastMessage(lastMessageUserId, chatRoom.lastMessageUserName, val lastMessage = mapLastMessage(lastMessageUserId, chatRoom.lastMessageUserName,
chatRoom.lastMessageUserFullName, lastMessageText, isUnread, chatRoom.lastMessageUserFullName, lastMessageText, isUnread,
type is RoomType.DirectMessage) type is RoomType.DirectMessage)
val open = open
RoomUiModel( RoomUiModel(
id = id, id = id,
name = roomName, name = roomName,
type = type, type = type,
avatar = avatar, avatar = avatar,
open = open,
date = timestamp, date = timestamp,
unread = unread, unread = unread,
alert = isUnread, alert = isUnread,
......
...@@ -8,6 +8,7 @@ data class RoomUiModel( ...@@ -8,6 +8,7 @@ data class RoomUiModel(
val type: RoomType, val type: RoomType,
val name: CharSequence, val name: CharSequence,
val avatar: String, val avatar: String,
val open: Boolean = false,
val date: CharSequence? = null, val date: CharSequence? = null,
val unread: String? = null, val unread: String? = null,
val alert: Boolean = false, val alert: Boolean = false,
......
...@@ -87,12 +87,10 @@ class ChatRoomsFragmentModule { ...@@ -87,12 +87,10 @@ class ChatRoomsFragmentModule {
fun provideRoomMapper( fun provideRoomMapper(
context: Application, context: Application,
repository: SettingsRepository, repository: SettingsRepository,
localRepository: LocalRepository,
userInteractor: GetCurrentUserInteractor, userInteractor: GetCurrentUserInteractor,
@Named("currentServer") serverUrl: String @Named("currentServer") serverUrl: String
): RoomUiModelMapper { ): RoomUiModelMapper {
return RoomUiModelMapper(context, repository.get(serverUrl), localRepository, return RoomUiModelMapper(context, repository.get(serverUrl), userInteractor, serverUrl)
userInteractor, serverUrl)
} }
@Provides @Provides
......
...@@ -9,8 +9,8 @@ import chat.rocket.android.helper.UserHelper ...@@ -9,8 +9,8 @@ import chat.rocket.android.helper.UserHelper
import chat.rocket.android.infrastructure.LocalRepository import chat.rocket.android.infrastructure.LocalRepository
import chat.rocket.android.main.presentation.MainNavigator import chat.rocket.android.main.presentation.MainNavigator
import chat.rocket.android.server.domain.SettingsRepository import chat.rocket.android.server.domain.SettingsRepository
import chat.rocket.android.server.domain.useSpecialCharsOnRoom
import chat.rocket.android.server.domain.useRealName import chat.rocket.android.server.domain.useRealName
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.retryIO import chat.rocket.android.util.retryIO
...@@ -20,9 +20,12 @@ import chat.rocket.common.model.User ...@@ -20,9 +20,12 @@ import chat.rocket.common.model.User
import chat.rocket.common.model.roomTypeOf import chat.rocket.common.model.roomTypeOf
import chat.rocket.core.internal.realtime.createDirectMessage import chat.rocket.core.internal.realtime.createDirectMessage
import chat.rocket.core.internal.rest.me import chat.rocket.core.internal.rest.me
import chat.rocket.core.internal.rest.show
import kotlinx.coroutines.experimental.withTimeout
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Named import javax.inject.Named
import kotlin.coroutines.experimental.suspendCoroutine
class ChatRoomsPresenter @Inject constructor( class ChatRoomsPresenter @Inject constructor(
private val view: ChatRoomsView, private val view: ChatRoomsView,
...@@ -44,7 +47,7 @@ class ChatRoomsPresenter @Inject constructor( ...@@ -44,7 +47,7 @@ class ChatRoomsPresenter @Inject constructor(
try { try {
val room = dbManager.getRoom(chatRoom.id) val room = dbManager.getRoom(chatRoom.id)
if (room != null) { if (room != null) {
loadChatRoom(room.chatRoom) loadChatRoom(room.chatRoom, true)
} else { } else {
with(chatRoom) { with(chatRoom) {
val entity = ChatRoomEntity( val entity = ChatRoomEntity(
...@@ -53,44 +56,57 @@ class ChatRoomsPresenter @Inject constructor( ...@@ -53,44 +56,57 @@ class ChatRoomsPresenter @Inject constructor(
type = type.toString(), type = type.toString(),
name = username ?: name.toString(), name = username ?: name.toString(),
fullname = name.toString(), fullname = name.toString(),
open = false open = open
) )
loadChatRoom(entity) loadChatRoom(entity, false)
} }
} }
} catch (ex: Exception) {
Timber.d(ex, "Error loading channel")
view.showGenericErrorMessage()
} finally { } finally {
view.hideLoadingRoom() view.hideLoadingRoom()
} }
} }
} }
fun loadChatRoom(chatRoom: ChatRoomEntity) { suspend fun loadChatRoom(chatRoom: ChatRoomEntity, local: Boolean = false) {
with(chatRoom) { with(chatRoom) {
val isDirectMessage = roomTypeOf(type) is RoomType.DirectMessage val isDirectMessage = roomTypeOf(type) is RoomType.DirectMessage
val roomName = if (settings.useSpecialCharsOnRoom() || (isDirectMessage && settings.useRealName())) { val roomName = if (settings.useSpecialCharsOnRoom() || (isDirectMessage && settings.useRealName())) {
fullname ?: name fullname ?: name
} else { } else {
name name
} }
launchUI(strategy) { val myself = getCurrentUser()
val myself = getCurrentUser() if (myself?.username == null) {
if (myself?.username == null) { view.showMessage(R.string.msg_generic_error)
view.showMessage(R.string.msg_generic_error) } else {
} else { val id = if (isDirectMessage && !open) {
val id = if (isDirectMessage && !open) { val fromTo = mutableListOf(myself.id, id).apply {
retryIO("createDirectMessage($name)") { sort()
client.createDirectMessage(name) }
} val roomId = fromTo.joinToString("")
val fromTo = mutableListOf(myself.id, id).apply {
sort() if (local) {
retryIO {
client.show(id, roomTypeOf(RoomType.DIRECT_MESSAGE))
} }
fromTo.joinToString("")
} else {
id id
} else {
retryIO("createDirectMessage($name)") {
withTimeout(10000) {
createDirectMessage(name)
}
}
roomId
} }
} else {
id
}
navigator.toChatRoom( navigator.toChatRoom(
chatRoomId = id, chatRoomId = id,
chatRoomName = roomName, chatRoomName = roomName,
chatRoomType = type, chatRoomType = type,
...@@ -99,8 +115,7 @@ class ChatRoomsPresenter @Inject constructor( ...@@ -99,8 +115,7 @@ class ChatRoomsPresenter @Inject constructor(
isSubscribed = open, isSubscribed = open,
isCreator = ownerId == myself.id || isDirectMessage, isCreator = ownerId == myself.id || isDirectMessage,
isFavorite = favorite ?: false isFavorite = favorite ?: false
) )
}
} }
} }
} }
...@@ -126,4 +141,10 @@ class ChatRoomsPresenter @Inject constructor( ...@@ -126,4 +141,10 @@ class ChatRoomsPresenter @Inject constructor(
} }
return null return null
} }
private suspend fun createDirectMessage(name: String): Boolean = suspendCoroutine { cont ->
client.createDirectMessage(name) { success, _ ->
cont.resume(success)
}
}
} }
\ No newline at end of file
...@@ -21,19 +21,23 @@ abstract class ChatRoomDao : BaseDao<ChatRoomEntity> { ...@@ -21,19 +21,23 @@ abstract class ChatRoomDao : BaseDao<ChatRoomEntity> {
abstract fun get(id: String): ChatRoom? abstract fun get(id: String): ChatRoom?
@Transaction @Transaction
@Query("$BASE_QUERY") @Query("$BASE_QUERY $FILTER_NOT_OPENED")
abstract fun getAllSync(): List<ChatRoom> abstract fun getAllSync(): List<ChatRoom>
@Transaction @Transaction
@Query("""$BASE_QUERY WHERE chatrooms.name LIKE '%' || :query || '%' OR users.name LIKE '%' || :query || '%'""") @Query("""$BASE_QUERY
WHERE chatrooms.name LIKE '%' || :query || '%'
OR users.name LIKE '%' || :query || '%'
""")
abstract fun searchSync(query: String): List<ChatRoom> abstract fun searchSync(query: String): List<ChatRoom>
@Query("SELECT COUNT(id) FROM chatrooms") @Query("SELECT COUNT(id) FROM chatrooms WHERE open = 1")
abstract fun count(): Long abstract fun count(): Long
@Transaction @Transaction
@Query(""" @Query("""
$BASE_QUERY $BASE_QUERY
$FILTER_NOT_OPENED
ORDER BY ORDER BY
CASE CASE
WHEN lastMessageTimeStamp IS NOT NULL THEN lastMessageTimeStamp WHEN lastMessageTimeStamp IS NOT NULL THEN lastMessageTimeStamp
...@@ -45,6 +49,7 @@ abstract class ChatRoomDao : BaseDao<ChatRoomEntity> { ...@@ -45,6 +49,7 @@ abstract class ChatRoomDao : BaseDao<ChatRoomEntity> {
@Transaction @Transaction
@Query(""" @Query("""
$BASE_QUERY $BASE_QUERY
$FILTER_NOT_OPENED
ORDER BY ORDER BY
$TYPE_ORDER, $TYPE_ORDER,
CASE CASE
...@@ -57,6 +62,7 @@ abstract class ChatRoomDao : BaseDao<ChatRoomEntity> { ...@@ -57,6 +62,7 @@ abstract class ChatRoomDao : BaseDao<ChatRoomEntity> {
@Transaction @Transaction
@Query(""" @Query("""
$BASE_QUERY $BASE_QUERY
$FILTER_NOT_OPENED
ORDER BY name ORDER BY name
""") """)
abstract fun getAllAlphabetically(): LiveData<List<ChatRoom>> abstract fun getAllAlphabetically(): LiveData<List<ChatRoom>>
...@@ -64,6 +70,7 @@ abstract class ChatRoomDao : BaseDao<ChatRoomEntity> { ...@@ -64,6 +70,7 @@ abstract class ChatRoomDao : BaseDao<ChatRoomEntity> {
@Transaction @Transaction
@Query(""" @Query("""
$BASE_QUERY $BASE_QUERY
$FILTER_NOT_OPENED
ORDER BY ORDER BY
$TYPE_ORDER, $TYPE_ORDER,
name name
...@@ -113,6 +120,10 @@ abstract class ChatRoomDao : BaseDao<ChatRoomEntity> { ...@@ -113,6 +120,10 @@ abstract class ChatRoomDao : BaseDao<ChatRoomEntity> {
LEFT JOIN users AS lmUsers ON chatrooms.lastMessageUserId = lmUsers.id LEFT JOIN users AS lmUsers ON chatrooms.lastMessageUserId = lmUsers.id
""" """
const val FILTER_NOT_OPENED = """
WHERE chatrooms.open = 1
"""
const val TYPE_ORDER = """ const val TYPE_ORDER = """
CASE CASE
WHEN type = 'c' THEN 1 WHEN type = 'c' THEN 1
......
...@@ -45,6 +45,12 @@ class DatabaseManager(val context: Application, ...@@ -45,6 +45,12 @@ class DatabaseManager(val context: Application,
fun chatRoomDao(): ChatRoomDao = database.chatRoomDao() fun chatRoomDao(): ChatRoomDao = database.chatRoomDao()
fun userDao(): UserDao = database.userDao() fun userDao(): UserDao = database.userDao()
fun clearUsersStatus() {
launch(dbContext) {
userDao().clearStatus()
}
}
fun logout() { fun logout() {
database.clearAllTables() database.clearAllTables()
} }
......
...@@ -13,6 +13,12 @@ import timber.log.Timber ...@@ -13,6 +13,12 @@ import timber.log.Timber
@Dao @Dao
abstract class UserDao : BaseDao<UserEntity> { abstract class UserDao : BaseDao<UserEntity> {
@Query("""
UPDATE users set STATUS = "offline"
""")
abstract fun clearStatus()
@Update(onConflict = OnConflictStrategy.IGNORE) @Update(onConflict = OnConflictStrategy.IGNORE)
abstract fun update(user: UserEntity): Int abstract fun update(user: UserEntity): Int
......
...@@ -71,6 +71,7 @@ class ConnectionManager( ...@@ -71,6 +71,7 @@ class ConnectionManager(
Timber.d("Changing status to: $status") Timber.d("Changing status to: $status")
when (status) { when (status) {
is State.Connected -> { is State.Connected -> {
dbManager.clearUsersStatus()
client.subscribeSubscriptions { _, id -> client.subscribeSubscriptions { _, id ->
Timber.d("Subscribed to subscriptions: $id") Timber.d("Subscribed to subscriptions: $id")
subscriptionId = id subscriptionId = id
......
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