diff --git a/app/src/main/java/eu/darken/capod/reaction/core/ReactionHub.kt b/app/src/main/java/eu/darken/capod/reaction/core/ReactionHub.kt index f1ae1f32..eecd1893 100644 --- a/app/src/main/java/eu/darken/capod/reaction/core/ReactionHub.kt +++ b/app/src/main/java/eu/darken/capod/reaction/core/ReactionHub.kt @@ -5,6 +5,7 @@ import eu.darken.capod.common.debug.logging.logTag import eu.darken.capod.common.flow.setupCommonEventHandlers import eu.darken.capod.reaction.core.autoconnect.AutoConnect import eu.darken.capod.reaction.core.playpause.PlayPause +import eu.darken.capod.reaction.core.popup.PopUpReaction import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.combine import javax.inject.Inject @@ -13,12 +14,14 @@ import javax.inject.Inject class ReactionHub @Inject constructor( private val playPause: PlayPause, private val autoConnect: AutoConnect, + private val popUpReaction: PopUpReaction, ) { fun monitor(): Flow = combine( playPause.monitor(), - autoConnect.monitor() - ) { _, _ -> + autoConnect.monitor(), + popUpReaction.monitor() + ) { _, _, _ -> Unit } .setupCommonEventHandlers(TAG) { "monitor" } diff --git a/app/src/main/java/eu/darken/capod/reaction/core/ReactionSettings.kt b/app/src/main/java/eu/darken/capod/reaction/core/ReactionSettings.kt index eed788b1..fb69654f 100644 --- a/app/src/main/java/eu/darken/capod/reaction/core/ReactionSettings.kt +++ b/app/src/main/java/eu/darken/capod/reaction/core/ReactionSettings.kt @@ -42,10 +42,16 @@ class ReactionSettings @Inject constructor( moshi ) + val showPopUpOnCaseOpen = preferences.createFlowPreference( + "reaction.popup.caseopen", + false + ) + override val preferenceDataStore: PreferenceDataStore = PreferenceStoreMapper( autoPause, autoPlay, autoConnect, autoConnectCondition, + showPopUpOnCaseOpen, ) } \ No newline at end of file diff --git a/app/src/main/java/eu/darken/capod/reaction/core/popup/PopUpReaction.kt b/app/src/main/java/eu/darken/capod/reaction/core/popup/PopUpReaction.kt new file mode 100644 index 00000000..b4079e8f --- /dev/null +++ b/app/src/main/java/eu/darken/capod/reaction/core/popup/PopUpReaction.kt @@ -0,0 +1,57 @@ +package eu.darken.capod.reaction.core.popup + +import eu.darken.capod.common.bluetooth.BluetoothManager2 +import eu.darken.capod.common.debug.logging.Logging.Priority.INFO +import eu.darken.capod.common.debug.logging.Logging.Priority.VERBOSE +import eu.darken.capod.common.debug.logging.log +import eu.darken.capod.common.debug.logging.logTag +import eu.darken.capod.common.flow.setupCommonEventHandlers +import eu.darken.capod.common.flow.withPrevious +import eu.darken.capod.main.core.GeneralSettings +import eu.darken.capod.monitor.core.PodMonitor +import eu.darken.capod.pods.core.apple.DualApplePods +import eu.darken.capod.reaction.core.ReactionSettings +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.emptyFlow +import kotlinx.coroutines.flow.flatMapLatest +import kotlinx.coroutines.flow.map +import javax.inject.Inject +import javax.inject.Singleton + +@Singleton +class PopUpReaction @Inject constructor( + private val bluetoothManager: BluetoothManager2, + private val podMonitor: PodMonitor, + private val generalSettings: GeneralSettings, + private val reactionSettings: ReactionSettings +) { + + fun monitor(): Flow = reactionSettings.showPopUpOnCaseOpen.flow + .flatMapLatest { if (it) podMonitor.mainDevice else emptyFlow() } + .withPrevious() + .map { (previous, current) -> + + if (previous is DualApplePods? && current is DualApplePods) { + log(TAG) { "previous=${previous?.rawCaseLidState}, current=${current.rawCaseLidState}" } + log(TAG, VERBOSE) { "previous-id=${previous?.identifier}, current-id=${current.identifier}" } + + val isSameDeviceWithCaseNowOpen = + previous?.identifier == current.identifier && previous.caseLidState != current.caseLidState + val isNewDeviceWithJustOpenedCase = + previous?.identifier != current.identifier && previous?.caseLidState != current.caseLidState + + if (isSameDeviceWithCaseNowOpen || isNewDeviceWithJustOpenedCase) { + log(TAG) { "Case lid status changed for monitored device." } + + if (current.caseLidState == DualApplePods.LidState.OPEN) { + log(TAG, INFO) { "Show popup" } + } + } + } + } + .setupCommonEventHandlers(TAG) { "monitor" } + + companion object { + private val TAG = logTag("Reaction", "PopUp") + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_baseline_chat_24.xml b/app/src/main/res/drawable/ic_baseline_chat_24.xml new file mode 100644 index 00000000..91598350 --- /dev/null +++ b/app/src/main/res/drawable/ic_baseline_chat_24.xml @@ -0,0 +1,11 @@ + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c0fe60ce..4e12e1be 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -143,4 +143,6 @@ Upgrade CAPod Get additional features and support the developer. + Show popup + Show a popup when the device case is opened (experimental). \ No newline at end of file diff --git a/app/src/main/res/xml/preferences_reactions.xml b/app/src/main/res/xml/preferences_reactions.xml index 6dc3c330..af537ce6 100644 --- a/app/src/main/res/xml/preferences_reactions.xml +++ b/app/src/main/res/xml/preferences_reactions.xml @@ -25,4 +25,10 @@ android:summary="@string/settings_autoconnect_condition_description" android:title="@string/settings_autoconnect_condition_label" /> + + \ No newline at end of file