-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix notification actions (open on phone, replies, etc.)
- Loading branch information
Showing
6 changed files
with
60 additions
and
56 deletions.
There are no files selected for viewing
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
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
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
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
65 changes: 33 additions & 32 deletions
65
...mmonMain/kotlin/io/rebble/cobble/shared/domain/notifications/NotificationActionHandler.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 |
---|---|---|
@@ -1,56 +1,57 @@ | ||
package io.rebble.cobble.shared.domain.notifications | ||
|
||
import io.rebble.cobble.shared.Logging | ||
import io.rebble.cobble.shared.domain.common.PebbleDevice | ||
import io.rebble.cobble.shared.domain.timeline.TimelineActionManager | ||
import io.rebble.cobble.shared.handlers.CobbleHandler | ||
import io.rebble.libpebblecommon.services.blobdb.TimelineService | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.flow.catch | ||
import kotlinx.coroutines.flow.filter | ||
import kotlinx.coroutines.flow.launchIn | ||
import kotlinx.coroutines.flow.onEach | ||
import kotlinx.coroutines.flow.* | ||
import kotlinx.coroutines.launch | ||
import org.koin.core.component.KoinComponent | ||
import org.koin.core.component.inject | ||
|
||
class NotificationActionHandler(scope: CoroutineScope): KoinComponent, CobbleHandler { | ||
class NotificationActionHandler(private val pebbleDevice: PebbleDevice): KoinComponent, CobbleHandler { | ||
companion object { | ||
const val NOTIFICATION_UUID_PREFIX = "cafecafe" | ||
} | ||
private val timelineActionManager: TimelineActionManager by inject() | ||
private val notificationActionExecutor: PlatformNotificationActionExecutor by inject() | ||
|
||
private val timelineActionManager = pebbleDevice.timelineActionManager | ||
private val notificationActionFlow = timelineActionManager.actionFlow.filter { | ||
val (action, _) = it | ||
action.itemID.get().toString().startsWith(NOTIFICATION_UUID_PREFIX) | ||
} | ||
|
||
init { | ||
notificationActionFlow.onEach { | ||
val (action, deferred) = it | ||
val itemId = action.itemID.get() | ||
val response = try { | ||
if (action.actionID.get().toInt() < MetaNotificationAction.metaActionLength) { | ||
notificationActionExecutor.handleMetaNotificationAction( | ||
MetaNotificationAction.entries[action.actionID.get().toInt()], | ||
itemId, | ||
action.attributes.list | ||
) | ||
} else { | ||
notificationActionExecutor.handlePlatformAction( | ||
action.actionID.get().toInt(), | ||
itemId, | ||
action.attributes.list | ||
pebbleDevice.negotiationScope.launch { | ||
notificationActionFlow.onEach { | ||
val (action, deferred) = it | ||
val itemId = action.itemID.get() | ||
Logging.v("Handling notification action for item $itemId") | ||
val response = try { | ||
if (action.actionID.get().toInt() < MetaNotificationAction.metaActionLength) { | ||
notificationActionExecutor.handleMetaNotificationAction( | ||
MetaNotificationAction.entries[action.actionID.get().toInt()], | ||
itemId, | ||
action.attributes.list | ||
) | ||
} else { | ||
notificationActionExecutor.handlePlatformAction( | ||
action.actionID.get().toInt(), | ||
itemId, | ||
action.attributes.list | ||
) | ||
} | ||
} catch (e: NoSuchElementException) { | ||
Logging.e("Error while handling notification action", e) | ||
TimelineService.ActionResponse( | ||
success = false | ||
) | ||
} | ||
} catch (e: NoSuchElementException) { | ||
Logging.e("Error while handling notification action", e) | ||
TimelineService.ActionResponse( | ||
success = false | ||
) | ||
} | ||
deferred.complete(response) | ||
}.catch { | ||
Logging.e("Error while handling notification action", it) | ||
}.launchIn(scope) | ||
deferred.complete(response) | ||
}.catch { | ||
Logging.e("Error while handling notification action", it) | ||
}.launchIn(pebbleDevice.connectionScope.filterNotNull().first()) | ||
} | ||
} | ||
} |
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