Skip to content

Commit

Permalink
begin handling timeline/notification actions under kmp
Browse files Browse the repository at this point in the history
  • Loading branch information
crc-32 committed Jul 24, 2024
1 parent 0d06a8b commit a1e3cc1
Show file tree
Hide file tree
Showing 17 changed files with 174 additions and 854 deletions.
213 changes: 1 addition & 212 deletions android/app/src/main/kotlin/io/rebble/cobble/pigeons/Pigeons.java
Original file line number Diff line number Diff line change
Expand Up @@ -1193,155 +1193,6 @@ ArrayList<Object> toList() {
}
}

/** Generated class from Pigeon that represents data sent in messages. */
public static final class ActionTrigger {
private @Nullable String itemId;

public @Nullable String getItemId() {
return itemId;
}

public void setItemId(@Nullable String setterArg) {
this.itemId = setterArg;
}

private @Nullable Long actionId;

public @Nullable Long getActionId() {
return actionId;
}

public void setActionId(@Nullable Long setterArg) {
this.actionId = setterArg;
}

private @Nullable String attributesJson;

public @Nullable String getAttributesJson() {
return attributesJson;
}

public void setAttributesJson(@Nullable String setterArg) {
this.attributesJson = setterArg;
}

public static final class Builder {

private @Nullable String itemId;

public @NonNull Builder setItemId(@Nullable String setterArg) {
this.itemId = setterArg;
return this;
}

private @Nullable Long actionId;

public @NonNull Builder setActionId(@Nullable Long setterArg) {
this.actionId = setterArg;
return this;
}

private @Nullable String attributesJson;

public @NonNull Builder setAttributesJson(@Nullable String setterArg) {
this.attributesJson = setterArg;
return this;
}

public @NonNull ActionTrigger build() {
ActionTrigger pigeonReturn = new ActionTrigger();
pigeonReturn.setItemId(itemId);
pigeonReturn.setActionId(actionId);
pigeonReturn.setAttributesJson(attributesJson);
return pigeonReturn;
}
}

@NonNull
ArrayList<Object> toList() {
ArrayList<Object> toListResult = new ArrayList<Object>(3);
toListResult.add(itemId);
toListResult.add(actionId);
toListResult.add(attributesJson);
return toListResult;
}

static @NonNull ActionTrigger fromList(@NonNull ArrayList<Object> list) {
ActionTrigger pigeonResult = new ActionTrigger();
Object itemId = list.get(0);
pigeonResult.setItemId((String) itemId);
Object actionId = list.get(1);
pigeonResult.setActionId((actionId == null) ? null : ((actionId instanceof Integer) ? (Integer) actionId : (Long) actionId));
Object attributesJson = list.get(2);
pigeonResult.setAttributesJson((String) attributesJson);
return pigeonResult;
}
}

/** Generated class from Pigeon that represents data sent in messages. */
public static final class ActionResponsePigeon {
private @Nullable Boolean success;

public @Nullable Boolean getSuccess() {
return success;
}

public void setSuccess(@Nullable Boolean setterArg) {
this.success = setterArg;
}

private @Nullable String attributesJson;

public @Nullable String getAttributesJson() {
return attributesJson;
}

public void setAttributesJson(@Nullable String setterArg) {
this.attributesJson = setterArg;
}

public static final class Builder {

private @Nullable Boolean success;

public @NonNull Builder setSuccess(@Nullable Boolean setterArg) {
this.success = setterArg;
return this;
}

private @Nullable String attributesJson;

public @NonNull Builder setAttributesJson(@Nullable String setterArg) {
this.attributesJson = setterArg;
return this;
}

public @NonNull ActionResponsePigeon build() {
ActionResponsePigeon pigeonReturn = new ActionResponsePigeon();
pigeonReturn.setSuccess(success);
pigeonReturn.setAttributesJson(attributesJson);
return pigeonReturn;
}
}

@NonNull
ArrayList<Object> toList() {
ArrayList<Object> toListResult = new ArrayList<Object>(2);
toListResult.add(success);
toListResult.add(attributesJson);
return toListResult;
}

static @NonNull ActionResponsePigeon fromList(@NonNull ArrayList<Object> list) {
ActionResponsePigeon pigeonResult = new ActionResponsePigeon();
Object success = list.get(0);
pigeonResult.setSuccess((Boolean) success);
Object attributesJson = list.get(1);
pigeonResult.setAttributesJson((String) attributesJson);
return pigeonResult;
}
}

