-
-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#378 customise fingerprint gesture options
- Loading branch information
Showing
13 changed files
with
515 additions
and
37 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
68 changes: 68 additions & 0 deletions
68
.../main/java/io/github/sds100/keymapper/data/model/behavior/FingerprintGestureMapOptions.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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package io.github.sds100.keymapper.data.model.behavior | ||
|
||
import io.github.sds100.keymapper.data.model.FingerprintGestureMap | ||
import io.github.sds100.keymapper.data.model.behavior.BehaviorOption.Companion.applyBehaviorOption | ||
import io.github.sds100.keymapper.data.model.getData | ||
import io.github.sds100.keymapper.util.result.valueOrNull | ||
import splitties.bitflags.hasFlag | ||
import java.io.Serializable | ||
|
||
/** | ||
* Created by sds100 on 18/11/20. | ||
*/ | ||
|
||
class FingerprintGestureMapOptions(val gestureId: String, fingerprintGestureMap: FingerprintGestureMap) : Serializable { | ||
companion object { | ||
const val ID_VIBRATE = "vibrate" | ||
const val ID_VIBRATION_DURATION = "vibration_duration" | ||
} | ||
|
||
val vibrate = BehaviorOption( | ||
id = ID_VIBRATE, | ||
value = fingerprintGestureMap.flags.hasFlag(FingerprintGestureMap.FLAG_VIBRATE), | ||
isAllowed = true | ||
) | ||
|
||
val vibrateDuration: BehaviorOption<Int> | ||
|
||
init { | ||
val vibrateDurationValue = | ||
fingerprintGestureMap.extras.getData(FingerprintGestureMap.EXTRA_VIBRATION_DURATION).valueOrNull()?.toInt() | ||
|
||
vibrateDuration = BehaviorOption( | ||
id = ID_VIBRATION_DURATION, | ||
value = vibrateDurationValue ?: BehaviorOption.DEFAULT, | ||
isAllowed = vibrate.value | ||
) | ||
|
||
} | ||
|
||
fun setValue(id: String, value: Boolean): FingerprintGestureMapOptions { | ||
when (id) { | ||
ID_VIBRATE -> { | ||
vibrate.value = value | ||
vibrateDuration.isAllowed = value | ||
} | ||
} | ||
|
||
return this | ||
} | ||
|
||
fun setValue(id: String, value: Int): FingerprintGestureMapOptions { | ||
when (id) { | ||
ID_VIBRATION_DURATION -> vibrateDuration.value = value | ||
} | ||
|
||
return this | ||
} | ||
|
||
fun applyToFingerprintGestureMap(fingerprintGestureMap: FingerprintGestureMap): FingerprintGestureMap { | ||
val newFlags = fingerprintGestureMap.flags | ||
.applyBehaviorOption(vibrate, FingerprintGestureMap.FLAG_VIBRATE) | ||
|
||
val newExtras = fingerprintGestureMap.extras | ||
.applyBehaviorOption(vibrateDuration, FingerprintGestureMap.EXTRA_VIBRATION_DURATION) | ||
|
||
return fingerprintGestureMap.clone(flags = newFlags, extras = newExtras) | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
...n/java/io/github/sds100/keymapper/data/viewmodel/FingerprintGestureMapOptionsViewModel.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 |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package io.github.sds100.keymapper.data.viewmodel | ||
|
||
import android.os.Bundle | ||
import androidx.lifecycle.* | ||
import io.github.sds100.keymapper.R | ||
import io.github.sds100.keymapper.data.model.CheckBoxListItemModel | ||
import io.github.sds100.keymapper.data.model.SliderListItemModel | ||
import io.github.sds100.keymapper.data.model.SliderModel | ||
import io.github.sds100.keymapper.data.model.behavior.BehaviorOption.Companion.nullIfDefault | ||
import io.github.sds100.keymapper.data.model.behavior.FingerprintGestureMapOptions | ||
import io.github.sds100.keymapper.util.Data | ||
import io.github.sds100.keymapper.util.Loading | ||
import io.github.sds100.keymapper.util.State | ||
import io.github.sds100.keymapper.util.ifIsData | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.asSharedFlow | ||
import kotlinx.coroutines.launch | ||
|
||
/** | ||
* Created by sds100 on 18/11/20. | ||
*/ | ||
|
||
class FingerprintGestureMapOptionsViewModel : ViewModel() { | ||
companion object { | ||
private const val STATE_KEY = "state_fingerprint_gesture_map_behavior" | ||
} | ||
|
||
private val _options: MutableLiveData<State<FingerprintGestureMapOptions>> = MutableLiveData(Loading()) | ||
val options: LiveData<State<FingerprintGestureMapOptions>> = _options | ||
|
||
private val _onSave: MutableSharedFlow<FingerprintGestureMapOptions> = MutableSharedFlow() | ||
val onSave = _onSave.asSharedFlow() | ||
|
||
fun setOptions(behavior: FingerprintGestureMapOptions) { | ||
_options.value = Data(behavior) | ||
} | ||
|
||
val checkBoxModels = options.map { | ||
if (it !is Data) return@map listOf() | ||
|
||
return@map sequence { | ||
if (it.data.vibrate.isAllowed) { | ||
yield(CheckBoxListItemModel( | ||
id = it.data.vibrate.id, | ||
label = R.string.flag_vibrate, | ||
isChecked = it.data.vibrate.value | ||
)) | ||
} | ||
}.toList() | ||
} | ||
|
||
val sliderModels = options.map { | ||
if (it !is Data) return@map listOf() | ||
|
||
return@map sequence { | ||
if (it.data.vibrateDuration.isAllowed) { | ||
yield(SliderListItemModel( | ||
id = it.data.vibrateDuration.id, | ||
label = R.string.extra_label_vibration_duration, | ||
sliderModel = SliderModel( | ||
value = it.data.vibrateDuration.value.nullIfDefault, | ||
isDefaultStepEnabled = true, | ||
min = R.integer.vibrate_duration_min, | ||
maxSlider = R.integer.vibrate_duration_max, | ||
stepSize = R.integer.vibrate_duration_step_size | ||
) | ||
)) | ||
} | ||
|
||
}.toList() | ||
} | ||
|
||
fun setValue(id: String, newValue: Int) { | ||
options.value?.ifIsData { | ||
_options.value = Data(it.setValue(id, newValue)) | ||
} | ||
} | ||
|
||
fun setValue(id: String, newValue: Boolean) { | ||
options.value?.ifIsData { | ||
_options.value = Data(it.setValue(id, newValue)) | ||
} | ||
} | ||
|
||
fun save() { | ||
options.value?.ifIsData { | ||
viewModelScope.launch { | ||
_onSave.emit(it) | ||
} | ||
} | ||
} | ||
|
||
fun saveState(outState: Bundle) { | ||
options.value?.ifIsData { | ||
outState.putSerializable(STATE_KEY, it) | ||
} | ||
} | ||
|
||
fun restoreState(state: Bundle) { | ||
_options.value = Loading() | ||
|
||
val behavior = state.getSerializable(STATE_KEY) as FingerprintGestureMapOptions | ||
setOptions(behavior) | ||
} | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
class Factory : ViewModelProvider.NewInstanceFactory() { | ||
|
||
override fun <T : ViewModel?> create(modelClass: Class<T>): T { | ||
return FingerprintGestureMapOptionsViewModel() as T | ||
} | ||
} | ||
} |
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
Oops, something went wrong.