-
-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathMainActivity.kt
More file actions
245 lines (229 loc) · 10.4 KB
/
Copy pathMainActivity.kt
File metadata and controls
245 lines (229 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package indi.dmzz_yyhyy.lightnovelreader
import android.Manifest.permission.POST_NOTIFICATIONS
import android.content.Intent
import android.content.pm.PackageManager
import android.content.res.Configuration
import android.content.res.Resources
import android.os.Build
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.graphics.Color
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.WorkManager
import dagger.hilt.android.AndroidEntryPoint
import indi.dmzz_yyhyy.lightnovelreader.data.bookshelf.BookshelfRepository
import indi.dmzz_yyhyy.lightnovelreader.data.logging.LoggerRepository
import indi.dmzz_yyhyy.lightnovelreader.data.plugin.PluginManager
import indi.dmzz_yyhyy.lightnovelreader.data.update.UpdateCheckRepository
import indi.dmzz_yyhyy.lightnovelreader.data.userdata.UserDataRepository
import indi.dmzz_yyhyy.lightnovelreader.data.web.WebBookDataSourceProvider
import indi.dmzz_yyhyy.lightnovelreader.data.work.CheckUpdateWork
import indi.dmzz_yyhyy.lightnovelreader.theme.LightNovelReaderTheme
import indi.dmzz_yyhyy.lightnovelreader.ui.LightNovelReaderApp
import indi.dmzz_yyhyy.lightnovelreader.utils.FormattingSettings
import indi.dmzz_yyhyy.lightnovelreader.utils.LogUtils
import io.nightfish.lightnovelreader.api.bookshelf.BookshelfSortType
import io.nightfish.lightnovelreader.api.ui.ReaderStyle
import io.nightfish.lightnovelreader.api.userdata.UserDataPath
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.launch
import java.util.concurrent.TimeUnit
import javax.inject.Inject
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
@Inject lateinit var loggerRepository: LoggerRepository
@Inject lateinit var bookshelfRepository: BookshelfRepository
private val intentChannel = Channel<Intent>(capacity = 4, onBufferOverflow = BufferOverflow.DROP_OLDEST)
val intentFlow = intentChannel.receiveAsFlow()
@Inject lateinit var userDataRepository: UserDataRepository
@Inject lateinit var updateCheckRepository: UpdateCheckRepository
@Inject lateinit var workManager: WorkManager
@Inject lateinit var pluginManager: PluginManager
@Inject lateinit var webBookDataSourceProvider: WebBookDataSourceProvider
private val coroutineScope: CoroutineScope = CoroutineScope(Dispatchers.IO)
private var appLocale by mutableStateOf(
Resources.getSystem().configuration.locales[0].let { "${it.language}-${it.country}" }
)
private var darkMode by mutableStateOf("FollowSystem")
private var dynamicColor by mutableStateOf(false)
private var enableM3E by mutableStateOf(false)
private var lightThemeName by mutableStateOf("light_default")
private var darkThemeName by mutableStateOf("dark_default")
override fun onCreate(savedInstanceState: Bundle?) {
installSplashScreen()
enableEdgeToEdge()
super.onCreate(savedInstanceState)
Thread.setDefaultUncaughtExceptionHandler(LogUtils(applicationContext, loggerRepository))
workManager.enqueueUniquePeriodicWork(
"checkUpdate",
ExistingPeriodicWorkPolicy.KEEP,
PeriodicWorkRequestBuilder<CheckUpdateWork>(12, TimeUnit.HOURS)
.build()
)
initDefaultBookshelf()
observeDisplaySettings()
observeFormattingSettings()
handleIntent(intent)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { /* Android 13 + */
if (ContextCompat.checkSelfPermission(this, POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
this, arrayOf(POST_NOTIFICATIONS), 0
)
}
}
val fontSizeUserData = userDataRepository.floatUserData(UserDataPath.Reader.FontSize.path)
val fontLineHeightUserData = userDataRepository.floatUserData(UserDataPath.Reader.FontLineHeight.path)
val fontWeightUserData = userDataRepository.floatUserData(UserDataPath.Reader.FontWeigh.path)
val textColorUserData = userDataRepository.colorUserData(UserDataPath.Reader.TextColor.path)
val textDarkColorUserData = userDataRepository.colorUserData(UserDataPath.Reader.TextDarkColor.path)
setContent {
val readerStyle by remember {
combine(
fontSizeUserData.getFlowWithDefault(15f),
fontLineHeightUserData.getFlowWithDefault(7f),
fontWeightUserData.getFlowWithDefault(500f),
textColorUserData.getFlowWithDefault(Color.Unspecified),
textDarkColorUserData.getFlowWithDefault(Color.Unspecified)
) { fontSize, lineHeight, weight, textColor, textDarkColor ->
ReaderStyle(
fontSize = fontSize,
fontLineHeight = lineHeight,
fontWeight = weight,
textColor = textColor,
textDarkColor = textDarkColor,
)
}
}.collectAsState(initial = ReaderStyle(
fontSize = 15f,
fontLineHeight = 7f,
fontWeight = 500f,
textColor = Color.Unspecified,
textDarkColor = Color.Unspecified,
))
LightNovelReaderTheme(
darkMode = darkMode,
appLocale = appLocale,
isDynamicColor = dynamicColor,
enableM3E = enableM3E,
lightThemeName = lightThemeName,
darkThemeName = darkThemeName
) {
LightNovelReaderApp(
readerStyle = readerStyle,
intentFlow = intentFlow,
onBuildNavHost = {
with(pluginManager) {
onBuildNavHost()
}
},
imageHeaderGetter = { webBookDataSourceProvider.default.imageHeader }
)
}
}
}
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
intentChannel.trySend(intent)
}
private fun handleIntent(intent: Intent) {
intentChannel.trySend(intent)
}
private fun initDefaultBookshelf() {
coroutineScope.launch(Dispatchers.IO) {
if (bookshelfRepository.getAllBookshelfIds().isEmpty())
bookshelfRepository.createBookShelf(
id = 1145140721,
name = "已收藏",
sortType = BookshelfSortType.Default,
sortReversed = false,
autoCache = false,
systemUpdateReminder = false
)
}
}
private fun observeDisplaySettings() {
coroutineScope.launch(Dispatchers.IO) {
userDataRepository.stringUserData(UserDataPath.Settings.Display.AppLocale.path)
.getFlow()
.collect { value ->
val locale = Resources.getSystem().configuration.locales[0]
val systemLocale = "${locale.language}-${locale.country}"
appLocale = if (value.isNullOrBlank() || value == "none") systemLocale
else value
}
}
coroutineScope.launch(Dispatchers.IO) {
userDataRepository.stringUserData(UserDataPath.Settings.Display.DarkMode.path).getFlow().collect {
it?.let { darkMode = it }
}
}
coroutineScope.launch(Dispatchers.IO) {
userDataRepository.stringUserData(UserDataPath.Settings.Display.LightThemeName.path).getFlow().collect {
it?.let { lightThemeName = it }
}
}
coroutineScope.launch(Dispatchers.IO) {
userDataRepository.stringUserData(UserDataPath.Settings.Display.DarkThemeName.path).getFlow().collect {
it?.let { darkThemeName = it }
}
}
coroutineScope.launch(Dispatchers.IO) {
userDataRepository.booleanUserData(UserDataPath.Settings.Display.EnableM3E.path).getFlow().collect {
it?.let { enableM3E = it }
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
coroutineScope.launch(Dispatchers.IO) {
userDataRepository.booleanUserData(UserDataPath.Settings.Display.DynamicColors.path).getFlow().collect {
dynamicColor = it == true
}
}
}
}
private fun observeFormattingSettings() {
coroutineScope.launch(Dispatchers.IO) {
userDataRepository.stringUserData(UserDataPath.Settings.Display.DateStyle.path).getFlow().collect {
it?.let { FormattingSettings.dateFormat = it }
}
}
coroutineScope.launch(Dispatchers.IO) {
userDataRepository.booleanUserData(UserDataPath.Settings.Display.DateShowYear.path).getFlow().collect {
it?.let { FormattingSettings.dateShowYear = it }
}
}
coroutineScope.launch(Dispatchers.IO) {
userDataRepository.stringUserData(UserDataPath.Settings.Display.DateOrder.path).getFlow().collect {
it?.let { FormattingSettings.dateOrder = it }
}
}
coroutineScope.launch(Dispatchers.IO) {
userDataRepository.booleanUserData(UserDataPath.Settings.Display.RelativeTimeStyle.path).getFlow().collect {
it?.let { FormattingSettings.useRelativeTime = it }
}
}
}
override fun onDestroy() {
super.onDestroy()
coroutineScope.cancel()
}
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
}
}