Fix calculating remaining time

This commit is contained in:
Jonas Lochmann 2023-12-11 01:00:00 +01:00
parent 2f8ff7ace2
commit 19e1e94ff4
No known key found for this signature in database
GPG key ID: 8B8C9AEE10FA5B36
4 changed files with 40 additions and 39 deletions

View file

@ -45,8 +45,8 @@ abstract class UsedTimeDao {
@Query("SELECT * FROM used_time WHERE category_id = :categoryId AND day_of_epoch = :dayOfEpoch AND start_time_of_day = :start AND end_time_of_day = :end")
abstract fun getUsedTimeItemSync(categoryId: String, dayOfEpoch: Int, start: Int, end: Int): UsedTimeItem?
@Query("SELECT * FROM used_time WHERE category_id = :categoryId AND day_of_epoch = :dayOfEpoch AND start_time_of_day <= :start AND end_time_of_day >= :end")
abstract fun getUsedTimeItemsSyncIncludingBigger(categoryId: String, dayOfEpoch: Int, start: Int, end: Int): List<UsedTimeItem>
@Query("SELECT * FROM used_time WHERE category_id = :categoryId AND day_of_epoch = :dayOfEpoch AND start_time_of_day >= :start AND end_time_of_day <= :end")
abstract fun getUsedTimeItemsSyncIncludingSmaller(categoryId: String, dayOfEpoch: Int, start: Int, end: Int): List<UsedTimeItem>
@Query("DELETE FROM used_time WHERE category_id = :categoryId")
abstract fun deleteUsedTimeItems(categoryId: String)

View file

@ -88,31 +88,39 @@ data class RemainingTime(val includingExtraTime: Long, val default: Long) {
private fun getRemainingTime(usedTimes: List<UsedTimeItem>, relatedRules: List<TimeLimitRule>, assumeMaximalExtraTime: Boolean, firstDayOfWeekAsEpochDay: Int, dayOfWeek: Int): Long? {
return relatedRules.filter { (!assumeMaximalExtraTime) || it.applyToExtraTimeUsage }.map { rule ->
var usedTime = 0L
usedTimes.forEach { usedTimeItem ->
val doesWeekMatch = usedTimeItem.dayOfEpoch >= firstDayOfWeekAsEpochDay && usedTimeItem.dayOfEpoch <= firstDayOfWeekAsEpochDay + 6
if (doesWeekMatch) {
val usedTimeItemDayOfWeek = usedTimeItem.dayOfEpoch - firstDayOfWeekAsEpochDay
val doesDayMaskMatch = (rule.dayMask.toInt() and (1 shl usedTimeItemDayOfWeek)) != 0
val doesCurrentDayMatch = dayOfWeek == usedTimeItemDayOfWeek
val usedTimeItemMatching = if (rule.perDay) doesCurrentDayMatch else doesDayMaskMatch
if (usedTimeItemMatching) {
if (rule.startMinuteOfDay >= usedTimeItem.startTimeOfDay && rule.endMinuteOfDay <= usedTimeItem.endTimeOfDay) {
usedTime += usedTimeItem.usedMillis
}
}
}
}
val usedTime = getUsedTime(
usedTimes = usedTimes,
rule = rule,
firstDayOfWeekAsEpochDay = firstDayOfWeekAsEpochDay,
dayOfWeekForDailyRule = if (rule.perDay) dayOfWeek else null
)
val maxTime = rule.maximumTimeInMillis
val remaining = Math.max(0, maxTime - usedTime)
remaining
(maxTime - usedTime).coerceAtLeast(0)
}.minOrNull()
}
fun getUsedTime(usedTimes: List<UsedTimeItem>, rule: TimeLimitRule, firstDayOfWeekAsEpochDay: Int, dayOfWeekForDailyRule: Int?): Long {
val usedTimeByDay = longArrayOf(0, 0, 0, 0, 0, 0, 0)
usedTimes.forEach { usedTimeItem ->
val doesWeekMatch = usedTimeItem.dayOfEpoch >= firstDayOfWeekAsEpochDay && usedTimeItem.dayOfEpoch <= firstDayOfWeekAsEpochDay + 6
if (doesWeekMatch) {
val usedTimeItemDayOfWeek = usedTimeItem.dayOfEpoch - firstDayOfWeekAsEpochDay
val doesDayMaskMatch = (rule.dayMask.toInt() and (1 shl usedTimeItemDayOfWeek)) != 0
val dayMatch = rule.perDay or doesDayMaskMatch
val hourMatch = rule.startMinuteOfDay <= usedTimeItem.startTimeOfDay && rule.endMinuteOfDay >= usedTimeItem.endTimeOfDay
if (dayMatch && hourMatch)
usedTimeByDay[usedTimeItemDayOfWeek] = usedTimeByDay[usedTimeItemDayOfWeek].coerceAtLeast(usedTimeItem.usedMillis)
}
}
return if (dayOfWeekForDailyRule == null) usedTimeByDay.sum()
else usedTimeByDay[dayOfWeekForDailyRule]
}
}
}

View file

@ -55,7 +55,7 @@ object LocalDatabaseAppLogicActionDispatcher {
if (updatedRows == 0) {
// create new entry
val oldTime = database.usedTimes().getUsedTimeItemsSyncIncludingBigger(
val oldTime = database.usedTimes().getUsedTimeItemsSyncIncludingSmaller(
item.categoryId, action.dayOfEpoch, start, end
).map { it.usedMillis }.maxOrNull() ?: 0

View file

@ -1,5 +1,5 @@
/*
* TimeLimit Copyright <C> 2019 - 2022 Jonas Lochmann
* TimeLimit Copyright <C> 2019 - 2023 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -27,6 +27,7 @@ import io.timelimit.android.date.DateInTimezone
import io.timelimit.android.extensions.MinuteOfDay
import io.timelimit.android.logic.DefaultAppLogic
import io.timelimit.android.logic.DummyApps
import io.timelimit.android.logic.RemainingTime
import io.timelimit.android.ui.manage.category.timelimit_rules.TimeLimitRulesHandlers
import io.timelimit.android.util.DayNameUtil
import io.timelimit.android.util.TimeTextUtil
@ -156,20 +157,12 @@ class AppAndRuleAdapter: RecyclerView.Adapter<AppAndRuleAdapter.Holder>() {
val binding = holder.itemView.tag as FragmentCategoryTimeLimitRuleItemBinding
val context = binding.root.context
val usedTime = date?.let { date ->
usedTimes.filter { usedTime ->
val usedTimeDayOfWeek = usedTime.dayOfEpoch - date.firstDayOfWeekAsEpochDay
val matchingWeek = usedTimeDayOfWeek in 0..6
if (matchingWeek) {
val matchingSlot = usedTime.startTimeOfDay == rule.startMinuteOfDay && usedTime.endTimeOfDay == rule.endMinuteOfDay
val maskToCheck = if (rule.perDay && rule.appliesToMultipleDays) {
rule.dayMask.takeHighestOneBit().toInt().coerceAtMost(1 shl date.dayOfWeek)
} else rule.dayMask.toInt()
val matchingMask = (maskToCheck and (1 shl usedTimeDayOfWeek) != 0)
matchingSlot && matchingMask
} else false
}.map { it.usedMillis }.sum().toInt()
RemainingTime.getUsedTime(
usedTimes = usedTimes,
rule = rule,
firstDayOfWeekAsEpochDay = date.firstDayOfWeekAsEpochDay,
dayOfWeekForDailyRule = if (rule.perDay) date.dayOfWeek else null
).toInt()
} ?: 0
binding.maxTimeString = rule.maximumTimeInMillis.let { time ->