Preparations for filtering conversations

This commit is contained in:
Mario Danic 2020-01-04 17:41:44 +01:00
parent 53e4f70c69
commit 51568aa00e
No known key found for this signature in database
GPG Key ID: CDE0BBD2738C4CC0
5 changed files with 28 additions and 8 deletions

View File

@ -57,10 +57,18 @@ class ConversationsRepositoryImpl(val conversationsDao: ConversationsDao) :
.deleteConversation(userId, conversationId)
}
override fun getConversationsForUser(userId: Long): LiveData<List<Conversation>> {
return conversationsDao.getConversationsForUser(userId).distinctUntilChanged().map { data ->
data.map {
it.toConversation()
override fun getConversationsForUser(userId: Long, filter: String?): LiveData<List<Conversation>> {
filter?.let {
return conversationsDao.getConversationsForUserWithFilter(userId, it).distinctUntilChanged().map { data ->
data.map {conversationEntity ->
conversationEntity.toConversation()
}
}
} ?: run {
return conversationsDao.getConversationsForUser(userId).distinctUntilChanged().map { data ->
data.map {
it.toConversation()
}
}
}
}

View File

@ -24,7 +24,7 @@ import androidx.lifecycle.LiveData
import com.nextcloud.talk.models.json.conversations.Conversation
interface ConversationsRepository {
fun getConversationsForUser(userId: Long): LiveData<List<Conversation>>
fun getConversationsForUser(userId: Long, filter: String?): LiveData<List<Conversation>>
fun getShortcutTargetConversations(userId: Long): LiveData<List<Conversation>>
suspend fun getConversationForUserWithToken(userId: Long, token: String): Conversation?

View File

@ -22,8 +22,11 @@ package com.nextcloud.talk.newarch.features.conversationsList
import android.app.Application
import android.graphics.drawable.Drawable
import android.os.Handler
import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.distinctUntilChanged
import androidx.lifecycle.viewModelScope
import coil.Coil
import coil.api.get
@ -47,6 +50,7 @@ import kotlinx.coroutines.launch
import org.koin.core.parameter.parametersOf
import java.util.concurrent.locks.ReentrantLock
class ConversationsListViewModel constructor(
application: Application,
private val getConversationsUseCase: GetConversationsUseCase,
@ -62,13 +66,18 @@ class ConversationsListViewModel constructor(
var messageData: String? = null
val networkStateLiveData: MutableLiveData<ConversationsListViewNetworkState> = MutableLiveData(ConversationsListViewNetworkState.LOADING)
val avatar: MutableLiveData<Drawable> = MutableLiveData(DisplayUtils.getRoundedDrawable(context.getDrawable(R.drawable.ic_settings_white_24dp)))
val conversationsLiveData = Transformations.switchMap(globalService.currentUserLiveData) {
val filterLiveData: MutableLiveData<String?> = MutableLiveData(null)
val conversationsLiveData = Transformations.switchMap(globalService.currentUserLiveData) { user ->
if (networkStateLiveData.value != ConversationsListViewNetworkState.LOADING) {
networkStateLiveData.postValue(ConversationsListViewNetworkState.LOADING)
}
loadConversations()
loadAvatar()
conversationsRepository.getConversationsForUser(it.id!!)
filterLiveData.value = null
Transformations.switchMap(filterLiveData.distinctUntilChanged()) { filter ->
conversationsRepository.getConversationsForUser(user.id!!, filter)
}
}
fun leaveConversation(conversation: Conversation) {

View File

@ -33,7 +33,7 @@ class DebouncingQueryTextListener(
lifecycle: Lifecycle,
private val onDebouncingQueryTextChange: (String?) -> Unit
) : OnQueryTextListener {
var debouncePeriod: Long = 500
private var debouncePeriod: Long = 500
private val coroutineScope = lifecycle.coroutineScope

View File

@ -30,6 +30,9 @@ abstract class ConversationsDao {
@Query("SELECT * FROM conversations WHERE user_id = :userId ORDER BY favorite DESC, last_activity DESC")
abstract fun getConversationsForUser(userId: Long): LiveData<List<ConversationEntity>>
@Query("SELECT * FROM conversations WHERE user_id = :userId AND display_name LIKE '%' || :filter || '%' ORDER BY favorite DESC, last_activity DESC")
abstract fun getConversationsForUserWithFilter(userId: Long, filter: String): LiveData<List<ConversationEntity>>
@Query("SELECT * FROM conversations WHERE user_id = :userId ORDER BY favorite DESC, last_activity DESC LIMIT 4")
abstract fun getShortcutTargetConversations(userId: Long): LiveData<List<ConversationEntity>>