mirror of
https://github.com/oxen-io/session-android.git
synced 2023-12-14 02:53:01 +01:00
65 lines
2.2 KiB
Kotlin
65 lines
2.2 KiB
Kotlin
|
package org.thoughtcrime.securesms.preferences
|
||
|
|
||
|
import android.content.Context
|
||
|
import androidx.lifecycle.LiveData
|
||
|
import androidx.lifecycle.MutableLiveData
|
||
|
import androidx.lifecycle.ViewModel
|
||
|
import androidx.lifecycle.viewModelScope
|
||
|
import app.cash.copper.flow.observeQuery
|
||
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||
|
import kotlinx.coroutines.Dispatchers.IO
|
||
|
import kotlinx.coroutines.Dispatchers.Main
|
||
|
import kotlinx.coroutines.SupervisorJob
|
||
|
import kotlinx.coroutines.channels.Channel
|
||
|
import kotlinx.coroutines.flow.collect
|
||
|
import kotlinx.coroutines.flow.onEach
|
||
|
import kotlinx.coroutines.flow.onStart
|
||
|
import kotlinx.coroutines.launch
|
||
|
import kotlinx.coroutines.plus
|
||
|
import kotlinx.coroutines.withContext
|
||
|
import org.session.libsession.utilities.recipients.Recipient
|
||
|
import org.thoughtcrime.securesms.database.DatabaseContentProviders
|
||
|
import org.thoughtcrime.securesms.database.Storage
|
||
|
import javax.inject.Inject
|
||
|
|
||
|
@HiltViewModel
|
||
|
class BlockedContactsViewModel @Inject constructor(private val storage: Storage): ViewModel() {
|
||
|
|
||
|
private val executor = viewModelScope + SupervisorJob()
|
||
|
|
||
|
private val listUpdateChannel = Channel<Unit>(capacity = Channel.CONFLATED)
|
||
|
|
||
|
private val _contacts = MutableLiveData(BlockedContactsViewState(emptyList()))
|
||
|
|
||
|
fun subscribe(context: Context): LiveData<BlockedContactsViewState> {
|
||
|
executor.launch(IO) {
|
||
|
context.contentResolver
|
||
|
.observeQuery(DatabaseContentProviders.Recipient.CONTENT_URI)
|
||
|
.onStart {
|
||
|
listUpdateChannel.trySend(Unit)
|
||
|
}
|
||
|
.onEach {
|
||
|
listUpdateChannel.trySend(Unit)
|
||
|
}
|
||
|
.collect()
|
||
|
}
|
||
|
executor.launch(IO) {
|
||
|
for (update in listUpdateChannel) {
|
||
|
val blockedContactState = BlockedContactsViewState(storage.blockedContacts().sortedBy { it.name })
|
||
|
withContext(Main) {
|
||
|
_contacts.value = blockedContactState
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return _contacts
|
||
|
}
|
||
|
|
||
|
fun unblock(toUnblock: List<Recipient>) {
|
||
|
storage.unblock(toUnblock)
|
||
|
}
|
||
|
|
||
|
data class BlockedContactsViewState(
|
||
|
val blockedContacts: List<Recipient>
|
||
|
)
|
||
|
|
||
|
}
|