Skip to content

Commit

Permalink
Added update error toast
Browse files Browse the repository at this point in the history
  • Loading branch information
sirekanian committed Feb 11, 2024
1 parent 1bbf11a commit fa08a60
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
10 changes: 10 additions & 0 deletions app/src/main/java/org/sirekanyan/outline/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.core.view.WindowCompat
import kotlinx.coroutines.launch
import org.sirekanyan.outline.ext.showToast
import org.sirekanyan.outline.ui.AboutDialogContent
import org.sirekanyan.outline.ui.AddServerContent
import org.sirekanyan.outline.ui.DeleteKeyContent
Expand All @@ -25,6 +29,12 @@ class MainActivity : ComponentActivity() {
val router = rememberRouter()
val state = rememberMainState(router)
OutlineTheme {
val context = LocalContext.current
LaunchedEffect(Unit) {
launch {
state.observeToasts().collect(context::showToast)
}
}
Surface(Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
BackHandler(state.drawer.isOpen) {
state.closeDrawer()
Expand Down
14 changes: 13 additions & 1 deletion app/src/main/java/org/sirekanyan/outline/MainState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.platform.LocalContext
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
Expand All @@ -19,6 +21,8 @@ import kotlinx.parcelize.Parcelize
import org.sirekanyan.outline.api.model.Key
import org.sirekanyan.outline.api.model.Server
import org.sirekanyan.outline.db.KeyValueDao
import org.sirekanyan.outline.ext.LocalizedString
import org.sirekanyan.outline.ext.PluralsResource
import org.sirekanyan.outline.ext.asyncAll
import org.sirekanyan.outline.ext.rememberStateScope
import org.sirekanyan.outline.feature.keys.KeysErrorState
Expand Down Expand Up @@ -52,6 +56,8 @@ class MainState(
private val prefs: KeyValueDao,
) : CoroutineScope by scope {

private val toasts = MutableSharedFlow<LocalizedString>()
fun observeToasts(): Flow<LocalizedString> = toasts
val drawer = router.drawer
val drawerDisabled by derivedStateOf { search.isOpened && drawer.isClosed }
var page by router.pageState
Expand Down Expand Up @@ -99,14 +105,20 @@ class MainState(
withContext(Dispatchers.IO) {
page.keys = KeysLoadingState
page.keys = try {
servers.getServers()
val errors = servers.getServers()
.asyncAll { server ->
runCatching {
withTimeout(5.seconds) {
keys.updateKeys(server)
}
}
}
.mapNotNull { result ->
result.exceptionOrNull()
}
if (errors.isNotEmpty()) {
toasts.emit(PluralsResource(R.plurals.outln_update_error, errors.size))
}
KeysIdleState
} catch (exception: Exception) {
exception.printStackTrace()
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/org/sirekanyan/outline/ext/LocalizedString.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.sirekanyan.outline.ext

import android.content.Context
import androidx.annotation.PluralsRes
import androidx.annotation.StringRes

fun Context.getString(resource: LocalizedString): String =
when (resource) {
is StringResource ->
resources.getString(resource.id)
is PluralsResource ->
resources.getQuantityString(resource.id, resource.quantity, resource.quantity)
}

sealed class LocalizedString

class StringResource(@StringRes val id: Int) : LocalizedString()

class PluralsResource(@PluralsRes val id: Int, val quantity: Int) : LocalizedString()
4 changes: 4 additions & 0 deletions app/src/main/java/org/sirekanyan/outline/ext/Toast.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ import androidx.annotation.StringRes
fun Context.showToast(@StringRes text: Int) {
Toast.makeText(this, text, Toast.LENGTH_SHORT).show()
}

fun Context.showToast(text: LocalizedString) {
Toast.makeText(this, getString(text), Toast.LENGTH_SHORT).show()
}

0 comments on commit fa08a60

Please sign in to comment.