From fe718fdfec21076d9f0b3b2749cc2727e5163bd5 Mon Sep 17 00:00:00 2001 From: Abdelilah El Aissaoui Date: Mon, 31 Jan 2022 14:17:54 +0100 Subject: [PATCH] Fixed errors not being displayed --- src/main/kotlin/app/ErrorsManager.kt | 12 ++++++++---- src/main/kotlin/app/git/TabState.kt | 15 +++++++-------- src/main/kotlin/app/ui/AppTab.kt | 10 +++++++--- src/main/kotlin/app/viewmodels/TabViewModel.kt | 11 +++++++---- 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/main/kotlin/app/ErrorsManager.kt b/src/main/kotlin/app/ErrorsManager.kt index 514bc29..e1a8408 100644 --- a/src/main/kotlin/app/ErrorsManager.kt +++ b/src/main/kotlin/app/ErrorsManager.kt @@ -1,24 +1,28 @@ -package app.app +package app +import app.di.TabScope +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.withContext import javax.inject.Inject +@TabScope class ErrorsManager @Inject constructor() { private val _errorsList = MutableStateFlow(listOf()) val errorsList: StateFlow> get() = _errorsList private val _lastError = MutableStateFlow(null) - val lastError: StateFlow - get() = _lastError + val lastError: StateFlow = _lastError - fun addError(error: Error) { + suspend fun addError(error: Error) = withContext(Dispatchers.IO) { _errorsList.value = _errorsList.value.toMutableList().apply { add(error) } _lastError.value = error + println("LastError flow: ${_lastError.value}") } fun removeError(error: Error) { diff --git a/src/main/kotlin/app/git/TabState.kt b/src/main/kotlin/app/git/TabState.kt index 8628767..731dbcb 100644 --- a/src/main/kotlin/app/git/TabState.kt +++ b/src/main/kotlin/app/git/TabState.kt @@ -1,7 +1,7 @@ package app.git -import app.app.Error -import app.app.newErrorNow +import app.ErrorsManager +import app.newErrorNow import app.di.TabScope import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -18,7 +18,9 @@ import javax.inject.Inject import kotlin.coroutines.cancellation.CancellationException @TabScope -class TabState @Inject constructor() { +class TabState @Inject constructor( + val errorsManager: ErrorsManager, +) { var git: Git? = null val safeGit: Git get() { @@ -34,10 +36,7 @@ class TabState @Inject constructor() { private val _refreshData = MutableSharedFlow() val refreshData: Flow = _refreshData - suspend fun refreshData(refreshType: RefreshType) = _refreshData.emit(refreshType) - private val _errors = MutableSharedFlow() - val errors: Flow = _errors val managerScope = CoroutineScope(SupervisorJob()) @@ -65,7 +64,7 @@ class TabState @Inject constructor() { ex.printStackTrace() if (showError) - _errors.emit(newErrorNow(ex, ex.localizedMessage)) + errorsManager.addError(newErrorNow(ex, ex.localizedMessage)) } finally { _processing.value = false operationRunning = false @@ -88,7 +87,7 @@ class TabState @Inject constructor() { ex.printStackTrace() if (showError) - _errors.emit(newErrorNow(ex, ex.localizedMessage)) + errorsManager.addError(newErrorNow(ex, ex.localizedMessage)) } finally { _processing.value = false operationRunning = false diff --git a/src/main/kotlin/app/ui/AppTab.kt b/src/main/kotlin/app/ui/AppTab.kt index 6ffee10..3184ee1 100644 --- a/src/main/kotlin/app/ui/AppTab.kt +++ b/src/main/kotlin/app/ui/AppTab.kt @@ -37,12 +37,16 @@ fun AppTab( val lastError by errorManager.lastError.collectAsState() var showError by remember { mutableStateOf(false) } - if (lastError != null) + println("lastError $lastError") + + + if (lastError != null) { LaunchedEffect(lastError) { showError = true delay(5000) showError = false } + } val repositorySelectionStatus by tabViewModel.repositorySelectionStatus.collectAsState() val isProcessing by tabViewModel.processing.collectAsState() @@ -68,7 +72,6 @@ fun AppTab( Box(modifier = Modifier.fillMaxSize()) { Crossfade(targetState = repositorySelectionStatus) { - @Suppress("UnnecessaryVariable") // Don't inline it because smart cast won't work when (repositorySelectionStatus) { RepositorySelectionStatus.None -> { @@ -90,6 +93,7 @@ fun AppTab( val safeLastError = lastError if (safeLastError != null) { + println("safeLastError $safeLastError\nshowError $showError") AnimatedVisibility( visible = showError, modifier = Modifier @@ -122,7 +126,7 @@ fun AppTab( ) // TODO Add more descriptive title Text( - text = safeLastError.message, + text = lastError?.message ?: "", modifier = Modifier .padding(top = 8.dp, bottom = 16.dp) .widthIn(max = 600.dp) diff --git a/src/main/kotlin/app/viewmodels/TabViewModel.kt b/src/main/kotlin/app/viewmodels/TabViewModel.kt index 53fe306..32d6647 100644 --- a/src/main/kotlin/app/viewmodels/TabViewModel.kt +++ b/src/main/kotlin/app/viewmodels/TabViewModel.kt @@ -1,16 +1,19 @@ package app.viewmodels import app.AppStateManager -import app.app.ErrorsManager -import app.app.newErrorNow +import app.ErrorsManager +import app.newErrorNow import app.credentials.CredentialsState import app.credentials.CredentialsStateManager import app.git.* import app.ui.SelectedItem -import kotlinx.coroutines.* +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.cancel import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import org.eclipse.jgit.api.Git import org.eclipse.jgit.lib.ObjectId import org.eclipse.jgit.lib.Repository @@ -34,10 +37,10 @@ class TabViewModel @Inject constructor( private val repositoryManager: RepositoryManager, private val remoteOperationsManager: RemoteOperationsManager, private val tabState: TabState, - val errorsManager: ErrorsManager, val appStateManager: AppStateManager, private val fileChangesWatcher: FileChangesWatcher, ) { + val errorsManager: ErrorsManager = tabState.errorsManager private val _selectedItem = MutableStateFlow(SelectedItem.None) val selectedItem: StateFlow = _selectedItem