/** Generated class from Pigeon that represents data sent in messages. */
public static final class NotifActionExecuteReq {
private @Nullable String itemId;
Expand Down Expand Up @@ -3066,7 +2917,7 @@ public CalendarCallbacks(@NonNull BinaryMessenger argBinaryMessenger) {
this.binaryMessenger = argBinaryMessenger;
}

/** Public interface for sending reply. */
/** Public interface for sending reply. */
@SuppressWarnings("UnknownNullness")
public interface Reply<T> {
void reply(T reply);
Expand Down Expand Up @@ -3324,68 +3175,6 @@ public void onWatchPairComplete(@NonNull StringWrapper addressArg, @NonNull Repl
}
}

private static class TimelineCallbacksCodec extends StandardMessageCodec {
public static final TimelineCallbacksCodec INSTANCE = new TimelineCallbacksCodec();

private TimelineCallbacksCodec() {}

@Override
protected Object readValueOfType(byte type, @NonNull ByteBuffer buffer) {
switch (type) {
case (byte) 128:
return ActionResponsePigeon.fromList((ArrayList<Object>) readValue(buffer));
case (byte) 129:
return ActionTrigger.fromList((ArrayList<Object>) readValue(buffer));
default:
return super.readValueOfType(type, buffer);
}
}

@Override
protected void writeValue(@NonNull ByteArrayOutputStream stream, Object value) {
if (value instanceof ActionResponsePigeon) {
stream.write(128);
writeValue(stream, ((ActionResponsePigeon) value).toList());
} else if (value instanceof ActionTrigger) {
stream.write(129);
writeValue(stream, ((ActionTrigger) value).toList());
} else {
super.writeValue(stream, value);
}
}
}

/** Generated class from Pigeon that represents Flutter messages that can be called from Java. */
public static class TimelineCallbacks {
private final @NonNull BinaryMessenger binaryMessenger;

public TimelineCallbacks(@NonNull BinaryMessenger argBinaryMessenger) {
this.binaryMessenger = argBinaryMessenger;
}

/** Public interface for sending reply. */
@SuppressWarnings("UnknownNullness")
public interface Reply<T> {
void reply(T reply);
}
/** The codec used by TimelineCallbacks. */
static @NonNull MessageCodec<Object> getCodec() {
return TimelineCallbacksCodec.INSTANCE;
}
public void handleTimelineAction(@NonNull ActionTrigger actionTriggerArg, @NonNull Reply<ActionResponsePigeon> callback) {
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
binaryMessenger, "dev.flutter.pigeon.TimelineCallbacks.handleTimelineAction", getCodec());
channel.send(
new ArrayList<Object>(Collections.singletonList(actionTriggerArg)),
channelReply -> {
@SuppressWarnings("ConstantConditions")
ActionResponsePigeon output = (ActionResponsePigeon) channelReply;
callback.reply(output);
});
}
}

