Skip to content

Commit

Permalink
feat(player): Subtitle settings + refactor + crash fixes (#1152)
Browse files Browse the repository at this point in the history
Co-authored-by: jmir1 <[email protected]>
Co-authored-by: Abdallah <[email protected]>
  • Loading branch information
3 people authored and LuftVerbot committed Oct 27, 2023
1 parent 2431d8a commit f0783f5
Show file tree
Hide file tree
Showing 61 changed files with 1,890 additions and 1,066 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import androidx.core.view.WindowInsetsControllerCompat
import cafe.adriel.voyager.core.annotation.InternalVoyagerApi
import cafe.adriel.voyager.core.lifecycle.DisposableEffectIgnoringConfiguration
import cafe.adriel.voyager.core.screen.Screen
import cafe.adriel.voyager.navigator.Navigator
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import eu.kanade.presentation.util.ScreenTransition
import eu.kanade.presentation.util.isTabletUi
import tachiyomi.presentation.core.components.AdaptiveSheet as AdaptiveSheetImpl
Expand Down Expand Up @@ -70,6 +72,7 @@ fun NavigatorAdaptiveSheet(
*/
@Composable
fun AdaptiveSheet(
hideSystemBars: Boolean = false,
tonalElevation: Dp = 1.dp,
enableSwipeDismiss: Boolean = true,
onDismissRequest: () -> Unit,
Expand All @@ -81,6 +84,12 @@ fun AdaptiveSheet(
onDismissRequest = onDismissRequest,
properties = dialogProperties,
) {
if (hideSystemBars) {
rememberSystemUiController().apply {
isSystemBarsVisible = false
systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
}
}
AdaptiveSheetImpl(
isTabletUi = isTabletUi,
tonalElevation = tonalElevation,
Expand Down
105 changes: 105 additions & 0 deletions app/src/main/java/eu/kanade/presentation/components/SettingsItems.kt
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@
package eu.kanade.presentation.components

import android.view.MotionEvent
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.ContentAlpha
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.AddCircle
import androidx.compose.material.icons.outlined.RemoveCircle
import androidx.compose.material.icons.rounded.CheckBox
import androidx.compose.material.icons.rounded.CheckBoxOutlineBlank
import androidx.compose.material.icons.rounded.DisabledByDefault
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.ExposedDropdownMenuBox
import androidx.compose.material3.ExposedDropdownMenuDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.pointer.pointerInteropFilter
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.delay
import tachiyomi.domain.entries.TriStateFilter
import tachiyomi.presentation.core.components.SettingsItemsPaddings

Expand Down Expand Up @@ -126,3 +138,96 @@ fun SelectItem(
}
}
}

@Composable
fun RepeatingIconButton(
modifier: Modifier = Modifier,
onClick: () -> Unit,
enabled: Boolean = true,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
maxDelayMillis: Long = 750,
minDelayMillis: Long = 5,
delayDecayFactor: Float = .25f,
content: @Composable () -> Unit,
) {
val currentClickListener by rememberUpdatedState(onClick)
var pressed by remember { mutableStateOf(false) }

IconButton(
modifier = modifier.pointerInteropFilter {
pressed = when (it.action) {
MotionEvent.ACTION_DOWN -> true

else -> false
}

true
},
onClick = {},
enabled = enabled,
interactionSource = interactionSource,
content = content,
)

LaunchedEffect(pressed, enabled) {
var currentDelayMillis = maxDelayMillis

while (enabled && pressed) {
currentClickListener()
delay(currentDelayMillis)
currentDelayMillis =
(currentDelayMillis - (currentDelayMillis * delayDecayFactor))
.toLong().coerceAtLeast(minDelayMillis)
}
}
}

@Composable
fun OutlinedNumericChooser(
label: String,
placeholder: String,
suffix: String,
value: Int,
step: Int,
min: Int? = null,
onValueChanged: (Int) -> Unit,
) {
var currentValue = value

val updateValue: (Boolean) -> Unit = {
currentValue += if (it) step else -step

if (min != null) currentValue = if (currentValue < min) min else currentValue

onValueChanged(currentValue)
}

Row(verticalAlignment = Alignment.CenterVertically) {
RepeatingIconButton(
onClick = { updateValue(false) },
) { Icon(imageVector = Icons.Outlined.RemoveCircle, contentDescription = null) }

OutlinedTextField(
value = "%d".format(currentValue),
modifier = Modifier.widthIn(min = 140.dp),

onValueChange = {
// Don't allow multiple decimal points, non-numeric characters, or leading zeros
currentValue = it.trim().replace(Regex("[^-\\d.]"), "").toIntOrNull()
?: currentValue
onValueChanged(currentValue)
},

label = { Text(text = label) },
placeholder = { Text(text = placeholder) },
suffix = { Text(text = suffix) },

singleLine = true,
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
)

RepeatingIconButton(
onClick = { updateValue(true) },
) { Icon(imageVector = Icons.Outlined.AddCircle, contentDescription = null) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.util.fastForEachIndexed
Expand All @@ -42,9 +43,13 @@ fun TabbedDialog(
onDismissRequest: () -> Unit,
tabTitles: List<String>,
tabOverflowMenuContent: (@Composable ColumnScope.(() -> Unit) -> Unit)? = null,
onOverflowMenuClicked: (() -> Unit)? = null,
overflowIcon: ImageVector? = null,
hideSystemBars: Boolean = false,
content: @Composable (Int) -> Unit,
) {
AdaptiveSheet(
hideSystemBars = hideSystemBars,
onDismissRequest = onDismissRequest,
) {
val scope = rememberCoroutineScope()
Expand Down Expand Up @@ -77,7 +82,7 @@ fun TabbedDialog(
}
}

tabOverflowMenuContent?.let { MoreMenu(it) }
MoreMenu(onOverflowMenuClicked, tabOverflowMenuContent, overflowIcon)
}
Divider()

Expand All @@ -94,21 +99,29 @@ fun TabbedDialog(

@Composable
private fun MoreMenu(
content: @Composable ColumnScope.(() -> Unit) -> Unit,
onClickIcon: (() -> Unit)?,
content: @Composable (ColumnScope.(() -> Unit) -> Unit)?,
overflowIcon: ImageVector? = null,
) {
if (onClickIcon == null && content == null) return

var expanded by remember { mutableStateOf(false) }
val onClick = onClickIcon ?: { expanded = true }

Box(modifier = Modifier.wrapContentSize(Alignment.TopStart)) {
IconButton(onClick = { expanded = true }) {
IconButton(onClick = onClick) {
Icon(
imageVector = Icons.Default.MoreVert,
imageVector = overflowIcon ?: Icons.Default.MoreVert,
contentDescription = stringResource(R.string.label_more),
)
}
DropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false },
) {
content { expanded = false }
if (onClickIcon == null) {
DropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false },
) {
content!! { expanded = false }
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ object AdvancedPlayerSettingsScreen : SearchableSettings {
@Composable
override fun getPreferences(): List<Preference> {
val playerPreferences = remember { Injekt.get<PlayerPreferences>() }
val scope = rememberCoroutineScope()
val context = LocalContext.current
val mpvConf = playerPreferences.mpvConf()
val mpvInput = playerPreferences.mpvInput()
val scope = rememberCoroutineScope()

return listOf(
Preference.PreferenceItem.MultiLineEditTextPreference(
Expand Down
Loading

0 comments on commit f0783f5

Please sign in to comment.