Skip to content

Commit ec9d01f

Browse files
committed
switch array to list in wear communication
1 parent c9a4f80 commit ec9d01f

File tree

13 files changed

+110
-122
lines changed

13 files changed

+110
-122
lines changed

features/feature_wear/src/main/java/com/example/util/simpletimetracker/feature_wear/WearCommunicationInteractor.kt

+33-36
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
package com.example.util.simpletimetracker.feature_wear
77

8+
import com.example.util.simpletimetracker.domain.extension.orZero
89
import com.example.util.simpletimetracker.domain.interactor.PrefsInteractor
910
import com.example.util.simpletimetracker.domain.interactor.RecordTagInteractor
1011
import com.example.util.simpletimetracker.domain.interactor.RecordTypeInteractor
@@ -30,32 +31,32 @@ class WearCommunicationInteractor @Inject constructor(
3031
private val appColorMapper: AppColorMapper,
3132
) : WearCommunicationAPI {
3233

33-
override suspend fun queryActivities(): Array<Activity> {
34+
override suspend fun queryActivities(): List<Activity> {
3435
return recordTypeInteractor.getAll()
3536
.filter { recordType -> !recordType.hidden }
3637
.map { recordType ->
3738
Activity(
3839
id = recordType.id,
3940
name = recordType.name,
4041
icon = recordType.icon,
41-
color = asColor(recordType.color),
42+
color = mapColor(recordType.color),
4243
)
43-
}.toTypedArray()
44+
}
4445
}
4546

46-
override suspend fun queryCurrentActivities(): Array<CurrentActivity> {
47+
override suspend fun queryCurrentActivities(): List<CurrentActivity> {
4748
return runningRecordInteractor.getAll().map { record ->
4849
CurrentActivity(
49-
record.id,
50-
record.timeStarted,
51-
record.tagIds.map { tagId ->
52-
asTag(recordTagInteractor.get(tagId))
53-
}.filter { it.id > 0 }.toTypedArray(),
50+
id = record.id,
51+
startedAt = record.timeStarted,
52+
tags = record.tagIds.mapNotNull { tagId ->
53+
recordTagInteractor.get(tagId)?.let(::mapTag)
54+
},
5455
)
55-
}.toTypedArray()
56+
}
5657
}
5758

58-
override suspend fun setCurrentActivities(activities: Array<CurrentActivity>) {
59+
override suspend fun setCurrentActivities(activities: List<CurrentActivity>) {
5960
val currents = queryCurrentActivities()
6061
val unchanged = currents.filter { c -> activities.any { a -> a == c } }
6162
val stopped = currents.filter { c -> unchanged.none { u -> u == c } }
@@ -73,41 +74,37 @@ class WearCommunicationInteractor @Inject constructor(
7374
)
7475
}
7576

76-
override suspend fun queryTagsForActivity(activityId: Long): Array<Tag> {
77-
val activityColor = recordTypeInteractor.get(activityId)?.color
77+
override suspend fun queryTagsForActivity(activityId: Long): List<Tag> {
78+
val activity = recordTypeInteractor.get(activityId) ?: return emptyList()
79+
val activityColor = mapColor(activity.color)
7880
return recordTagInteractor.getByTypeOrUntyped(activityId)
7981
.filter { !it.archived }
80-
.map { asTag(it, asColor(activityColor)) }
82+
.map { mapTag(it, activityColor) }
8183
.sortedBy { it.name }
8284
.sortedBy { it.isGeneral }
83-
.toTypedArray()
8485
}
8586

86-
private fun asTag(recordTag: RecordTag?, activityColor: Long = 0x00000000): Tag {
87-
return if (recordTag != null) {
88-
val isGeneral = recordTag.typeId == 0L
89-
val tagColor = if (isGeneral) {
90-
asColor(recordTag.color)
91-
} else {
92-
activityColor
93-
}
94-
Tag(
95-
id = recordTag.id,
96-
name = recordTag.name,
97-
isGeneral = isGeneral,
98-
color = tagColor,
99-
)
87+
private fun mapTag(
88+
recordTag: RecordTag,
89+
activityColor: Long? = null,
90+
): Tag {
91+
val isGeneral = recordTag.typeId == 0L
92+
val tagColor = if (isGeneral) {
93+
mapColor(recordTag.color)
10094
} else {
101-
Tag(id = -1, name = "", isGeneral = true, color = 0xFF555555)
95+
activityColor
10296
}
97+
98+
return Tag(
99+
id = recordTag.id,
100+
name = recordTag.name,
101+
isGeneral = isGeneral,
102+
color = tagColor.orZero(),
103+
)
103104
}
104105

105-
private fun asColor(appColor: AppColor?): Long {
106-
return if (appColor == null) {
107-
0x00000000
108-
} else {
109-
appColorMapper.mapToColorInt(appColor).toLong()
110-
}
106+
private fun mapColor(appColor: AppColor): Long {
107+
return appColorMapper.mapToColorInt(appColor).toLong()
111108
}
112109

113110
override suspend fun querySettings(): Settings {

features/feature_wear/src/main/java/com/example/util/simpletimetracker/feature_wear/WearRPCServer.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class WearRPCServer @Inject constructor(
4444
}
4545

4646
private suspend fun onSetCurrentActivities(request: ByteArray): ByteArray? {
47-
val activities: Array<CurrentActivity> = mapRequest(request) ?: return null
47+
val activities: List<CurrentActivity> = mapRequest(request) ?: return null
4848
api.setCurrentActivities(activities)
4949
return ByteArray(0)
5050
}

wear/src/main/java/com/example/util/simpletimetracker/presentation/components/ActivitiesList.kt

+8-8
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import com.example.util.simpletimetracker.wearrpc.CurrentActivity
2020

2121
@Composable
2222
fun ActivitiesList(
23-
activities: Array<Activity>,
24-
currentActivities: Array<CurrentActivity>,
23+
activities: List<Activity>,
24+
currentActivities: List<CurrentActivity>,
2525
onSelectActivity: (activity: Activity) -> Unit,
2626
onEnableActivity: (activity: Activity) -> Unit,
2727
onDisableActivity: (activity: Activity) -> Unit,
@@ -43,7 +43,7 @@ fun ActivitiesList(
4343
ActivityChip(
4444
activity,
4545
startedAt = currentActivity?.startedAt,
46-
tags = currentActivity?.tags ?: arrayOf(),
46+
tags = currentActivity?.tags.orEmpty(),
4747
onClick = { onSelectActivity(activity) },
4848
onToggleOn = { onEnableActivity(activity) },
4949
onToggleOff = { onDisableActivity(activity) },
@@ -61,8 +61,8 @@ fun ActivitiesList(
6161
@Composable
6262
private fun NoActivities() {
6363
ActivitiesList(
64-
activities = arrayOf(),
65-
currentActivities = arrayOf(),
64+
activities = emptyList(),
65+
currentActivities = emptyList(),
6666
onSelectActivity = { /* `it` is the selected activity */ },
6767
onEnableActivity = { /* `it` is the enabled activity */ },
6868
onDisableActivity = { /* `it` is the disabled activity */ },
@@ -73,12 +73,12 @@ private fun NoActivities() {
7373
@Preview(device = WearDevices.LARGE_ROUND)
7474
@Composable
7575
private fun Preview() {
76-
val activities = arrayOf(
76+
val activities = listOf(
7777
Activity(1234, "Chores", "🧹", 0xFFFA0000),
7878
Activity(4321, "Sleep", "🛏️", 0xFF0000FA),
7979
)
80-
val currents = arrayOf(
81-
CurrentActivity(id = 4321, startedAt = 1708241427000L, tags = arrayOf()),
80+
val currents = listOf(
81+
CurrentActivity(id = 4321, startedAt = 1708241427000L, tags = emptyList()),
8282
)
8383
ActivitiesList(
8484
activities = activities,

wear/src/main/java/com/example/util/simpletimetracker/presentation/components/ActivityChip.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ private const val ISO_MISSING_MINUTES_REGEX = "(\\d+H) (\\d+S)"
3939
fun ActivityChip(
4040
activity: Activity,
4141
startedAt: Long? = null,
42-
tags: Array<Tag> = arrayOf(),
42+
tags: List<Tag> = emptyList(),
4343
onClick: () -> Unit = {},
4444
onToggleOn: () -> Unit = {},
4545
onToggleOff: () -> Unit = {},
@@ -189,7 +189,7 @@ fun CurrentlyRunningWithTags() {
189189
ActivityChip(
190190
Activity(456, "Sleeping", "🛏️", 0xFFABCDEF),
191191
startedAt = Instant.now().toEpochMilli() - 360000,
192-
tags = arrayOf(
192+
tags = listOf(
193193
Tag(id = 2, name = "Work", isGeneral = true, color = 0xFFFFAA22),
194194
Tag(id = 4, name = "Hotel", isGeneral = false, color = 0xFFABCDEF),
195195
),

wear/src/main/java/com/example/util/simpletimetracker/presentation/components/TagList.kt

+8-8
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ import com.example.util.simpletimetracker.wearrpc.Tag
2323

2424
@Composable
2525
fun TagList(
26-
tags: Array<Tag>,
26+
tags: List<Tag>,
2727
mode: TagSelectionMode = TagSelectionMode.SINGLE,
28-
onSelectionComplete: (tags: Array<Tag>) -> Unit = {},
28+
onSelectionComplete: (tags: List<Tag>) -> Unit = {},
2929
) {
3030
var selectedTags: List<Tag> by remember { mutableStateOf(listOf()) }
3131
ScaffoldedScrollingColumn {
@@ -42,30 +42,30 @@ fun TagList(
4242
TagChip(
4343
tag = tag, mode = mode,
4444
onClick = {
45-
onSelectionComplete(selectedTags.minus(tag).plus(tag).toTypedArray())
46-
// ^No duplicate tags^
45+
// No duplicate tags
46+
onSelectionComplete(selectedTags.minus(tag).plus(tag))
4747
},
4848
onToggleOn = { selectedTags = selectedTags.plus(tag) },
4949
onToggleOff = { selectedTags = selectedTags.minus(tag) },
5050
)
5151
}
5252
}
5353
}
54-
item { SubmitButton(onClick = { onSelectionComplete(selectedTags.toTypedArray()) }) }
54+
item { SubmitButton(onClick = { onSelectionComplete(selectedTags) }) }
5555
}
5656
}
5757

5858
@Preview(device = WearDevices.LARGE_ROUND)
5959
@Composable
6060
private fun NoTags() {
61-
TagList(tags = arrayOf(), onSelectionComplete = {})
61+
TagList(tags = emptyList(), onSelectionComplete = {})
6262
}
6363

6464
@Preview(device = WearDevices.LARGE_ROUND)
6565
@Composable
6666
private fun WithSomeTags() {
6767
TagList(
68-
tags = arrayOf(
68+
tags = listOf(
6969
Tag(id = 123, name = "Sleep", isGeneral = false, color = 0xFF123456),
7070
Tag(id = 124, name = "Personal", isGeneral = true, color = 0xFF123456),
7171
),
@@ -76,7 +76,7 @@ private fun WithSomeTags() {
7676
@Composable
7777
private fun MultiSelectMode() {
7878
TagList(
79-
tags = arrayOf(
79+
tags = listOf(
8080
Tag(id = 123, name = "Sleep", isGeneral = false, color = 0xFF123456),
8181
Tag(id = 124, name = "Personal", isGeneral = true, color = 0xFF123456),
8282
),

wear/src/main/java/com/example/util/simpletimetracker/presentation/mediators/CurrentActivitiesMediator.kt

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import com.example.util.simpletimetracker.wearrpc.WearRPCClient
77

88
class CurrentActivitiesMediator(
99
private val rpc: WearRPCClient,
10-
private val currents: Array<CurrentActivity>,
10+
private val currents: List<CurrentActivity>,
1111
) {
1212
suspend fun start(activityId: Long) {
13-
start(activityId, arrayOf())
13+
start(activityId, emptyList())
1414
}
15-
suspend fun start(activityId: Long, tags: Array<Tag>) {
15+
suspend fun start(activityId: Long, tags: List<Tag>) {
1616
val newCurrent = CurrentActivity(
1717
id = activityId,
1818
startedAt = System.currentTimeMillis(),
@@ -21,12 +21,12 @@ class CurrentActivitiesMediator(
2121
if (settings().allowMultitasking) {
2222
this.rpc.setCurrentActivities(currents.plus(newCurrent))
2323
} else {
24-
this.rpc.setCurrentActivities(arrayOf(newCurrent))
24+
this.rpc.setCurrentActivities(listOf(newCurrent))
2525
}
2626
}
2727

2828
suspend fun stop(currentId: Long) {
29-
val remaining = currents.filter { it.id != currentId }.toTypedArray()
29+
val remaining = currents.filter { it.id != currentId }
3030
this.rpc.setCurrentActivities(remaining)
3131
}
3232
private suspend fun settings(): Settings {

wear/src/main/java/com/example/util/simpletimetracker/presentation/remember/activities.kt

+3-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import androidx.compose.runtime.mutableStateOf
1313
import androidx.compose.runtime.remember
1414
import androidx.compose.runtime.setValue
1515
import com.example.util.simpletimetracker.wearrpc.Activity
16-
import com.example.util.simpletimetracker.wearrpc.WearRPCClient
1716
import kotlinx.coroutines.Dispatchers
1817
import kotlinx.coroutines.async
1918

@@ -34,9 +33,9 @@ import kotlinx.coroutines.async
3433
* from the phone.
3534
*/
3635
@Composable
37-
fun rememberActivities(): Pair<Array<Activity>, () -> Unit> {
38-
var rpc = rememberRPCClient()
39-
var activities: Array<Activity> by remember { mutableStateOf(arrayOf()) }
36+
fun rememberActivities(): Pair<List<Activity>, () -> Unit> {
37+
val rpc = rememberRPCClient()
38+
var activities: List<Activity> by remember { mutableStateOf(emptyList()) }
4039
var activitiesQueryCount by remember { mutableIntStateOf(0) }
4140
LaunchedEffect(activitiesQueryCount) {
4241
async(Dispatchers.Default) {

wear/src/main/java/com/example/util/simpletimetracker/presentation/remember/currentActivities.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ import kotlinx.coroutines.async
3535
* from the phone.
3636
*/
3737
@Composable
38-
fun rememberCurrentActivities(): Pair<Array<CurrentActivity>, () -> Unit> {
39-
var rpc = rememberRPCClient()
40-
var currentActivities: Array<CurrentActivity> by remember { mutableStateOf(arrayOf()) }
38+
fun rememberCurrentActivities(): Pair<List<CurrentActivity>, () -> Unit> {
39+
val rpc = rememberRPCClient()
40+
var currentActivities: List<CurrentActivity> by remember { mutableStateOf(emptyList()) }
4141
var currentActivitiesQueryCount by remember { mutableIntStateOf(0) }
4242
val queryCurrentActivities = { currentActivitiesQueryCount++ }
4343
LaunchedEffect(currentActivitiesQueryCount) {
4444
async(Dispatchers.Default) {
4545
currentActivities = rpc.queryCurrentActivities()
4646
}
4747
}
48-
return Pair(currentActivities, { queryCurrentActivities() })
48+
return currentActivities to { queryCurrentActivities() }
4949
}

wear/src/main/java/com/example/util/simpletimetracker/presentation/remember/tags.kt

+3-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import androidx.compose.runtime.mutableStateOf
1313
import androidx.compose.runtime.remember
1414
import androidx.compose.runtime.setValue
1515
import com.example.util.simpletimetracker.wearrpc.Tag
16-
import com.example.util.simpletimetracker.wearrpc.WearRPCClient
1716
import kotlinx.coroutines.Dispatchers
1817
import kotlinx.coroutines.async
1918

@@ -35,9 +34,9 @@ import kotlinx.coroutines.async
3534
* from the phone.
3635
*/
3736
@Composable
38-
fun rememberTags(activityId: Long): Pair<Array<Tag>, () -> Unit> {
39-
var rpc = rememberRPCClient()
40-
var tags: Array<Tag> by remember { mutableStateOf(arrayOf()) }
37+
fun rememberTags(activityId: Long): Pair<List<Tag>, () -> Unit> {
38+
val rpc = rememberRPCClient()
39+
var tags: List<Tag> by remember { mutableStateOf(emptyList()) }
4140
var tagsQueryCount by remember { mutableIntStateOf(0) }
4241
LaunchedEffect(tagsQueryCount) {
4342
async(Dispatchers.Default) {

wearrpc/src/main/java/com/example/util/simpletimetracker/wearrpc/DTO.kt

+19-25
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,25 @@ package com.example.util.simpletimetracker.wearrpc
1111
* Object definitions for records sent between Wear/Mobile
1212
*/
1313

14-
data class Activity(val id: Long, val name: String, val icon: String, val color: Long)
15-
16-
data class CurrentActivity(val id: Long, val startedAt: Long, val tags: Array<Tag>) {
17-
override fun equals(other: Any?): Boolean {
18-
// autogenerated
19-
if (this === other) return true
20-
if (javaClass != other?.javaClass) return false
21-
22-
other as CurrentActivity
23-
24-
if (id != other.id) return false
25-
if (startedAt != other.startedAt) return false
26-
return tags.contentEquals(other.tags)
27-
}
28-
29-
override fun hashCode(): Int {
30-
// autogenerated
31-
var result = id.toInt()
32-
result = 31 * result + startedAt.hashCode()
33-
result = 31 * result + tags.contentHashCode()
34-
return result
35-
}
36-
}
37-
38-
data class Tag(val id: Long, val name: String, val isGeneral: Boolean, val color: Long)
14+
data class Activity(
15+
val id: Long,
16+
val name: String,
17+
val icon: String,
18+
val color: Long,
19+
)
20+
21+
data class CurrentActivity(
22+
val id: Long,
23+
val startedAt: Long,
24+
val tags: List<Tag>,
25+
)
26+
27+
data class Tag(
28+
val id: Long,
29+
val name: String,
30+
val isGeneral: Boolean,
31+
val color: Long,
32+
)
3933

4034
data class Settings(
4135
val allowMultitasking: Boolean,

0 commit comments

Comments
 (0)