|
| 1 | +package io.github.sds100.keymapper.base.onboarding |
| 2 | + |
| 3 | +import dagger.hilt.android.scopes.ViewModelScoped |
| 4 | +import io.github.sds100.keymapper.base.R |
| 5 | +import io.github.sds100.keymapper.base.keymaps.DisplayKeyMapUseCase |
| 6 | +import io.github.sds100.keymapper.base.trigger.ConfigTriggerUseCase |
| 7 | +import io.github.sds100.keymapper.base.trigger.KeyCodeTriggerKey |
| 8 | +import io.github.sds100.keymapper.base.trigger.KeyEventTriggerKey |
| 9 | +import io.github.sds100.keymapper.base.trigger.Trigger |
| 10 | +import io.github.sds100.keymapper.base.trigger.TriggerMode |
| 11 | +import io.github.sds100.keymapper.base.utils.ui.ResourceProvider |
| 12 | +import io.github.sds100.keymapper.common.utils.dataOrNull |
| 13 | +import io.github.sds100.keymapper.data.Keys |
| 14 | +import io.github.sds100.keymapper.data.repositories.PreferenceRepository |
| 15 | +import io.github.sds100.keymapper.data.utils.PrefDelegate |
| 16 | +import io.github.sds100.keymapper.system.inputevents.KeyEventUtils |
| 17 | +import kotlinx.coroutines.CoroutineScope |
| 18 | +import kotlinx.coroutines.flow.MutableStateFlow |
| 19 | +import kotlinx.coroutines.flow.StateFlow |
| 20 | +import kotlinx.coroutines.flow.mapNotNull |
| 21 | +import kotlinx.coroutines.launch |
| 22 | +import javax.inject.Inject |
| 23 | +import javax.inject.Named |
| 24 | + |
| 25 | +@ViewModelScoped |
| 26 | +class OnboardingTipDelegateImpl @Inject constructor( |
| 27 | + @Named("viewmodel") |
| 28 | + private val viewModelScope: CoroutineScope, |
| 29 | + private val preferenceRepository: PreferenceRepository, |
| 30 | + private val configTriggerUseCase: ConfigTriggerUseCase, |
| 31 | + private val displayKeyMap: DisplayKeyMapUseCase, |
| 32 | + resourceProvider: ResourceProvider, |
| 33 | +) : OnboardingTipDelegate, |
| 34 | + PreferenceRepository by preferenceRepository, |
| 35 | + ResourceProvider by resourceProvider { |
| 36 | + |
| 37 | + companion object { |
| 38 | + private const val POWER_BUTTON_EMERGENCY_TIP_ID = "power_button_emergency_tip" |
| 39 | + private const val PARALLEL_TRIGGER_TIP_ID = "parallel_trigger_tip" |
| 40 | + private const val SEQUENCE_TRIGGER_TIP_ID = "sequence_trigger_tip" |
| 41 | + private const val TRIGGER_CONSTRAINTS_TIP_ID = "trigger_constraints_tip" |
| 42 | + const val CAPS_LOCK_TIP_ID = "caps_lock_tip" |
| 43 | + const val SCREEN_PINNING_TIP_ID = "screen_pinning_tip" |
| 44 | + const val IME_DETECTION_TIP_ID = "ime_detection_tip" |
| 45 | + } |
| 46 | + |
| 47 | + override val triggerTip: MutableStateFlow<OnboardingTipModel?> = MutableStateFlow(null) |
| 48 | + |
| 49 | + private var shownParallelTriggerOrderExplanation: Boolean by PrefDelegate( |
| 50 | + Keys.shownParallelTriggerOrderExplanation, |
| 51 | + false, |
| 52 | + ) |
| 53 | + |
| 54 | + private var shownSequenceTriggerExplanation: Boolean by PrefDelegate( |
| 55 | + Keys.shownSequenceTriggerExplanation, |
| 56 | + false, |
| 57 | + ) |
| 58 | + |
| 59 | + private var shownTriggerConstraintsTip: Boolean by PrefDelegate( |
| 60 | + Keys.shownTriggerConstraintsTip, |
| 61 | + false, |
| 62 | + ) |
| 63 | + |
| 64 | + private var shownCapsLockTip: Boolean by PrefDelegate( |
| 65 | + Keys.shownCapsLockTip, |
| 66 | + false, |
| 67 | + ) |
| 68 | + |
| 69 | + private var shownScreenPinningTip: Boolean by PrefDelegate( |
| 70 | + Keys.shownScreenPinningTip, |
| 71 | + false, |
| 72 | + ) |
| 73 | + |
| 74 | + private var shownImeDetectionTip: Boolean by PrefDelegate( |
| 75 | + Keys.shownTriggerKeyboardIconExplanation, |
| 76 | + false, |
| 77 | + ) |
| 78 | + |
| 79 | + init { |
| 80 | + viewModelScope.launch { |
| 81 | + configTriggerUseCase.keyMap |
| 82 | + .mapNotNull { it.dataOrNull()?.trigger } |
| 83 | + .collect { trigger -> |
| 84 | + onCollectTrigger(trigger) |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + override fun onDismissClick() { |
| 90 | + val currentTip = triggerTip.value |
| 91 | + |
| 92 | + when (currentTip?.id) { |
| 93 | + PARALLEL_TRIGGER_TIP_ID -> { |
| 94 | + shownParallelTriggerOrderExplanation = true |
| 95 | + } |
| 96 | + |
| 97 | + SEQUENCE_TRIGGER_TIP_ID -> { |
| 98 | + shownSequenceTriggerExplanation = true |
| 99 | + } |
| 100 | + |
| 101 | + TRIGGER_CONSTRAINTS_TIP_ID -> { |
| 102 | + shownTriggerConstraintsTip = true |
| 103 | + } |
| 104 | + |
| 105 | + CAPS_LOCK_TIP_ID -> { |
| 106 | + shownCapsLockTip = true |
| 107 | + } |
| 108 | + |
| 109 | + SCREEN_PINNING_TIP_ID -> { |
| 110 | + shownScreenPinningTip = true |
| 111 | + } |
| 112 | + |
| 113 | + IME_DETECTION_TIP_ID -> { |
| 114 | + shownImeDetectionTip = true |
| 115 | + } |
| 116 | + |
| 117 | + // POWER_BUTTON_EMERGENCY_TIP_ID doesn't need preference setting as it's non-dismissable |
| 118 | + } |
| 119 | + |
| 120 | + triggerTip.value = null |
| 121 | + } |
| 122 | + |
| 123 | + private suspend fun onCollectTrigger(trigger: Trigger) { |
| 124 | + val showPowerButtonEmergencyTip = trigger.keys.any { |
| 125 | + it is KeyCodeTriggerKey && KeyEventUtils.isPowerButtonKey( |
| 126 | + it.keyCode, |
| 127 | + it.scanCode ?: -1, |
| 128 | + ) |
| 129 | + } |
| 130 | + |
| 131 | + val hasCapsLockKey = |
| 132 | + trigger.keys.any { it is KeyEventTriggerKey && it.keyCode == android.view.KeyEvent.KEYCODE_CAPS_LOCK } |
| 133 | + val hasBackKey = |
| 134 | + trigger.keys.any { it is KeyEventTriggerKey && it.keyCode == android.view.KeyEvent.KEYCODE_BACK } |
| 135 | + val hasImeKey = trigger.keys.any { it is KeyEventTriggerKey && it.requiresIme } |
| 136 | + |
| 137 | + when { |
| 138 | + showPowerButtonEmergencyTip -> { |
| 139 | + val tipModel = OnboardingTipModel( |
| 140 | + id = POWER_BUTTON_EMERGENCY_TIP_ID, |
| 141 | + title = getString(R.string.pro_mode_emergency_tip_title), |
| 142 | + message = getString(R.string.pro_mode_emergency_tip_text), |
| 143 | + isDismissable = false, |
| 144 | + ) |
| 145 | + |
| 146 | + triggerTip.value = tipModel |
| 147 | + } |
| 148 | + |
| 149 | + trigger.mode is TriggerMode.Parallel && !shownParallelTriggerOrderExplanation -> { |
| 150 | + val tipModel = OnboardingTipModel( |
| 151 | + id = PARALLEL_TRIGGER_TIP_ID, |
| 152 | + title = getString(R.string.tip_parallel_trigger_title), |
| 153 | + message = getString(R.string.dialog_message_parallel_trigger_order), |
| 154 | + isDismissable = true, |
| 155 | + ) |
| 156 | + |
| 157 | + triggerTip.value = tipModel |
| 158 | + } |
| 159 | + |
| 160 | + trigger.mode is TriggerMode.Sequence && !shownSequenceTriggerExplanation -> { |
| 161 | + val tipModel = OnboardingTipModel( |
| 162 | + id = SEQUENCE_TRIGGER_TIP_ID, |
| 163 | + title = getString(R.string.tip_sequence_trigger_title), |
| 164 | + message = getString(R.string.dialog_message_sequence_trigger_explanation), |
| 165 | + isDismissable = true, |
| 166 | + ) |
| 167 | + |
| 168 | + triggerTip.value = tipModel |
| 169 | + } |
| 170 | + |
| 171 | + trigger.keys.isNotEmpty() && !shownTriggerConstraintsTip -> { |
| 172 | + val tipModel = OnboardingTipModel( |
| 173 | + id = TRIGGER_CONSTRAINTS_TIP_ID, |
| 174 | + title = getString(R.string.trigger_constraints_tip_title), |
| 175 | + message = getString(R.string.trigger_constraints_tip_text), |
| 176 | + isDismissable = true, |
| 177 | + ) |
| 178 | + |
| 179 | + triggerTip.value = tipModel |
| 180 | + } |
| 181 | + |
| 182 | + hasCapsLockKey && !shownCapsLockTip -> { |
| 183 | + val tip = OnboardingTipModel( |
| 184 | + id = CAPS_LOCK_TIP_ID, |
| 185 | + title = getString(R.string.tip_caps_lock_title), |
| 186 | + message = getString(R.string.tip_caps_lock_text), |
| 187 | + isDismissable = true, |
| 188 | + ) |
| 189 | + triggerTip.value = tip |
| 190 | + } |
| 191 | + |
| 192 | + hasBackKey && !shownScreenPinningTip -> { |
| 193 | + val tip = OnboardingTipModel( |
| 194 | + id = SCREEN_PINNING_TIP_ID, |
| 195 | + title = getString(R.string.tip_screen_pinning_title), |
| 196 | + message = getString(R.string.tip_screen_pinning_text), |
| 197 | + isDismissable = true, |
| 198 | + ) |
| 199 | + triggerTip.value = tip |
| 200 | + } |
| 201 | + |
| 202 | + hasImeKey && !shownImeDetectionTip -> { |
| 203 | + val tip = OnboardingTipModel( |
| 204 | + id = IME_DETECTION_TIP_ID, |
| 205 | + title = getString(R.string.tip_ime_detection_title), |
| 206 | + message = getString(R.string.tip_ime_detection_text), |
| 207 | + isDismissable = true, |
| 208 | + ) |
| 209 | + triggerTip.value = tip |
| 210 | + } |
| 211 | + |
| 212 | + else -> { |
| 213 | + triggerTip.value = null |
| 214 | + } |
| 215 | + } |
| 216 | + } |
| 217 | +} |
| 218 | + |
| 219 | +interface OnboardingTipDelegate { |
| 220 | + val triggerTip: StateFlow<OnboardingTipModel?> |
| 221 | + |
| 222 | + fun onDismissClick() |
| 223 | +} |
0 commit comments