Catch exceptions during app startup

This commit is contained in:
Damontecres 2025-12-25 08:49:57 -05:00
parent 72f910582f
commit c00c7b8aa9
No known key found for this signature in database

View file

@ -56,10 +56,7 @@ import com.github.damontecres.wholphin.ui.util.ProvideLocalClock
import com.github.damontecres.wholphin.util.DebugLogTree import com.github.damontecres.wholphin.util.DebugLogTree
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.firstOrNull import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import org.jellyfin.sdk.model.serializer.toUUIDOrNull import org.jellyfin.sdk.model.serializer.toUUIDOrNull
import timber.log.Timber import timber.log.Timber
@ -269,16 +266,16 @@ class MainActivityViewModel
private val backdropService: BackdropService, private val backdropService: BackdropService,
) : ViewModel() { ) : ViewModel() {
fun appStart() { fun appStart() {
viewModelScope.launch { viewModelScope.launchIO {
val prefs = preferences.data.firstOrNull() ?: AppPreferences.getDefaultInstance() try {
val prefs =
preferences.data.firstOrNull() ?: AppPreferences.getDefaultInstance()
if (prefs.signInAutomatically) { if (prefs.signInAutomatically) {
val current = val current =
withContext(Dispatchers.IO) {
serverRepository.restoreSession( serverRepository.restoreSession(
prefs.currentServerId?.toUUIDOrNull(), prefs.currentServerId?.toUUIDOrNull(),
prefs.currentUserId?.toUUIDOrNull(), prefs.currentUserId?.toUUIDOrNull(),
) )
}
if (current != null) { if (current != null) {
// Restored // Restored
navigationManager.navigateTo(SetupDestination.AppContent(current)) navigationManager.navigateTo(SetupDestination.AppContent(current))
@ -292,9 +289,7 @@ class MainActivityViewModel
val currentServerId = prefs.currentServerId?.toUUIDOrNull() val currentServerId = prefs.currentServerId?.toUUIDOrNull()
if (currentServerId != null) { if (currentServerId != null) {
val currentServer = val currentServer =
withContext(Dispatchers.IO) {
serverRepository.serverDao.getServer(currentServerId)?.server serverRepository.serverDao.getServer(currentServerId)?.server
}
if (currentServer != null) { if (currentServer != null) {
navigationManager.navigateTo(SetupDestination.UserList(currentServer)) navigationManager.navigateTo(SetupDestination.UserList(currentServer))
} else { } else {
@ -304,6 +299,10 @@ class MainActivityViewModel
navigationManager.navigateTo(SetupDestination.ServerList) navigationManager.navigateTo(SetupDestination.ServerList)
} }
} }
} catch (ex: Exception) {
Timber.e(ex, "Error during appStart")
navigationManager.navigateTo(SetupDestination.ServerList)
}
} }
viewModelScope.launchIO { viewModelScope.launchIO {
// Create the mediaCodecCapabilitiesTest if needed // Create the mediaCodecCapabilitiesTest if needed