Skip to content

Commit

Permalink
新增多语言设置页面
Browse files Browse the repository at this point in the history
  • Loading branch information
guozhiqiang123 committed Nov 13, 2022
1 parent 3c125da commit ca9b426
Show file tree
Hide file tree
Showing 19 changed files with 270 additions and 53 deletions.
6 changes: 5 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ dependencies {
releaseImplementation "com.github.chuckerteam.chucker:library-no-op:3.5.2"


implementation 'androidx.core:core-ktx:1.9.0'
implementation "androidx.core:core-ktx:$core_ktx"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$androidx_lifecycle"
implementation 'androidx.activity:activity-compose:1.6.1'
implementation "androidx.compose.ui:ui:$compose_version"
Expand Down Expand Up @@ -117,4 +117,8 @@ dependencies {
//AgentWeb
//https://github.com/Justson/AgentWeb
implementation 'com.github.Justson.AgentWeb:agentweb-core:v5.0.6-androidx'

//多语言
//https://github.com/YarikSOffice/lingver
implementation "com.github.YarikSOffice:lingver:1.3.0"
}
12 changes: 11 additions & 1 deletion app/src/main/java/com/gzq/wanandroid/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import androidx.multidex.MultiDex
import com.github.panpf.sketch.Sketch
import com.github.panpf.sketch.SketchFactory
import com.github.panpf.sketch.decode.SvgBitmapDecoder
import com.gzq.wanandroid.core.log.CrashReportingTree
import com.gzq.wanandroid.net.NetHelper
import com.tencent.mmkv.MMKV
import com.gzq.wanandroid.core.log.CrashReportingTree
import com.yariksoffice.lingver.Lingver
import com.yariksoffice.lingver.store.PreferenceLocaleStore
import timber.log.Timber

class App : Application(), SketchFactory {
Expand All @@ -19,6 +21,7 @@ class App : Application(), SketchFactory {

override fun onCreate() {
super.onCreate()
appCtx = this
//初始化日志库
Timber.plant(if (BuildConfig.DEBUG) Timber.DebugTree() else CrashReportingTree())

Expand All @@ -27,6 +30,9 @@ class App : Application(), SketchFactory {
//初始网络工具类
NetHelper.init(this)

//多语言
val store = PreferenceLocaleStore(this)
Lingver.init(this, store)
}

/**
Expand All @@ -39,4 +45,8 @@ class App : Application(), SketchFactory {
}
}.build()
}

companion object {
lateinit var appCtx: App
}
}
5 changes: 5 additions & 0 deletions app/src/main/java/com/gzq/wanandroid/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.gzq.wanandroid

import android.content.Context
import android.content.res.Configuration
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
Expand All @@ -9,16 +11,19 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.navigation.compose.currentBackStackEntryAsState
import com.gzq.wanandroid.core.log.TAG_INFO
import com.gzq.wanandroid.core.log.TAG_LIFECYCLE
import com.gzq.wanandroid.features.main.MainPage
import com.gzq.wanandroid.features.main.rememberAppState
import timber.log.Timber


class MainActivity : ComponentActivity() {
@OptIn(ExperimentalMaterial3WindowSizeClassApi::class)
override fun onCreate(savedInstanceState: Bundle?) {
setTheme(R.style.Theme_AndroidTemplate)
super.onCreate(savedInstanceState)

Timber.tag(TAG_LIFECYCLE).d("onCreate")
setContent {
val windowSizeClass = calculateWindowSizeClass(this)
val appState = rememberAppState(windowSizeClass = windowSizeClass)
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/gzq/wanandroid/core/log/Tags.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ private const val TAG_PREFIX = "FUCK"
const val TAG_DEBUG = "${TAG_PREFIX}_DEBUG"
const val TAG_INFO = "${TAG_PREFIX}_INFO"
const val TAG_RECOMPOSE = "${TAG_PREFIX}_重组"
const val TAG_WEBVIEW="${TAG_PREFIX}_WEBVIEW"
const val TAG_WEBVIEW="${TAG_PREFIX}_WEBVIEW"
const val TAG_LIFECYCLE="${TAG_PREFIX}_生命周期"
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package com.gzq.wanandroid.features.change_language

import android.content.Intent
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Divider
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.RadioButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
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.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavHostController
import androidx.navigation.compose.composable
import com.gzq.wanandroid.MainActivity
import com.gzq.wanandroid.router.Router
import com.gzq.wanandroid.widget.MyTopAppBar
import com.yariksoffice.lingver.Lingver
import java.util.Locale


fun NavGraphBuilder.changeLanguagePage(
navController: NavHostController
) {
composable(Router.ChangeLanguagePage.route) {
ChangeThemePage(clickBack = {
navController.popBackStack()
})
}
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ChangeThemePage(clickBack: () -> Unit) {

var language by remember {
mutableStateOf(Lingver.getInstance().getLocale())
}

var hasChange by remember { mutableStateOf(false) }

val ctx = LocalContext.current

if (hasChange) {
DisposableEffect(Unit) {
Intent(ctx, MainActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
}.also { ctx.startActivity(it) }

onDispose { }
}
}

Scaffold(topBar = {
MyTopAppBar(titleStr = "切换语言", clickBack = clickBack)
}) { paddingValues ->
Column(
modifier = Modifier
.fillMaxSize()
.padding(paddingValues)
) {
Row(
Modifier
.fillMaxWidth()
.height(56.dp)
.clickable {
if (Lingver
.getInstance()
.isFollowingSystemLocale()
) return@clickable
Lingver
.getInstance()
.setFollowSystemLocale(ctx)
hasChange = true
}
.padding(horizontal = 16.dp),
verticalAlignment = Alignment.CenterVertically
) {
Text(text = "跟随系统", modifier = Modifier.weight(1.0f))
RadioButton(
selected = Lingver.getInstance().isFollowingSystemLocale(),
onClick = null
)
}
Divider(Modifier.padding(horizontal = 16.dp))
Row(
Modifier
.fillMaxWidth()
.height(56.dp)
.clickable {
if (language == Locale.SIMPLIFIED_CHINESE &&
!Lingver
.getInstance()
.isFollowingSystemLocale()
) return@clickable
language = Locale.SIMPLIFIED_CHINESE
Lingver
.getInstance()
.setLocale(ctx, Locale.SIMPLIFIED_CHINESE)
hasChange = true
}
.padding(horizontal = 16.dp),
verticalAlignment = Alignment.CenterVertically
) {
Text(text = "中文", modifier = Modifier.weight(1.0f))
RadioButton(
selected = language == Locale.SIMPLIFIED_CHINESE &&
!Lingver.getInstance().isFollowingSystemLocale(),
onClick = null
)
}
Divider(Modifier.padding(horizontal = 16.dp))
Row(
Modifier
.fillMaxWidth()
.height(56.dp)
.clickable {
if (language == Locale.ENGLISH) return@clickable
language = Locale.ENGLISH
Lingver
.getInstance()
.setLocale(ctx, Locale.ENGLISH)
hasChange = true
}
.padding(horizontal = 16.dp),
verticalAlignment = Alignment.CenterVertically
) {
Text(text = "英文", modifier = Modifier.weight(1.0f))
RadioButton(selected = language == Locale.ENGLISH, onClick = null)
}
Divider(Modifier.padding(horizontal = 16.dp))
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.gzq.wanandroid.features
package com.gzq.wanandroid.features.change_theme

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
Expand All @@ -25,13 +25,11 @@ import androidx.navigation.NavHostController
import androidx.navigation.compose.composable
import com.gzq.wanandroid.LocalKey
import com.gzq.wanandroid.core.quality.LogCompositions
import com.gzq.wanandroid.core.quality.recomposeHighlighter
import com.gzq.wanandroid.router.Router
import com.gzq.wanandroid.ui.theme.Theme
import com.gzq.wanandroid.ui.theme.themeState
import com.gzq.wanandroid.widget.MyTopAppBar
import com.tencent.mmkv.MMKV
import timber.log.Timber


fun NavGraphBuilder.changeThemePage(
Expand Down Expand Up @@ -72,8 +70,8 @@ fun ChangeThemePage(clickBack: () -> Unit) {
Modifier
.fillMaxWidth()
.height(56.dp)
.padding(horizontal = 16.dp)
.clickable { theme = Theme.Default },
.clickable { theme = Theme.Default }
.padding(horizontal = 16.dp),
verticalAlignment = Alignment.CenterVertically
) {
Text(text = "默认", modifier = Modifier.weight(1.0f))
Expand All @@ -84,8 +82,8 @@ fun ChangeThemePage(clickBack: () -> Unit) {
Modifier
.fillMaxWidth()
.height(56.dp)
.padding(horizontal = 16.dp)
.clickable { theme = Theme.Green },
.clickable { theme = Theme.Green }
.padding(horizontal = 16.dp),
verticalAlignment = Alignment.CenterVertically
) {
Text(text = "绿色", modifier = Modifier.weight(1.0f))
Expand All @@ -96,13 +94,14 @@ fun ChangeThemePage(clickBack: () -> Unit) {
Modifier
.fillMaxWidth()
.height(56.dp)
.padding(horizontal = 16.dp)
.clickable { theme = Theme.Purple },
.clickable { theme = Theme.Purple }
.padding(horizontal = 16.dp),
verticalAlignment = Alignment.CenterVertically
) {
Text(text = "紫色", modifier = Modifier.weight(1.0f))
RadioButton(selected = theme == Theme.Purple, onClick = null)
}
Divider(Modifier.padding(horizontal = 16.dp))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,12 @@ fun NavGraphBuilder.profileMainPage(
ProfileMainPage(launchLoginPage = {
showBottomNavigationBar(false)
navController.navigate(Router.LoginPage.route)
}, loginController = loginController, launchChangeThemePage = {
navController.navigate(Router.ChangeThemePage.route)
})
}, loginController = loginController,
launchChangeLanguagePage = {
navController.navigate(Router.ChangeLanguagePage.route)
}, launchChangeThemePage = {
navController.navigate(Router.ChangeThemePage.route)
})
}
}

Expand All @@ -106,6 +109,7 @@ fun ProfileMainPage(
viewModel: ProfileMainViewModel = viewModel(),
launchLoginPage: () -> Unit,
loginController: (Boolean, Boolean) -> Unit,
launchChangeLanguagePage: () -> Unit,
launchChangeThemePage: () -> Unit
) {
MyBackHandler()
Expand Down Expand Up @@ -171,9 +175,10 @@ fun ProfileMainPage(
} else {
//TODO:检查更新
}
}, showOnDevelop = {
showOnDevelopDialog = true
}, launchChangeThemePage = launchChangeThemePage)
},
launchChangeLanguagePage = launchChangeLanguagePage,
launchChangeThemePage = launchChangeThemePage
)

//头像
ProfileAvatarC(
Expand Down Expand Up @@ -270,7 +275,7 @@ fun MenuList(
scroll: ScrollState,
exitLogin: () -> Unit,
checkUpdate: () -> Unit,
showOnDevelop: () -> Unit,
launchChangeLanguagePage: () -> Unit,
launchChangeThemePage: () -> Unit
) {
Column {
Expand All @@ -296,7 +301,7 @@ fun MenuList(
Column(Modifier.fillMaxWidth()) {
MenuNormal()
Spacer(modifier = Modifier.height(8.dp))
MenuSetup(showOnDevelop, launchChangeThemePage)
MenuSetup(launchChangeLanguagePage, launchChangeThemePage)
Spacer(modifier = Modifier.height(8.dp))
MenuAccount(exitLogin, checkUpdate)
//为了演示滑动效果,故意增高
Expand Down Expand Up @@ -335,7 +340,7 @@ fun MenuNormal() {
}

@Composable
fun MenuSetup(showOnDevelop: () -> Unit, launchChangeThemePage: () -> Unit) {
fun MenuSetup(launchChangeLanguagePage: () -> Unit, launchChangeThemePage: () -> Unit) {
Column(
modifier = Modifier
.padding(top = 8.dp)
Expand All @@ -349,7 +354,7 @@ fun MenuSetup(showOnDevelop: () -> Unit, launchChangeThemePage: () -> Unit) {
Spacer(modifier = Modifier.height(12.dp))
Card {
ProfileMenuItemC(iconRes = R.drawable.icon_001, title = "切换语言") {
showOnDevelop()
launchChangeLanguagePage()
}
ProfileMenuItemC(iconRes = R.drawable.icon_002, title = "切换主题") {
launchChangeThemePage()
Expand Down Expand Up @@ -394,8 +399,11 @@ fun PreviewProfileMainPage() {
AndroidTemplateTheme {
ProfileMainPage(launchLoginPage = { /*TODO*/ }, loginController = { login, show ->

}, launchChangeThemePage = {
})
},
launchChangeLanguagePage = {

}, launchChangeThemePage = {
})
}
}

Loading

0 comments on commit ca9b426

Please sign in to comment.