Skip to content

Commit

Permalink
add days count selection for Last days range
Browse files Browse the repository at this point in the history
  • Loading branch information
Razeeman committed Jul 14, 2024
1 parent 721eca5 commit 78ed1a5
Show file tree
Hide file tree
Showing 56 changed files with 673 additions and 178 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.example.util.simpletimetracker.core.dialog

interface DurationDialogListener {

fun onDurationSet(duration: Long, tag: String? = null)
fun onDurationSet(duration: Long, tag: String? = null) {}

fun onCountSet(count: Long, tag: String? = null) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ class StatisticsDetailNavigationInteractor @Inject constructor(
start = rangeLength.range.timeStarted,
end = rangeLength.range.timeEnded,
)
is RangeLength.Last -> StatisticsDetailParams.RangeLengthParams.Last
is RangeLength.Last -> StatisticsDetailParams.RangeLengthParams.Last(
days = rangeLength.days,
)
},
shift = shift,
preview = StatisticsDetailParams.Preview(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.example.util.simpletimetracker.core.repo.ResourceRepo
import com.example.util.simpletimetracker.core.viewData.RangeViewData
import com.example.util.simpletimetracker.core.viewData.RangesViewData
import com.example.util.simpletimetracker.core.viewData.SelectDateViewData
import com.example.util.simpletimetracker.core.viewData.SelectLastDaysViewData
import com.example.util.simpletimetracker.core.viewData.SelectRangeViewData
import com.example.util.simpletimetracker.domain.extension.orZero
import com.example.util.simpletimetracker.domain.model.DayOfWeek
Expand All @@ -16,15 +17,27 @@ class RangeViewDataMapper @Inject constructor(
private val timeMapper: TimeMapper,
) {

fun mapToRanges(currentRange: RangeLength, addSelection: Boolean = true): RangesViewData {
fun mapToRanges(
currentRange: RangeLength,
addSelection: Boolean,
lastDaysCount: Int,
): RangesViewData {
val selectDateButton = mapToSelectDateName(currentRange)
?.takeIf { addSelection }?.let(::listOf) ?: emptyList()
val selectRangeButton = mapToSelectRange()
.takeIf { addSelection }?.let(::listOf) ?: emptyList()
val selectLastDaysButton = mapToSelectLastDays(lastDaysCount)
.let(::listOf)

val data = selectDateButton + selectRangeButton + ranges.map(::mapToRangeName)
val selectedPosition = data.indexOfFirst {
(it as? RangeViewData)?.range == currentRange
val data = selectDateButton +
selectRangeButton +
selectLastDaysButton +
ranges.mapNotNull(::mapToRangeName)

val selectedPosition = when (currentRange) {
is RangeLength.Custom -> data.indexOfFirst { it is SelectRangeViewData }
is RangeLength.Last -> data.indexOfFirst { it is SelectLastDaysViewData }
else -> data.indexOfFirst { (it as? RangeViewData)?.range == currentRange }
}.takeUnless { it == -1 }.orZero()

return RangesViewData(
Expand All @@ -45,8 +58,8 @@ class RangeViewDataMapper @Inject constructor(
is RangeLength.Month -> timeMapper.toMonthTitle(position, startOfDayShift)
is RangeLength.Year -> timeMapper.toYearTitle(position, startOfDayShift)
is RangeLength.All -> resourceRepo.getString(R.string.range_overall)
is RangeLength.Custom -> resourceRepo.getString(R.string.range_custom)
is RangeLength.Last -> resourceRepo.getString(R.string.range_last)
is RangeLength.Custom -> mapToSelectRangeName()
is RangeLength.Last -> mapToSelectLastDaysName(rangeLength.days)
}
}

Expand All @@ -68,15 +81,16 @@ class RangeViewDataMapper @Inject constructor(
}
}

private fun mapToRangeName(rangeLength: RangeLength): RangeViewData {
private fun mapToRangeName(rangeLength: RangeLength): RangeViewData? {
val text = when (rangeLength) {
is RangeLength.Day -> R.string.range_day
is RangeLength.Week -> R.string.range_week
is RangeLength.Month -> R.string.range_month
is RangeLength.Year -> R.string.range_year
is RangeLength.All -> R.string.range_overall
is RangeLength.Custom -> R.string.range_custom
is RangeLength.Last -> R.string.range_last
// These ranges mapped separately
is RangeLength.Custom -> return null
is RangeLength.Last -> return null
}.let(resourceRepo::getString)

return RangeViewData(
Expand All @@ -98,12 +112,25 @@ class RangeViewDataMapper @Inject constructor(
}

private fun mapToSelectRange(): SelectRangeViewData {
return SelectRangeViewData(text = resourceRepo.getString(R.string.range_custom))
val text = mapToSelectRangeName()
return SelectRangeViewData(text)
}

private fun mapToSelectRangeName(): String {
return resourceRepo.getString(R.string.range_custom)
}

private fun mapToSelectLastDays(days: Int): SelectLastDaysViewData {
val text = mapToSelectLastDaysName(days)
return SelectLastDaysViewData(text)
}

private fun mapToSelectLastDaysName(days: Int): String {
return resourceRepo.getQuantityString(R.plurals.range_last, days, days)
}

companion object {
private val ranges: List<RangeLength> = listOf(
RangeLength.Last,
RangeLength.All,
RangeLength.Year,
RangeLength.Month,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ class TimeMapper @Inject constructor(

is RangeLength.Last -> {
rangeEnd = calendar.apply { add(Calendar.DATE, 1) }.timeInMillis
rangeStart = calendar.apply { add(Calendar.DATE, -rangeLength.DAYS) }.timeInMillis
rangeStart = calendar.apply { add(Calendar.DATE, -rangeLength.days) }.timeInMillis
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class ResourceRepo @Inject constructor(
return context.resources.getQuantityString(stringResId, quantity)
}

fun getQuantityString(@PluralsRes stringResId: Int, quantity: Int, vararg args: Any): String {
return context.resources.getQuantityString(stringResId, quantity, *args)
}

fun getDimenInDp(@DimenRes dimenResId: Int): Int {
return context.resources.getDimension(dimenResId).pxToDp()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.util.simpletimetracker.core.viewData

import com.example.util.simpletimetracker.feature_views.spinner.CustomSpinner

data class SelectLastDaysViewData(
override val text: String,
) : CustomSpinner.CustomSpinnerItem()
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ class PrefsRepoImpl @Inject constructor(
KEY_STATISTICS_RANGE_CUSTOM_END, 0,
)

override var statisticsRangeLastDays: Int by prefs.delegate(
KEY_STATISTICS_RANGE_LAST_DAYS, RANGE_LAST_DAYS_DEFAULT,
)

override var statisticsDetailRange: Int by prefs.delegate(
KEY_STATISTICS_DETAIL_RANGE, 0,
)
Expand All @@ -74,6 +78,10 @@ class PrefsRepoImpl @Inject constructor(
KEY_STATISTICS_DETAIL_RANGE_CUSTOM_END, 0,
)

override var statisticsDetailRangeLastDays: Int by prefs.delegate(
KEY_STATISTICS_DETAIL_RANGE_LAST_DAYS, RANGE_LAST_DAYS_DEFAULT,
)

override var keepStatisticsRange: Boolean by prefs.delegate(
KEY_KEEP_STATISTICS_RANGE, false,
)
Expand Down Expand Up @@ -328,13 +336,19 @@ class PrefsRepoImpl @Inject constructor(
is RangeLength.Last -> 5
is RangeLength.Custom -> 0 // Not possible
}
val rangeDataLastDays = (data.rangeLength as? RangeLength.Last)?.days
val filteredTypesData = data.filteredTypes.map(Long::toString).toSet()
val filteredCategoriesData = data.filteredCategories.map(Long::toString).toSet()
val filteredTagsData = data.filteredTags.map(Long::toString).toSet()

prefs.edit()
.putInt(KEY_STATISTICS_WIDGET_FILTER_TYPE + widgetId, filterTypeData)
.putInt(KEY_STATISTICS_WIDGET_RANGE + widgetId, rangeData)
.apply {
if (rangeDataLastDays != null) {
putInt(KEY_STATISTICS_WIDGET_RANGE_LAST_DAYS + widgetId, rangeDataLastDays)
}
}
.putStringSet(KEY_STATISTICS_WIDGET_FILTERED_TYPES + widgetId, filteredTypesData)
.putStringSet(KEY_STATISTICS_WIDGET_FILTERED_CATEGORIES + widgetId, filteredCategoriesData)
.putStringSet(KEY_STATISTICS_WIDGET_FILTERED_TAGS + widgetId, filteredTagsData)
Expand All @@ -355,7 +369,9 @@ class PrefsRepoImpl @Inject constructor(
2 -> RangeLength.Month
3 -> RangeLength.Year
4 -> RangeLength.All
5 -> RangeLength.Last
5 -> RangeLength.Last(
days = getStatisticsWidgetLastDays(widgetId),
)
else -> RangeLength.Day
}
val filteredTypes = prefs
Expand All @@ -377,11 +393,19 @@ class PrefsRepoImpl @Inject constructor(
)
}

override fun getStatisticsWidgetLastDays(widgetId: Int): Int {
return prefs.getInt(
KEY_STATISTICS_WIDGET_RANGE_LAST_DAYS + widgetId,
RANGE_LAST_DAYS_DEFAULT,
)
}

override fun removeStatisticsWidget(widgetId: Int) {
logPrefsDataAccess("removeStatisticsWidget $widgetId")
prefs.edit()
.remove(KEY_STATISTICS_WIDGET_FILTER_TYPE + widgetId)
.remove(KEY_STATISTICS_WIDGET_RANGE + widgetId)
.remove(KEY_STATISTICS_WIDGET_RANGE_LAST_DAYS + widgetId)
.remove(KEY_STATISTICS_WIDGET_FILTERED_TYPES + widgetId)
.remove(KEY_STATISTICS_WIDGET_FILTERED_CATEGORIES + widgetId)
.remove(KEY_STATISTICS_WIDGET_FILTERED_TAGS + widgetId)
Expand Down Expand Up @@ -492,6 +516,7 @@ class PrefsRepoImpl @Inject constructor(
private const val POMODORO_DEFAULT_BREAK_TIME_SEC: Long = 60 * 5 // 5 min
private const val POMODORO_DEFAULT_LONG_BREAK_TIME_SEC: Long = 60 * 15 // 15 min
private const val POMODORO_DEFAULT_UNTIL_LONG_BREAK: Long = 4
private const val RANGE_LAST_DAYS_DEFAULT: Int = 7

private const val KEY_RECORD_TYPES_FILTERED_ON_CHART = "recordTypesFilteredOnChart"
private const val KEY_CATEGORIES_TYPES_FILTERED_ON_CHART = "categoriesFilteredOnChart"
Expand All @@ -503,9 +528,11 @@ class PrefsRepoImpl @Inject constructor(
private const val KEY_STATISTICS_RANGE = "statisticsRange"
private const val KEY_STATISTICS_RANGE_CUSTOM_START = "statisticsRangeCustomStart"
private const val KEY_STATISTICS_RANGE_CUSTOM_END = "statisticsRangeCustomEnd"
private const val KEY_STATISTICS_RANGE_LAST_DAYS = "statisticsRangeLastDays"
private const val KEY_STATISTICS_DETAIL_RANGE = "statisticsDetailRange"
private const val KEY_STATISTICS_DETAIL_RANGE_CUSTOM_START = "statisticsDetailRangeCustomStart"
private const val KEY_STATISTICS_DETAIL_RANGE_CUSTOM_END = "statisticsDetailRangeCustomEnd"
private const val KEY_STATISTICS_DETAIL_RANGE_LAST_DAYS = "statisticsDetailRangeLastDays"
private const val KEY_KEEP_STATISTICS_RANGE = "keepStatisticsRange"
private const val KEY_FIRST_DAY_OF_WEEK = "firstDayOfWeek"
private const val KEY_START_OF_DAY_SHIFT = "startOfDayShift"
Expand Down Expand Up @@ -567,6 +594,7 @@ class PrefsRepoImpl @Inject constructor(
private const val KEY_STATISTICS_WIDGET_FILTERED_TAGS = "statistics_widget_filtered_tags_"
private const val KEY_STATISTICS_WIDGET_FILTER_TYPE = "statistics_widget_filter_type_"
private const val KEY_STATISTICS_WIDGET_RANGE = "statistics_widget_range_"
private const val KEY_STATISTICS_WIDGET_RANGE_LAST_DAYS = "statistics_widget_range_last_days_"
private const val KEY_QUICK_SETTINGS_WIDGET_TYPE = "quick_settings_widget_type_"
private const val KEY_CARD_ORDER_MANUAL = "cardOrderManual"
private const val KEY_CATEGORY_ORDER_MANUAL = "categoryOrderManual"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.util.simpletimetracker.domain.interactor

import javax.inject.Inject

class GetProcessedLastDaysCountInteractor @Inject constructor() {

fun execute(enteredCount: Long): Int {
return enteredCount.toInt().coerceIn(2..365)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ class PrefsInteractor @Inject constructor(
prefsRepo.statisticsRangeCustomStart = rangeLength.range.timeStarted
prefsRepo.statisticsRangeCustomEnd = rangeLength.range.timeEnded
}
if (rangeLength is RangeLength.Last) {
prefsRepo.statisticsRangeLastDays = rangeLength.days
}
}

suspend fun getStatisticsLastDays(): Int = withContext(Dispatchers.IO) {
prefsRepo.statisticsRangeLastDays
}

suspend fun getStatisticsDetailRange(): RangeLength = withContext(Dispatchers.IO) {
Expand All @@ -115,6 +122,13 @@ class PrefsInteractor @Inject constructor(
prefsRepo.statisticsDetailRangeCustomStart = rangeLength.range.timeStarted
prefsRepo.statisticsDetailRangeCustomEnd = rangeLength.range.timeEnded
}
if (rangeLength is RangeLength.Last) {
prefsRepo.statisticsDetailRangeLastDays = rangeLength.days
}
}

suspend fun getStatisticsDetailLastDays(): Int = withContext(Dispatchers.IO) {
prefsRepo.statisticsDetailRangeLastDays
}

suspend fun getKeepStatisticsRange(): Boolean = withContext(Dispatchers.IO) {
Expand Down Expand Up @@ -548,6 +562,10 @@ class PrefsInteractor @Inject constructor(
prefsRepo.getStatisticsWidget(widgetId)
}

suspend fun getStatisticsWidgetLastDays(widgetId: Int): Int = withContext(Dispatchers.IO) {
prefsRepo.getStatisticsWidgetLastDays(widgetId)
}

suspend fun removeStatisticsWidget(widgetId: Int) = withContext(Dispatchers.IO) {
prefsRepo.removeStatisticsWidget(widgetId)
}
Expand Down Expand Up @@ -707,7 +725,13 @@ class PrefsInteractor @Inject constructor(
)
}.let(RangeLength::Custom)
}
6 -> RangeLength.Last
6 -> {
if (forDetail) {
prefsRepo.statisticsDetailRangeLastDays
} else {
prefsRepo.statisticsRangeLastDays
}.let(RangeLength::Last)
}
else -> RangeLength.Day
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@ sealed class RangeLength {
object Year : RangeLength()
object All : RangeLength()
data class Custom(val range: Range) : RangeLength()
object Last : RangeLength() {
const val DAYS: Int = 7
}
data class Last(val days: Int) : RangeLength()
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ interface PrefsRepo {
var statisticsRange: Int
var statisticsRangeCustomStart: Long
var statisticsRangeCustomEnd: Long
var statisticsRangeLastDays: Int

var statisticsDetailRange: Int
var statisticsDetailRangeCustomStart: Long
var statisticsDetailRangeCustomEnd: Long
var statisticsDetailRangeLastDays: Int

var keepStatisticsRange: Boolean

Expand Down Expand Up @@ -145,6 +147,8 @@ interface PrefsRepo {

fun getStatisticsWidget(widgetId: Int): StatisticsWidgetData

fun getStatisticsWidgetLastDays(widgetId: Int): Int

fun removeStatisticsWidget(widgetId: Int)

fun setQuickSettingsWidget(widgetId: Int, data: QuickSettingsWidgetType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.example.util.simpletimetracker.core.base.BaseFragment
import com.example.util.simpletimetracker.core.di.BaseViewModelFactory
import com.example.util.simpletimetracker.core.dialog.CustomRangeSelectionDialogListener
import com.example.util.simpletimetracker.core.dialog.DateTimeDialogListener
import com.example.util.simpletimetracker.core.dialog.DurationDialogListener
import com.example.util.simpletimetracker.core.view.SafeFragmentStateAdapter
import com.example.util.simpletimetracker.core.viewData.RangesViewData
import com.example.util.simpletimetracker.domain.model.Range
Expand All @@ -25,6 +26,7 @@ import javax.inject.Inject
class StatisticsContainerFragment :
BaseFragment<Binding>(),
DateTimeDialogListener,
DurationDialogListener,
CustomRangeSelectionDialogListener {

override val inflater: (LayoutInflater, ViewGroup?, Boolean) -> Binding =
Expand Down Expand Up @@ -65,6 +67,10 @@ class StatisticsContainerFragment :
settingsViewModel.onCustomRangeSelected(range)
}

override fun onCountSet(count: Long, tag: String?) {
settingsViewModel.onCountSet(count, tag)
}

override fun initViewModel() {
with(viewModel) {
title.observe(::updateTitle)
Expand Down
Loading

0 comments on commit 78ed1a5

Please sign in to comment.