Skip to content

Commit

Permalink
Use synchronization results to display error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
DianQK committed Jan 29, 2023
1 parent dfd13cb commit 92fc87d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ fun NotesPage(
}
}
) { innerPadding ->
uiState.syncErrorMessage?.let { errorMessage ->
uiState.syncResult?.onFailure { e ->
Box(
modifier = Modifier
.padding(innerPadding)
Expand All @@ -198,7 +198,7 @@ fun NotesPage(
)
SelectionContainer {
Text(
text = errorMessage,
text = e.localizedMessage ?: e.toString(),
style = MaterialTheme.typography.labelSmall,
color = MaterialTheme.colorScheme.error
)
Expand Down
21 changes: 11 additions & 10 deletions app/src/main/java/org/dianqk/ruslin/ui/page/notes/NotesViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import kotlinx.coroutines.withContext
import org.dianqk.ruslin.data.NotesRepository
import uniffi.ruslin.FfiAbbrNote
import uniffi.ruslin.FfiFolder
import uniffi.ruslin.FfiSyncInfo
import javax.inject.Inject

data class Folder(
Expand All @@ -36,7 +37,7 @@ data class NotesUiState(
val conflictNoteExists: Boolean = false,
val showConflictNotes: Boolean = false,
val isSyncing: Boolean = false,
val syncErrorMessage: String? = null,
val syncResult: Result<FfiSyncInfo>? = null,
)

const val TAG = "NotesViewModel"
Expand All @@ -55,6 +56,9 @@ class NotesViewModel @Inject constructor(
checkConflictNoteExists()
viewModelScope.launch {
notesRepository.syncFinished.collect { syncResult ->
_uiState.update {
it.copy(syncResult = syncResult)
}
syncResult
.onSuccess { syncInfo ->
if (syncInfo.conflictNoteCount > 0
Expand All @@ -67,16 +71,17 @@ class NotesViewModel @Inject constructor(
}
.onFailure { e ->
Log.d(TAG, "sync failed: $e")
_uiState.update {
it.copy(syncErrorMessage = e.localizedMessage ?: e.toString())
}
}
}
}
viewModelScope.launch {
notesRepository.isSyncing.collect { isSyncing ->
_uiState.update {
it.copy(isSyncing = isSyncing)
if (isSyncing) {
it.copy(isSyncing = true, syncResult = null)
} else {
it.copy(isSyncing = false)
}
}
}
}
Expand Down Expand Up @@ -110,17 +115,14 @@ class NotesViewModel @Inject constructor(
fun syncConfigExists(): Boolean = notesRepository.syncConfigExists()

fun sync() {
_uiState.update {
it.copy(syncErrorMessage = null)
}
viewModelScope.launch {
notesRepository.doSync(isOnStart = false, fromScratch = false)
}
}

fun dismissSyncErrorMessage() {
_uiState.update {
it.copy(syncErrorMessage = null)
it.copy(syncResult = null)
}
}

Expand All @@ -135,7 +137,6 @@ class NotesViewModel @Inject constructor(
_uiState.update {
it.copy(
isLoading = true,
syncErrorMessage = null,
)
}
viewModelScope.launch {
Expand Down

0 comments on commit 92fc87d

Please sign in to comment.