Skip to content

Commit

Permalink
Don't use append writing mode for compatibility with more content pro…
Browse files Browse the repository at this point in the history
…viders (#162)

<!---
  Thanks for contributing to NanoLedger!  Make sure all GitHub actions
  (test, lint & build) will pass and fill out the template.

  If any changes to your PR are necessary, we will ask for them
  throughout the review process.
  --->
 
<!---
  Please include a summary of the change and which issue is
  fixed. Please also include relevant motivation and context. If your
  pull request includes visual changes (which will probably be the
  case for anything that isn't a pure logic fix), please include
  screenshots showing those changes.
  --->

Fixes #161.

<!---
  If you did any manual testing (please do so), describe here what you
  tested and how. Provide instructions so that your tests can be
  easily run again by a maintainer.
  --->
  • Loading branch information
chvp authored Sep 4, 2024
1 parent b684b8f commit c36d9b3
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
34 changes: 27 additions & 7 deletions app/src/main/java/be/chvp/nanoledger/data/LedgerRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,39 @@ class LedgerRepository
fileUri: Uri,
text: String,
onFinish: suspend () -> Unit,
onMismatch: suspend () -> Unit,
onWriteError: suspend (IOException) -> Unit,
onReadError: suspend (IOException) -> Unit,
) {
try {
context.contentResolver.openOutputStream(fileUri, "wa")
?.let { OutputStreamWriter(it) }
?.use {
if (!fileContents.value!!.isEmpty() && fileContents.value!!.last() != "") {
it.write("\n")
val result = ArrayList<String>()
fileUri
.let { context.contentResolver.openInputStream(it) }
?.let { BufferedReader(InputStreamReader(it)) }
?.use { reader ->
var line = reader.readLine()
while (line != null) {
result.add(line)
line = reader.readLine()
}
it.write(text)
}
readFrom(fileUri, onFinish, onReadError)

if (!result.equals(fileContents.value)) {
onMismatch()
} else {
context.contentResolver.openOutputStream(fileUri, "w")
?.let { OutputStreamWriter(it) }
?.use {
fileContents.value!!.forEach { line ->
it.write("${line}\n")
}
if (!fileContents.value!!.isEmpty() && fileContents.value!!.last() != "") {
it.write("\n")
}
it.write(text)
}
readFrom(fileUri, onFinish, onReadError)
}
} catch (e: IOException) {
onWriteError(e)
}
Expand Down
18 changes: 17 additions & 1 deletion app/src/main/java/be/chvp/nanoledger/ui/add/AddActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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 Down Expand Up @@ -84,11 +85,12 @@ class AddActivity() : ComponentActivity() {
setContent {
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) }

val errorMessage = stringResource(R.string.error_writing_file)
var errorDialogMessage by rememberSaveable { mutableStateOf("") }
LaunchedEffect(latestError) {
val error = latestError?.get()
Expand All @@ -108,6 +110,20 @@ class AddActivity() : ComponentActivity() {
}
}
}

val latestMismatch by addViewModel.latestMismatch.observeAsState()
val mismatchMessage = stringResource(R.string.mismatch_no_write)
LaunchedEffect(latestMismatch) {
val error = latestMismatch?.get()
if (error != null) {
Toast.makeText(
context,
mismatchMessage,
Toast.LENGTH_LONG,
).show()
}
}

BackHandler(enabled = true) {
finish()
startActivity(
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/be/chvp/nanoledger/ui/add/AddViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ class AddViewModel
private val _latestError = MutableLiveData<Event<IOException>?>(null)
val latestError: LiveData<Event<IOException>?> = _latestError

private val _latestMismatch = MutableLiveData<Event<Int>?>(null)
val latestMismatch: LiveData<Event<Int>?> = _latestMismatch

val currencyBeforeAmount: LiveData<Boolean> = preferencesDataSource.currencyBeforeAmount

fun append(onFinish: suspend () -> Unit) {
Expand Down Expand Up @@ -146,6 +149,10 @@ class AddViewModel
_saving.postValue(false)
onFinish()
},
{
_saving.postValue(false)
_latestMismatch.postValue(Event(1))
},
{
_saving.postValue(false)
_latestError.postValue(Event(it))
Expand Down
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 @@ -18,6 +18,7 @@
<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="mismatch_no_write">File contents outdated, aborting write</string>
<string name="mismatch_no_delete">File contents outdated, aborting delete</string>
<string name="file">File</string>
<string name="note">Note</string>
Expand Down

0 comments on commit c36d9b3

Please sign in to comment.