-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from matejdro/app_reorder
App reorder
- Loading branch information
Showing
3 changed files
with
120 additions
and
5 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
src/commonMain/kotlin/io/rebble/libpebblecommon/packets/AppReorder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package io.rebble.libpebblecommon.packets | ||
|
||
import com.benasher44.uuid.Uuid | ||
import io.rebble.libpebblecommon.protocolhelpers.PacketRegistry | ||
import io.rebble.libpebblecommon.protocolhelpers.PebblePacket | ||
import io.rebble.libpebblecommon.protocolhelpers.ProtocolEndpoint | ||
import io.rebble.libpebblecommon.structmapper.* | ||
|
||
class AppReorderResult() : | ||
PebblePacket(ProtocolEndpoint.APP_REORDER) { | ||
/** | ||
* Result code. See [AppOrderResultCode]. | ||
*/ | ||
val status = SUByte(m) | ||
|
||
} | ||
|
||
sealed class AppReorderOutgoingPacket(type: AppReorderType) : | ||
PebblePacket(ProtocolEndpoint.APP_REORDER) { | ||
|
||
/** | ||
* Packet type. See [AppReorderType]. | ||
*/ | ||
val command = SUByte(m, type.value) | ||
} | ||
|
||
enum class AppOrderResultCode(val value: UByte) { | ||
SUCCESS(0x01u), | ||
FAILED(0x02u), | ||
INVALID(0x03u), | ||
RETRY(0x04u); | ||
|
||
companion object { | ||
fun fromByte(value: UByte): AppOrderResultCode { | ||
return values().firstOrNull { it.value == value } ?: error("Unknown result: $value") | ||
} | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Packet sent from the watch when user opens an app that is not in the watch storage. | ||
*/ | ||
class AppReorderRequest( | ||
appList: List<Uuid> | ||
) : AppReorderOutgoingPacket(AppReorderType.REORDER_APPS) { | ||
val appCount = SByte(m, appList.size.toByte()) | ||
|
||
val appList = SFixedList<SUUID>( | ||
mapper = m, | ||
count = appList.size, | ||
default = appList.map { SUUID(StructMapper(), it) }, | ||
itemFactory = { | ||
SUUID( | ||
StructMapper() | ||
) | ||
}) | ||
} | ||
|
||
enum class AppReorderType(val value: UByte) { | ||
REORDER_APPS(0x01u) | ||
} | ||
|
||
|
||
fun appReorderIncomingRegister() { | ||
PacketRegistry.register( | ||
ProtocolEndpoint.APP_REORDER, | ||
) { AppReorderResult() } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/commonMain/kotlin/io/rebble/libpebblecommon/services/AppReorderService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.rebble.libpebblecommon.services | ||
|
||
import io.rebble.libpebblecommon.ProtocolHandler | ||
import io.rebble.libpebblecommon.packets.AppOrderResultCode | ||
import io.rebble.libpebblecommon.packets.AppReorderOutgoingPacket | ||
import io.rebble.libpebblecommon.packets.AppReorderResult | ||
import io.rebble.libpebblecommon.protocolhelpers.PebblePacket | ||
import io.rebble.libpebblecommon.protocolhelpers.ProtocolEndpoint | ||
import kotlinx.coroutines.channels.Channel | ||
|
||
class AppReorderService(private val protocolHandler: ProtocolHandler) : ProtocolService { | ||
val receivedMessages = Channel<AppReorderResult>(Channel.BUFFERED) | ||
private var lastOrderPacket: AppReorderOutgoingPacket? = null | ||
|
||
init { | ||
protocolHandler.registerReceiveCallback(ProtocolEndpoint.APP_REORDER, this::receive) | ||
} | ||
|
||
suspend fun send(packet: AppReorderOutgoingPacket) { | ||
protocolHandler.send(packet) | ||
} | ||
|
||
suspend fun receive(packet: PebblePacket) { | ||
if (packet !is AppReorderResult) { | ||
throw IllegalStateException("Received invalid packet type: $packet") | ||
} | ||
|
||
if (packet.status.get() == AppOrderResultCode.RETRY.value) { | ||
lastOrderPacket?.let { send(it) } | ||
return | ||
} | ||
|
||
lastOrderPacket = null | ||
receivedMessages.offer(packet) | ||
} | ||
} |