@@ -2,6 +2,7 @@ package indi.dmzz_yyhyy.lightnovelreader
22
33import android.Manifest.permission.POST_NOTIFICATIONS
44import android.content.pm.PackageManager
5+ import android.content.res.Configuration
56import android.content.res.Resources
67import android.os.Build
78import android.os.Bundle
@@ -14,7 +15,6 @@ import androidx.compose.runtime.mutableStateOf
1415import androidx.compose.runtime.remember
1516import androidx.compose.runtime.setValue
1617import androidx.compose.ui.graphics.Color
17- import androidx.compose.ui.text.intl.Locale
1818import androidx.core.app.ActivityCompat
1919import androidx.core.content.ContextCompat
2020import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
@@ -31,6 +31,7 @@ import indi.dmzz_yyhyy.lightnovelreader.data.web.WebBookDataSourceProvider
3131import indi.dmzz_yyhyy.lightnovelreader.data.work.CheckUpdateWork
3232import indi.dmzz_yyhyy.lightnovelreader.theme.LightNovelReaderTheme
3333import indi.dmzz_yyhyy.lightnovelreader.ui.LightNovelReaderApp
34+ import indi.dmzz_yyhyy.lightnovelreader.utils.FormattingSettings
3435import indi.dmzz_yyhyy.lightnovelreader.utils.LogUtils
3536import io.nightfish.lightnovelreader.api.bookshelf.BookshelfSortType
3637import io.nightfish.lightnovelreader.api.ui.ReaderStyle
@@ -54,65 +55,31 @@ class MainActivity : ComponentActivity() {
5455 @Inject lateinit var webBookDataSourceProvider: WebBookDataSourceProvider
5556 private val coroutineScope: CoroutineScope = CoroutineScope (Dispatchers .IO )
5657
58+ private var appLocale by mutableStateOf(
59+ Resources .getSystem().configuration.locales[0 ].let { " ${it.language} -${it.country} " }
60+ )
61+ private var darkMode by mutableStateOf(" FollowSystem" )
62+ private var dynamicColor by mutableStateOf(false )
63+ private var enableM3E by mutableStateOf(false )
64+ private var lightThemeName by mutableStateOf(" light_default" )
65+ private var darkThemeName by mutableStateOf(" dark_default" )
66+
5767 override fun onCreate (savedInstanceState : Bundle ? ) {
5868 installSplashScreen()
5969 enableEdgeToEdge()
6070 super .onCreate(savedInstanceState)
6171 Thread .setDefaultUncaughtExceptionHandler(LogUtils (applicationContext, loggerRepository))
62- var appLocale by mutableStateOf(" ${Locale .current.platformLocale.language} -${Locale .current.platformLocale.variant} " )
63- var darkMode by mutableStateOf(" FollowSystem" )
64- var dynamicColor by mutableStateOf(false )
65- var enableM3E by mutableStateOf(false )
66- var lightThemeName by mutableStateOf(" light_default" )
67- var darkThemeName by mutableStateOf(" dark_default" )
6872
6973 workManager.enqueueUniquePeriodicWork(
7074 " checkUpdate" ,
7175 ExistingPeriodicWorkPolicy .KEEP ,
7276 PeriodicWorkRequestBuilder <CheckUpdateWork >(12 , TimeUnit .HOURS )
7377 .build()
7478 )
75- coroutineScope.launch(Dispatchers .IO ) {
76- if (bookshelfRepository.getAllBookshelfIds().isEmpty())
77- bookshelfRepository.createBookShelf(
78- id = 1145140721 ,
79- name = " 已收藏" ,
80- sortType = BookshelfSortType .Default ,
81- autoCache = false ,
82- systemUpdateReminder = false
83- )
84- }
85- coroutineScope.launch(Dispatchers .IO ) {
86- userDataRepository.stringUserData(UserDataPath .Settings .Display .AppLocale .path)
87- .getFlow()
88- .collect { value ->
89- val locale = Resources .getSystem().configuration.locales[0 ]
90- val systemLocale = " ${locale.language} -${locale.country} "
91- appLocale = if (value.isNullOrBlank() || value == " none" ) systemLocale
92- else value
93- }
94- }
79+ initDefaultBookshelf()
80+ observeDisplaySettings()
81+ observeFormattingSettings()
9582
96- coroutineScope.launch(Dispatchers .IO ) {
97- userDataRepository.stringUserData(UserDataPath .Settings .Display .DarkMode .path).getFlow().collect {
98- darkMode = it ? : " FollowSystem"
99- }
100- }
101- coroutineScope.launch(Dispatchers .IO ) {
102- userDataRepository.stringUserData(UserDataPath .Settings .Display .LightThemeName .path).getFlow().collect {
103- it?.let { lightThemeName = it }
104- }
105- }
106- coroutineScope.launch(Dispatchers .IO ) {
107- userDataRepository.stringUserData(UserDataPath .Settings .Display .DarkThemeName .path).getFlow().collect {
108- it?.let { darkThemeName = it }
109- }
110- }
111- coroutineScope.launch(Dispatchers .IO ) {
112- userDataRepository.booleanUserData(UserDataPath .Settings .Display .EnableM3E .path).getFlow().collect {
113- it?.let { enableM3E = it }
114- }
115- }
11683 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ) { /* Android 13 + */
11784 if (ContextCompat .checkSelfPermission(this , POST_NOTIFICATIONS ) != PackageManager .PERMISSION_GRANTED ) {
11885 ActivityCompat .requestPermissions(
@@ -121,14 +88,6 @@ class MainActivity : ComponentActivity() {
12188 }
12289 }
12390
124- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S ) {
125- coroutineScope.launch(Dispatchers .IO ) {
126- userDataRepository.booleanUserData(UserDataPath .Settings .Display .DynamicColors .path).getFlow().collect {
127- dynamicColor = it == true
128- }
129- }
130- }
131-
13291 val fontSizeUserData = userDataRepository.floatUserData(UserDataPath .Reader .FontSize .path)
13392 val fontLineHeightUserData = userDataRepository.floatUserData(UserDataPath .Reader .FontLineHeight .path)
13493 val fontWeightUserData = userDataRepository.floatUserData(UserDataPath .Reader .FontWeigh .path)
@@ -179,8 +138,88 @@ class MainActivity : ComponentActivity() {
179138 }
180139 }
181140
141+ private fun initDefaultBookshelf () {
142+ coroutineScope.launch(Dispatchers .IO ) {
143+ if (bookshelfRepository.getAllBookshelfIds().isEmpty())
144+ bookshelfRepository.createBookShelf(
145+ id = 1145140721 ,
146+ name = " 已收藏" ,
147+ sortType = BookshelfSortType .Default ,
148+ autoCache = false ,
149+ systemUpdateReminder = false
150+ )
151+ }
152+ }
153+
154+ private fun observeDisplaySettings () {
155+ coroutineScope.launch(Dispatchers .IO ) {
156+ userDataRepository.stringUserData(UserDataPath .Settings .Display .AppLocale .path)
157+ .getFlow()
158+ .collect { value ->
159+ val locale = Resources .getSystem().configuration.locales[0 ]
160+ val systemLocale = " ${locale.language} -${locale.country} "
161+ appLocale = if (value.isNullOrBlank() || value == " none" ) systemLocale
162+ else value
163+ }
164+ }
165+ coroutineScope.launch(Dispatchers .IO ) {
166+ userDataRepository.stringUserData(UserDataPath .Settings .Display .DarkMode .path).getFlow().collect {
167+ it?.let { darkMode = it }
168+ }
169+ }
170+ coroutineScope.launch(Dispatchers .IO ) {
171+ userDataRepository.stringUserData(UserDataPath .Settings .Display .LightThemeName .path).getFlow().collect {
172+ it?.let { lightThemeName = it }
173+ }
174+ }
175+ coroutineScope.launch(Dispatchers .IO ) {
176+ userDataRepository.stringUserData(UserDataPath .Settings .Display .DarkThemeName .path).getFlow().collect {
177+ it?.let { darkThemeName = it }
178+ }
179+ }
180+ coroutineScope.launch(Dispatchers .IO ) {
181+ userDataRepository.booleanUserData(UserDataPath .Settings .Display .EnableM3E .path).getFlow().collect {
182+ it?.let { enableM3E = it }
183+ }
184+ }
185+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S ) {
186+ coroutineScope.launch(Dispatchers .IO ) {
187+ userDataRepository.booleanUserData(UserDataPath .Settings .Display .DynamicColors .path).getFlow().collect {
188+ dynamicColor = it == true
189+ }
190+ }
191+ }
192+ }
193+
194+ private fun observeFormattingSettings () {
195+ coroutineScope.launch(Dispatchers .IO ) {
196+ userDataRepository.stringUserData(UserDataPath .Settings .Display .DateStyle .path).getFlow().collect {
197+ it?.let { FormattingSettings .dateFormat = it }
198+ }
199+ }
200+ coroutineScope.launch(Dispatchers .IO ) {
201+ userDataRepository.booleanUserData(UserDataPath .Settings .Display .DateShowYear .path).getFlow().collect {
202+ it?.let { FormattingSettings .dateShowYear = it }
203+ }
204+ }
205+ coroutineScope.launch(Dispatchers .IO ) {
206+ userDataRepository.stringUserData(UserDataPath .Settings .Display .DateOrder .path).getFlow().collect {
207+ it?.let { FormattingSettings .dateOrder = it }
208+ }
209+ }
210+ coroutineScope.launch(Dispatchers .IO ) {
211+ userDataRepository.booleanUserData(UserDataPath .Settings .Display .RelativeTimeStyle .path).getFlow().collect {
212+ it?.let { FormattingSettings .useRelativeTime = it }
213+ }
214+ }
215+ }
216+
182217 override fun onDestroy () {
183218 super .onDestroy()
184219 coroutineScope.cancel()
185220 }
221+
222+ override fun onConfigurationChanged (newConfig : Configuration ) {
223+ super .onConfigurationChanged(newConfig)
224+ }
186225}
0 commit comments