Skip to content

Commit

Permalink
feat: better monthly grouping
Browse files Browse the repository at this point in the history
  • Loading branch information
yuriykulikov committed Jul 4, 2024
1 parent af25d6a commit d0ed262
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 29 deletions.
4 changes: 2 additions & 2 deletions app-android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ android {
applicationId = "simple.payment.tracker"
minSdk = 28
targetSdk = 34
versionCode = 10801
versionName = "1.08.01"
versionCode = 10901
versionName = "1.09.01"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled = true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fun NavigationBottomBar(currentScreen: MutableState<Screen>) {
IconButton(
onClick = { currentScreen.value = Screen.Regular },
modifier = Modifier.weight(1f).highlightIf(currentScreen, Screen.Regular)) {
Text(text = "Reg")
Text(text = "Month")
}

IconButton(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,9 @@ private fun AppContent(
list.value,
firebaseSignIn.signedInUserEmail(),
bottomBar,
showMonthDetails = { monthDetailsToShow.value = Screen.MonthDetails(it) })
showMonthDetails = { monthDetailsToShow.value = Screen.MonthDetails(it) },
MonthlyGrouping.DEFAULT,
)
}
is Screen.Irregular -> {
val list: State<List<GroupReport>> =
Expand All @@ -226,7 +228,9 @@ private fun AppContent(
list.value,
firebaseSignIn.signedInUserEmail(),
bottomBar,
showMonthDetails = { monthDetailsToShow.value = Screen.MonthDetails(it) })
showMonthDetails = { monthDetailsToShow.value = Screen.MonthDetails(it) },
MonthlyGrouping.NONE,
)
}
is Screen.MonthDetails ->
GroupDetailsScreen(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ fun NavigationBottomBar(currentScreen: Screen?, onChange: (Screen) -> Unit) {
}

IconButton(
onClick = { onChange(Screen.Regular) },
modifier = Modifier.weight(1f).highlightIf(currentScreen, Screen.Regular)) {
Text(text = "Regular")
onClick = { onChange(Screen.Monthly) },
modifier = Modifier.weight(1f).highlightIf(currentScreen, Screen.Monthly)) {
Text(text = "Monthly")
}

IconButton(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import simple.payment.tracker.TripStatistics
import simple.payment.tracker.compose.DetailsScreen
import simple.payment.tracker.compose.GroupDetailsScreen
import simple.payment.tracker.compose.InboxList
import simple.payment.tracker.compose.MonthlyGrouping
import simple.payment.tracker.compose.MonthlyScreen
import simple.payment.tracker.compose.TransactionsList
import simple.payment.tracker.compose.TripsScreen
Expand All @@ -81,7 +82,7 @@ sealed interface Screen {

object ListAll : Screen

object Regular : Screen
object Monthly : Screen

object Irregular : Screen

Expand Down Expand Up @@ -323,15 +324,16 @@ private fun LeftCrossFade(
search = search,
listState = allListState,
)
is Screen.Regular -> {
is Screen.Monthly -> {
val list: State<List<GroupReport>> =
remember { monthlyStatistics.reports(GroupReport.Type.REGULAR) }
remember { monthlyStatistics.reports(GroupReport.Type.ALL) }
.collectAsState(initial = emptyList())
MonthlyScreen(
list.value,
userNameStore.data.stateIn(scope, SharingStarted.Eagerly, ""),
bottomBar,
showMonthDetails)
showMonthDetails,
MonthlyGrouping.DEFAULT)
}
is Screen.Irregular -> {
val list: State<List<GroupReport>> =
Expand All @@ -341,7 +343,8 @@ private fun LeftCrossFade(
list.value,
userNameStore.data.stateIn(scope, SharingStarted.Eagerly, ""),
bottomBar,
showMonthDetails)
showMonthDetails,
MonthlyGrouping.NONE)
}
is Screen.Trips ->
TripsScreen(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package simple.payment.tracker.compose

interface MonthlyGrouping {
fun group(category: String): Grouping

data class Grouping(val group: String, val prio: Int)

companion object {
val NONE: (String) -> Grouping = { Grouping("", 0) }
val DEFAULT: (String) -> Grouping = { category ->
when (category) {
"Жилье",
"Машина",
"Садик",
"Интернет" -> Grouping("Constant", 0)
"Еда",
"Ресторан",
"Гедонизм",
"Кот",
"Образование",
"Няня",
"Фитнесс",
"Drogerie" -> Grouping("Regular", 2)
else -> Grouping("Irregular", 1)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ fun MonthlyScreen(
userName: StateFlow<String?>,
bottomBar: @Composable () -> Unit,
showMonthDetails: (GroupReport) -> Unit,
grouping: (String) -> MonthlyGrouping.Grouping,
) {
Scaffold(
topBar = {
Expand All @@ -39,7 +40,8 @@ fun MonthlyScreen(
bottomBar = bottomBar,
content = { paddingValues ->
val name = userName.collectAsState().value
StatisticsContent(Modifier.padding(paddingValues), reports, showMonthDetails, name ?: "")
StatisticsContent(
Modifier.padding(paddingValues), reports, showMonthDetails, name ?: "", grouping)
},
)
}
Expand All @@ -61,7 +63,13 @@ fun TripsScreen(
val list: State<List<GroupReport>> =
remember { tripStatistics.reports() }.collectAsState(initial = emptyList())

StatisticsContent(Modifier.padding(paddingValues), list.value, showTripDetails, name ?: "")
StatisticsContent(
Modifier.padding(paddingValues),
list.value,
showTripDetails,
name ?: "",
MonthlyGrouping.DEFAULT,
)
},
)
}
Expand All @@ -72,11 +80,12 @@ private fun StatisticsContent(
reports: List<GroupReport>,
showMonthDetails: (GroupReport) -> Unit,
username: String,
grouping: (String) -> MonthlyGrouping.Grouping,
) {

LazyColumn(modifier = modifier.fillMaxSize().padding(horizontal = 16.dp)) {
items(reports) { stats ->
MonthEntry(stats, username, showMonthDetails)
MonthEntry(stats, username, showMonthDetails, grouping)
ListDivider()
}
}
Expand All @@ -90,7 +99,8 @@ private fun GroupReport.categorySums(forCat: String, forUser: String): Int {
private fun MonthEntry(
stats: GroupReport,
username: String,
showMonthDetails: (GroupReport) -> Unit
showMonthDetails: (GroupReport) -> Unit,
grouping: (String) -> MonthlyGrouping.Grouping,
) {
Column(Modifier.clickable { showMonthDetails(stats) }) {
Row(modifier = Modifier.padding(top = 8.dp)) {
Expand All @@ -111,20 +121,41 @@ private fun MonthEntry(
}
}
stats.categorySums
.sortedByDescending { (_, sum) -> sum }
.forEach { (cat, sum) ->
Row {
Column(Modifier.weight(2F)) { Text(cat, style = typography.body1) }
Column(Modifier.weight(1F)) { Text(sum.toString(), style = typography.body1) }
Column(Modifier.weight(1F)) {
Text(
stats
.categorySums(forCat = cat, forUser = username)
.takeIf { it != 0 }
?.toString() ?: "",
style = typography.body2)
.map { (cat, sum) -> cat to sum }
.groupBy { (cat, _) -> grouping(cat) }
.entries
.sortedByDescending { (group, _) -> group.prio }
.forEach { (group, groupSums) ->
val (groupName, prio) = group
if (groupName.isNotEmpty()) {
Row {
Column(Modifier.weight(2F)) { Text(groupName, style = typography.h6) }
Column(Modifier.weight(1F)) {
Text(
groupSums.sumOf { it.second }.toString(),
style = typography.h6,
color = Theme.colors.textAccent,
)
}
Column(Modifier.weight(1F)) {}
}
}
groupSums
.sortedByDescending { (_, sum) -> sum }
.forEach { (cat, sum) ->
Row {
Column(Modifier.weight(2F)) { Text(cat, style = typography.body1) }
Column(Modifier.weight(1F)) { Text(sum.toString(), style = typography.body1) }
Column(Modifier.weight(1F)) {
Text(
stats
.categorySums(forCat = cat, forUser = username)
.takeIf { it != 0 }
?.toString() ?: "",
style = typography.body2)
}
}
}
}
}
}

0 comments on commit d0ed262

Please sign in to comment.