Add logic for popup detection

This commit is contained in:
darken
2022-01-18 14:38:48 +01:00
parent 17d2ebf863
commit 26dc5a4d75
6 changed files with 87 additions and 2 deletions
@@ -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<Unit> = combine(
playPause.monitor(),
autoConnect.monitor()
) { _, _ ->
autoConnect.monitor(),
popUpReaction.monitor()
) { _, _, _ ->
Unit
}
.setupCommonEventHandlers(TAG) { "monitor" }
@@ -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,
)
}
@@ -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<Unit> = 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")
}
}
@@ -0,0 +1,11 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal"
android:autoMirrored="true">
<path
android:fillColor="@android:color/white"
android:pathData="M20,2L4,2c-1.1,0 -1.99,0.9 -1.99,2L2,22l4,-4h14c1.1,0 2,-0.9 2,-2L22,4c0,-1.1 -0.9,-2 -2,-2zM6,9h12v2L6,11L6,9zM14,14L6,14v-2h8v2zM18,8L6,8L6,6h12v2z" />
</vector>
+2
View File
@@ -143,4 +143,6 @@
<string name="upgrade_capod_label">Upgrade CAPod</string>
<string name="upgrade_capod_description">Get additional features and support the developer.</string>
<string name="settings_popup_caseopen_label">Show popup</string>
<string name="settings_popup_caseopen_description">Show a popup when the device case is opened (experimental).</string>
</resources>
@@ -25,4 +25,10 @@
android:summary="@string/settings_autoconnect_condition_description"
android:title="@string/settings_autoconnect_condition_label" />
<CheckBoxPreference
android:icon="@drawable/ic_baseline_chat_24"
android:key="reaction.popup.caseopen"
android:summary="@string/settings_popup_caseopen_description"
android:title="@string/settings_popup_caseopen_label" />
</PreferenceScreen>