Fixed errors not being displayed

This commit is contained in:
Abdelilah El Aissaoui 2022-01-31 14:17:54 +01:00
parent 2354602e6d
commit fe718fdfec
4 changed files with 29 additions and 19 deletions

View File

@ -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.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.withContext
import javax.inject.Inject import javax.inject.Inject
@TabScope
class ErrorsManager @Inject constructor() { class ErrorsManager @Inject constructor() {
private val _errorsList = MutableStateFlow(listOf<Error>()) private val _errorsList = MutableStateFlow(listOf<Error>())
val errorsList: StateFlow<List<Error>> val errorsList: StateFlow<List<Error>>
get() = _errorsList get() = _errorsList
private val _lastError = MutableStateFlow<Error?>(null) private val _lastError = MutableStateFlow<Error?>(null)
val lastError: StateFlow<Error?> val lastError: StateFlow<Error?> = _lastError
get() = _lastError
fun addError(error: Error) { suspend fun addError(error: Error) = withContext(Dispatchers.IO) {
_errorsList.value = _errorsList.value.toMutableList().apply { _errorsList.value = _errorsList.value.toMutableList().apply {
add(error) add(error)
} }
_lastError.value = error _lastError.value = error
println("LastError flow: ${_lastError.value}")
} }
fun removeError(error: Error) { fun removeError(error: Error) {

View File

@ -1,7 +1,7 @@
package app.git package app.git
import app.app.Error import app.ErrorsManager
import app.app.newErrorNow import app.newErrorNow
import app.di.TabScope import app.di.TabScope
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -18,7 +18,9 @@ import javax.inject.Inject
import kotlin.coroutines.cancellation.CancellationException import kotlin.coroutines.cancellation.CancellationException
@TabScope @TabScope
class TabState @Inject constructor() { class TabState @Inject constructor(
val errorsManager: ErrorsManager,
) {
var git: Git? = null var git: Git? = null
val safeGit: Git val safeGit: Git
get() { get() {
@ -34,10 +36,7 @@ class TabState @Inject constructor() {
private val _refreshData = MutableSharedFlow<RefreshType>() private val _refreshData = MutableSharedFlow<RefreshType>()
val refreshData: Flow<RefreshType> = _refreshData val refreshData: Flow<RefreshType> = _refreshData
suspend fun refreshData(refreshType: RefreshType) = _refreshData.emit(refreshType)
private val _errors = MutableSharedFlow<Error>()
val errors: Flow<Error> = _errors
val managerScope = CoroutineScope(SupervisorJob()) val managerScope = CoroutineScope(SupervisorJob())
@ -65,7 +64,7 @@ class TabState @Inject constructor() {
ex.printStackTrace() ex.printStackTrace()
if (showError) if (showError)
_errors.emit(newErrorNow(ex, ex.localizedMessage)) errorsManager.addError(newErrorNow(ex, ex.localizedMessage))
} finally { } finally {
_processing.value = false _processing.value = false
operationRunning = false operationRunning = false
@ -88,7 +87,7 @@ class TabState @Inject constructor() {
ex.printStackTrace() ex.printStackTrace()
if (showError) if (showError)
_errors.emit(newErrorNow(ex, ex.localizedMessage)) errorsManager.addError(newErrorNow(ex, ex.localizedMessage))
} finally { } finally {
_processing.value = false _processing.value = false
operationRunning = false operationRunning = false

View File

@ -37,12 +37,16 @@ fun AppTab(
val lastError by errorManager.lastError.collectAsState() val lastError by errorManager.lastError.collectAsState()
var showError by remember { mutableStateOf(false) } var showError by remember { mutableStateOf(false) }
if (lastError != null) println("lastError $lastError")
if (lastError != null) {
LaunchedEffect(lastError) { LaunchedEffect(lastError) {
showError = true showError = true
delay(5000) delay(5000)
showError = false showError = false
} }
}
val repositorySelectionStatus by tabViewModel.repositorySelectionStatus.collectAsState() val repositorySelectionStatus by tabViewModel.repositorySelectionStatus.collectAsState()
val isProcessing by tabViewModel.processing.collectAsState() val isProcessing by tabViewModel.processing.collectAsState()
@ -68,7 +72,6 @@ fun AppTab(
Box(modifier = Modifier.fillMaxSize()) { Box(modifier = Modifier.fillMaxSize()) {
Crossfade(targetState = repositorySelectionStatus) { Crossfade(targetState = repositorySelectionStatus) {
@Suppress("UnnecessaryVariable") // Don't inline it because smart cast won't work @Suppress("UnnecessaryVariable") // Don't inline it because smart cast won't work
when (repositorySelectionStatus) { when (repositorySelectionStatus) {
RepositorySelectionStatus.None -> { RepositorySelectionStatus.None -> {
@ -90,6 +93,7 @@ fun AppTab(
val safeLastError = lastError val safeLastError = lastError
if (safeLastError != null) { if (safeLastError != null) {
println("safeLastError $safeLastError\nshowError $showError")
AnimatedVisibility( AnimatedVisibility(
visible = showError, visible = showError,
modifier = Modifier modifier = Modifier
@ -122,7 +126,7 @@ fun AppTab(
) // TODO Add more descriptive title ) // TODO Add more descriptive title
Text( Text(
text = safeLastError.message, text = lastError?.message ?: "",
modifier = Modifier modifier = Modifier
.padding(top = 8.dp, bottom = 16.dp) .padding(top = 8.dp, bottom = 16.dp)
.widthIn(max = 600.dp) .widthIn(max = 600.dp)

View File

@ -1,16 +1,19 @@
package app.viewmodels package app.viewmodels
import app.AppStateManager import app.AppStateManager
import app.app.ErrorsManager import app.ErrorsManager
import app.app.newErrorNow import app.newErrorNow
import app.credentials.CredentialsState import app.credentials.CredentialsState
import app.credentials.CredentialsStateManager import app.credentials.CredentialsStateManager
import app.git.* import app.git.*
import app.ui.SelectedItem import app.ui.SelectedItem
import kotlinx.coroutines.* import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.eclipse.jgit.api.Git import org.eclipse.jgit.api.Git
import org.eclipse.jgit.lib.ObjectId import org.eclipse.jgit.lib.ObjectId
import org.eclipse.jgit.lib.Repository import org.eclipse.jgit.lib.Repository
@ -34,10 +37,10 @@ class TabViewModel @Inject constructor(
private val repositoryManager: RepositoryManager, private val repositoryManager: RepositoryManager,
private val remoteOperationsManager: RemoteOperationsManager, private val remoteOperationsManager: RemoteOperationsManager,
private val tabState: TabState, private val tabState: TabState,
val errorsManager: ErrorsManager,
val appStateManager: AppStateManager, val appStateManager: AppStateManager,
private val fileChangesWatcher: FileChangesWatcher, private val fileChangesWatcher: FileChangesWatcher,
) { ) {
val errorsManager: ErrorsManager = tabState.errorsManager
private val _selectedItem = MutableStateFlow<SelectedItem>(SelectedItem.None) private val _selectedItem = MutableStateFlow<SelectedItem>(SelectedItem.None)
val selectedItem: StateFlow<SelectedItem> = _selectedItem val selectedItem: StateFlow<SelectedItem> = _selectedItem