Skip to content

Commit

Permalink
add custom range to date filters on records filters and data export
Browse files Browse the repository at this point in the history
  • Loading branch information
Razeeman committed Nov 9, 2024
1 parent 5c85c19 commit 7024066
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class RecordFilterInteractor @Inject constructor(
return RecordsFilter.Date(rangeLength, rangePosition)
}

// RangeLength.All return empty range, need to check separately.
suspend fun getRange(
filter: RecordsFilter.Date,
): Range {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,20 @@ class RangeViewDataMapper @Inject constructor(
position: Int,
startOfDayShift: Long,
firstDayOfWeek: DayOfWeek,
useShortCustomRange: Boolean = false,
): String {
return when (rangeLength) {
is RangeLength.Day -> timeMapper.toDayTitle(position, startOfDayShift)
is RangeLength.Week -> timeMapper.toWeekTitle(position, startOfDayShift, firstDayOfWeek)
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 -> mapToCustomRangeTitle(rangeLength.range)
is RangeLength.Last -> mapToLastDaysTitle(rangeLength.days)
is RangeLength.Custom -> if (useShortCustomRange) {
mapToSelectRangeName()
} else {
mapToCustomRangeTitle(rangeLength.range)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ fun Int?.orZero(): Int = this ?: 0

fun Float?.orZero(): Float = this ?: 0f

fun Range?.orEmpty(): Range = this ?: Range(0,0)

fun <T> List<T>?.orEmpty(): List<T> = this ?: emptyList()

fun <T> List<T>.rotateLeft(n: Int): List<T> = drop(n) + take(n)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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.extension.orEmpty
import com.example.util.simpletimetracker.domain.interactor.PrefsInteractor
import com.example.util.simpletimetracker.domain.model.DayOfWeek
import com.example.util.simpletimetracker.domain.model.Range
Expand Down Expand Up @@ -72,6 +73,7 @@ class CsvExportSettingsViewDataInteractor @Inject constructor(
val startOfDayShift = prefsInteractor.getStartOfDayShift()
val firstDayOfWeek = prefsInteractor.getFirstDayOfWeek()
val lastDays = prefsInteractor.getFileExportLastDays()
val customRange = (currentRange as? RangeLength.Custom)?.range.orEmpty()

return listOf(
RangeLength.Day,
Expand All @@ -80,6 +82,7 @@ class CsvExportSettingsViewDataInteractor @Inject constructor(
RangeLength.Year,
RangeLength.All,
RangeLength.Last(lastDays),
RangeLength.Custom(customRange),
).mapIndexed { index, rangeLength ->
mapDateRangeFilter(
rangeLength = rangeLength,
Expand Down Expand Up @@ -110,6 +113,7 @@ class CsvExportSettingsViewDataInteractor @Inject constructor(
position = 0,
startOfDayShift = startOfDayShift,
firstDayOfWeek = firstDayOfWeek,
useShortCustomRange = true,
),
color = if (selected) {
colorMapper.toActiveColor(isDarkTheme)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,14 @@ class CsvExportSettingsViewModel @Inject constructor(

fun onFilterClick(item: FilterViewData) = viewModelScope.launch {
val itemType = item.type as? CsvExportSettingsFilterType ?: return@launch
onNewRangeSelected(itemType.rangeLength)
val rangeLength = itemType.rangeLength
if (rangeLength is RangeLength.Custom) {
val currentRange = getRange()
val newRange = Range(currentRange.timeStarted, currentRange.timeEnded)
onNewRangeSelected(RangeLength.Custom(newRange))
} else {
onNewRangeSelected(itemType.rangeLength)
}
}

fun onRangeStartClick() = viewModelScope.launch {
Expand Down Expand Up @@ -126,17 +133,14 @@ class CsvExportSettingsViewModel @Inject constructor(
}

private suspend fun getRange(): Range {
val firstDayOfWeek = prefsInteractor.getFirstDayOfWeek()
val startOfDayShift = prefsInteractor.getStartOfDayShift()

return if (rangeLength is RangeLength.All) {
return Range(0, System.currentTimeMillis())
} else {
timeMapper.getRangeStartAndEnd(
rangeLength = rangeLength,
shift = 0,
firstDayOfWeek = firstDayOfWeek,
startOfDayShift = startOfDayShift,
firstDayOfWeek = prefsInteractor.getFirstDayOfWeek(),
startOfDayShift = prefsInteractor.getStartOfDayShift(),
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/inputCsvExportSettingsFileName"
style="@style/ChangeItemInputField"
style="@style/ChangeItemInputFieldDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="@dimen/edit_screen_margin_horizontal"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,19 @@ class RecordsFilterUpdateInteractor @Inject constructor(
fun handleRangeSet(
currentFilters: List<RecordsFilter>,
itemType: FilterViewData.Type,
currentRange: Range,
): List<RecordsFilter> {
val rangeLength = (itemType as? RecordFilterDateType)?.rangeLength
?: return currentFilters
val newRange = if (rangeLength is RangeLength.Custom) {
val newCustomRange = Range(currentRange.timeStarted, currentRange.timeEnded)
RangeLength.Custom(newCustomRange)
} else {
rangeLength
}
val filters = currentFilters.toMutableList()
filters.removeAll { it is RecordsFilter.Date }
filters.add(RecordsFilter.Date(rangeLength, 0))
filters.add(RecordsFilter.Date(newRange, 0))
return filters
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import com.example.util.simpletimetracker.domain.extension.hasMultitaskFilter
import com.example.util.simpletimetracker.domain.extension.hasUncategorizedItem
import com.example.util.simpletimetracker.domain.extension.hasUntaggedItem
import com.example.util.simpletimetracker.domain.extension.hasUntrackedFilter
import com.example.util.simpletimetracker.domain.extension.orEmpty
import com.example.util.simpletimetracker.domain.interactor.FilterSelectableTagsInteractor
import com.example.util.simpletimetracker.domain.interactor.PrefsInteractor
import com.example.util.simpletimetracker.domain.interactor.RecordInteractor
Expand Down Expand Up @@ -497,7 +498,7 @@ class RecordsFilterViewDataInteractor @Inject constructor(

suspend fun getDateFilterSelectionViewData(
filters: List<RecordsFilter>,
defaultRange: Range,
currentRange: Range,
extra: RecordsFilterParams,
): List<ViewHolderType> = withContext(Dispatchers.Default) {
val result: MutableList<ViewHolderType> = mutableListOf()
Expand All @@ -512,10 +513,7 @@ class RecordsFilterViewDataInteractor @Inject constructor(
} else {
extra.defaultLastDaysNumber
}
val range = filter
?.takeUnless { it.range is RangeLength.All }
?.let { recordFilterInteractor.getRange(it) }
?: defaultRange
val customRange = (filterRange as? RangeLength.Custom)?.range.orEmpty()

result += EmptySpaceViewData(
id = 1,
Expand All @@ -529,6 +527,7 @@ class RecordsFilterViewDataInteractor @Inject constructor(
RangeLength.Year,
RangeLength.All,
RangeLength.Last(lastDays),
RangeLength.Custom(customRange),
).mapIndexed { index, rangeLength ->
mapper.mapDateRangeFilter(
rangeLength = rangeLength,
Expand All @@ -544,12 +543,12 @@ class RecordsFilterViewDataInteractor @Inject constructor(
result += RecordsFilterRangeViewData(
id = 1L, // Only one at the time.
timeStarted = timeMapper.formatDateTimeYear(
time = range.timeStarted,
time = currentRange.timeStarted,
useMilitaryTime = useMilitaryTime,
),
timeStartedHint = resourceRepo.getString(R.string.change_record_date_time_start),
timeEnded = timeMapper.formatDateTimeYear(
time = range.timeEnded,
time = currentRange.timeEnded,
useMilitaryTime = useMilitaryTime,
),
timeEndedHint = resourceRepo.getString(R.string.change_record_date_time_end),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ class RecordsFilterViewDataMapper @Inject constructor(
position = 0,
startOfDayShift = startOfDayShift,
firstDayOfWeek = firstDayOfWeek,
useShortCustomRange = true,
),
color = if (selected) {
colorMapper.toActiveColor(isDarkTheme)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.example.util.simpletimetracker.domain.interactor.RecordTypeToTagInter
import com.example.util.simpletimetracker.domain.model.Category
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.domain.model.RecordTag
import com.example.util.simpletimetracker.domain.model.RecordType
import com.example.util.simpletimetracker.domain.model.RecordTypeCategory
Expand Down Expand Up @@ -222,7 +223,7 @@ class RecordsFilterViewModel @Inject constructor(
updateViewDataOnFiltersChanged()
}

fun onInnerFilterClick(item: FilterViewData) {
fun onInnerFilterClick(item: FilterViewData) = viewModelScope.launch {
when (item.type) {
is RecordFilterCommentType -> handleCommentFilterClick(item)
is RecordFilterDateType -> onDateRangeClick(item)
Expand Down Expand Up @@ -313,10 +314,11 @@ class RecordsFilterViewModel @Inject constructor(
updateViewDataOnFiltersChanged()
}

private fun onDateRangeClick(viewData: FilterViewData) {
private suspend fun onDateRangeClick(viewData: FilterViewData) {
filters = recordsFilterUpdateInteractor.handleRangeSet(
currentFilters = filters,
itemType = viewData.type,
currentRange = getCurrentRange(),
)
}

Expand Down Expand Up @@ -441,9 +443,7 @@ class RecordsFilterViewModel @Inject constructor(
}

private suspend fun handleDateFieldClick(fieldType: RecordsFilterRangeViewData.FieldType) {
val range = filters.getDate()
?.let { recordFilterInteractor.getRange(it) }
?: defaultRange
val range = getCurrentRange()

when (fieldType) {
RecordsFilterRangeViewData.FieldType.TIME_STARTED -> {
Expand Down Expand Up @@ -504,9 +504,7 @@ class RecordsFilterViewModel @Inject constructor(
}

private suspend fun handleDateSet(timestamp: Long, tag: String?) {
var (rangeStart, rangeEnd) = filters.getDate()
?.let { recordFilterInteractor.getRange(it) }
?: defaultRange
var (rangeStart, rangeEnd) = getCurrentRange()

when (tag) {
TIME_STARTED_TAG -> {
Expand Down Expand Up @@ -558,6 +556,16 @@ class RecordsFilterViewModel @Inject constructor(
updateRecords()
}

private suspend fun getCurrentRange(): Range {
val filter = filters.getDate() ?: return defaultRange

return if (filter.range is RangeLength.All) {
Range(0, System.currentTimeMillis())
} else {
recordFilterInteractor.getRange(filter)
}
}

private suspend fun getTypesCache(): List<RecordType> {
return types.takeUnless { it.isEmpty() }
?: run { recordTypeInteractor.getAll().also { types = it } }
Expand Down Expand Up @@ -688,7 +696,7 @@ class RecordsFilterViewModel @Inject constructor(
RecordFilterType.Date -> {
viewDataInteractor.getDateFilterSelectionViewData(
filters = filters,
defaultRange = defaultRange,
currentRange = getCurrentRange(),
extra = extra,
)
}
Expand Down

0 comments on commit 7024066

Please sign in to comment.