Skip to content

Commit 7f47c3a

Browse files
committed
show preselected tags in tag selection dialog
1 parent b16df2e commit 7f47c3a

File tree

3 files changed

+53
-13
lines changed

3 files changed

+53
-13
lines changed

domain/src/main/java/com/example/util/simpletimetracker/domain/record/interactor/AddRunningRecordMediator.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ class AddRunningRecordMediator @Inject constructor(
280280
)
281281
}
282282

283-
private suspend fun processRules(
283+
suspend fun processRules(
284284
typeId: Long,
285285
timeStarted: Long,
286286
prevRecords: SuspendLazy<List<Record>>,

features/feature_tag_selection/src/main/java/com/example/util/simpletimetracker/feature_tag_selection/interactor/RecordTagSelectionViewDataInteractor.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import javax.inject.Inject
1515

1616
class RecordTagSelectionViewDataInteractor @Inject constructor(
1717
private val resourceRepo: ResourceRepo,
18-
private val shouldCloseAfterOneTagInteractor: ShouldCloseAfterOneTagInteractor,
1918
private val recordTagViewDataInteractor: RecordTagViewDataInteractor,
2019
private val recordCommentSearchViewDataInteractor: RecordCommentSearchViewDataInteractor,
2120
) {
@@ -24,11 +23,11 @@ class RecordTagSelectionViewDataInteractor @Inject constructor(
2423
extra: RecordTagSelectionParams,
2524
selectedTags: List<RecordBase.Tag>,
2625
showAllTags: Boolean,
26+
multipleChoiceAvailable: Boolean,
2727
comment: String,
2828
fromCommentChange: Boolean,
2929
): List<ViewHolderType> {
3030
val typeId = extra.typeId
31-
val closeAfterOneTagSelected = shouldCloseAfterOneTagInteractor.execute(typeId)
3231
val shouldShowCommentSelection = RecordTagSelectionParams.Field.Comment in extra.fields
3332
val shouldShowTagSelection = RecordTagSelectionParams.Field.Tags in extra.fields
3433

@@ -77,7 +76,7 @@ class RecordTagSelectionViewDataInteractor @Inject constructor(
7776
selectedTags = selectedTags,
7877
typeIds = listOf(typeId),
7978
showAllTags = showAllTags,
80-
multipleChoiceAvailable = !closeAfterOneTagSelected,
79+
multipleChoiceAvailable = multipleChoiceAvailable,
8180
showAddButton = false,
8281
showArchived = false,
8382
showUntaggedButton = true,

features/feature_tag_selection/src/main/java/com/example/util/simpletimetracker/feature_tag_selection/viewModel/RecordTagSelectionViewModel.kt

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ import com.example.util.simpletimetracker.core.extension.set
99
import com.example.util.simpletimetracker.core.interactor.RecordCommentSearchViewDataInteractor
1010
import com.example.util.simpletimetracker.core.interactor.ShouldCloseAfterOneTagInteractor
1111
import com.example.util.simpletimetracker.core.viewData.CommentFilterTypeViewData
12+
import com.example.util.simpletimetracker.domain.base.CurrentTimestampProvider
13+
import com.example.util.simpletimetracker.domain.base.suspendLazy
1214
import com.example.util.simpletimetracker.domain.extension.addOrRemove
1315
import com.example.util.simpletimetracker.domain.prefs.interactor.PrefsInteractor
1416
import com.example.util.simpletimetracker.domain.record.interactor.AddRunningRecordMediator
17+
import com.example.util.simpletimetracker.domain.record.interactor.RecordInteractor
1518
import com.example.util.simpletimetracker.domain.record.model.RecordBase
1619
import com.example.util.simpletimetracker.domain.recordTag.interactor.AddTagToTypeIfNotExistMediator
1720
import com.example.util.simpletimetracker.domain.recordTag.interactor.NeedTagValueSelectionInteractor
21+
import com.example.util.simpletimetracker.domain.recordTag.interactor.RecordTypeToDefaultTagInteractor
1822
import com.example.util.simpletimetracker.feature_base_adapter.ViewHolderType
1923
import com.example.util.simpletimetracker.feature_base_adapter.category.CategoryViewData
2024
import com.example.util.simpletimetracker.feature_base_adapter.loader.LoaderViewData
@@ -26,6 +30,7 @@ import com.example.util.simpletimetracker.navigation.params.screen.RecordTagSele
2630
import com.example.util.simpletimetracker.navigation.params.screen.RecordTagValueSelectionParams
2731
import dagger.hilt.android.lifecycle.HiltViewModel
2832
import kotlinx.coroutines.Job
33+
import kotlinx.coroutines.coroutineScope
2934
import kotlinx.coroutines.launch
3035
import javax.inject.Inject
3136

@@ -37,6 +42,9 @@ class RecordTagSelectionViewModel @Inject constructor(
3742
private val prefsInteractor: PrefsInteractor,
3843
private val addTagToTypeIfNotExistMediator: AddTagToTypeIfNotExistMediator,
3944
private val needTagValueSelectionInteractor: NeedTagValueSelectionInteractor,
45+
private val recordTypeToDefaultTagInteractor: RecordTypeToDefaultTagInteractor,
46+
private val recordInteractor: RecordInteractor,
47+
private val currentTimestampProvider: CurrentTimestampProvider,
4048
private val recordCommentSearchViewDataInteractor: RecordCommentSearchViewDataInteractor,
4149
private val shouldCloseAfterOneTagInteractor: ShouldCloseAfterOneTagInteractor,
4250
) : BaseViewModel() {
@@ -53,12 +61,14 @@ class RecordTagSelectionViewModel @Inject constructor(
5361
initial
5462
}
5563
}
56-
val saveButtonVisibility: LiveData<Boolean> by lazySuspend { loadButtonVisibility() }
64+
val saveButtonVisibility: LiveData<Boolean> = MutableLiveData()
5765
val saveClicked: LiveData<Unit> = MutableLiveData()
5866

5967
private var newComment: String = ""
6068
private var newTags: List<RecordBase.Tag> = emptyList()
69+
private var initialSelectedTagsLoaded: Boolean = false
6170
private var searchLoadJob: Job? = null
71+
private var isMultipleChoiceAvailable: Boolean = true
6272

6373
// Keep in mind that tags would be added to new types only if show all was selected before,
6474
// for optimisation reasons, to not call on every save.
@@ -137,11 +147,7 @@ class RecordTagSelectionViewModel @Inject constructor(
137147
}
138148

139149
private suspend fun onTagSelected() {
140-
if (shouldCloseAfterOneTagInteractor.execute(extra.typeId)) {
141-
saveClicked()
142-
} else {
143-
updateViewData()
144-
}
150+
if (isMultipleChoiceAvailable) updateViewData() else saveClicked()
145151
}
146152

147153
private suspend fun saveClicked() {
@@ -159,13 +165,45 @@ class RecordTagSelectionViewModel @Inject constructor(
159165
saveClicked.set(Unit)
160166
}
161167

162-
private suspend fun loadButtonVisibility(): Boolean {
163-
val closeAfterOneTag = shouldCloseAfterOneTagInteractor.execute(extra.typeId)
168+
private suspend fun loadPreselectedTagIds(): Set<Long> = coroutineScope {
169+
// TODO TAG add "close after one tag" exclusion check to wear
170+
// TODO TAG check retroactive mode?
171+
// TODO TAG ability to deselect preselected tags
172+
// TODO TAG multiple choice from notification
173+
// TODO TAG show preselected on wear and notification
174+
val defaultTags = recordTypeToDefaultTagInteractor.getTags(extra.typeId)
175+
val timeStarted = currentTimestampProvider.get()
176+
val ruleTags = addRunningRecordMediator.processRules(
177+
typeId = extra.typeId,
178+
timeStarted = timeStarted,
179+
prevRecords = suspendLazy { emptyList() },
180+
).tagsIds
181+
defaultTags + ruleTags
182+
}
183+
184+
private suspend fun initializePreselectedTags() {
185+
if (initialSelectedTagsLoaded) return
186+
val initialIds = loadPreselectedTagIds()
187+
if (initialIds.isNotEmpty()) {
188+
newTags = initialIds.map { RecordBase.Tag(tagId = it, numericValue = null) }
189+
}
190+
// If there are preselected tags - ignore setting.
191+
isMultipleChoiceAvailable = newTags.isNotEmpty() ||
192+
!shouldCloseAfterOneTagInteractor.execute(extra.typeId)
193+
updateButtonVisibility()
194+
initialSelectedTagsLoaded = true
195+
}
196+
197+
private fun updateButtonVisibility() {
198+
saveButtonVisibility.set(loadButtonVisibility())
199+
}
200+
201+
private fun loadButtonVisibility(): Boolean {
164202
val showTags = RecordTagSelectionParams.Field.Tags in extra.fields
165203
val showCommentInput = RecordTagSelectionParams.Field.Comment in extra.fields
166204

167205
return when {
168-
showTags -> !closeAfterOneTag
206+
showTags -> isMultipleChoiceAvailable
169207
showCommentInput -> true
170208
else -> false
171209
}
@@ -184,10 +222,13 @@ class RecordTagSelectionViewModel @Inject constructor(
184222
private suspend fun loadViewData(
185223
fromCommentChange: Boolean,
186224
): List<ViewHolderType> {
225+
initializePreselectedTags()
226+
187227
return viewDataInteractor.getViewData(
188228
extra = extra,
189229
selectedTags = newTags,
190230
showAllTags = showAllTags,
231+
multipleChoiceAvailable = isMultipleChoiceAvailable,
191232
comment = newComment,
192233
fromCommentChange = fromCommentChange,
193234
)

0 commit comments

Comments
 (0)