1
+ /*
2
+ * This Source Code Form is subject to the terms of the Mozilla Public
3
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
4
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5
+ */
6
+ package com.example.util.simpletimetracker.wear
7
+
8
+ import com.example.util.simpletimetracker.domain.interactor.PrefsInteractor
9
+ import com.example.util.simpletimetracker.domain.interactor.RecordTagInteractor
10
+ import com.example.util.simpletimetracker.domain.interactor.RecordTypeInteractor
11
+ import com.example.util.simpletimetracker.domain.interactor.RemoveRunningRecordMediator
12
+ import com.example.util.simpletimetracker.domain.interactor.RunningRecordInteractor
13
+ import com.example.util.simpletimetracker.domain.mapper.AppColorMapper
14
+ import com.example.util.simpletimetracker.domain.model.AppColor
15
+ import com.example.util.simpletimetracker.domain.model.RecordTag
16
+ import com.example.util.simpletimetracker.domain.model.RunningRecord
17
+ import com.example.util.simpletimetracker.wearrpc.Activity
18
+ import com.example.util.simpletimetracker.wearrpc.CurrentActivity
19
+ import com.example.util.simpletimetracker.wearrpc.Settings
20
+ import com.example.util.simpletimetracker.wearrpc.SimpleTimeTrackerAPI
21
+ import com.example.util.simpletimetracker.wearrpc.Tag
22
+
23
+ class DomainAPI (
24
+ private val prefsInteractor : PrefsInteractor ,
25
+ private val recordTypeInteractor : RecordTypeInteractor ,
26
+ private val recordTagInteractor : RecordTagInteractor ,
27
+ private val runningRecordInteractor : RunningRecordInteractor ,
28
+ private val removeRunningRecordMediator : RemoveRunningRecordMediator ,
29
+ private val appColorMapper : AppColorMapper ,
30
+ ) : SimpleTimeTrackerAPI {
31
+
32
+ override suspend fun queryActivities (): Array <Activity > {
33
+ return recordTypeInteractor.getAll().filter { recordType -> ! recordType.hidden }
34
+ .map { recordType ->
35
+ Activity (
36
+ id = recordType.id,
37
+ name = recordType.name,
38
+ icon = recordType.icon,
39
+ color = asColor(recordType.color),
40
+ )
41
+ }.toTypedArray()
42
+ }
43
+
44
+ override suspend fun queryCurrentActivities (): Array <CurrentActivity > {
45
+ return runningRecordInteractor.getAll().map { record ->
46
+ CurrentActivity (
47
+ record.id,
48
+ record.timeStarted,
49
+ record.tagIds.map { tagId ->
50
+ asTag(recordTagInteractor.get(tagId))
51
+ }.filter { it.id > 0 }.toTypedArray(),
52
+ )
53
+ }.toTypedArray()
54
+ }
55
+
56
+ override suspend fun setCurrentActivities (activities : Array <CurrentActivity >) {
57
+ val currents = queryCurrentActivities()
58
+ val unchanged = currents.filter { c -> activities.any { a -> a == c } }
59
+ val stopped = currents.filter { c -> unchanged.none { u -> u == c } }
60
+ val started = activities.filter { a -> currents.none { c -> a == c } }
61
+ stopped.forEach { removeRunningRecordMediator.removeWithRecordAdd(asRunningRecord(it)) }
62
+ started.forEach { runningRecordInteractor.add(asRunningRecord(it)) }
63
+ }
64
+
65
+ private fun asRunningRecord (currentActivity : CurrentActivity ): RunningRecord {
66
+ return RunningRecord (
67
+ id = currentActivity.id,
68
+ timeStarted = currentActivity.startedAt,
69
+ comment = " " ,
70
+ tagIds = currentActivity.tags.map { t -> t.id },
71
+ )
72
+ }
73
+
74
+ override suspend fun queryTagsForActivity (activityId : Long ): Array <Tag > {
75
+ val activityColor = recordTypeInteractor.get(activityId)?.color
76
+ return recordTagInteractor.getByTypeOrUntyped(activityId).filter { ! it.archived }
77
+ .map { asTag(it, asColor(activityColor)) }.sortedBy { it.name }
78
+ .sortedBy { it.isGeneral }.toTypedArray()
79
+ }
80
+
81
+ private fun asTag (recordTag : RecordTag ? , activityColor : Long = 0x00000000): Tag {
82
+ return if (recordTag != null ) {
83
+ val isGeneral = recordTag.typeId == 0L
84
+ val tagColor = if (isGeneral) {
85
+ asColor(recordTag.color)
86
+ } else {
87
+ activityColor
88
+ }
89
+ Tag (
90
+ id = recordTag.id,
91
+ name = recordTag.name,
92
+ isGeneral = isGeneral,
93
+ color = tagColor,
94
+ )
95
+ } else {
96
+ Tag (id = - 1 , name = " " , isGeneral = true , color = 0xFF555555 )
97
+ }
98
+ }
99
+
100
+ private fun asColor (appColor : AppColor ? ): Long {
101
+ return if (appColor == null ) {
102
+ 0x00000000
103
+ } else {
104
+ appColorMapper.mapToColorInt(appColor).toLong()
105
+ }
106
+ }
107
+
108
+ override suspend fun querySettings (): Settings {
109
+ return Settings (
110
+ allowMultitasking = prefsInteractor.getAllowMultitasking(),
111
+ showRecordTagSelection = prefsInteractor.getShowRecordTagSelection(),
112
+ recordTagSelectionCloseAfterOne = prefsInteractor.getRecordTagSelectionCloseAfterOne(),
113
+ recordTagSelectionEvenForGeneralTags = prefsInteractor.getRecordTagSelectionEvenForGeneralTags(),
114
+ )
115
+ }
116
+
117
+ }
0 commit comments