@@ -7,12 +7,15 @@ import androidx.lifecycle.viewModelScope
77import com.example.util.simpletimetracker.core.base.SingleLiveEvent
88import com.example.util.simpletimetracker.core.extension.set
99import com.example.util.simpletimetracker.core.extension.toParams
10+ import com.example.util.simpletimetracker.core.interactor.RecordFilterInteractor
1011import com.example.util.simpletimetracker.core.repo.DataEditRepo
1112import com.example.util.simpletimetracker.core.repo.ResourceRepo
1213import com.example.util.simpletimetracker.domain.extension.orZero
1314import com.example.util.simpletimetracker.domain.record.extension.getTypeIds
15+ import com.example.util.simpletimetracker.domain.record.model.Record
1416import com.example.util.simpletimetracker.domain.record.model.RecordsFilter
1517import com.example.util.simpletimetracker.domain.recordTag.interactor.RecordTypeToTagInteractor
18+ import com.example.util.simpletimetracker.domain.statistics.model.RangeLength
1619import com.example.util.simpletimetracker.feature_data_edit.R
1720import com.example.util.simpletimetracker.feature_data_edit.interactor.DateEditChangeInteractor
1821import com.example.util.simpletimetracker.feature_data_edit.interactor.DateEditViewDataInteractor
@@ -44,6 +47,7 @@ class DataEditViewModel @Inject constructor(
4447 private val dataEditViewDataInteractor : DateEditViewDataInteractor ,
4548 private val dataEditChangeInteractor : DateEditChangeInteractor ,
4649 private val recordTypeToTagInteractor : RecordTypeToTagInteractor ,
50+ private val recordFilterInteractor : RecordFilterInteractor ,
4751) : ViewModel() {
4852
4953 val selectedRecordsCountViewData: LiveData <DataEditRecordsCountState > by lazy {
@@ -73,10 +77,13 @@ class DataEditViewModel @Inject constructor(
7377 initial
7478 }
7579 }
80+ val deleteTodayRecordsButtonText: LiveData <String > = MutableLiveData ()
7681 val disableButtons: LiveData <Unit > = SingleLiveEvent <Unit >()
7782 val keyboardVisibility: LiveData <Boolean > = MutableLiveData (false )
7883
7984 private var filters: List <RecordsFilter > = emptyList()
85+ private var todayFilters: List <RecordsFilter > = emptyList()
86+ private var todayFiltersInitial: List <RecordsFilter > = emptyList()
8087 private var typeState: DataEditChangeActivityState = DataEditChangeActivityState .Disabled
8188 private var commentState: DataEditChangeCommentState = DataEditChangeCommentState .Disabled
8289 private var addTagState: DataEditAddTagsState = DataEditAddTagsState .Disabled
@@ -92,20 +99,17 @@ class DataEditViewModel @Inject constructor(
9299 deleteState is DataEditDeleteRecordsState .Enabled
93100 )
94101
102+ fun initialize () = viewModelScope.launch {
103+ initializeTodayFilters()
104+ updateDeleteTodayRecordsButtonText()
105+ }
106+
95107 fun onSelectRecordsClick () {
96- RecordsFilterParams (
97- tag = FILTER_TAG ,
98- title = resourceRepo.getString(R .string.chart_filter_hint),
99- flags = RecordsFilterParams .Flags (
100- dateSelectionAvailable = true ,
101- untrackedSelectionAvailable = false ,
102- multitaskSelectionAvailable = false ,
103- duplicationsSelectionAvailable = true ,
104- addRunningRecords = false ,
105- ),
106- filters = filters.map(RecordsFilter ::toParams),
107- defaultLastDaysNumber = 7 ,
108- ).let (router::navigate)
108+ onSelectRecordsClick(FILTER_TAG , filters)
109+ }
110+
111+ fun onViewTodayRecordsClick () {
112+ onSelectRecordsClick(TODAY_FILTER_TAG , todayFilters)
109113 }
110114
111115 fun onChangeActivityClick () {
@@ -223,6 +227,10 @@ class DataEditViewModel @Inject constructor(
223227 router.navigate(DataEditDuplicateTypeDialogParams )
224228 }
225229
230+ fun onDeleteTodayRecordsClick () {
231+ showAlert(tag = DELETE_TODAY_RECORDS_ALERT_DIALOG_TAG )
232+ }
233+
226234 fun onDeleteAllRecordsClick () {
227235 showAlert(tag = DELETE_RECORDS_ALERT_DIALOG_TAG )
228236 }
@@ -234,21 +242,49 @@ class DataEditViewModel @Inject constructor(
234242 fun onPositiveDialogClick (tag : String? ) {
235243 when (tag) {
236244 CHANGE_ALERT_DIALOG_TAG -> onChangeConfirmed()
245+ DELETE_TODAY_RECORDS_ALERT_DIALOG_TAG -> onDeleteTodayRecordsConfirmed()
237246 DELETE_RECORDS_ALERT_DIALOG_TAG -> onDeleteRecordsConfirmed()
238247 DELETE_DATA_ALERT_DIALOG_TAG -> onDeleteDataConfirmed()
239248 }
240249 }
241250
242251 fun onFilterSelected (result : RecordsFilterResultParams ) {
243- if (result.tag != FILTER_TAG ) return
244- filters = result.filters
252+ when (result.tag) {
253+ FILTER_TAG -> filters = result.filters
254+ TODAY_FILTER_TAG -> todayFilters = result.filters
255+ }
245256 // Update is on dismiss.
246257 }
247258
248259 fun onFilterDismissed (tag : String ) {
249- if (tag != FILTER_TAG ) return
250- checkTagStateConsistency()
251- updateSelectedRecordsCountViewData()
260+ when (tag) {
261+ FILTER_TAG -> {
262+ checkTagStateConsistency()
263+ updateSelectedRecordsCountViewData()
264+ }
265+ TODAY_FILTER_TAG -> {
266+ updateDeleteTodayRecordsButtonText()
267+ }
268+ }
269+ }
270+
271+ private fun onSelectRecordsClick (
272+ tag : String ,
273+ filters : List <RecordsFilter >,
274+ ) {
275+ RecordsFilterParams (
276+ tag = tag,
277+ title = resourceRepo.getString(R .string.chart_filter_hint),
278+ flags = RecordsFilterParams .Flags (
279+ dateSelectionAvailable = true ,
280+ untrackedSelectionAvailable = false ,
281+ multitaskSelectionAvailable = false ,
282+ duplicationsSelectionAvailable = true ,
283+ addRunningRecords = false ,
284+ ),
285+ filters = filters.map(RecordsFilter ::toParams),
286+ defaultLastDaysNumber = 7 ,
287+ ).let (router::navigate)
252288 }
253289
254290 private fun showAlert (tag : String ) {
@@ -275,6 +311,13 @@ class DataEditViewModel @Inject constructor(
275311 }
276312 }
277313
314+ private fun onDeleteTodayRecordsConfirmed () {
315+ doDataEditWork {
316+ val ids = getTodayRecords().map { it.id }
317+ dataEditChangeInteractor.deleteTodayRecords(ids)
318+ }
319+ }
320+
278321 private fun onDeleteRecordsConfirmed () {
279322 doDataEditWork {
280323 dataEditChangeInteractor.deleteAllRecords()
@@ -361,6 +404,46 @@ class DataEditViewModel @Inject constructor(
361404 ?.firstOrNull()
362405 }
363406
407+ private suspend fun getTodayRecords (): List <Record > {
408+ return recordFilterInteractor
409+ .getByFilter(todayFilters)
410+ .filterIsInstance<Record >()
411+ }
412+
413+ private suspend fun initializeTodayFilters () {
414+ val filter = RecordsFilter .Date (range = RangeLength .Day , position = 0 )
415+ val todayRange = recordFilterInteractor.getRange(filter)
416+ val todayFilter = RecordsFilter .Date (
417+ range = RangeLength .Custom (todayRange),
418+ position = 0 ,
419+ )
420+ todayFilters = listOf (todayFilter)
421+ todayFiltersInitial = todayFilters
422+ }
423+
424+ private fun updateDeleteTodayRecordsButtonText () = viewModelScope.launch {
425+ deleteTodayRecordsButtonText.set(loadDeleteTodayRecordsButtonText())
426+ }
427+
428+ private suspend fun loadDeleteTodayRecordsButtonText (): String {
429+ val recordsCounts = getTodayRecords().size
430+ val recordsHint = resourceRepo.getQuantityString(
431+ R .plurals.statistics_detail_times_tracked,
432+ recordsCounts,
433+ )
434+ val todayHint = if (todayFilters == todayFiltersInitial) {
435+ resourceRepo.getString(R .string.title_today)
436+ } else {
437+ null
438+ }
439+
440+ return listOfNotNull(
441+ resourceRepo.getString(R .string.archive_dialog_delete),
442+ todayHint,
443+ " $recordsCounts $recordsHint " ,
444+ ).joinToString(separator = " - " )
445+ }
446+
364447 private fun updateSelectedRecordsCountViewData () = viewModelScope.launch {
365448 val data = loadSelectedRecordsCountViewData()
366449 selectedRecordsCountViewData.set(data)
@@ -402,9 +485,11 @@ class DataEditViewModel @Inject constructor(
402485
403486 companion object {
404487 private const val FILTER_TAG = " DATA_EDIT_FILTER_TAG"
488+ private const val TODAY_FILTER_TAG = " DATA_EDIT_TODAY_FILTER_TAG"
405489 private const val ADD_TAGS_TAG = " DATA_EDIT_ADD_TAGS_TAG"
406490 private const val REMOVE_TAGS_TAG = " DATA_EDIT_REMOVE_TAGS_TAG"
407491 private const val CHANGE_ALERT_DIALOG_TAG = " DATA_EDIT_CHANGE_ALERT_DIALOG_TAG"
492+ private const val DELETE_TODAY_RECORDS_ALERT_DIALOG_TAG = " DELETE_TODAY_RECORDS_ALERT_DIALOG_TAG"
408493 private const val DELETE_RECORDS_ALERT_DIALOG_TAG = " DATA_EDIT_DELETE_RECORDS_ALERT_DIALOG_TAG"
409494 private const val DELETE_DATA_ALERT_DIALOG_TAG = " DATA_EDIT_DELETE_DATA_ALERT_DIALOG_TAG"
410495 }
0 commit comments