From 1cdaed60e1b4c365e7d1f82ed173f5b68b95b5fe Mon Sep 17 00:00:00 2001 From: Jonas Lochmann Date: Mon, 28 Oct 2024 01:00:00 +0100 Subject: [PATCH] Fix incorrect used time at rules that apply per day at days where they do not apply --- .../appsandrules/AppAndRuleAdapter.kt | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/io/timelimit/android/ui/manage/category/appsandrules/AppAndRuleAdapter.kt b/app/src/main/java/io/timelimit/android/ui/manage/category/appsandrules/AppAndRuleAdapter.kt index 2312e86..be2a04a 100644 --- a/app/src/main/java/io/timelimit/android/ui/manage/category/appsandrules/AppAndRuleAdapter.kt +++ b/app/src/main/java/io/timelimit/android/ui/manage/category/appsandrules/AppAndRuleAdapter.kt @@ -31,6 +31,7 @@ import io.timelimit.android.logic.RemainingTime import io.timelimit.android.ui.manage.category.timelimit_rules.TimeLimitRulesHandlers import io.timelimit.android.ui.util.DateUtil import io.timelimit.android.util.DayNameUtil +import io.timelimit.android.util.Option import io.timelimit.android.util.TimeTextUtil import kotlin.properties.Delegates @@ -159,12 +160,24 @@ class AppAndRuleAdapter: RecyclerView.Adapter() { val binding = holder.itemView.tag as FragmentCategoryTimeLimitRuleItemBinding val context = binding.root.context val usedTime = date?.let { date -> - RemainingTime.getUsedTime( - usedTimes = usedTimes, - rule = rule, - firstDayOfWeekAsEpochDay = date.firstDayOfWeekAsEpochDay, - dayOfWeekForDailyRule = if (rule.perDay) date.dayOfWeek else null - ).toInt() + val dayOfWeekForDailyRule: Option? = + if (rule.perDay) { + (0 until 7) + .map { (7 + date.dayOfWeek - it) % 7 } // make the current day the last one + .firstOrNull { rule.dayMask.toInt() and (1 shl it) != 0 } + ?.let { Option.Some(it) } // skip calculation if no day matches + } else Option.Some(null) // use the value null + + dayOfWeekForDailyRule?.let { + RemainingTime.getUsedTime( + usedTimes = usedTimes, + rule = rule, + firstDayOfWeekAsEpochDay = date.firstDayOfWeekAsEpochDay, + dayOfWeekForDailyRule = + if (it is Option.Some) it.value + else null + ).toInt() + } } ?: 0 binding.maxTimeString = rule.maximumTimeInMillis.let { time ->