Private
Public Access
1
0

Adds ServerConfigRepository that view models listen to

* app/src/main/java/net/buzzert/kordophonedroid/ui/shared/ServerConfigRepository.kt:
This commit is contained in:
2024-02-26 01:10:55 -08:00
parent 94bb9baa72
commit 175a83ca21
3 changed files with 63 additions and 5 deletions

View File

@@ -1,6 +1,7 @@
package net.buzzert.kordophonedroid.ui.conversationlist
import android.util.Log
import androidx.compose.runtime.collectAsState
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
@@ -12,15 +13,20 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import net.buzzert.kordophone.backend.model.Conversation
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 kotlin.coroutines.CoroutineContext
const val CL_VM_LOG: String = "ConversationListViewModel"
@HiltViewModel
class ConversationListViewModel @Inject constructor(
private val repository: ChatRepository
private val chatRepository: ChatRepository,
private val serverConfigRepository: ServerConfigRepository,
) : ViewModel() {
val conversations: Flow<List<Conversation>>
get() = repository.conversationChanges
get() = chatRepository.conversationChanges
.map {
it.sortedBy { it.date }
.reversed()
@@ -30,14 +36,26 @@ class ConversationListViewModel @Inject constructor(
// TODO: Is this the best place to put these?
// TODO: Need error handling (exceptions thrown below)
// Perform initial sync
viewModelScope.launch {
withContext(Dispatchers.IO) {
repository.synchronize()
chatRepository.synchronize()
}
}
// Watch for config changes
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)
}
}
}

View File

@@ -6,10 +6,14 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import net.buzzert.kordophonedroid.ui.shared.ServerAuthentication
import net.buzzert.kordophonedroid.ui.shared.ServerConfigRepository
import javax.inject.Inject
@HiltViewModel
class SettingsViewModel @Inject constructor() : ViewModel() {
class SettingsViewModel @Inject constructor(
val serverConfigRepository: ServerConfigRepository
) : ViewModel() {
private val _serverPreference: MutableStateFlow<String> = MutableStateFlow("")
var serverPreference = _serverPreference.asStateFlow()
@@ -21,10 +25,18 @@ class SettingsViewModel @Inject constructor() : ViewModel() {
fun saveServerPreference(serverName: String) {
_serverPreference.value = serverName
serverConfigRepository.applyConfig {
this.serverName = serverName
}
}
fun saveAuthenticationPreferences(username: String, password: String) {
_usernamePreference.value = username
_passwordPreference.value = password
serverConfigRepository.applyConfig {
authentication = ServerAuthentication(username, password)
}
}
}

View File

@@ -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)
}
}