Skip to content

Commit

Permalink
feat: add system locale settings for Android 13+
Browse files Browse the repository at this point in the history
  • Loading branch information
JunkFood02 committed Dec 22, 2023
1 parent c33c37e commit 1cc50f9
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 14 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ android {
val name =
output.filters.find { it.filterType == FilterConfiguration.FilterType.ABI }?.identifier

val baseAbiCode = abiCodes[name]
val baseAbiCode = abiCodes[name]?:2

if (baseAbiCode != null) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,55 @@
package com.junkfood.seal.ui.page.settings.appearance

import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.provider.Settings
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.selection.selectableGroup
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.outlined.ArrowForwardIos
import androidx.compose.material.icons.outlined.Translate
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.rememberTopAppBarState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalUriHandler
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.junkfood.seal.MainActivity
import com.junkfood.seal.R
import com.junkfood.seal.ui.component.BackButton
import com.junkfood.seal.ui.component.HorizontalDivider
import com.junkfood.seal.ui.component.LargeTopAppBar
import com.junkfood.seal.ui.component.PreferenceSingleChoiceItem
import com.junkfood.seal.ui.component.PreferencesHintCard
import com.junkfood.seal.ui.page.settings.about.weblate
import com.junkfood.seal.ui.theme.SealTheme
import com.junkfood.seal.util.LANGUAGE
import com.junkfood.seal.util.PreferenceUtil
import com.junkfood.seal.util.PreferenceUtil.getLanguageConfiguration
Expand All @@ -37,20 +58,60 @@ import com.junkfood.seal.util.getLanguageDesc
import com.junkfood.seal.util.languageMap


@Composable
fun LanguagePage(onBackPressed: () -> Unit = {}) {
var language by remember { mutableStateOf(PreferenceUtil.getLanguageNumber()) }
val context = LocalContext.current
val intent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
Intent(Settings.ACTION_APP_LOCALE_SETTINGS).apply {
val uri = Uri.fromParts("package", context.packageName, null)
data = uri
}
} else {
Intent()
}

val isSystemLocaleSettingsAvailable =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
context.packageManager.queryIntentActivities(
intent, PackageManager.MATCH_ALL
).isNotEmpty()
} else {
false
}
LanguagePageImpl(
onBackPressed = onBackPressed,
languageMap = languageMap,
isSystemLocaleSettingsAvailable = isSystemLocaleSettingsAvailable,
onNavigateToSystemLocaleSettings = {
if (isSystemLocaleSettingsAvailable) {
context.startActivity(intent)
}
},
selectedLanguage = language,
) {
language = it
PreferenceUtil.encodeInt(LANGUAGE, language)
MainActivity.setLanguage(getLanguageConfiguration())
}
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun LanguagePage(onBackPressed: () -> Unit) {
private fun LanguagePageImpl(
onBackPressed: () -> Unit = {},
languageMap: Map<Int, String>,
isSystemLocaleSettingsAvailable: Boolean = false,
onNavigateToSystemLocaleSettings: () -> Unit,
selectedLanguage: Int,
onLanguageSelected: (Int) -> Unit = {}
) {
val scrollBehavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior(
rememberTopAppBarState(),
canScroll = { true }
)
var language by remember { mutableStateOf(PreferenceUtil.getLanguageNumber()) }
val uriHandler = LocalUriHandler.current
fun setLanguage(selectedLanguage: Int) {
language = selectedLanguage
PreferenceUtil.encodeInt(LANGUAGE, language)
MainActivity.setLanguage(getLanguageConfiguration())
}

Scaffold(
modifier = Modifier
.fillMaxSize()
Expand All @@ -72,7 +133,6 @@ fun LanguagePage(onBackPressed: () -> Unit) {
LazyColumn(
modifier = Modifier
.padding(it)
.selectableGroup()
) {
item {
PreferencesHintCard(
Expand All @@ -84,19 +144,79 @@ fun LanguagePage(onBackPressed: () -> Unit) {
item {
PreferenceSingleChoiceItem(
text = stringResource(R.string.follow_system),
selected = language == SYSTEM_DEFAULT,
selected = selectedLanguage == SYSTEM_DEFAULT,
contentPadding = PaddingValues(horizontal = 12.dp, vertical = 18.dp)
) { setLanguage(SYSTEM_DEFAULT) }
) { onLanguageSelected(SYSTEM_DEFAULT) }
}

for (languageData in languageMap) {
item {
PreferenceSingleChoiceItem(
text = getLanguageDesc(languageData.key),
selected = language == languageData.key,
selected = selectedLanguage == languageData.key,
contentPadding = PaddingValues(horizontal = 12.dp, vertical = 18.dp)
) { setLanguage(languageData.key) }
) { onLanguageSelected(languageData.key) }
}
}
if (isSystemLocaleSettingsAvailable) {
item {
HorizontalDivider()
Surface(
modifier = Modifier.clickable(
onClick = onNavigateToSystemLocaleSettings
)
) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(PaddingValues(horizontal = 12.dp, vertical = 18.dp)),
verticalAlignment = Alignment.CenterVertically,
) {
Column(
modifier = Modifier
.weight(1f)
.padding(start = 10.dp)
) {
Text(
text = stringResource(R.string.system_settings),
maxLines = 1,
style = MaterialTheme.typography.titleLarge.copy(fontSize = 20.sp),
color = MaterialTheme.colorScheme.onSurface,
overflow = TextOverflow.Ellipsis
)
}
Icon(
imageVector = Icons.AutoMirrored.Outlined.ArrowForwardIos,
contentDescription = null,
modifier = Modifier.padding(end = 16.dp).size(18.dp)
)
}
}
}
}
}
})
}

@Preview
@Composable
private fun LanguagePagePreview() {
var language by remember {
mutableIntStateOf(1)
}
val map = buildMap<Int, String> {
repeat(38) {
put(it + 1, "")
}
}
SealTheme {
LanguagePageImpl(
languageMap = map,
isSystemLocaleSettingsAvailable = true,
onNavigateToSystemLocaleSettings = { /*TODO*/ },
selectedLanguage = language
) {
language = it
}
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@
<string name="website">Website</string>
<string name="playlist_title">Playlist title</string>
<string name="subdirectory_hint">Your downloads will be saved as:</string>
<string name="system_settings">System settings</string>
<plurals name="item_count">
<item quantity="one">%d item</item>
<item quantity="other">%d items</item>
Expand Down

0 comments on commit 1cc50f9

Please sign in to comment.