diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 4198b16..f517025 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -33,6 +33,7 @@ android { } } compileOptions { + isCoreLibraryDesugaringEnabled = true sourceCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17 } @@ -82,6 +83,7 @@ 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 773fa47..3578ac9 100644 --- a/app/src/main/java/be/chvp/nanoledger/data/LedgerRepository.kt +++ b/app/src/main/java/be/chvp/nanoledger/data/LedgerRepository.kt @@ -70,13 +70,7 @@ class LedgerRepository 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() - } - } + ?.use { it.lines().forEach { result.add(it) } } 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 a91dc4f..3705894 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 = date?.getTime()) + val datePickerState = rememberDatePickerState(initialSelectedDateMillis = dateMillis) 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 a164c24..7fc7361 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,8 +15,11 @@ import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.launch import java.io.IOException import java.math.BigDecimal -import java.text.SimpleDateFormat -import java.util.Date +import java.time.Instant +import java.time.LocalDate +import java.time.ZoneId +import java.time.ZoneOffset +import java.time.format.DateTimeFormatter import javax.inject.Inject @HiltViewModel @@ -30,9 +33,10 @@ class AddViewModel private val _saving = MutableLiveData(false) val saving: LiveData = _saving - private val _date = MutableLiveData(Date()) - val date: LiveData = _date - val formattedDate: LiveData = _date.map { SimpleDateFormat("yyyy-MM-dd").format(it) } + 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 _status = MutableLiveData(preferencesDataSource.getDefaultStatus()) val status: LiveData = _status @@ -113,7 +117,7 @@ class AddViewModel _saving.value = true viewModelScope.launch(IO) { val transaction = StringBuilder() - transaction.append(SimpleDateFormat("yyyy-MM-dd").format(date.value!!)) + transaction.append(_date.value!!.format(DateTimeFormatter.ISO_DATE)) if (status.value!! != " ") { transaction.append(" ${status.value}") } @@ -165,7 +169,7 @@ class AddViewModel } fun setDate(dateMillis: Long) { - _date.value = Date(dateMillis) + _date.value = Instant.ofEpochMilli(dateMillis).atZone(ZoneId.of("UTC")).toLocalDate() } fun setStatus(newStatus: String) { @@ -186,14 +190,7 @@ class AddViewModel ) { val result = ArrayList(postings.value!!) result[index] = Triple(newAccount, result[index].second, result[index].third) - val filteredResult = ArrayList>() - for (triple in result) { - if (triple.first != "" || triple.third != "") { - filteredResult.add(triple) - } - } - filteredResult.add(emptyPosting()) - _postings.value = filteredResult + _postings.value = removeEmpty(result) } fun setCurrency( @@ -211,14 +208,18 @@ class AddViewModel ) { val result = ArrayList(postings.value!!) result[index] = Triple(result[index].first, result[index].second, newAmount) - val filteredResult = ArrayList>() - for (triple in result) { - if (triple.first != "" || triple.third != "") { - filteredResult.add(triple) + _postings.value = removeEmpty(result) + } + + fun removeEmpty(postings: List>): List> { + val result = ArrayList>() + for (p in postings) { + if (p.first != "" || p.third != "") { + result.add(p) } } - filteredResult.add(emptyPosting()) - _postings.value = filteredResult + result.add(emptyPosting()) + return result } fun emptyPosting(): Triple { diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 60c31c6..fe8d031 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,5 +1,6 @@ [versions] agp = "8.3.0" +desugar = "2.0.4" kotlin = "1.9.22" ksp = "1.9.22-1.0.18" hilt = "2.51" @@ -16,6 +17,7 @@ material = "1.11.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" }