Skip to content

Commit

Permalink
#27 add language setting in the app
Browse files Browse the repository at this point in the history
  • Loading branch information
javadjafari1 committed Dec 17, 2023
1 parent 3145659 commit 46b85f8
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import ir.thatsmejavad.backgroundable.screens.medialist.MediaListScreen
import ir.thatsmejavad.backgroundable.screens.search.SearchScreen
import ir.thatsmejavad.backgroundable.screens.settings.SettingsScreen
import ir.thatsmejavad.backgroundable.screens.settings.imagequalitysetting.ImageQualitySettingScreen
import ir.thatsmejavad.backgroundable.screens.settings.language.LanguageScreen
import ir.thatsmejavad.backgroundable.screens.settings.themesetting.ThemeSettingScreen
import kotlinx.serialization.json.Json

Expand Down Expand Up @@ -170,4 +171,11 @@ fun NavGraphBuilder.settingNavGraph(navController: NavHostController) {
navController = navController
)
}
animatedComposable(
route = AppScreens.Language.route,
) {
LanguageScreen(
navController = navController
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,8 @@ internal sealed class AppScreens(val route: String) {
data object AboutUs : AppScreens(
"about-us"
)

data object Language : AppScreens(
"language"
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ package ir.thatsmejavad.backgroundable.main
import android.Manifest
import android.os.Build
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.SystemBarStyle
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.Crossfade
import androidx.compose.animation.slideInVertically
Expand Down Expand Up @@ -65,7 +65,7 @@ import ir.thatsmejavad.backgroundable.settingNavGraph
import ir.thatsmejavad.backgroundable.ui.BackgroundableTheme
import javax.inject.Inject

class MainActivity : ComponentActivity() {
class MainActivity : AppCompatActivity() {

@Inject
lateinit var viewModel: MainViewModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ fun SettingsScreen(
.fillMaxSize(),
verticalArrangement = Arrangement.spacedBy(16.dp)
) {
SettingItem(
textId = R.string.label_language,
imageId = R.drawable.ic_language,
onClick = {
navigateTo(AppScreens.Language.route)
}
)

SettingItem(
textId = R.string.label_quality,
imageId = R.drawable.ic_high_quality,
Expand All @@ -77,6 +85,14 @@ fun SettingsScreen(
}
)

SettingItem(
textId = R.string.label_about_us,
imageId = R.drawable.ic_info,
onClick = {
navigateTo(AppScreens.AboutUs.route)
}
)

SettingItem(
textId = R.string.label_provide_feedback,
imageId = R.drawable.ic_feedback,
Expand All @@ -87,13 +103,6 @@ fun SettingsScreen(
}
)

SettingItem(
textId = R.string.label_about_us,
imageId = R.drawable.ic_info,
onClick = {
navigateTo(AppScreens.AboutUs.route)
}
)
Spacer(modifier = Modifier.weight(1f))

Text(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package ir.thatsmejavad.backgroundable.screens.settings.language

import androidx.appcompat.app.AppCompatDelegate
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
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.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.MediumTopAppBar
import androidx.compose.material3.RadioButton
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.rememberTopAppBarState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.core.os.LocaleListCompat
import androidx.navigation.NavController
import ir.thatsmejavad.backgroundable.R
import ir.thatsmejavad.backgroundable.common.ui.BackgroundableScaffold

@Composable
fun LanguageScreen(
navController: NavController,
) {
LanguageScreen(
onBackClicked = { navController.navigateUp() },
)
}

@Composable
private fun LanguageScreen(
onBackClicked: () -> Unit,
) {
val scrollBehavior = TopAppBarDefaults.enterAlwaysScrollBehavior(rememberTopAppBarState())

val isPersianSelected = remember {
AppCompatDelegate.getApplicationLocales().get(0)?.language == "fa"
}
BackgroundableScaffold(
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
topBar = {
MediumTopAppBar(
title = {
Text(text = stringResource(R.string.label_language))
},
navigationIcon = {
IconButton(onClick = onBackClicked) {
Icon(
imageVector = Icons.AutoMirrored.Filled.ArrowBack,
contentDescription = "Navigate back"
)
}
},
scrollBehavior = scrollBehavior
)
}
) { paddingValues ->
Column(
modifier = Modifier
.fillMaxSize()
.padding(paddingValues)
.verticalScroll(rememberScrollState())
.padding(vertical = 16.dp),
verticalArrangement = Arrangement.spacedBy(24.dp)
) {
LanguageItem(
text = stringResource(R.string.label_english_en),
description = stringResource(R.string.label_english_lang_desc),
isSelected = !isPersianSelected,
onClick = {
AppCompatDelegate.setApplicationLocales(
LocaleListCompat.getEmptyLocaleList()
)
}
)
LanguageItem(
text = stringResource(R.string.label_persian),
description = stringResource(R.string.label_persian_lang_desc),
isSelected = isPersianSelected,
onClick = {
AppCompatDelegate.setApplicationLocales(
LocaleListCompat.forLanguageTags("fa")
)
}
)
}
}
}

@Composable
private fun LanguageItem(
text: String,
description: String,
isSelected: Boolean,
onClick: () -> Unit
) {
Row(
modifier = Modifier
.clickable(onClick = onClick)
.fillMaxWidth()
.padding(
horizontal = 32.dp,
vertical = 8.dp
)
) {
Column {
Text(
text = text,
style = MaterialTheme.typography.titleMedium,
color = MaterialTheme.colorScheme.onSurface
)

Spacer(modifier = Modifier.size(8.dp))

Text(
text = description,
style = MaterialTheme.typography.titleSmall,
color = MaterialTheme.colorScheme.primary
)
}

Spacer(modifier = Modifier.weight(1f))

RadioButton(
selected = isSelected,
onClick = onClick
)
}
}
12 changes: 12 additions & 0 deletions app/src/main/res/drawable/ic_language.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="32dp"
android:height="32dp"
android:viewportWidth="32"
android:viewportHeight="32">
<group>
<clip-path android:pathData="M0,0h32v32h-32z" />
<path
android:fillColor="#000"
android:pathData="M15.987,2.667C8.627,2.667 2.667,8.64 2.667,16C2.667,23.36 8.627,29.333 15.987,29.333C23.36,29.333 29.333,23.36 29.333,16C29.333,8.64 23.36,2.667 15.987,2.667ZM25.227,10.667H21.293C20.867,9 20.253,7.4 19.453,5.92C21.907,6.76 23.947,8.467 25.227,10.667ZM16,5.387C17.107,6.987 17.973,8.76 18.547,10.667H13.453C14.027,8.76 14.893,6.987 16,5.387ZM5.68,18.667C5.467,17.813 5.333,16.92 5.333,16C5.333,15.08 5.467,14.187 5.68,13.333H10.187C10.08,14.213 10,15.093 10,16C10,16.907 10.08,17.787 10.187,18.667H5.68ZM6.773,21.333H10.707C11.133,23 11.747,24.6 12.547,26.08C10.093,25.24 8.053,23.547 6.773,21.333ZM10.707,10.667H6.773C8.053,8.453 10.093,6.76 12.547,5.92C11.747,7.4 11.133,9 10.707,10.667ZM16,26.613C14.893,25.013 14.027,23.24 13.453,21.333H18.547C17.973,23.24 17.107,25.013 16,26.613ZM19.12,18.667H12.88C12.76,17.787 12.667,16.907 12.667,16C12.667,15.093 12.76,14.2 12.88,13.333H19.12C19.24,14.2 19.333,15.093 19.333,16C19.333,16.907 19.24,17.787 19.12,18.667ZM19.453,26.08C20.253,24.6 20.867,23 21.293,21.333H25.227C23.947,23.533 21.907,25.24 19.453,26.08ZM21.813,18.667C21.92,17.787 22,16.907 22,16C22,15.093 21.92,14.213 21.813,13.333H26.32C26.533,14.187 26.667,15.08 26.667,16C26.667,16.92 26.533,17.813 26.32,18.667H21.813Z" />
</group>
</vector>
1 change: 1 addition & 0 deletions app/src/main/res/values-fa/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@
<string name="label_original">اصلی</string>
<string name="label_small">کوچک</string>
<string name="label_tiny">خیلی کوچک</string>
<string name="label_language">زبان</string>
</resources>
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<string name="label_ok">OK</string>
<string name="label_send_to">Share via</string>
<string name="label_quality">Image Quality</string>
<string name="label_language">Language</string>
<string name="label_provide_feedback">Provide Feedback</string>
<string name="label_material_you_desc">Android 12+ Only</string>
<string name="label_image_quality_desc">Only category photos are affected by your choice.</string>
Expand All @@ -48,4 +49,8 @@
<string name="label_tiny">Tiny</string>
<string name="label_landscape" translatable="false">Landscape</string>
<string name="label_portrait" translatable="false">Portrait</string>
<string name="label_english_en" translatable="false">English</string>
<string name="label_persian" translatable="false">فارسی</string>
<string name="label_english_lang_desc" translatable="false">The text of the app changes to English</string>
<string name="label_persian_lang_desc" translatable="false">متن های برنامه به فارسی تغییر می کند</string>
</resources>
2 changes: 1 addition & 1 deletion app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="Theme.Backgroundable" parent="android:Theme.Material.Light.NoActionBar" />
<style name="Theme.Backgroundable" parent="Theme.AppCompat.NoActionBar" />

<style name="Theme.App.Starting" parent="Theme.SplashScreen">
<item name="windowSplashScreenAnimatedIcon">@drawable/ic_logo_splash</item>
Expand Down

0 comments on commit 46b85f8

Please sign in to comment.