Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reapply "Use Android desugaring to use more modern Java constructs" #143

Merged
merged 1 commit into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ android {
}
}
compileOptions {
isCoreLibraryDesugaringEnabled = true
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
Expand Down Expand Up @@ -83,6 +84,7 @@ configure<org.jlleitschuh.gradle.ktlint.KtlintExtension> {
}

dependencies {
coreLibraryDesugaring(libs.android.desugar)
implementation(libs.activity.compose)
implementation(libs.activity.ktx)
implementation(platform(libs.compose.bom))
Expand Down
16 changes: 2 additions & 14 deletions app/src/main/java/be/chvp/nanoledger/data/LedgerRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,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) } }

if (!result.equals(fileContents.value)) {
onMismatch()
Expand Down Expand Up @@ -116,13 +110,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)
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/be/chvp/nanoledger/ui/add/AddActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?: "",
Expand All @@ -302,7 +302,7 @@ fun DateSelector(
},
)
if (dateDialogOpen) {
val datePickerState = rememberDatePickerState(initialSelectedDateMillis = date?.getTime())
val datePickerState = rememberDatePickerState(initialSelectedDateMillis = dateMillis)
DatePickerDialog(
onDismissRequest = { dateDialogOpen = false },
confirmButton = {
Expand Down
43 changes: 22 additions & 21 deletions app/src/main/java/be/chvp/nanoledger/ui/add/AddViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,9 +33,10 @@ class AddViewModel
private val _saving = MutableLiveData<Boolean>(false)
val saving: LiveData<Boolean> = _saving

private val _date = MutableLiveData<Date>(Date())
val date: LiveData<Date> = _date
val formattedDate: LiveData<String> = _date.map { SimpleDateFormat("yyyy-MM-dd").format(it) }
private val _date = MutableLiveData<LocalDate>(LocalDate.now())
val date: LiveData<LocalDate> = _date
val formattedDate: LiveData<String> = _date.map { it.format(DateTimeFormatter.ISO_DATE) }
val dateMillis: LiveData<Long> = _date.map { it.atStartOfDay().toInstant(ZoneOffset.UTC).toEpochMilli() }

private val _status = MutableLiveData<String>(preferencesDataSource.getDefaultStatus())
val status: LiveData<String> = _status
Expand Down Expand Up @@ -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}")
}
Expand Down Expand Up @@ -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) {
Expand All @@ -186,14 +190,7 @@ class AddViewModel
) {
val result = ArrayList(postings.value!!)
result[index] = Triple(newAccount, result[index].second, result[index].third)
val filteredResult = ArrayList<Triple<String, String, String>>()
for (triple in result) {
if (triple.first != "" || triple.third != "") {
filteredResult.add(triple)
}
}
filteredResult.add(emptyPosting())
_postings.value = filteredResult
_postings.value = removeEmpty(result)
}

fun setCurrency(
Expand All @@ -211,14 +208,18 @@ class AddViewModel
) {
val result = ArrayList(postings.value!!)
result[index] = Triple(result[index].first, result[index].second, newAmount)
val filteredResult = ArrayList<Triple<String, String, String>>()
for (triple in result) {
if (triple.first != "" || triple.third != "") {
filteredResult.add(triple)
_postings.value = removeEmpty(result)
}

fun removeEmpty(postings: List<Triple<String, String, String>>): List<Triple<String, String, String>> {
val result = ArrayList<Triple<String, String, String>>()
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<String, String, String> {
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[versions]
agp = "8.5.0"
desugar = "2.0.4"
kotlin = "1.9.24"
ksp = "1.9.24-1.0.20"
hilt = "2.51.1"
Expand All @@ -16,6 +17,7 @@ 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" }
Expand Down
Loading