|
| 1 | +package eu.kanade.tachiyomi.ui.reader |
| 2 | + |
| 3 | +import android.annotation.SuppressLint |
| 4 | +import android.app.Dialog |
| 5 | +import android.os.Bundle |
| 6 | +import androidx.appcompat.app.AppCompatDelegate |
| 7 | +import androidx.compose.foundation.clickable |
| 8 | +import androidx.compose.foundation.isSystemInDarkTheme |
| 9 | +import androidx.compose.foundation.layout.Arrangement |
| 10 | +import androidx.compose.foundation.layout.Box |
| 11 | +import androidx.compose.foundation.layout.Column |
| 12 | +import androidx.compose.foundation.layout.Row |
| 13 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 14 | +import androidx.compose.foundation.layout.padding |
| 15 | +import androidx.compose.material3.LocalTextStyle |
| 16 | +import androidx.compose.material3.MaterialTheme |
| 17 | +import androidx.compose.material3.ProvideTextStyle |
| 18 | +import androidx.compose.material3.Switch |
| 19 | +import androidx.compose.runtime.Composable |
| 20 | +import androidx.compose.runtime.MutableState |
| 21 | +import androidx.compose.runtime.State |
| 22 | +import androidx.compose.runtime.collectAsState |
| 23 | +import androidx.compose.runtime.getValue |
| 24 | +import androidx.compose.runtime.mutableStateOf |
| 25 | +import androidx.compose.runtime.remember |
| 26 | +import androidx.compose.runtime.setValue |
| 27 | +import androidx.compose.ui.Alignment |
| 28 | +import androidx.compose.ui.Modifier |
| 29 | +import androidx.compose.ui.graphics.Color |
| 30 | +import androidx.compose.ui.platform.ComposeView |
| 31 | +import androidx.compose.ui.platform.LocalContext |
| 32 | +import androidx.compose.ui.unit.dp |
| 33 | +import androidx.fragment.app.DialogFragment |
| 34 | +import com.chargemap.compose.numberpicker.ListItemPicker |
| 35 | +import com.google.android.material.dialog.MaterialAlertDialogBuilder |
| 36 | +import eu.kanade.tachiyomi.R |
| 37 | +import eu.kanade.tachiyomi.data.preference.PreferencesHelper |
| 38 | +import uy.kohesive.injekt.injectLazy |
| 39 | + |
| 40 | +class AutoPlayDialogFragment : DialogFragment() { |
| 41 | + |
| 42 | + private var positiveListener: ((Int) -> Unit)? = null |
| 43 | + |
| 44 | + private val preferences: PreferencesHelper by injectLazy() |
| 45 | + |
| 46 | + @SuppressLint("ResourceType") |
| 47 | + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { |
| 48 | + val possibleValues = (1..15).map { |
| 49 | + "$it S" |
| 50 | + } |
| 51 | + val state = mutableStateOf(possibleValues[0]) |
| 52 | + |
| 53 | + return MaterialAlertDialogBuilder(requireContext()) |
| 54 | + .setTitle(requireContext().getString(R.string.auto_play)) |
| 55 | + .setView( |
| 56 | + ComposeView(requireContext()).apply { |
| 57 | + setContent { |
| 58 | + val isDarkTheme = when (AppCompatDelegate.getDefaultNightMode()) { |
| 59 | + AppCompatDelegate.MODE_NIGHT_YES -> true |
| 60 | + AppCompatDelegate.MODE_NIGHT_NO -> false |
| 61 | + else -> isSystemInDarkTheme() // You can define this function to check system theme preference if needed |
| 62 | + } |
| 63 | + MaterialTheme { |
| 64 | + ProvideTextStyle( |
| 65 | + value = LocalTextStyle.current.copy( |
| 66 | + color = if (isDarkTheme) Color.White else Color.Black, |
| 67 | + ), |
| 68 | + ) { |
| 69 | + AutoPlayDialogContent( |
| 70 | + state, |
| 71 | + possibleValues, |
| 72 | + preferences.useAutoPlayProgress().asFlow() |
| 73 | + .collectAsState( |
| 74 | + initial = preferences.useAutoPlayProgress().get(), |
| 75 | + ), |
| 76 | + ) { |
| 77 | + preferences.useAutoPlayProgress().set(it) |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + }, |
| 83 | + ).apply { |
| 84 | + setPositiveButton(requireContext().getString(R.string.start)) { _, _ -> |
| 85 | + val position = possibleValues.indexOf(state.value) + 1 |
| 86 | + positiveListener?.invoke(position * 1000) |
| 87 | + } |
| 88 | + setNegativeButton(requireContext().getString(R.string.cancel)) { _, _ -> |
| 89 | + } |
| 90 | + } |
| 91 | + .create() |
| 92 | + } |
| 93 | + |
| 94 | + fun setPositiveListener(listener: (Int) -> Unit) { |
| 95 | + positiveListener = listener |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +@Composable |
| 100 | +fun AutoPlayDialogContent( |
| 101 | + number: MutableState<String>, |
| 102 | + possibleValues: List<String>, |
| 103 | + useProgress: State<Boolean>, |
| 104 | + onProgressEnable: (Boolean) -> Unit = {}, |
| 105 | +) { |
| 106 | + Box( |
| 107 | + contentAlignment = Alignment.Center, |
| 108 | + ) { |
| 109 | + var state by remember { number } |
| 110 | + val isUseProgress by remember { useProgress } |
| 111 | + Column( |
| 112 | + modifier = Modifier |
| 113 | + .fillMaxWidth(), |
| 114 | + horizontalAlignment = Alignment.CenterHorizontally, |
| 115 | + ) { |
| 116 | + ListItemPicker( |
| 117 | + label = { it }, |
| 118 | + value = state, |
| 119 | + onValueChange = { state = it }, |
| 120 | + dividersColor = MaterialTheme.colorScheme.primary, |
| 121 | + list = possibleValues, |
| 122 | + textStyle = LocalTextStyle.current, |
| 123 | + ) |
| 124 | + Row( |
| 125 | + modifier = Modifier |
| 126 | + .fillMaxWidth() |
| 127 | + .clickable { |
| 128 | + onProgressEnable.invoke(useProgress.value.not()) |
| 129 | + } |
| 130 | + .padding(horizontal = 16.dp, vertical = 8.dp), |
| 131 | + horizontalArrangement = Arrangement.SpaceBetween, |
| 132 | + verticalAlignment = Alignment.CenterVertically, |
| 133 | + ) { |
| 134 | + androidx.compose.material3.Text( |
| 135 | + text = LocalContext.current.getString(R.string.use_auto_play_progress), |
| 136 | + fontSize = MaterialTheme.typography.titleMedium.fontSize, |
| 137 | + ) |
| 138 | + Switch( |
| 139 | + checked = isUseProgress, |
| 140 | + onCheckedChange = { |
| 141 | + onProgressEnable.invoke(it) |
| 142 | + }, |
| 143 | + ) |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments