Skip to content

Commit

Permalink
Use DropDown instead of slider on Android TV. Fixes #413
Browse files Browse the repository at this point in the history
Tweak alpha and beta icons
  • Loading branch information
rumboalla committed Aug 22, 2023
1 parent b63074a commit 21a55ed
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 19 deletions.
64 changes: 59 additions & 5 deletions app/src/main/java/com/apkupdater/ui/component/Settings.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.apkupdater.ui.component

import androidx.annotation.DrawableRes
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
Expand All @@ -9,11 +10,13 @@ import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ExposedDropdownMenuBox
import androidx.compose.material3.ExposedDropdownMenuDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.SegmentedButton
import androidx.compose.material3.SegmentedButtonDefaults
Expand All @@ -34,8 +37,12 @@ import androidx.compose.ui.Alignment.Companion.CenterStart
import androidx.compose.ui.Alignment.Companion.CenterVertically
import androidx.compose.ui.Alignment.Companion.Start
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.platform.LocalTextInputService
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.apkupdater.R

Expand Down Expand Up @@ -138,7 +145,8 @@ fun DropDownSetting(
options: List<String>,
getValue: () -> Int,
setValue: (Int) -> Unit,
@DrawableRes icon: Int
@DrawableRes icon: Int,
width: Int = 100
) = Box(Modifier.padding(top = 16.dp, start = 16.dp, end = 16.dp, bottom = 8.dp).fillMaxWidth()) {
var expanded by remember { mutableStateOf(false) }
var selectedOptionText by remember { mutableStateOf(options[getValue()]) }
Expand All @@ -154,16 +162,14 @@ fun DropDownSetting(
ExposedDropdownMenuBox(
expanded = expanded,
onExpandedChange = { expanded = !expanded },
modifier = Modifier
.align(CenterEnd)
.width(150.dp)
modifier = Modifier.align(CenterEnd).width(width.dp)
) {
CompositionLocalProvider(LocalTextInputService provides null) { // Disable Keyboard
OutlinedTextField(
readOnly = true,
value = selectedOptionText,
onValueChange = { setValue(options.indexOf(it)) },
modifier = Modifier.menuAnchor(),
modifier = Modifier.menuAnchor().clickable { expanded = !expanded },
trailingIcon = { ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded) },
colors = ExposedDropdownMenuDefaults.textFieldColors()
)
Expand All @@ -185,3 +191,51 @@ fun DropDownSetting(
}
}
}

@Suppress("unused")
@Composable
fun TextFieldSetting(
text: String,
valueRange: IntRange = 0..23,
getValue: () -> Int,
setValue: (Int) -> Unit,
@DrawableRes icon: Int
) = Box(Modifier.padding(top = 16.dp, start = 16.dp, end = 16.dp, bottom = 8.dp).fillMaxWidth()) {

var value by remember { mutableStateOf(getValue().toString()) }

Row(Modifier.align(CenterStart)) {
Icon(
painterResource(id = icon),
text,
Modifier.align(CenterVertically).padding(end = 16.dp).size(24.dp)
)
Text(text, Modifier.align(CenterVertically))
}
OutlinedTextField(
modifier = Modifier
.align(CenterEnd)
.width(100.dp)
.onFocusChanged { if (!it.hasFocus && value == "") value = getValue().toString() },
value = value,
singleLine = true,
maxLines = 1,
textStyle = LocalTextStyle.current.copy(textAlign = TextAlign.End),
onValueChange = {
var new = it.toIntOrNull()
if (new != null) {
if (new < valueRange.first) {
new = valueRange.first
} else if (new > valueRange.last) {
new = valueRange.last
}
value = new.toString()
setValue(new)
} else {
value = ""
}
},
keyboardOptions = KeyboardOptions.Default.copy(keyboardType = KeyboardType.Number, imeAction = ImeAction.Done),
)

}
29 changes: 21 additions & 8 deletions app/src/main/java/com/apkupdater/ui/screen/SettingsScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Alignment.Companion.CenterHorizontally
import androidx.compose.ui.Alignment.Companion.CenterVertically
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalUriHandler
import androidx.compose.ui.platform.UriHandler
import androidx.compose.ui.res.painterResource
Expand All @@ -40,6 +41,7 @@ import com.apkupdater.BuildConfig
import com.apkupdater.R
import com.apkupdater.data.ui.GitHubSource
import com.apkupdater.data.ui.SettingsUiState
import com.apkupdater.ui.component.DropDownSetting
import com.apkupdater.ui.component.LargeTitle
import com.apkupdater.ui.component.LoadingImageApp
import com.apkupdater.ui.component.MediumText
Expand All @@ -49,6 +51,7 @@ import com.apkupdater.ui.component.SliderSetting
import com.apkupdater.ui.component.SourceIcon
import com.apkupdater.ui.component.SwitchSetting
import com.apkupdater.ui.theme.statusBarColor
import com.apkupdater.util.isAndroidTv
import com.apkupdater.viewmodel.SettingsViewModel
import org.koin.androidx.compose.koinViewModel

Expand Down Expand Up @@ -256,14 +259,24 @@ fun Settings(viewModel: SettingsViewModel) = LazyColumn {
text = stringResource(R.string.settings_alarm),
icon = R.drawable.ic_alarm
)
SliderSetting(
getValue = { viewModel.getAlarmHour().toFloat() },
setValue = { viewModel.setAlarmHour(it.toInt()) },
text = stringResource(R.string.settings_hour),
valueRange = 0f..23f,
steps = 23,
R.drawable.ic_hour
)
if (LocalContext.current.isAndroidTv()) {
DropDownSetting(
text = stringResource(R.string.settings_hour),
options = (0..23).map { it.toString() },
getValue = { viewModel.getAlarmHour() },
setValue = { viewModel.setAlarmHour(it) },
icon = R.drawable.ic_hour
)
} else {
SliderSetting(
getValue = { viewModel.getAlarmHour().toFloat() },
setValue = { viewModel.setAlarmHour(it.toInt()) },
text = stringResource(R.string.settings_hour),
valueRange = 0f..23f,
steps = 23,
R.drawable.ic_hour
)
}
SegmentedButtonSetting(
stringResource(R.string.frequency),
listOf(
Expand Down
12 changes: 9 additions & 3 deletions app/src/main/res/drawable/ic_alpha.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<vector android:height="24dp" android:viewportHeight="256"
android:viewportWidth="256" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:fillType="nonZero" android:pathData="M188.71,37.52L220.97,37.52C212.73,63.32 205.2,94.15 198.39,130.71C206.28,175.52 213.81,206.7 220.97,223.55L189.43,223.55C184.41,211.72 180.83,200.61 177.96,189.49C168.28,214.94 148.21,227.85 117.38,227.85C94.09,227.85 75.09,218.89 61.11,201.32C46.77,184.12 39.6,160.1 39.6,130.35C39.6,99.53 46.77,75.51 61.11,58.66C75.45,41.82 94.44,33.57 117.74,33.57C132.44,33.57 144.27,36.44 153.59,42.18C162.91,48.27 171.51,57.23 179.39,69.06C180.11,65.12 183.34,54.72 188.71,37.52ZM123.12,59.38C108.06,59.38 96.24,65.47 87.28,77.66C78.67,89.85 74.37,107.41 74.37,130.35C74.37,152.58 78.31,170.14 86.92,182.68C95.16,195.59 106.63,201.68 121.33,201.68C136.02,201.68 147.85,195.59 157.53,183.04C167.21,170.5 172.23,152.58 172.23,128.92C172.23,105.98 167.57,88.77 158.61,76.94C149.64,65.47 137.82,59.38 123.12,59.38Z"/>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="108dp"
android:height="108dp"
android:viewportWidth="108"
android:viewportHeight="108">
<path
android:fillColor="#FF000000"
android:pathData="M73.53,26.49L73.53,67.01C73.64,71.7 74.99,74.1 77.6,74.1C78.22,74.1 78.85,73.99 79.37,73.89L80.51,80.87C78.85,82.01 76.87,82.53 74.37,82.53C69.37,82.53 66.24,79.72 64.89,74.2C61.24,79.72 55.93,82.53 48.85,82.53C42.28,82.53 37.18,80.14 33.22,75.35C29.37,70.55 27.49,63.99 27.49,55.76L27.49,55.03C27.49,46.07 29.37,38.89 33.22,33.47C37.08,28.16 42.28,25.45 48.95,25.45C55.83,25.45 61.03,28.16 64.68,33.68L65.62,26.49L73.53,26.49ZM36.87,56.07C36.87,62.01 38.12,66.6 40.62,69.93C43.12,73.26 46.56,74.93 51.14,74.93C57.08,74.93 61.35,71.7 64.16,65.45L64.16,42.74C61.24,36.39 56.97,33.16 51.24,33.16C46.76,33.16 43.22,35.14 40.72,38.99C38.12,42.74 36.87,48.47 36.87,56.07Z"
android:fillType="nonZero"/>
</vector>
12 changes: 9 additions & 3 deletions app/src/main/res/drawable/ic_beta.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<vector android:height="24dp" android:viewportHeight="256"
android:viewportWidth="256" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:fillType="nonZero" android:pathData="M91.92,177.15L91.92,242.71L70.15,242.71L70.15,72C70.15,58.39 72.13,47.76 75.84,40.09C79.3,32.42 85.73,25.98 94.64,20.79C103.55,15.84 113.44,13.37 124.58,13.37C140.66,13.37 153.03,18.07 161.19,27.22C169.6,36.62 173.81,46.77 173.81,57.9C173.81,68.54 170.34,77.2 163.66,83.88C156.98,90.56 149.32,94.51 140.66,96C154.51,97.48 165.4,102.43 173.07,111.09C180.49,120 184.45,130.64 184.45,143.25C184.45,157.11 179.99,169.23 170.84,180.11C161.93,191 149.07,196.44 132,196.44C114.68,196.44 101.32,190.01 91.92,177.15ZM110.97,89.07C125.81,89.07 136.7,86.6 143.38,81.4C150.3,76.21 153.52,68.79 153.52,59.14C153.52,51.22 150.8,44.54 144.86,39.1C139.17,33.9 132,31.18 123.34,31.18C115.92,31.18 109.48,33.16 103.79,37.12C98.35,41.08 94.89,45.53 93.65,50.23C92.41,55.18 91.92,63.59 91.92,75.71L91.92,124.45C91.92,138.3 92.91,148.45 94.64,154.88C96.62,161.56 100.82,167.25 107.26,172.69C113.44,177.89 120.86,180.36 129.28,180.36C139.17,180.36 147.09,176.9 153.03,170.22C158.96,163.29 161.69,154.63 161.69,144.49C161.69,136.08 159.71,128.66 155.25,121.98C151.05,115.3 145.6,110.84 139.42,108.37C132.99,106.14 124.58,104.91 114.18,104.91L110.97,104.91L110.97,89.07Z"/>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="108dp"
android:height="108dp"
android:viewportWidth="108"
android:viewportHeight="108">
<path
android:fillColor="#FF000000"
android:pathData="M52.66,6.67C59.22,6.67 64.54,8.54 68.49,12.29C72.45,15.94 74.43,20.83 74.43,26.67C74.43,29.79 73.6,32.6 71.93,35.21C70.37,37.71 67.97,39.79 65.06,41.35C69.22,42.92 72.35,45.31 74.64,48.65C76.83,51.98 77.97,55.83 77.97,60.21C77.97,67.19 75.79,72.71 71.62,76.67C67.35,80.73 61.83,82.81 54.85,82.81C48.7,82.81 43.6,81.15 39.54,77.92L39.54,101.35L30.06,101.35L30.06,27.19C30.06,23.54 31.1,20.1 33.18,16.88C35.26,13.75 38.08,11.25 41.51,9.38C45.06,7.6 48.81,6.67 52.66,6.67ZM65.06,26.46C65.06,23.02 63.91,20.1 61.62,17.81C59.33,15.63 56.31,14.48 52.66,14.48C49.01,14.48 45.89,15.63 43.39,18.02C40.89,20.42 39.54,23.44 39.54,27.08L39.54,69.27C40.99,71.04 42.97,72.4 45.47,73.54C47.87,74.58 50.68,75.1 53.91,75.1C58.39,75.1 62.04,73.75 64.64,71.04C67.24,68.33 68.49,64.79 68.49,60.42C68.49,56.46 67.35,53.02 64.95,50.21C62.56,47.4 59.54,45.94 55.89,45.83L48.18,45.83L48.18,38.13L52.56,38.13C56.62,38.13 59.74,37.08 61.83,35.1C64.01,33.13 65.06,30.21 65.06,26.46Z"
android:fillType="nonZero"/>
</vector>

0 comments on commit 21a55ed

Please sign in to comment.