Private
Public Access
1
0

Much better error/badconfig handling

This commit is contained in:
2024-03-23 19:01:20 -07:00
parent 611ad15997
commit f266e04895
7 changed files with 101 additions and 45 deletions

View File

@@ -5,6 +5,7 @@ import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import net.buzzert.kordophone.backend.db.CachedChatDatabase
import net.buzzert.kordophone.backend.server.APIClientFactory
import net.buzzert.kordophone.backend.server.Authentication
import net.buzzert.kordophone.backend.server.ChatRepository
import net.buzzert.kordophone.backend.server.RetrofitAPIClient
@@ -19,18 +20,10 @@ object AppModule {
@Provides
fun provideChatRepository(configRepository: ServerConfigRepository): ChatRepository {
val serverConfig = configRepository.serverConfig.value
val authentication = serverConfig.authentication?.let {
Authentication(it.username, it.password)
}
// TODO: This is really bad error handling...
val baseURL: URL = try { URL(serverConfig.serverName) }
catch (e: java.net.MalformedURLException) { URL("http://localhost") }
val client = RetrofitAPIClient(
baseURL = baseURL,
authentication = authentication ?: Authentication("", "")
)
val server = serverConfig.serverName
val authentication = serverConfig.authentication?.toBackendAuthentication()
val client = APIClientFactory.createClient(server, authentication)
val database = CachedChatDatabase.liveDatabase()
return ChatRepository(client, database)

View File

@@ -20,6 +20,8 @@ import net.buzzert.kordophone.backend.server.ChatRepository
import net.buzzert.kordophonedroid.ui.messagelist.MVM_LOG
import javax.inject.Inject
const val AVM_LOG: String = "AttachmentViewModel"
data class AttachmentFetchData(
val guid: String,
val preview: Boolean = false
@@ -72,7 +74,7 @@ private class AttachmentFetcher(
val data: AttachmentFetchData
): Fetcher {
override suspend fun fetch(): FetchResult {
Log.d(MVM_LOG, "Loading attachment ${data.guid} from network")
Log.d(AVM_LOG, "Loading attachment ${data.guid} from network")
val source = repository.fetchAttachmentDataSource(data.guid, data.preview)
return SourceResult(
source = ImageSource(source, context),

View File

@@ -10,10 +10,13 @@ import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import net.buzzert.kordophone.backend.model.Conversation
import net.buzzert.kordophone.backend.server.APIClientFactory
import net.buzzert.kordophone.backend.server.Authentication
import net.buzzert.kordophone.backend.server.ChatRepository
import net.buzzert.kordophone.backend.server.RetrofitAPIClient
import net.buzzert.kordophonedroid.ui.shared.ServerConfigRepository
import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import java.net.URL
import javax.inject.Inject
@@ -37,21 +40,11 @@ class ConversationListViewModel @Inject constructor(
serverConfigRepository.serverConfig.collect { config ->
Log.d(CL_VM_LOG, "Got settings change.")
// Check settings
try {
val baseURL = URL(config.serverName)
val authentication = config.authentication?.let { serverAuth ->
Authentication(serverAuth.username, serverAuth.password)
} ?: throw Error("No authentication data.")
// Make new APIClient
val apiClient = RetrofitAPIClient(baseURL, authentication)
chatRepository.updateAPIClient(apiClient)
} catch (e: Error) {
Log.e(CL_VM_LOG, "Error re-creating API client for settings change: $e")
} catch (e: java.net.MalformedURLException) {
Log.e(CL_VM_LOG, "Malformed server URL")
}
// Make new APIClient
val baseURL = config.serverName
val authentication = config.authentication?.toBackendAuthentication()
val apiClient = APIClientFactory.createClient(baseURL, authentication)
chatRepository.updateAPIClient(apiClient)
// Perform db synchronization
withContext(Dispatchers.IO) {

View File

@@ -8,6 +8,7 @@ import androidx.security.crypto.MasterKey
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import net.buzzert.kordophone.backend.server.Authentication
import java.lang.reflect.Constructor
import javax.inject.Inject
import javax.inject.Singleton
@@ -84,6 +85,10 @@ data class ServerAuthentication(
apply()
}
}
fun toBackendAuthentication(): Authentication {
return Authentication(username, password)
}
}
@Singleton