chore: storeFlow.value

This commit is contained in:
lisonge 2024-02-19 18:57:28 +08:00
parent a726e2b22b
commit b72f834566
5 changed files with 50 additions and 98 deletions

View File

@ -28,6 +28,7 @@ import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import li.songe.gkd.BuildConfig
@ -56,7 +57,6 @@ import li.songe.gkd.util.storeFlow
import li.songe.gkd.util.subsIdToRawFlow
import li.songe.gkd.util.subsItemsFlow
import li.songe.gkd.util.toast
import li.songe.gkd.util.updateStorage
import li.songe.gkd.util.updateSubscription
import li.songe.selector.Selector
import java.util.concurrent.ExecutorService
@ -412,10 +412,9 @@ class GkdAbService : CompositionAbService({
} catch (e: Exception) {
LogUtils.d("创建无障碍悬浮窗失败", e)
toast("创建无障碍悬浮窗失败")
updateStorage(
storeFlow,
storeFlow.value.copy(enableAbFloatWindow = false)
)
storeFlow.update { store ->
store.copy(enableAbFloatWindow = false)
}
}
}
} else {

View File

@ -81,7 +81,6 @@ import li.songe.gkd.util.launchTry
import li.songe.gkd.util.navigate
import li.songe.gkd.util.storeFlow
import li.songe.gkd.util.toast
import li.songe.gkd.util.updateStorage
import li.songe.gkd.util.usePollState
import rikka.shizuku.Shizuku
@ -145,20 +144,16 @@ fun DebugPage() {
val tasks =
newActivityTaskManager()?.safeGetTasks()?.firstOrNull()
if (tasks != null) {
updateStorage(
storeFlow, store.copy(
enableShizuku = true
)
storeFlow.value = store.copy(
enableShizuku = true
)
} else {
toast("Shizuku方法校验失败,无法使用")
}
}
} else {
updateStorage(
storeFlow, store.copy(
enableShizuku = false
)
storeFlow.value = store.copy(
enableShizuku = false
)
}
@ -246,10 +241,8 @@ fun DebugPage() {
desc = "当HTTP服务关闭时,保留内存订阅",
checked = !store.autoClearMemorySubs
) {
updateStorage(
storeFlow, store.copy(
autoClearMemorySubs = !it
)
storeFlow.value = store.copy(
autoClearMemorySubs = !it
)
}
HorizontalDivider()
@ -308,10 +301,8 @@ fun DebugPage() {
desc = "当音量变化时,生成快照,如果悬浮窗按钮不工作,可以使用这个",
checked = store.captureVolumeChange
) {
updateStorage(
storeFlow, store.copy(
captureVolumeChange = it
)
storeFlow.value = store.copy(
captureVolumeChange = it
)
}
@ -321,10 +312,8 @@ fun DebugPage() {
desc = "当用户截屏时保存快照(需手动替换快照图片),仅支持部分小米设备",
checked = store.captureScreenshot
) {
updateStorage(
storeFlow, store.copy(
captureScreenshot = it
)
storeFlow.value = store.copy(
captureScreenshot = it
)
}
@ -334,10 +323,8 @@ fun DebugPage() {
desc = "当保存快照时,隐藏截图里的顶部状态栏高度区域",
checked = store.hideSnapshotStatusBar
) {
updateStorage(
storeFlow, store.copy(
hideSnapshotStatusBar = it
)
storeFlow.value = store.copy(
hideSnapshotStatusBar = it
)
}
@ -374,10 +361,8 @@ fun DebugPage() {
toast("请输入在 5000~65535 的任意数字")
return@TextButton
}
updateStorage(
storeFlow, store.copy(
httpServerPort = newPort
)
storeFlow.value = store.copy(
httpServerPort = newPort
)
showPortDlg = false
}) {

View File

@ -47,7 +47,6 @@ import li.songe.gkd.util.launchTry
import li.songe.gkd.util.navigate
import li.songe.gkd.util.ruleSummaryFlow
import li.songe.gkd.util.storeFlow
import li.songe.gkd.util.updateStorage
import li.songe.gkd.util.usePollState
val controlNav = BottomNavItem(label = "主页", icon = Icons.Outlined.Home)
@ -101,10 +100,8 @@ fun useControlPage(): ScaffoldExt {
desc = "根据订阅规则匹配屏幕目标节点",
checked = store.enableService,
onCheckedChange = {
updateStorage(
storeFlow, store.copy(
enableService = it
)
storeFlow.value = store.copy(
enableService = it
)
})
}
@ -144,17 +141,13 @@ fun useControlPage(): ScaffoldExt {
if (!checkOrRequestNotifPermission(context)) {
return@TextSwitch
}
updateStorage(
storeFlow, store.copy(
enableStatusService = true
)
storeFlow.value = store.copy(
enableStatusService = true
)
ManageService.start(context)
} else {
updateStorage(
storeFlow, store.copy(
enableStatusService = false
)
storeFlow.value = store.copy(
enableStatusService = false
)
ManageService.stop(context)
}

View File

@ -63,7 +63,6 @@ import li.songe.gkd.util.logZipDir
import li.songe.gkd.util.navigate
import li.songe.gkd.util.storeFlow
import li.songe.gkd.util.toast
import li.songe.gkd.util.updateStorage
import java.io.File
val settingsNav = BottomNavItem(
@ -111,20 +110,16 @@ fun useSettingsPage(): ScaffoldExt {
.fillMaxWidth()
.selectable(selected = (option.second == store.updateSubsInterval),
onClick = {
updateStorage(
storeFlow,
storeFlow.value.copy(updateSubsInterval = option.second)
)
storeFlow.value =
store.copy(updateSubsInterval = option.second)
})
.padding(horizontal = 16.dp)
) {
RadioButton(
selected = (option.second == store.updateSubsInterval),
onClick = {
updateStorage(
storeFlow,
storeFlow.value.copy(updateSubsInterval = option.second)
)
storeFlow.value = store.copy(updateSubsInterval = option.second)
})
Text(
text = option.first, modifier = Modifier.padding(start = 16.dp)
@ -152,20 +147,15 @@ fun useSettingsPage(): ScaffoldExt {
.fillMaxWidth()
.selectable(selected = (option.second == store.enableDarkTheme),
onClick = {
updateStorage(
storeFlow,
storeFlow.value.copy(enableDarkTheme = option.second)
)
storeFlow.value =
store.copy(enableDarkTheme = option.second)
})
.padding(horizontal = 16.dp)
) {
RadioButton(
selected = (option.second == store.enableDarkTheme),
onClick = {
updateStorage(
storeFlow,
storeFlow.value.copy(enableDarkTheme = option.second)
)
storeFlow.value = store.copy(enableDarkTheme = option.second)
})
Text(
text = option.first, modifier = Modifier.padding(start = 16.dp)
@ -199,10 +189,8 @@ fun useSettingsPage(): ScaffoldExt {
)
}, onDismissRequest = { showToastInputDlg = false }, confirmButton = {
TextButton(onClick = {
updateStorage(
storeFlow, store.copy(
clickToast = value
)
storeFlow.value = store.copy(
clickToast = value
)
showToastInputDlg = false
}) {
@ -349,10 +337,8 @@ fun useSettingsPage(): ScaffoldExt {
desc = "在[最近任务]界面中隐藏本应用",
checked = store.excludeFromRecents,
onCheckedChange = {
updateStorage(
storeFlow, store.copy(
excludeFromRecents = it
)
storeFlow.value = store.copy(
excludeFromRecents = it
)
})
HorizontalDivider()
@ -361,10 +347,8 @@ fun useSettingsPage(): ScaffoldExt {
desc = "添加透明悬浮窗,关闭可能导致不点击/点击缓慢",
checked = store.enableAbFloatWindow,
onCheckedChange = {
updateStorage(
storeFlow, store.copy(
enableAbFloatWindow = it
)
storeFlow.value = store.copy(
enableAbFloatWindow = it
)
})
HorizontalDivider()
@ -380,10 +364,8 @@ fun useSettingsPage(): ScaffoldExt {
authActionFlow.value = canDrawOverlaysAuthAction
return@TextSwitch
}
updateStorage(
storeFlow, store.copy(
toastWhenClick = it
)
storeFlow.value = store.copy(
toastWhenClick = it
)
})
HorizontalDivider()
@ -415,10 +397,8 @@ fun useSettingsPage(): ScaffoldExt {
desc = "打开应用时自动检测是否存在新版本",
checked = store.autoCheckAppUpdate,
onCheckedChange = {
updateStorage(
storeFlow, store.copy(
autoCheckAppUpdate = it
)
storeFlow.value = store.copy(
autoCheckAppUpdate = it
)
})
HorizontalDivider()
@ -461,10 +441,8 @@ fun useSettingsPage(): ScaffoldExt {
desc = "保存最近7天的日志,大概占用您5M的空间",
checked = store.log2FileSwitch,
onCheckedChange = {
updateStorage(
storeFlow, store.copy(
log2FileSwitch = it
)
storeFlow.value = store.copy(
log2FileSwitch = it
)
if (!it) {
appScope.launchTry(Dispatchers.IO) {

View File

@ -3,8 +3,8 @@ package li.songe.gkd.util
import com.blankj.utilcode.util.LogUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.serialization.Serializable
@ -14,7 +14,7 @@ import li.songe.gkd.appScope
private inline fun <reified T> createStorageFlow(
key: String,
crossinline init: () -> T,
): StateFlow<T> {
): MutableStateFlow<T> {
val str = kv.getString(key, null)
val initValue = if (str != null) {
try {
@ -38,10 +38,6 @@ private inline fun <reified T> createStorageFlow(
return stateFlow
}
fun <T> updateStorage(stateFlow: StateFlow<T>, newState: T) {
(stateFlow as MutableStateFlow).value = newState
}
@Serializable
data class Store(
val enableService: Boolean = true,
@ -82,10 +78,11 @@ val clickCountFlow by lazy {
private val log2FileSwitchFlow by lazy { storeFlow.map(appScope) { s -> s.log2FileSwitch } }
fun increaseClickCount(n: Int = 1) {
updateStorage(
recordStoreFlow,
recordStoreFlow.value.copy(clickCount = recordStoreFlow.value.clickCount + n)
)
recordStoreFlow.update {
it.copy(
clickCount = it.clickCount + n
)
}
}
fun initStore() {