From 9544cb031d8d37382efd3c2442d3244c6713cb83 Mon Sep 17 00:00:00 2001 From: Charlotte Van Petegem Date: Mon, 4 Mar 2024 21:25:30 +0100 Subject: [PATCH] Stop using some more modern Java constructs --- .../chvp/nanoledger/data/LedgerRepository.kt | 8 ++++++- .../be/chvp/nanoledger/ui/add/AddViewModel.kt | 22 ++++++++++++++----- 2 files changed, 23 insertions(+), 7 deletions(-) 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 3578ac9..773fa47 100644 --- a/app/src/main/java/be/chvp/nanoledger/data/LedgerRepository.kt +++ b/app/src/main/java/be/chvp/nanoledger/data/LedgerRepository.kt @@ -70,7 +70,13 @@ class LedgerRepository fileUri ?.let { context.contentResolver.openInputStream(it) } ?.let { BufferedReader(InputStreamReader(it)) } - ?.use { it.lines().forEach { result.add(it) } } + ?.use { reader -> + var line = reader.readLine() + while (line != null) { + result.add(line) + line = reader.readLine() + } + } val extracted = extractTransactions(result) _fileContents.postValue(result) _transactions.postValue(extracted) 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 6435a11..a164c24 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 @@ -186,9 +186,14 @@ class AddViewModel ) { val result = ArrayList(postings.value!!) result[index] = Triple(newAccount, result[index].second, result[index].third) - result.removeIf { it.first == "" && it.third == "" } - result.add(emptyPosting()) - _postings.value = result + val filteredResult = ArrayList>() + for (triple in result) { + if (triple.first != "" || triple.third != "") { + filteredResult.add(triple) + } + } + filteredResult.add(emptyPosting()) + _postings.value = filteredResult } fun setCurrency( @@ -206,9 +211,14 @@ class AddViewModel ) { val result = ArrayList(postings.value!!) result[index] = Triple(result[index].first, result[index].second, newAmount) - result.removeIf { it.first == "" && it.third == "" } - result.add(emptyPosting()) - _postings.value = result + val filteredResult = ArrayList>() + for (triple in result) { + if (triple.first != "" || triple.third != "") { + filteredResult.add(triple) + } + } + filteredResult.add(emptyPosting()) + _postings.value = filteredResult } fun emptyPosting(): Triple {