Add playback controls
This commit is contained in:
parent
3e7f11c588
commit
b69a48407b
27 changed files with 1618 additions and 6 deletions
|
|
@ -1,6 +1,9 @@
|
|||
package com.github.damontecres.dolphin
|
||||
|
||||
import android.view.KeyEvent
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.focus.FocusRequester
|
||||
import androidx.compose.ui.input.key.onPreviewKeyEvent
|
||||
import timber.log.Timber
|
||||
import kotlin.contracts.ExperimentalContracts
|
||||
import kotlin.contracts.contract
|
||||
|
|
@ -24,3 +27,61 @@ fun FocusRequester.tryRequestFocus(): Boolean =
|
|||
Timber.w(ex, "Failed to request focus")
|
||||
false
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to apply modifiers conditionally.
|
||||
*/
|
||||
fun Modifier.ifElse(
|
||||
condition: () -> Boolean,
|
||||
ifTrueModifier: Modifier,
|
||||
ifFalseModifier: Modifier = Modifier,
|
||||
): Modifier = then(if (condition()) ifTrueModifier else ifFalseModifier)
|
||||
|
||||
/**
|
||||
* Used to apply modifiers conditionally.
|
||||
*/
|
||||
fun Modifier.ifElse(
|
||||
condition: Boolean,
|
||||
ifTrueModifier: Modifier,
|
||||
ifFalseModifier: Modifier = Modifier,
|
||||
): Modifier = ifElse({ condition }, ifTrueModifier, ifFalseModifier)
|
||||
|
||||
/**
|
||||
* Handles horizontal (Left & Right) D-Pad Keys and consumes the event(s) so that the focus doesn't
|
||||
* accidentally move to another element.
|
||||
* */
|
||||
fun Modifier.handleDPadKeyEvents(
|
||||
onLeft: (() -> Unit)? = null,
|
||||
onRight: (() -> Unit)? = null,
|
||||
onCenter: (() -> Unit)? = null,
|
||||
triggerOnAction: Int = KeyEvent.ACTION_UP,
|
||||
) = onPreviewKeyEvent {
|
||||
fun onActionUp(block: () -> Unit) {
|
||||
if (it.nativeKeyEvent.action == triggerOnAction) block()
|
||||
}
|
||||
|
||||
when (it.nativeKeyEvent.keyCode) {
|
||||
KeyEvent.KEYCODE_DPAD_LEFT, KeyEvent.KEYCODE_SYSTEM_NAVIGATION_LEFT -> {
|
||||
onLeft?.apply {
|
||||
onActionUp(::invoke)
|
||||
return@onPreviewKeyEvent true
|
||||
}
|
||||
}
|
||||
|
||||
KeyEvent.KEYCODE_DPAD_RIGHT, KeyEvent.KEYCODE_SYSTEM_NAVIGATION_RIGHT -> {
|
||||
onRight?.apply {
|
||||
onActionUp(::invoke)
|
||||
return@onPreviewKeyEvent true
|
||||
}
|
||||
}
|
||||
|
||||
KeyEvent.KEYCODE_DPAD_CENTER, KeyEvent.KEYCODE_ENTER, KeyEvent.KEYCODE_NUMPAD_ENTER -> {
|
||||
onCenter?.apply {
|
||||
onActionUp(::invoke)
|
||||
return@onPreviewKeyEvent true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue