diff --git a/core/src/main/java/com/example/util/simpletimetracker/core/interactor/RecordFilterInteractor.kt b/core/src/main/java/com/example/util/simpletimetracker/core/interactor/RecordFilterInteractor.kt index 39cabde76..b6cc9f0d2 100644 --- a/core/src/main/java/com/example/util/simpletimetracker/core/interactor/RecordFilterInteractor.kt +++ b/core/src/main/java/com/example/util/simpletimetracker/core/interactor/RecordFilterInteractor.kt @@ -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 { diff --git a/core/src/main/java/com/example/util/simpletimetracker/core/mapper/RangeViewDataMapper.kt b/core/src/main/java/com/example/util/simpletimetracker/core/mapper/RangeViewDataMapper.kt index 4a73ee091..d704afadb 100644 --- a/core/src/main/java/com/example/util/simpletimetracker/core/mapper/RangeViewDataMapper.kt +++ b/core/src/main/java/com/example/util/simpletimetracker/core/mapper/RangeViewDataMapper.kt @@ -52,6 +52,7 @@ class RangeViewDataMapper @Inject constructor( position: Int, startOfDayShift: Long, firstDayOfWeek: DayOfWeek, + useShortCustomRange: Boolean = false, ): String { return when (rangeLength) { is RangeLength.Day -> timeMapper.toDayTitle(position, startOfDayShift) @@ -59,8 +60,12 @@ 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 -> mapToCustomRangeTitle(rangeLength.range) is RangeLength.Last -> mapToLastDaysTitle(rangeLength.days) + is RangeLength.Custom -> if (useShortCustomRange) { + mapToSelectRangeName() + } else { + mapToCustomRangeTitle(rangeLength.range) + } } } diff --git a/domain/src/main/java/com/example/util/simpletimetracker/domain/extension/DomainExtensions.kt b/domain/src/main/java/com/example/util/simpletimetracker/domain/extension/DomainExtensions.kt index beaaff317..4289c3db4 100644 --- a/domain/src/main/java/com/example/util/simpletimetracker/domain/extension/DomainExtensions.kt +++ b/domain/src/main/java/com/example/util/simpletimetracker/domain/extension/DomainExtensions.kt @@ -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 List?.orEmpty(): List = this ?: emptyList() fun List.rotateLeft(n: Int): List = drop(n) + take(n) diff --git a/features/feature_dialogs/src/main/java/com/example/util/simpletimetracker/feature_dialogs/csvExportSettings/interactor/CsvExportSettingsViewDataInteractor.kt b/features/feature_dialogs/src/main/java/com/example/util/simpletimetracker/feature_dialogs/csvExportSettings/interactor/CsvExportSettingsViewDataInteractor.kt index aa88c0574..c22624f84 100644 --- a/features/feature_dialogs/src/main/java/com/example/util/simpletimetracker/feature_dialogs/csvExportSettings/interactor/CsvExportSettingsViewDataInteractor.kt +++ b/features/feature_dialogs/src/main/java/com/example/util/simpletimetracker/feature_dialogs/csvExportSettings/interactor/CsvExportSettingsViewDataInteractor.kt @@ -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 @@ -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, @@ -80,6 +82,7 @@ class CsvExportSettingsViewDataInteractor @Inject constructor( RangeLength.Year, RangeLength.All, RangeLength.Last(lastDays), + RangeLength.Custom(customRange), ).mapIndexed { index, rangeLength -> mapDateRangeFilter( rangeLength = rangeLength, @@ -110,6 +113,7 @@ class CsvExportSettingsViewDataInteractor @Inject constructor( position = 0, startOfDayShift = startOfDayShift, firstDayOfWeek = firstDayOfWeek, + useShortCustomRange = true, ), color = if (selected) { colorMapper.toActiveColor(isDarkTheme) diff --git a/features/feature_dialogs/src/main/java/com/example/util/simpletimetracker/feature_dialogs/csvExportSettings/viewModel/CsvExportSettingsViewModel.kt b/features/feature_dialogs/src/main/java/com/example/util/simpletimetracker/feature_dialogs/csvExportSettings/viewModel/CsvExportSettingsViewModel.kt index 073ccb550..d9465201a 100644 --- a/features/feature_dialogs/src/main/java/com/example/util/simpletimetracker/feature_dialogs/csvExportSettings/viewModel/CsvExportSettingsViewModel.kt +++ b/features/feature_dialogs/src/main/java/com/example/util/simpletimetracker/feature_dialogs/csvExportSettings/viewModel/CsvExportSettingsViewModel.kt @@ -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 { @@ -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(), ) } } diff --git a/features/feature_dialogs/src/main/res/layout/csv_export_settings_fragment.xml b/features/feature_dialogs/src/main/res/layout/csv_export_settings_fragment.xml index b352c51d5..7c514a83a 100644 --- a/features/feature_dialogs/src/main/res/layout/csv_export_settings_fragment.xml +++ b/features/feature_dialogs/src/main/res/layout/csv_export_settings_fragment.xml @@ -40,7 +40,7 @@ , itemType: FilterViewData.Type, + currentRange: Range, ): List { 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 } diff --git a/features/feature_records_filter/src/main/java/com/example/util/simpletimetracker/feature_records_filter/interactor/RecordsFilterViewDataInteractor.kt b/features/feature_records_filter/src/main/java/com/example/util/simpletimetracker/feature_records_filter/interactor/RecordsFilterViewDataInteractor.kt index a6d92b228..4fe0d76d1 100644 --- a/features/feature_records_filter/src/main/java/com/example/util/simpletimetracker/feature_records_filter/interactor/RecordsFilterViewDataInteractor.kt +++ b/features/feature_records_filter/src/main/java/com/example/util/simpletimetracker/feature_records_filter/interactor/RecordsFilterViewDataInteractor.kt @@ -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 @@ -497,7 +498,7 @@ class RecordsFilterViewDataInteractor @Inject constructor( suspend fun getDateFilterSelectionViewData( filters: List, - defaultRange: Range, + currentRange: Range, extra: RecordsFilterParams, ): List = withContext(Dispatchers.Default) { val result: MutableList = mutableListOf() @@ -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, @@ -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, @@ -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), diff --git a/features/feature_records_filter/src/main/java/com/example/util/simpletimetracker/feature_records_filter/mapper/RecordsFilterViewDataMapper.kt b/features/feature_records_filter/src/main/java/com/example/util/simpletimetracker/feature_records_filter/mapper/RecordsFilterViewDataMapper.kt index b7d410d89..6ec33e056 100644 --- a/features/feature_records_filter/src/main/java/com/example/util/simpletimetracker/feature_records_filter/mapper/RecordsFilterViewDataMapper.kt +++ b/features/feature_records_filter/src/main/java/com/example/util/simpletimetracker/feature_records_filter/mapper/RecordsFilterViewDataMapper.kt @@ -223,6 +223,7 @@ class RecordsFilterViewDataMapper @Inject constructor( position = 0, startOfDayShift = startOfDayShift, firstDayOfWeek = firstDayOfWeek, + useShortCustomRange = true, ), color = if (selected) { colorMapper.toActiveColor(isDarkTheme) diff --git a/features/feature_records_filter/src/main/java/com/example/util/simpletimetracker/feature_records_filter/viewModel/RecordsFilterViewModel.kt b/features/feature_records_filter/src/main/java/com/example/util/simpletimetracker/feature_records_filter/viewModel/RecordsFilterViewModel.kt index 026008894..7108866b8 100644 --- a/features/feature_records_filter/src/main/java/com/example/util/simpletimetracker/feature_records_filter/viewModel/RecordsFilterViewModel.kt +++ b/features/feature_records_filter/src/main/java/com/example/util/simpletimetracker/feature_records_filter/viewModel/RecordsFilterViewModel.kt @@ -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 @@ -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) @@ -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(), ) } @@ -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 -> { @@ -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 -> { @@ -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 { return types.takeUnless { it.isEmpty() } ?: run { recordTypeInteractor.getAll().also { types = it } } @@ -688,7 +696,7 @@ class RecordsFilterViewModel @Inject constructor( RecordFilterType.Date -> { viewDataInteractor.getDateFilterSelectionViewData( filters = filters, - defaultRange = defaultRange, + currentRange = getCurrentRange(), extra = extra, ) }