Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow remote resource files that do not exist #9

Merged
merged 3 commits into from
Jan 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions app/src/main/java/org/dianqk/ruslin/ui/page/notes/NotesPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@ package org.dianqk.ruslin.ui.page.notes
import android.util.Log
import androidx.activity.compose.BackHandler
import androidx.compose.animation.core.*
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.*
import androidx.compose.material.icons.outlined.Close
import androidx.compose.material.icons.outlined.DeleteForever
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.zIndex
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -179,6 +182,38 @@ fun NotesPage(
}
}
) { innerPadding ->
uiState.syncResult?.onFailure { e ->
Box(
modifier = Modifier
.padding(innerPadding)
.background(MaterialTheme.colorScheme.onError)
.zIndex(1f)
) {
Row(modifier = Modifier.padding(horizontal = 16.dp, vertical = 10.dp)) {
Column(modifier = Modifier.weight(1f)) {
Text(
text = stringResource(id = R.string.sync_failed),
style = MaterialTheme.typography.labelLarge,
color = MaterialTheme.colorScheme.error
)
SelectionContainer {
Text(
text = e.localizedMessage ?: e.toString(),
style = MaterialTheme.typography.labelSmall,
color = MaterialTheme.colorScheme.error
)
}
}
IconButton(onClick = viewModel::dismissSyncErrorMessage) {
Icon(
imageVector = Icons.Outlined.Close,
tint = MaterialTheme.colorScheme.error,
contentDescription = null
)
}
}
}
}
if (notes == null) {
ContentLoadingState {
Text(text = stringResource(id = R.string.notes_loading))
Expand Down
41 changes: 30 additions & 11 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 @@ -35,7 +36,8 @@ data class NotesUiState(
val isLoading: Boolean = false,
val conflictNoteExists: Boolean = false,
val showConflictNotes: Boolean = false,
val isSyncing: Boolean = false
val isSyncing: Boolean = false,
val syncResult: Result<FfiSyncInfo>? = null,
)

const val TAG = "NotesViewModel"
Expand All @@ -54,21 +56,32 @@ class NotesViewModel @Inject constructor(
checkConflictNoteExists()
viewModelScope.launch {
notesRepository.syncFinished.collect { syncResult ->
syncResult.onSuccess { syncInfo ->
if (syncInfo.conflictNoteCount > 0
|| syncInfo.otherConflictCount > 0
|| syncInfo.pullCount > 0
|| syncInfo.deleteCount > 0
) {
reloadAllAfterSync()
}
_uiState.update {
it.copy(syncResult = syncResult)
}
syncResult
.onSuccess { syncInfo ->
if (syncInfo.conflictNoteCount > 0
|| syncInfo.otherConflictCount > 0
|| syncInfo.pullCount > 0
|| syncInfo.deleteCount > 0
) {
reloadAllAfterSync()
}
}
.onFailure { e ->
Log.d(TAG, "sync failed: $e")
}
}
}
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 @@ -107,6 +120,12 @@ class NotesViewModel @Inject constructor(
}
}

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

private fun reloadAllAfterSync() {
loadAbbrNotes()
loadFolders()
Expand All @@ -117,7 +136,7 @@ class NotesViewModel @Inject constructor(
val showConflictNotes = uiState.value.showConflictNotes
_uiState.update {
it.copy(
isLoading = true
isLoading = true,
)
}
viewModelScope.launch {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,5 @@
<string name="info_copied">信息已复制到剪贴板</string>
<string name="credits">致谢</string>
<string name="credits_desc">资源与开源软件</string>
<string name="sync_failed">同步失败</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,5 @@
<string name="github_issue">GitHub issue</string>
<string name="github_issue_desc">Submit an issue for bug report or feature request</string>
<string name="info_copied">Info copied to clipboard</string>
<string name="sync_failed">Sync failed</string>
</resources>