diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 82981ad..ed076e9 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -33,7 +33,6 @@ android { } } compileOptions { - isCoreLibraryDesugaringEnabled = true sourceCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17 } @@ -84,7 +83,6 @@ configure { } dependencies { - coreLibraryDesugaring(libs.android.desugar) implementation(libs.activity.compose) implementation(libs.activity.ktx) implementation(platform(libs.compose.bom)) 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 c908f32..777de5c 100644 --- a/app/src/main/java/be/chvp/nanoledger/data/LedgerRepository.kt +++ b/app/src/main/java/be/chvp/nanoledger/data/LedgerRepository.kt @@ -51,7 +51,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() + } + } if (!result.equals(fileContents.value)) { onMismatch() @@ -110,7 +116,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/AddActivity.kt b/app/src/main/java/be/chvp/nanoledger/ui/add/AddActivity.kt index 3705894..a91dc4f 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 @@ -280,8 +280,8 @@ fun DateSelector( addViewModel: AddViewModel = viewModel(), ) { val focusManager = LocalFocusManager.current + val date by addViewModel.date.observeAsState() val formattedDate by addViewModel.formattedDate.observeAsState() - val dateMillis by addViewModel.dateMillis.observeAsState() var dateDialogOpen by rememberSaveable { mutableStateOf(false) } OutlinedTextField( value = formattedDate ?: "", @@ -302,7 +302,7 @@ fun DateSelector( }, ) if (dateDialogOpen) { - val datePickerState = rememberDatePickerState(initialSelectedDateMillis = dateMillis) + val datePickerState = rememberDatePickerState(initialSelectedDateMillis = date?.getTime()) DatePickerDialog( onDismissRequest = { dateDialogOpen = false }, confirmButton = { 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 7fc7361..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 @@ -15,11 +15,8 @@ import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.launch import java.io.IOException import java.math.BigDecimal -import java.time.Instant -import java.time.LocalDate -import java.time.ZoneId -import java.time.ZoneOffset -import java.time.format.DateTimeFormatter +import java.text.SimpleDateFormat +import java.util.Date import javax.inject.Inject @HiltViewModel @@ -33,10 +30,9 @@ class AddViewModel private val _saving = MutableLiveData(false) val saving: LiveData = _saving - private val _date = MutableLiveData(LocalDate.now()) - val date: LiveData = _date - val formattedDate: LiveData = _date.map { it.format(DateTimeFormatter.ISO_DATE) } - val dateMillis: LiveData = _date.map { it.atStartOfDay().toInstant(ZoneOffset.UTC).toEpochMilli() } + private val _date = MutableLiveData(Date()) + val date: LiveData = _date + val formattedDate: LiveData = _date.map { SimpleDateFormat("yyyy-MM-dd").format(it) } private val _status = MutableLiveData(preferencesDataSource.getDefaultStatus()) val status: LiveData = _status @@ -117,7 +113,7 @@ class AddViewModel _saving.value = true viewModelScope.launch(IO) { val transaction = StringBuilder() - transaction.append(_date.value!!.format(DateTimeFormatter.ISO_DATE)) + transaction.append(SimpleDateFormat("yyyy-MM-dd").format(date.value!!)) if (status.value!! != " ") { transaction.append(" ${status.value}") } @@ -169,7 +165,7 @@ class AddViewModel } fun setDate(dateMillis: Long) { - _date.value = Instant.ofEpochMilli(dateMillis).atZone(ZoneId.of("UTC")).toLocalDate() + _date.value = Date(dateMillis) } fun setStatus(newStatus: String) { @@ -190,7 +186,14 @@ class AddViewModel ) { val result = ArrayList(postings.value!!) result[index] = Triple(newAccount, result[index].second, result[index].third) - _postings.value = removeEmpty(result) + val filteredResult = ArrayList>() + for (triple in result) { + if (triple.first != "" || triple.third != "") { + filteredResult.add(triple) + } + } + filteredResult.add(emptyPosting()) + _postings.value = filteredResult } fun setCurrency( @@ -208,18 +211,14 @@ class AddViewModel ) { val result = ArrayList(postings.value!!) result[index] = Triple(result[index].first, result[index].second, newAmount) - _postings.value = removeEmpty(result) - } - - fun removeEmpty(postings: List>): List> { - val result = ArrayList>() - for (p in postings) { - if (p.first != "" || p.third != "") { - result.add(p) + val filteredResult = ArrayList>() + for (triple in result) { + if (triple.first != "" || triple.third != "") { + filteredResult.add(triple) } } - result.add(emptyPosting()) - return result + filteredResult.add(emptyPosting()) + _postings.value = filteredResult } fun emptyPosting(): Triple { diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 1b0d789..2b8d1d1 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,6 +1,5 @@ [versions] agp = "8.5.0" -desugar = "2.0.4" kotlin = "1.9.24" ksp = "1.9.24-1.0.20" hilt = "2.51.1" @@ -17,7 +16,6 @@ material = "1.12.0" [libraries] activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activity" } activity-ktx = { group = "androidx.activity", name = "activity-ktx", version.ref = "activity" } -android-desugar = { group = "com.android.tools", name = "desugar_jdk_libs", version.ref = "desugar" } androidx-test-ext-junit = { group = "androidx.test.ext", name = "junit", version.ref = "androidx-test-ext-junit" } compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "compose-bom" } compose-material-icons-extended = { group = "androidx.compose.material", name = "material-icons-extended" }