Skip to content

Commit

Permalink
add date range filters to csv export
Browse files Browse the repository at this point in the history
  • Loading branch information
Razeeman committed Oct 5, 2024
1 parent 4020609 commit 6f42b31
Show file tree
Hide file tree
Showing 39 changed files with 451 additions and 257 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,22 @@ class PrefsRepoImpl @Inject constructor(
KEY_STATISTICS_DETAIL_RANGE_LAST_DAYS, RANGE_LAST_DAYS_DEFAULT,
)

override var fileExportRange: Int by prefs.delegate(
KEY_FILE_EXPORT_RANGE, 0,
)

override var fileExportRangeCustomStart: Long by prefs.delegate(
KEY_FILE_EXPORT_RANGE_CUSTOM_START, 0,
)

override var fileExportRangeCustomEnd: Long by prefs.delegate(
KEY_FILE_EXPORT_RANGE_CUSTOM_END, 0,
)

override var fileExportRangeLastDays: Int by prefs.delegate(
KEY_FILE_EXPORT_RANGE_LAST_DAYS, RANGE_LAST_DAYS_DEFAULT,
)

override var keepStatisticsRange: Boolean by prefs.delegate(
KEY_KEEP_STATISTICS_RANGE, false,
)
Expand Down Expand Up @@ -509,6 +525,10 @@ class PrefsRepoImpl @Inject constructor(
const val KEY_STATISTICS_DETAIL_RANGE_CUSTOM_START = "statisticsDetailRangeCustomStart"
const val KEY_STATISTICS_DETAIL_RANGE_CUSTOM_END = "statisticsDetailRangeCustomEnd"
const val KEY_STATISTICS_DETAIL_RANGE_LAST_DAYS = "statisticsDetailRangeLastDays"
const val KEY_FILE_EXPORT_RANGE = "fileExportRange"
const val KEY_FILE_EXPORT_RANGE_CUSTOM_START = "fileExportRangeCustomStart"
const val KEY_FILE_EXPORT_RANGE_CUSTOM_END = "fileExportRangeCustomEnd"
const val KEY_FILE_EXPORT_RANGE_LAST_DAYS = "fileExportRangeLastDays"
const val KEY_KEEP_STATISTICS_RANGE = "keepStatisticsRange"
const val KEY_FIRST_DAY_OF_WEEK = "firstDayOfWeek"
const val KEY_START_OF_DAY_SHIFT = "startOfDayShift"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import com.example.util.simpletimetracker.data_local.repo.PrefsRepoImpl.Companio
import com.example.util.simpletimetracker.data_local.repo.PrefsRepoImpl.Companion.KEY_DEFAULT_TYPES_HIDDEN
import com.example.util.simpletimetracker.data_local.repo.PrefsRepoImpl.Companion.KEY_ENABLE_POMODORO_MODE
import com.example.util.simpletimetracker.data_local.repo.PrefsRepoImpl.Companion.KEY_ENABLE_REPEAT_BUTTON
import com.example.util.simpletimetracker.data_local.repo.PrefsRepoImpl.Companion.KEY_FILE_EXPORT_RANGE
import com.example.util.simpletimetracker.data_local.repo.PrefsRepoImpl.Companion.KEY_FILE_EXPORT_RANGE_CUSTOM_END
import com.example.util.simpletimetracker.data_local.repo.PrefsRepoImpl.Companion.KEY_FILE_EXPORT_RANGE_CUSTOM_START
import com.example.util.simpletimetracker.data_local.repo.PrefsRepoImpl.Companion.KEY_FILE_EXPORT_RANGE_LAST_DAYS
import com.example.util.simpletimetracker.data_local.repo.PrefsRepoImpl.Companion.KEY_FIRST_DAY_OF_WEEK
import com.example.util.simpletimetracker.data_local.repo.PrefsRepoImpl.Companion.KEY_IGNORE_SHORT_RECORDS_DURATION
import com.example.util.simpletimetracker.data_local.repo.PrefsRepoImpl.Companion.KEY_IGNORE_SHORT_UNTRACKED_DURATION
Expand Down Expand Up @@ -140,6 +144,10 @@ class BackupPrefsRepo @Inject constructor(
PrefsProcessor(KEY_STATISTICS_DETAIL_RANGE_CUSTOM_START, ::statisticsDetailRangeCustomStart),
PrefsProcessor(KEY_STATISTICS_DETAIL_RANGE_CUSTOM_END, ::statisticsDetailRangeCustomEnd),
PrefsProcessor(KEY_STATISTICS_DETAIL_RANGE_LAST_DAYS, ::statisticsDetailRangeLastDays),
PrefsProcessor(KEY_FILE_EXPORT_RANGE, ::fileExportRange),
PrefsProcessor(KEY_FILE_EXPORT_RANGE_CUSTOM_START, ::fileExportRangeCustomStart),
PrefsProcessor(KEY_FILE_EXPORT_RANGE_CUSTOM_END, ::fileExportRangeCustomEnd),
PrefsProcessor(KEY_FILE_EXPORT_RANGE_LAST_DAYS, ::fileExportRangeLastDays),
PrefsProcessor(KEY_KEEP_STATISTICS_RANGE, ::keepStatisticsRange),
PrefsProcessor(KEY_FIRST_DAY_OF_WEEK, ::firstDayOfWeek),
PrefsProcessor(KEY_START_OF_DAY_SHIFT, ::startOfDayShift),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,12 @@ class PrefsInteractor @Inject constructor(
}

suspend fun getStatisticsRange(): RangeLength = withContext(Dispatchers.IO) {
mapToRange(prefsRepo.statisticsRange, forDetail = false)
mapToRange(
value = prefsRepo.statisticsRange,
customStart = prefsRepo.statisticsRangeCustomStart,
customEnd = prefsRepo.statisticsRangeCustomEnd,
lastDays = prefsRepo.statisticsRangeLastDays,
)
}

suspend fun setStatisticsRange(rangeLength: RangeLength) = withContext(Dispatchers.IO) {
Expand All @@ -127,7 +132,12 @@ class PrefsInteractor @Inject constructor(
}

suspend fun getStatisticsDetailRange(): RangeLength = withContext(Dispatchers.IO) {
mapToRange(prefsRepo.statisticsDetailRange, forDetail = true)
mapToRange(
value = prefsRepo.statisticsDetailRange,
customStart = prefsRepo.statisticsDetailRangeCustomStart,
customEnd = prefsRepo.statisticsDetailRangeCustomEnd,
lastDays = prefsRepo.statisticsDetailRangeLastDays,
)
}

suspend fun setStatisticsDetailRange(rangeLength: RangeLength) = withContext(Dispatchers.IO) {
Expand All @@ -146,6 +156,31 @@ class PrefsInteractor @Inject constructor(
prefsRepo.statisticsDetailRangeLastDays
}

suspend fun getFileExportRange(): RangeLength = withContext(Dispatchers.IO) {
mapToRange(
value = prefsRepo.fileExportRange,
customStart = prefsRepo.fileExportRangeCustomStart,
customEnd = prefsRepo.fileExportRangeCustomEnd,
lastDays = prefsRepo.fileExportRangeLastDays,
)
}

suspend fun setFileExportRange(rangeLength: RangeLength) = withContext(Dispatchers.IO) {
prefsRepo.fileExportRange = mapRange(rangeLength)

if (rangeLength is RangeLength.Custom) {
prefsRepo.fileExportRangeCustomStart = rangeLength.range.timeStarted
prefsRepo.fileExportRangeCustomEnd = rangeLength.range.timeEnded
}
if (rangeLength is RangeLength.Last) {
prefsRepo.fileExportRangeLastDays = rangeLength.days
}
}

suspend fun getFileExportLastDays(): Int = withContext(Dispatchers.IO) {
prefsRepo.fileExportRangeLastDays
}

suspend fun getKeepStatisticsRange(): Boolean = withContext(Dispatchers.IO) {
prefsRepo.keepStatisticsRange
}
Expand Down Expand Up @@ -736,33 +771,23 @@ class PrefsInteractor @Inject constructor(
prefsRepo.clearPomodoroSettingsClick()
}

private fun mapToRange(value: Int, forDetail: Boolean): RangeLength {
private fun mapToRange(
value: Int,
customStart: Long,
customEnd: Long,
lastDays: Int,
): RangeLength {
return when (value) {
0 -> RangeLength.Day
1 -> RangeLength.Week
2 -> RangeLength.Month
3 -> RangeLength.Year
4 -> RangeLength.All
5 -> {
if (forDetail) {
Range(
timeStarted = prefsRepo.statisticsDetailRangeCustomStart,
timeEnded = prefsRepo.statisticsDetailRangeCustomEnd,
)
} else {
Range(
timeStarted = prefsRepo.statisticsRangeCustomStart,
timeEnded = prefsRepo.statisticsRangeCustomEnd,
)
}.let(RangeLength::Custom)
}
6 -> {
if (forDetail) {
prefsRepo.statisticsDetailRangeLastDays
} else {
prefsRepo.statisticsRangeLastDays
}.let(RangeLength::Last)
}
5 -> Range(
timeStarted = customStart,
timeEnded = customEnd,
).let(RangeLength::Custom)
6 -> lastDays.let(RangeLength::Last)
else -> RangeLength.Day
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ interface PrefsRepo {
var statisticsDetailRangeCustomEnd: Long
var statisticsDetailRangeLastDays: Int

var fileExportRange: Int
var fileExportRangeCustomStart: Long
var fileExportRangeCustomEnd: Long
var fileExportRangeLastDays: Int

var keepStatisticsRange: Boolean

var firstDayOfWeek: Int
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.example.util.simpletimetracker.feature_dialogs.csvExportSettings.interactor

import com.example.util.simpletimetracker.core.mapper.ColorMapper
import com.example.util.simpletimetracker.core.mapper.RangeViewDataMapper
import com.example.util.simpletimetracker.core.mapper.TimeMapper
import com.example.util.simpletimetracker.core.repo.ResourceRepo
import com.example.util.simpletimetracker.domain.interactor.PrefsInteractor
import com.example.util.simpletimetracker.domain.model.DayOfWeek
import com.example.util.simpletimetracker.domain.model.Range
import com.example.util.simpletimetracker.domain.model.RangeLength
import com.example.util.simpletimetracker.feature_base_adapter.ViewHolderType
import com.example.util.simpletimetracker.feature_base_adapter.recordFilter.FilterViewData
import com.example.util.simpletimetracker.feature_dialogs.R
import com.example.util.simpletimetracker.feature_dialogs.csvExportSettings.model.CsvExportSettingsFilterType
import com.example.util.simpletimetracker.feature_dialogs.csvExportSettings.viewData.CsvExportSettingsViewData
import javax.inject.Inject

class CsvExportSettingsViewDataInteractor @Inject constructor(
private val timeMapper: TimeMapper,
private val colorMapper: ColorMapper,
private val resourceRepo: ResourceRepo,
private val prefsInteractor: PrefsInteractor,
private val rangeViewDataMapper: RangeViewDataMapper,
) {

suspend fun getViewData(
rangeLength: RangeLength,
range: Range,
): CsvExportSettingsViewData {
val useMilitaryTime = prefsInteractor.getUseMilitaryTimeFormat()
val isDarkTheme = prefsInteractor.getDarkMode()

return CsvExportSettingsViewData(
rangeStartString = timeMapper.formatDateTimeYear(
time = range.timeStarted,
useMilitaryTime = useMilitaryTime,
),
rangeEndString = timeMapper.formatDateTimeYear(
time = range.timeEnded,
useMilitaryTime = useMilitaryTime,
),
textColor = if (rangeLength is RangeLength.Custom) {
R.attr.appTextPrimaryColor
} else {
R.attr.appTextHintColor
}.let {
resourceRepo.getThemedAttr(it, isDarkTheme)
},
filters = getDateFiltersViewData(
currentRange = rangeLength,
),
)
}

private suspend fun getDateFiltersViewData(
currentRange: RangeLength,
): List<ViewHolderType> {
val isDarkTheme = prefsInteractor.getDarkMode()
val startOfDayShift = prefsInteractor.getStartOfDayShift()
val firstDayOfWeek = prefsInteractor.getFirstDayOfWeek()
val lastDays = prefsInteractor.getFileExportLastDays()

return listOf(
RangeLength.Day,
RangeLength.Week,
RangeLength.Month,
RangeLength.Year,
RangeLength.All,
RangeLength.Last(lastDays),
).mapIndexed { index, rangeLength ->
mapDateRangeFilter(
rangeLength = rangeLength,
currentRange = currentRange,
isDarkTheme = isDarkTheme,
startOfDayShift = startOfDayShift,
firstDayOfWeek = firstDayOfWeek,
index = index,
)
}
}

private fun mapDateRangeFilter(
rangeLength: RangeLength,
currentRange: RangeLength,
isDarkTheme: Boolean,
startOfDayShift: Long,
firstDayOfWeek: DayOfWeek,
index: Int,
): ViewHolderType {
val selected = currentRange == rangeLength

return FilterViewData(
id = index.toLong(),
type = CsvExportSettingsFilterType(rangeLength),
name = rangeViewDataMapper.mapToTitle(
rangeLength = rangeLength,
position = 0,
startOfDayShift = startOfDayShift,
firstDayOfWeek = firstDayOfWeek,
),
color = if (selected) {
colorMapper.toActiveColor(isDarkTheme)
} else {
colorMapper.toInactiveColor(isDarkTheme)
},
selected = selected,
removeBtnVisible = false,
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.util.simpletimetracker.feature_dialogs.csvExportSettings.model

import com.example.util.simpletimetracker.domain.model.RangeLength
import com.example.util.simpletimetracker.feature_base_adapter.recordFilter.FilterViewData

data class CsvExportSettingsFilterType(
val rangeLength: RangeLength,
) : FilterViewData.Type
Loading

0 comments on commit 6f42b31

Please sign in to comment.