Skip to content

Commit

Permalink
fix: improve TimeProvider and fix formatting for durations between on…
Browse files Browse the repository at this point in the history
…e and two days (#87)

Fix bug in getDurationString where a duration between one and two days would be formatted to the number of hours left in the day.

Ensure that weekly and monthly goals created by prepopulateDatabase() correctly start at the beginning of the week/month.

Remove offset parameters from TimeProvider methods to simplify testing. Offsets can easily be applied on the returned ZonedDateTime by using the functions plusDays(), plusWeeks(), etc..

Add extensive unit tests for TimeProvider.

Reverse order of "last five goals" in statistics.

Closes #78 and #79.
  • Loading branch information
matthiasemde authored Apr 22, 2024
1 parent f6f2891 commit 7d9a78d
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 116 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/app/musikus/ui/goals/GoalCard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ fun GoalCard(
modifier = Modifier.padding(horizontal = 16.dp),
text = getDurationString(
animatedProgress.toInt().seconds,
DurationFormat.HUMAN_PRETTY
DurationFormat.HM_DIGITAL_OR_MIN_HUMAN
),
style = MaterialTheme.typography.titleMedium.copy(
color = MaterialTheme.colorScheme.onSurface
Expand All @@ -202,7 +202,7 @@ fun GoalCard(
modifier = Modifier.padding(horizontal = 16.dp),
text = getDurationString(
animatedProgressLeft.toInt().seconds,
DurationFormat.HUMAN_PRETTY
DurationFormat.HM_DIGITAL_OR_MIN_HUMAN
),
style = MaterialTheme.typography.titleMedium.copy(
color = MaterialTheme.colorScheme.onSurface
Expand Down
28 changes: 12 additions & 16 deletions app/src/main/java/app/musikus/ui/statistics/StatisticsViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* Copyright (c) 2023 Matthias Emde
* Copyright (c) 2023-2024 Matthias Emde
*/

package app.musikus.ui.statistics
Expand Down Expand Up @@ -89,10 +89,11 @@ class StatisticsViewModel @Inject constructor(

/** Imported flows */

// Timeframe for the current month, but at least the last week
private val _timeframe = Pair(
minOf(
timeProvider.getStartOfMonth(),
timeProvider.getStartOfDay(dayOffset = -7)
timeProvider.getStartOfDay().minusWeeks(1)
),
timeProvider.getEndOfMonth(),
)
Expand Down Expand Up @@ -157,13 +158,8 @@ class StatisticsViewModel @Inject constructor(
return@flatMapLatest flow { emit(null) }
}

val lastSevenDays = (0..6).reversed().map { dayOffset ->
(getDayIndexOfWeek(dateTime = timeProvider.now()) - dayOffset).let {
timeProvider.getStartOfDayOfWeek(
dayIndex = (it-1).mod(7).toLong() + 1,
weekOffset = if (it > 0) 0 else -1
)
}
val lastSevenDays = (0L..6L).reversed().map { dayOffset ->
timeProvider.getStartOfDay().minusDays(dayOffset)
}

val groupedSessions = sessions.filter { session ->
Expand All @@ -190,12 +186,12 @@ class StatisticsViewModel @Inject constructor(
if (_noSessionsForDurationCard) {
emit(
StatisticsPracticeDurationCardUiState(
lastSevenDayPracticeDuration = lastSevenDayPracticeDuration.map { PracticeDurationPerDay(
day = it.day,
duration = 0.seconds
) },
totalPracticeDuration = totalPracticeDuration,
)
lastSevenDayPracticeDuration = lastSevenDayPracticeDuration.map { PracticeDurationPerDay(
day = it.day,
duration = 0.seconds
) },
totalPracticeDuration = totalPracticeDuration,
)
)
delay(350)
_noSessionsForDurationCard = false
Expand Down Expand Up @@ -242,7 +238,7 @@ class StatisticsViewModel @Inject constructor(
successRate = lastFiveGoals.count {
it.progress >= it.instance.target
} to lastFiveGoals.size,
lastGoalsDisplayData = lastFiveGoals.map {
lastGoalsDisplayData = lastFiveGoals.reversed().map {
GoalCardGoalDisplayData(
label = it.instance.startTimestamp.musikusFormat(DateFormat.DAY_AND_MONTH),
progress = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* Copyright (c) 2023 Matthias Emde
* Copyright (c) 2023-2024 Matthias Emde
*/

package app.musikus.ui.statistics.sessionstatistics
Expand Down Expand Up @@ -168,11 +168,11 @@ class SessionStatisticsViewModel @Inject constructor(
SessionStatisticsTab.DEFAULT,
when(SessionStatisticsTab.DEFAULT) {
SessionStatisticsTab.DAYS ->
timeProvider.getStartOfDay(dayOffset = -6) to timeProvider.getEndOfDay()
timeProvider.getStartOfDay().minusDays(6) to timeProvider.getEndOfDay()
SessionStatisticsTab.WEEKS ->
timeProvider.getStartOfWeek(weekOffset = -6) to timeProvider.getEndOfWeek()
timeProvider.getStartOfWeek().minusWeeks(6) to timeProvider.getEndOfWeek()
SessionStatisticsTab.MONTHS ->
timeProvider.getStartOfMonth(monthOffset = -6) to timeProvider.getEndOfMonth()
timeProvider.getStartOfMonth().minusMonths(6) to timeProvider.getEndOfMonth()
}
),
)
Expand Down Expand Up @@ -275,14 +275,14 @@ class SessionStatisticsViewModel @Inject constructor(
val timeframes = (0L..6L).reversed().map {
when(selectedTab) {
SessionStatisticsTab.DAYS ->
timeProvider.getStartOfDay(dayOffset = -it, dateTime = timeframeEnd.minusSeconds(1)) to
timeProvider.getEndOfDay(dayOffset = -it, dateTime = timeframeEnd.minusSeconds(1))
timeProvider.getStartOfDay(dateTime = timeframeEnd.minusSeconds(1)).minusDays(it) to
timeProvider.getEndOfDay(dateTime = timeframeEnd.minusSeconds(1)).minusDays(it)
SessionStatisticsTab.WEEKS ->
timeProvider.getStartOfWeek(weekOffset = -it, dateTime = timeframeEnd.minusSeconds(1)) to
timeProvider.getEndOfWeek(weekOffset = -it, dateTime = timeframeEnd.minusSeconds(1))
timeProvider.getStartOfWeek(dateTime = timeframeEnd.minusSeconds(1)).minusWeeks(it) to
timeProvider.getEndOfWeek(dateTime = timeframeEnd.minusSeconds(1)).minusWeeks(it)
SessionStatisticsTab.MONTHS ->
timeProvider.getStartOfMonth(monthOffset = -it, dateTime = timeframeEnd.minusSeconds(1)) to
timeProvider.getEndOfMonth(monthOffset = -it, dateTime = timeframeEnd.minusSeconds(1))
timeProvider.getStartOfMonth(dateTime = timeframeEnd.minusSeconds(1)).minusMonths(it) to
timeProvider.getEndOfMonth(dateTime = timeframeEnd.minusSeconds(1)).minusMonths(it)
}
}

Expand Down Expand Up @@ -545,23 +545,23 @@ class SessionStatisticsViewModel @Inject constructor(
if (newEnd > endOfToday) {
newEnd = endOfToday
}
timeProvider.getStartOfDay(dayOffset = -6, dateTime = newEnd.minusSeconds(1)) to newEnd
timeProvider.getStartOfDay(dateTime = newEnd.minusSeconds(1)).minusDays(6) to newEnd
}
SessionStatisticsTab.WEEKS -> {
val endOfThisWeek = timeProvider.getEndOfWeek()
var newEnd = timeProvider.getEndOfWeek(dateTime = end.minusSeconds(1))
if (newEnd > endOfThisWeek) {
newEnd = endOfThisWeek
}
timeProvider.getStartOfWeek(weekOffset = -6, dateTime = newEnd.minusSeconds(1)) to newEnd
timeProvider.getStartOfWeek(dateTime = newEnd.minusSeconds(1)).minusWeeks(6) to newEnd
}
SessionStatisticsTab.MONTHS -> {
val endOfThisMonth = timeProvider.getEndOfMonth()
var newEnd = timeProvider.getEndOfMonth(dateTime = end.minusSeconds(1))
if (newEnd > endOfThisMonth) {
newEnd = endOfThisMonth
}
timeProvider.getStartOfMonth(monthOffset = -6, dateTime = newEnd.minusSeconds(1)) to newEnd
timeProvider.getStartOfMonth(dateTime = newEnd.minusSeconds(1)).minusMonths(6) to newEnd
}
}
)
Expand All @@ -581,23 +581,23 @@ class SessionStatisticsViewModel @Inject constructor(
if(newEnd > endOfToday) {
newEnd = endOfToday
}
timeProvider.getStartOfDay(dayOffset = -6, dateTime = newEnd.minusSeconds(1)) to newEnd
timeProvider.getStartOfDay(dateTime = newEnd.minusSeconds(1)).minusDays(6) to newEnd
}
SessionStatisticsTab.WEEKS -> {
val endOfThisWeek = timeProvider.getEndOfWeek()
var newEnd = end.plusWeeks(7)
if(newEnd > endOfThisWeek) {
newEnd = endOfThisWeek
}
timeProvider.getStartOfWeek(weekOffset = -6, dateTime = newEnd.minusSeconds(1)) to newEnd
timeProvider.getStartOfWeek(dateTime = newEnd.minusSeconds(1)).minusWeeks(6) to newEnd
}
SessionStatisticsTab.MONTHS -> {
val endOfThisMonth = timeProvider.getEndOfMonth()
var newEnd = end.plusMonths(7)
if(newEnd > endOfThisMonth) {
newEnd = endOfThisMonth
}
timeProvider.getStartOfMonth(monthOffset = -6, dateTime = newEnd.minusSeconds(1)) to newEnd
timeProvider.getStartOfMonth(dateTime = newEnd.minusSeconds(1)).minusMonths(6) to newEnd
}
}
)
Expand All @@ -616,21 +616,21 @@ class SessionStatisticsViewModel @Inject constructor(
if(newStart < release) {
newStart = release
}
newStart to timeProvider.getEndOfDay(dayOffset = 6, dateTime = newStart)
newStart to timeProvider.getEndOfDay(dateTime = newStart).plusDays(6)
}
SessionStatisticsTab.WEEKS -> {
var newStart = start.minusWeeks(7)
if(newStart < release) {
newStart = release
}
newStart to timeProvider.getEndOfWeek(weekOffset = 6, dateTime = newStart)
newStart to timeProvider.getEndOfWeek(dateTime = newStart).plusWeeks(6)
}
SessionStatisticsTab.MONTHS -> {
var newStart = start.minusMonths(7)
if(newStart < release) {
newStart = release
}
newStart to timeProvider.getEndOfMonth(monthOffset = 6, dateTime = newStart)
newStart to timeProvider.getEndOfMonth(dateTime = newStart).plusMonths(6)
}
}
)
Expand Down
21 changes: 11 additions & 10 deletions app/src/main/java/app/musikus/utils/DurationFormatter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,15 @@ fun getDurationString(
}
}

if (minutes > 0) {
if (minutes > 0 || duration == 0.seconds) {
append("$spaceOrNot$minutes")
withStyle(SpanStyle(fontSize = SCALE_FACTOR_FOR_SMALL_TEXT.em)) {
append("m")
}
}

if(totalHours == 0L && minutes == 0L) {
append("<" + spaceOrNot + "1")
if(duration < 1.minutes && duration > 0.seconds) {
append("<${spaceOrNot}1")
withStyle(SpanStyle(fontSize = SCALE_FACTOR_FOR_SMALL_TEXT.em)) {
append("m")
}
Expand Down Expand Up @@ -147,15 +147,16 @@ fun getDurationString(
append(when {
totalHours > 0 -> "%02d:%02d".format(totalHours, minutes)
minutes > 0 -> "%d min".format(minutes)
else -> "< 1min"
seconds > 0 -> "< 1min"
else -> "0 min"
})
}
DurationFormat.PRETTY_APPROX -> {
append(when {
// if time left is larger than a day, show the number of begun days
days > 1 -> "${days + 1} days"
// if days are zero, but hours is larger than 1 show the number of begun hours
hours > 1 -> "${hours + 1} hours"
days > 0 -> "${days + 1} days"
// if days are zero, but hours is larger than 1, show the number of begun hours
hours > 0 -> "${hours + 1} hours"
// else, show the number of begun minutes
else -> "${minutes + 1} minutes"
})
Expand All @@ -164,9 +165,9 @@ fun getDurationString(
DurationFormat.PRETTY_APPROX_SHORT -> {
append(when {
// if time left is larger than a day, show the number of begun days
days > 1 -> "${days + 1} days"
// show the number of begun hours
hours > 1 -> "${hours + 1}h"
days > 0 -> "${days + 1} days"
// if days are zero, but hours is larger than 1, show the number of begun hours
hours > 0 -> "${hours + 1}h"
// else, show the number of begun minutes
else -> "${minutes + 1}m"
})
Expand Down
24 changes: 11 additions & 13 deletions app/src/main/java/app/musikus/utils/PrepopulateDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* Copyright (c) 2023 Matthias Emde
* Copyright (c) 2023-2024 Matthias Emde
*/

package app.musikus.utils
Expand Down Expand Up @@ -102,21 +102,19 @@ suspend fun prepopulateDatabase(
periodUnit = GoalPeriodUnit.WEEK,
),
).forEach { goalDescriptionCreationAttributes ->
val offset = (
if (goalDescriptionCreationAttributes.repeat) 10L
else 1L
) * goalDescriptionCreationAttributes.periodInPeriodUnits

database.goalDescriptionDao.insert(
descriptionCreationAttributes = goalDescriptionCreationAttributes,
instanceCreationAttributes = GoalInstanceCreationAttributes(
startTimestamp = database.timeProvider.getStartOfDay(
dayOffset = -1 *
(
if (goalDescriptionCreationAttributes.repeat) 10L
else 1L
) * goalDescriptionCreationAttributes.periodInPeriodUnits
* when(goalDescriptionCreationAttributes.periodUnit) {
GoalPeriodUnit.DAY -> 1
GoalPeriodUnit.WEEK -> 7
GoalPeriodUnit.MONTH -> 31
}
),
startTimestamp = when(goalDescriptionCreationAttributes.periodUnit) {
GoalPeriodUnit.DAY -> database.timeProvider.getStartOfDay().minusDays(offset)
GoalPeriodUnit.WEEK -> database.timeProvider.getStartOfWeek().minusWeeks(offset)
GoalPeriodUnit.MONTH -> database.timeProvider.getStartOfMonth().minusMonths(offset)
},
target =((1..6).random() * 10 + 30).minutes
),
libraryItemIds =
Expand Down
Loading

0 comments on commit 7d9a78d

Please sign in to comment.