diff --git a/app/src/main/java/be/chvp/nanoledger/data/LedgerRepository.kt b/app/src/main/java/be/chvp/nanoledger/data/LedgerRepository.kt index 777de5c..89cefa9 100644 --- a/app/src/main/java/be/chvp/nanoledger/data/LedgerRepository.kt +++ b/app/src/main/java/be/chvp/nanoledger/data/LedgerRepository.kt @@ -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() + 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) } diff --git a/app/src/main/java/be/chvp/nanoledger/ui/add/AddActivity.kt b/app/src/main/java/be/chvp/nanoledger/ui/add/AddActivity.kt index a91dc4f..610320b 100644 --- a/app/src/main/java/be/chvp/nanoledger/ui/add/AddActivity.kt +++ b/app/src/main/java/be/chvp/nanoledger/ui/add/AddActivity.kt @@ -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 @@ -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() @@ -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( diff --git a/app/src/main/java/be/chvp/nanoledger/ui/add/AddViewModel.kt b/app/src/main/java/be/chvp/nanoledger/ui/add/AddViewModel.kt index a164c24..2bd8802 100644 --- a/app/src/main/java/be/chvp/nanoledger/ui/add/AddViewModel.kt +++ b/app/src/main/java/be/chvp/nanoledger/ui/add/AddViewModel.kt @@ -105,6 +105,9 @@ class AddViewModel private val _latestError = MutableLiveData?>(null) val latestError: LiveData?> = _latestError + private val _latestMismatch = MutableLiveData?>(null) + val latestMismatch: LiveData?> = _latestMismatch + val currencyBeforeAmount: LiveData = preferencesDataSource.currencyBeforeAmount fun append(onFinish: suspend () -> Unit) { @@ -146,6 +149,10 @@ class AddViewModel _saving.postValue(false) onFinish() }, + { + _saving.postValue(false) + _latestMismatch.postValue(Event(1)) + }, { _saving.postValue(false) _latestError.postValue(Event(it)) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 555baed..6001a62 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -18,6 +18,7 @@ Error Error while reading file Error while writing file + File contents outdated, aborting write File contents outdated, aborting delete File Note