Skip to content

Commit

Permalink
[UI] Friendly date string.
Browse files Browse the repository at this point in the history
  • Loading branch information
Konyaco committed Jan 29, 2022
1 parent bbc6763 commit 694df5c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,25 @@ class MainViewModel @Inject constructor(
private val localZoneId = ZoneId.systemDefault()

data class DailyRecord(
val date: String,
val date: Date,
val expenditure: Int,
val income: Int,
val records: List<Record>
)

data class Record(
val money: Int,
val date: String,
val date: Date,
val time: String,
val type: RecordType
)

data class Date(
val timestamp: Long,
val dateString: String,
val daysOffset: Int
)

data class RecordType(
val label: String,
val parent: String?,
Expand Down Expand Up @@ -120,11 +126,17 @@ class MainViewModel @Inject constructor(
var expenditure = 0
var income = 0

val now = ZonedDateTime.now()
val result = recordDao.loadAllByDateDesc(start, end).map {
val instant = Instant.ofEpochSecond(it.timestamp)
val zoned = instant.atZone(localZoneId)
val time = hhmFormatter.format(zoned)
val date = dateFormatter.format(zoned)

val date = Date(
it.timestamp,
dateFormatter.format(zoned),
Duration.between(zoned, now).toDays().toInt()
)
val label = recordTypeDao.loadAllByIds(it.typeId).first().mapToRecordType()

if (it.money < 0) {
Expand Down Expand Up @@ -270,6 +282,4 @@ class MainViewModel @Inject constructor(
localZoneId
).toEpochSecond()
}
}

private fun moneyToString(money: Int): String = "${money / 100}.${money % 100}"
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.sharp.Add
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
Expand Down Expand Up @@ -60,7 +61,7 @@ fun DetailScreen(
itemsIndexed(records) { index, item ->
DailyRecord(
Modifier.fillParentMaxWidth(),
item.date,
item.date.parseAsString(),
item.expenditure,
item.income,
item.records
Expand All @@ -82,6 +83,17 @@ fun DetailScreen(
}
}

@Stable
@Composable
private fun MainViewModel.Date.parseAsString(): String {
return when (daysOffset) {
0 -> "今天"
1 -> "昨天"
2 -> "前天"
else -> dateString
}
}

@Preview(showBackground = true)
@Composable
fun DetailPagePreview() {
Expand Down

0 comments on commit 694df5c

Please sign in to comment.