private static class IntentCallbacksCodec extends StandardMessageCodec {
public static final IntentCallbacksCodec INSTANCE = new IntentCallbacksCodec();

Expand Down
2 changes: 1 addition & 1 deletion android/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ kotlin = "2.0.20-Beta1"
kotlinxDatetime = "0.6.0"
kotlinxSerializationJson = "1.7.1"
ksp = "2.0.20-Beta1-1.0.22"
libpebblecommonVersion = "0.1.20"
libpebblecommonVersion = "0.1.21"

room = "2.7.0-alpha04"
room-sqlite = "2.5.0-alpha04"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ private fun CalendarEvent.makeAttributes(calendar: Calendar): List<TimelineAttri
private fun CalendarEvent.makeActions(): List<TimelineAction> {
val selfAttendee = attendees.find { it.isCurrentUser }
return buildList {
//TODO: Implement action handling on KMP
/*if (selfAttendee != null) {
if (selfAttendee != null) {
if (selfAttendee.attendanceStatus != EventAttendee.AttendanceStatus.Accepted) {
add(TimelineAction(
CalendarTimelineActionId.AcceptEvent.id,
Expand Down Expand Up @@ -154,7 +153,7 @@ private fun CalendarEvent.makeActions(): List<TimelineAction> {
listOf(
TimelineAttribute.title("Mute Calendar")
)
))*/
))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,7 @@ interface TimelinePinDao {

@Query("DELETE FROM TimelinePin WHERE parentId = :parentId")
suspend fun deletePinsForWatchapp(parentId: Uuid)

@Query("SELECT * FROM TimelinePin WHERE itemId = :itemId")
suspend fun get(itemId: Uuid): TimelinePin?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.rebble.cobble.shared.domain.calendar

enum class CalendarAction {
Remove,
Mute,
Accept,
Maybe,
Decline;

companion object {
fun fromID(id: Int): CalendarAction = entries.first {it.ordinal == id}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package io.rebble.cobble.shared.domain.calendar

import com.benasher44.uuid.Uuid
import io.rebble.cobble.shared.PlatformContext
import io.rebble.cobble.shared.data.calendarWatchappId
import io.rebble.cobble.shared.database.NextSyncAction
import io.rebble.cobble.shared.database.dao.TimelinePinDao
import io.rebble.cobble.shared.domain.timeline.TimelineActionManager
import io.rebble.cobble.shared.domain.timeline.WatchTimelineSyncer
import io.rebble.libpebblecommon.packets.blobdb.TimelineItem
import io.rebble.libpebblecommon.services.blobdb.TimelineService
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject

class CalendarActionHandler(private val scope: CoroutineScope): KoinComponent {
private val platformContext: PlatformContext by inject()
private val timelineActionManager: TimelineActionManager by inject()
private val timelinePinDao: TimelinePinDao by inject()
private val timelineSyncer: WatchTimelineSyncer by inject()

private val calendarActionFlow = timelineActionManager.actionFlowForApp(calendarWatchappId)

init {
calendarActionFlow.onEach {
val (action, deferred) = it
val itemId = action.itemID.get()
val response = try {
when (CalendarAction.fromID(action.actionID.get().toInt())) {
CalendarAction.Remove -> handleRemove(itemId)
CalendarAction.Mute -> handleMute(itemId)
CalendarAction.Accept -> handleAccept(itemId)
CalendarAction.Maybe -> handleMaybe(itemId)
CalendarAction.Decline -> handleDecline(itemId)
}
} catch (e: NoSuchElementException) {
TimelineService.ActionResponse(
success = false
)
}
deferred.complete(response)
}.launchIn(scope)
}

private suspend fun handleRemove(itemId: Uuid): TimelineService.ActionResponse {
timelinePinDao.setSyncActionForPins(listOf(itemId), NextSyncAction.Delete)
timelineSyncer.syncPinDatabaseWithWatch()

return TimelineService.ActionResponse(
success = true,
attributes = listOf(
TimelineItem.Attribute(

)
)
)
}

private suspend fun handleMute(itemId: Uuid): TimelineService.ActionResponse {
// Handle mute action
}

private suspend fun handleAccept(itemId: Uuid): TimelineService.ActionResponse {
// Handle accept action
}

private suspend fun handleMaybe(itemId: Uuid): TimelineService.ActionResponse {
// Handle maybe action
}

private suspend fun handleDecline(itemId: Uuid): TimelineService.ActionResponse {
// Handle decline action
}
}
Loading

0 comments on commit a1e3cc1

Please sign in to comment.