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