Skip to content

Commit 0e1102b

Browse files
authored
Merge pull request #12 from matejdro/timeline_actions
add TimelineService for timeline action callbacks
2 parents c042f24 + b1e37d6 commit 0e1102b

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package io.rebble.libpebblecommon.services.blobdb
2+
3+
import io.rebble.libpebblecommon.ProtocolHandler
4+
import io.rebble.libpebblecommon.packets.blobdb.TimelineAction
5+
import io.rebble.libpebblecommon.packets.blobdb.TimelineItem
6+
import io.rebble.libpebblecommon.protocolhelpers.PebblePacket
7+
import io.rebble.libpebblecommon.protocolhelpers.ProtocolEndpoint
8+
import io.rebble.libpebblecommon.services.ProtocolService
9+
10+
/**
11+
* Singleton that handles receiving of timeline actions.
12+
*
13+
* Consumer must set [actionHandler] that will then be called whenever user triggers timeline pin
14+
* action.
15+
*/
16+
class TimelineService(private val protocolHandler: ProtocolHandler) : ProtocolService {
17+
var actionHandler: (suspend (TimelineAction.InvokeAction) -> ActionResponse)? = null
18+
19+
init {
20+
protocolHandler.registerReceiveCallback(ProtocolEndpoint.TIMELINE_ACTIONS, this::receive)
21+
}
22+
23+
24+
suspend fun receive(packet: PebblePacket) {
25+
if (packet !is TimelineAction.InvokeAction) {
26+
throw IllegalStateException("Received invalid packet type: $packet")
27+
}
28+
29+
val result = actionHandler?.invoke(packet) ?: return
30+
31+
val returnPacket = TimelineAction.ActionResponse().apply {
32+
itemID.set(packet.itemID.get())
33+
response.set(if (result.success) 0u else 1u)
34+
numAttributes.set(result.attributes.size.toUByte())
35+
attributes.list = result.attributes
36+
}
37+
38+
protocolHandler.send(returnPacket)
39+
}
40+
41+
class ActionResponse(
42+
val success: Boolean,
43+
val attributes: List<TimelineItem.Attribute> = emptyList()
44+
)
45+
}

src/commonMain/kotlin/io/rebble/libpebblecommon/structmapper/types.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,6 @@ class SFixedList<T : Mappable>(
333333

334334
private val mapIndex = mapper.register(this)
335335
var list = default
336-
private set
337336

338337
init {
339338
if (count != default.size) throw PacketEncodeException("Fixed list count does not match default value count")

0 commit comments

Comments
 (0)