diff --git a/app/src/main/java/net/buzzert/kordophonedroid/ui/conversationlist/ConversationListViewModel.kt b/app/src/main/java/net/buzzert/kordophonedroid/ui/conversationlist/ConversationListViewModel.kt index e8e0abf..61cee6f 100644 --- a/app/src/main/java/net/buzzert/kordophonedroid/ui/conversationlist/ConversationListViewModel.kt +++ b/app/src/main/java/net/buzzert/kordophonedroid/ui/conversationlist/ConversationListViewModel.kt @@ -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> - 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) } } } \ No newline at end of file diff --git a/app/src/main/java/net/buzzert/kordophonedroid/ui/settings/SettingsViewModel.kt b/app/src/main/java/net/buzzert/kordophonedroid/ui/settings/SettingsViewModel.kt index 3813a0b..c12eb5b 100644 --- a/app/src/main/java/net/buzzert/kordophonedroid/ui/settings/SettingsViewModel.kt +++ b/app/src/main/java/net/buzzert/kordophonedroid/ui/settings/SettingsViewModel.kt @@ -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 = 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) + } } } \ No newline at end of file diff --git a/app/src/main/java/net/buzzert/kordophonedroid/ui/shared/ServerConfigRepository.kt b/app/src/main/java/net/buzzert/kordophonedroid/ui/shared/ServerConfigRepository.kt new file mode 100644 index 0000000..0f1147c --- /dev/null +++ b/app/src/main/java/net/buzzert/kordophonedroid/ui/shared/ServerConfigRepository.kt @@ -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 + + fun applyConfig(applicator: ServerConfig.() -> Unit) { + val config = _serverConfig.value.copy() + _serverConfig.value = config.apply(applicator) + } +}