Skip to content

Commit

Permalink
Change error toast to a snackbar with a dialog that allows copying
Browse files Browse the repository at this point in the history
  • Loading branch information
chvp committed Feb 4, 2024
1 parent 129a633 commit caae00b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 12 deletions.
52 changes: 45 additions & 7 deletions app/src/main/java/be/chvp/nanoledger/ui/add/AddActivity.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package be.chvp.nanoledger.ui.add

import android.app.Activity
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.BackHandler
import androidx.activity.compose.setContent
Expand All @@ -21,6 +23,7 @@ import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.Done
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.DatePicker
import androidx.compose.material3.DatePickerDialog
Expand All @@ -36,6 +39,10 @@ import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarDuration
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.SnackbarResult
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.TextField
Expand All @@ -47,6 +54,7 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
Expand Down Expand Up @@ -79,15 +87,27 @@ class AddActivity() : ComponentActivity() {
val context = LocalContext.current
val latestError by addViewModel.latestError.observeAsState()
val errorMessage = stringResource(R.string.error_writing_file)
val showMessage = stringResource(R.string.show)
val scope = rememberCoroutineScope()
val snackbarHostState = remember { SnackbarHostState() }
var openErrorDialog by rememberSaveable { mutableStateOf(false) }
var errorDialogMessage by rememberSaveable { mutableStateOf("") }
LaunchedEffect(latestError) {
val error = latestError?.get()
if (error != null) {
Log.e("be.chvp.nanoledger", "Exception while writing file", error)
Toast.makeText(
context,
errorMessage,
Toast.LENGTH_LONG,
).show()
scope.launch {
val result =
snackbarHostState.showSnackbar(
message = errorMessage,
actionLabel = showMessage,
duration = SnackbarDuration.Long,
)
if (result == SnackbarResult.ActionPerformed) {
openErrorDialog = true
errorDialogMessage = error.stackTraceToString()
}
}
}
}
BackHandler(enabled = true) {
Expand All @@ -98,13 +118,13 @@ class AddActivity() : ComponentActivity() {
),
)
}
val scope = rememberCoroutineScope()
val saving by addViewModel.saving.observeAsState()
val valid by addViewModel.valid.observeAsState()
val enabled = !(saving ?: true) && (valid ?: false)
NanoLedgerTheme {
Scaffold(
topBar = { Bar() },
snackbarHost = { SnackbarHost(hostState = snackbarHostState) },
floatingActionButton = {
FloatingActionButton(
onClick = {
Expand Down Expand Up @@ -143,6 +163,24 @@ class AddActivity() : ComponentActivity() {
},
) { contentPadding ->
Box(modifier = Modifier.padding(contentPadding).fillMaxSize()) {
if (openErrorDialog) {
AlertDialog(
onDismissRequest = { openErrorDialog = false },
confirmButton = {
TextButton(onClick = {
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager

val clip: ClipData = ClipData.newPlainText("simple text", errorDialogMessage)
clipboard.setPrimaryClip(clip)
}) { Text(stringResource(R.string.copy)) }
},
title = { Text(stringResource(R.string.error)) },
text = { Text(errorDialogMessage) },
dismissButton = {
TextButton(onClick = { openErrorDialog = false }) { Text(stringResource(R.string.dismiss)) }
},
)
}
Column(
modifier =
Modifier
Expand Down
14 changes: 9 additions & 5 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,29 @@
<string name="add_transaction">Add transaction</string>
<string name="back">Back</string>
<string name="cancel">Cancel</string>
<string name="change_default_currency">Change default currency</string>
<string name="copy">Copy</string>
<string name="currency_amount_order">Order of currency and amount</string>
<string name="currency_order_before">Currency before amount</string>
<string name="currency_order_after">Currency after amount</string>
<string name="currency_order_before">Currency before amount</string>
<string name="date">Date</string>
<string name="change_default_currency">Change default currency</string>
<string name="default_currency">Default currency</string>
<string name="default_status">Default status</string>
<string name="dismiss">Dismiss</string>
<string name="error">Error</string>
<string name="error_reading_file">Error while reading file</string>
<string name="error_writing_file">Error while writing file</string>
<string name="file">File</string>
<string name="note">Note</string>
<string name="ok">OK</string>
<string name="payee">Payee</string>
<string name="save">Save</string>
<string name="select_file">Select file…</string>
<string name="settings">Settings</string>
<string name="status_unmarked">Unmarked</string>
<string name="status_pending">Pending (!)</string>
<string name="show">Show</string>
<string name="status_cleared">Cleared (*)</string>
<string name="ok">OK</string>
<string name="status_pending">Pending (!)</string>
<string name="status_unmarked">Unmarked</string>

<string name="no_file_yet">You don&#8217;t have a file configured yet.</string>
<string name="go_to_settings">Go to the settings to configure one.</string>
Expand Down

0 comments on commit caae00b

Please sign in to comment.