Adds ServerConfigRepository that view models listen to
* app/src/main/java/net/buzzert/kordophonedroid/ui/shared/ServerConfigRepository.kt:
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package net.buzzert.kordophonedroid.ui.conversationlist
|
package net.buzzert.kordophonedroid.ui.conversationlist
|
||||||
|
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
|
import androidx.compose.runtime.collectAsState
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
@@ -12,15 +13,20 @@ import kotlinx.coroutines.launch
|
|||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import net.buzzert.kordophone.backend.model.Conversation
|
import net.buzzert.kordophone.backend.model.Conversation
|
||||||
import net.buzzert.kordophone.backend.server.ChatRepository
|
import net.buzzert.kordophone.backend.server.ChatRepository
|
||||||
|
import net.buzzert.kordophone.backend.server.REPO_LOG
|
||||||
|
import net.buzzert.kordophonedroid.ui.shared.ServerConfigRepository
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import kotlin.coroutines.CoroutineContext
|
import kotlin.coroutines.CoroutineContext
|
||||||
|
|
||||||
|
const val CL_VM_LOG: String = "ConversationListViewModel"
|
||||||
|
|
||||||
@HiltViewModel
|
@HiltViewModel
|
||||||
class ConversationListViewModel @Inject constructor(
|
class ConversationListViewModel @Inject constructor(
|
||||||
private val repository: ChatRepository
|
private val chatRepository: ChatRepository,
|
||||||
|
private val serverConfigRepository: ServerConfigRepository,
|
||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
val conversations: Flow<List<Conversation>>
|
val conversations: Flow<List<Conversation>>
|
||||||
get() = repository.conversationChanges
|
get() = chatRepository.conversationChanges
|
||||||
.map {
|
.map {
|
||||||
it.sortedBy { it.date }
|
it.sortedBy { it.date }
|
||||||
.reversed()
|
.reversed()
|
||||||
@@ -30,14 +36,26 @@ class ConversationListViewModel @Inject constructor(
|
|||||||
// TODO: Is this the best place to put these?
|
// TODO: Is this the best place to put these?
|
||||||
// TODO: Need error handling (exceptions thrown below)
|
// TODO: Need error handling (exceptions thrown below)
|
||||||
|
|
||||||
|
// Perform initial sync
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
withContext(Dispatchers.IO) {
|
withContext(Dispatchers.IO) {
|
||||||
repository.synchronize()
|
chatRepository.synchronize()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Watch for config changes
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
repository.beginWatchingForUpdates(this)
|
serverConfigRepository.serverConfig.collect {
|
||||||
|
Log.d(CL_VM_LOG, "Got settings change.")
|
||||||
|
|
||||||
|
// TODO: Respond to this change.
|
||||||
|
// Should probably just forward this directly to ChatRepository.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start watching for updates
|
||||||
|
viewModelScope.launch {
|
||||||
|
chatRepository.beginWatchingForUpdates(this)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,10 +6,14 @@ import dagger.hilt.android.lifecycle.HiltViewModel
|
|||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.asSharedFlow
|
import kotlinx.coroutines.flow.asSharedFlow
|
||||||
import kotlinx.coroutines.flow.asStateFlow
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
|
import net.buzzert.kordophonedroid.ui.shared.ServerAuthentication
|
||||||
|
import net.buzzert.kordophonedroid.ui.shared.ServerConfigRepository
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@HiltViewModel
|
@HiltViewModel
|
||||||
class SettingsViewModel @Inject constructor() : ViewModel() {
|
class SettingsViewModel @Inject constructor(
|
||||||
|
val serverConfigRepository: ServerConfigRepository
|
||||||
|
) : ViewModel() {
|
||||||
private val _serverPreference: MutableStateFlow<String> = MutableStateFlow("")
|
private val _serverPreference: MutableStateFlow<String> = MutableStateFlow("")
|
||||||
var serverPreference = _serverPreference.asStateFlow()
|
var serverPreference = _serverPreference.asStateFlow()
|
||||||
|
|
||||||
@@ -21,10 +25,18 @@ class SettingsViewModel @Inject constructor() : ViewModel() {
|
|||||||
|
|
||||||
fun saveServerPreference(serverName: String) {
|
fun saveServerPreference(serverName: String) {
|
||||||
_serverPreference.value = serverName
|
_serverPreference.value = serverName
|
||||||
|
|
||||||
|
serverConfigRepository.applyConfig {
|
||||||
|
this.serverName = serverName
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun saveAuthenticationPreferences(username: String, password: String) {
|
fun saveAuthenticationPreferences(username: String, password: String) {
|
||||||
_usernamePreference.value = username
|
_usernamePreference.value = username
|
||||||
_passwordPreference.value = password
|
_passwordPreference.value = password
|
||||||
|
|
||||||
|
serverConfigRepository.applyConfig {
|
||||||
|
authentication = ServerAuthentication(username, password)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package net.buzzert.kordophonedroid.ui.shared
|
||||||
|
|
||||||
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
|
import javax.inject.Inject
|
||||||
|
import javax.inject.Singleton
|
||||||
|
|
||||||
|
data class ServerConfig(
|
||||||
|
var serverName: String? = null,
|
||||||
|
var authentication: ServerAuthentication? = null,
|
||||||
|
)
|
||||||
|
|
||||||
|
data class ServerAuthentication(
|
||||||
|
val username: String,
|
||||||
|
val password: String,
|
||||||
|
)
|
||||||
|
|
||||||
|
@Singleton
|
||||||
|
class ServerConfigRepository @Inject constructor() {
|
||||||
|
// TODO: Initial config should be loaded from device settings.
|
||||||
|
private val _serverConfig = MutableStateFlow(ServerConfig()) // Initial config
|
||||||
|
val serverConfig: StateFlow<ServerConfig> = _serverConfig
|
||||||
|
|
||||||
|
fun applyConfig(applicator: ServerConfig.() -> Unit) {
|
||||||
|
val config = _serverConfig.value.copy()
|
||||||
|
_serverConfig.value = config.apply(applicator)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user