Skip to content

Commit

Permalink
Add LanguageItem
Browse files Browse the repository at this point in the history
  • Loading branch information
SanmerDev committed Jun 29, 2024
1 parent 451dc80 commit 9e19cad
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 92 deletions.
Original file line number Diff line number Diff line change
@@ -1,35 +1,57 @@
package dev.sanmer.mrepo.ui.screens.settings.app

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.provider.Settings
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.TopAppBarScrollBehavior
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import dev.sanmer.mrepo.R
import dev.sanmer.mrepo.app.Const
import dev.sanmer.mrepo.compat.BuildCompat
import dev.sanmer.mrepo.datastore.model.WorkingMode.Companion.isRoot
import dev.sanmer.mrepo.ui.component.NavigateUpTopBar
import dev.sanmer.mrepo.ui.component.SettingNormalItem
import dev.sanmer.mrepo.ui.component.SettingSwitchItem
import dev.sanmer.mrepo.ui.component.TextFieldDialog
import dev.sanmer.mrepo.ui.providable.LocalUserPreferences
import dev.sanmer.mrepo.ui.screens.settings.app.items.AppThemeItem
import dev.sanmer.mrepo.ui.screens.settings.app.items.DownloadPathItem
import dev.sanmer.mrepo.utils.extensions.applicationLocale
import dev.sanmer.mrepo.viewmodel.SettingsViewModel
import java.io.File
import java.util.Locale

@Composable
fun AppScreen(
navController: NavController,
viewModel: SettingsViewModel = hiltViewModel()
) {
val context = LocalContext.current
val userPreferences = LocalUserPreferences.current
val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior()

Expand All @@ -49,6 +71,10 @@ fun AppScreen(
.fillMaxSize()
.verticalScroll(rememberScrollState())
) {
LanguageItem(
context = context
)

AppThemeItem(
themeColor = userPreferences.themeColor,
darkMode = userPreferences.darkMode,
Expand All @@ -74,6 +100,93 @@ fun AppScreen(
}
}

@SuppressLint("InlinedApi")
@Composable
private fun LanguageItem(
context: Context
) = SettingNormalItem(
icon = R.drawable.world,
title = stringResource(id = R.string.settings_language),
desc = context.applicationLocale?.localizedDisplayName ?: stringResource(id = R.string.settings_language_system),
onClick = {
context.startActivity(
Intent(
Settings.ACTION_APP_LOCALE_SETTINGS,
Uri.fromParts("package", context.packageName, null)
)
)
},
enabled = BuildCompat.atLeastT
)

@Composable
private fun DownloadPathItem(
downloadPath: String,
onChange: (String) -> Unit
) {
var edit by remember { mutableStateOf(false) }
if (edit) EditDialog(
path = downloadPath,
onClose = { edit = false },
onConfirm = onChange
)

SettingNormalItem(
icon = R.drawable.files,
title = stringResource(id = R.string.settings_download_path),
desc = downloadPath,
onClick = { edit = true }
)
}

@Composable
private fun EditDialog(
path: String,
onClose: () -> Unit,
onConfirm: (String) -> Unit
) {
var name by remember {
mutableStateOf(
File(path).toRelativeString(Const.PUBLIC_DOWNLOADS)
)
}

TextFieldDialog(
shape = RoundedCornerShape(20.dp),
onDismissRequest = onClose,
title = { Text(text = stringResource(id = R.string.settings_download_path)) },
confirmButton = {
TextButton(
onClick = {
onConfirm(
File(Const.PUBLIC_DOWNLOADS, name.trim()).path
)
onClose()
},
) {
Text(text = stringResource(id = R.string.dialog_ok))
}
},
dismissButton = {
TextButton(
onClick = onClose
) {
Text(text = stringResource(id = R.string.dialog_cancel))
}
},
launchKeyboard = false
) {
OutlinedTextField(
textStyle = MaterialTheme.typography.bodyLarge,
value = name,
onValueChange = { name = it },
shape = RoundedCornerShape(15.dp),
label = { Text(text = Const.PUBLIC_DOWNLOADS.absolutePath) },
singleLine = true
)
}
}

@Composable
private fun TopBar(
scrollBehavior: TopAppBarScrollBehavior,
Expand All @@ -82,4 +195,14 @@ private fun TopBar(
title = stringResource(id = R.string.settings_app),
scrollBehavior = scrollBehavior,
navController = navController
)
)

private val Locale.localizedDisplayName: String
get() = getDisplayName(this)
.replaceFirstChar {
if (it.isLowerCase()) {
it.titlecase(this)
} else {
it.toString()
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ package dev.sanmer.mrepo.utils.extensions

import android.content.Context
import android.content.Intent
import androidx.core.app.LocaleManagerCompat
import androidx.core.app.ShareCompat

val Context.tmpDir get() = cacheDir.resolve("tmp")
.apply {
if (!exists()) mkdirs()
}

val Context.applicationLocale
get() = LocaleManagerCompat.getApplicationLocales(applicationContext)
.toList().firstOrNull()

fun Context.openUrl(url: String) {
Intent.parseUri(url, Intent.URI_INTENT_SCHEME).apply {
startActivity(this)
}
startActivity(
Intent.parseUri(url, Intent.URI_INTENT_SCHEME)
)
}

fun Context.shareText(text: String) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package dev.sanmer.mrepo.utils.extensions

import androidx.core.os.LocaleListCompat
import java.util.Locale

fun LocaleListCompat.toList(): List<Locale> = List(size()) { this[it]!! }
36 changes: 36 additions & 0 deletions app/src/main/res/drawable/world.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:pathData="M3,12a9,9 0,1 0,18 0a9,9 0,0 0,-18 0"
android:strokeLineJoin="round"
android:strokeWidth="2"
android:strokeColor="#ffff"
android:strokeLineCap="round"/>
<path
android:pathData="M3.6,9h16.8"
android:strokeLineJoin="round"
android:strokeWidth="2"
android:strokeColor="#ffff"
android:strokeLineCap="round"/>
<path
android:pathData="M3.6,15h16.8"
android:strokeLineJoin="round"
android:strokeWidth="2"
android:strokeColor="#ffff"
android:strokeLineCap="round"/>
<path
android:pathData="M11.5,3a17,17 0,0 0,0 18"
android:strokeLineJoin="round"
android:strokeWidth="2"
android:strokeColor="#ffff"
android:strokeLineCap="round"/>
<path
android:pathData="M12.5,3a17,17 0,0 1,0 18"
android:strokeLineJoin="round"
android:strokeWidth="2"
android:strokeColor="#ffff"
android:strokeLineCap="round"/>
</vector>
2 changes: 2 additions & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@
<!-- Non Root Item -->
<string name="settings_non_root_desc">功能受到限制</string>
<!-- App -->
<string name="settings_language">语言</string>
<string name="settings_language_system">系统默认</string>
<string name="settings_app_theme">主题</string>
<string name="settings_app_theme_desc">更改主题配色和深色模式</string>
<string name="settings_download_path">下载路径</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@
<!-- Non Root Item -->
<string name="settings_non_root_desc">Functionality is limited</string>
<!-- App -->
<string name="settings_language">Language</string>
<string name="settings_language_system">System default</string>
<string name="settings_app_theme">App Theme</string>
<string name="settings_app_theme_desc">Change color scheme and night mode</string>
<string name="settings_download_path">Download Path</string>
Expand Down

0 comments on commit 9e19cad

Please sign in to comment.