Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Commit

Permalink
feat: 设置检查更新开关
Browse files Browse the repository at this point in the history
  • Loading branch information
jing332 committed Aug 20, 2023
1 parent 19e212b commit 26a1563
Show file tree
Hide file tree
Showing 6 changed files with 233 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.github.jing332.alistandroid.config

import com.funny.data_saver.core.DataSaverPreferences
import com.funny.data_saver.core.mutableDataSaverStateOf
import com.github.jing332.alistandroid.app

object AppConfig {
private val pref =
DataSaverPreferences(app.getSharedPreferences("app", 0))


var isAutoCheckUpdate = mutableDataSaverStateOf(
dataSaverInterface = pref,
key = "isCheckUpdate",
initialValue = true
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.github.jing332.alistandroid.config.AppConfig
import com.github.jing332.alistandroid.model.AppUpdateChecker
import com.github.jing332.alistandroid.model.UpdateResult
import kotlinx.coroutines.Dispatchers
Expand All @@ -19,13 +20,14 @@ class MainViewModel : ViewModel() {
var showUpdateDialog by mutableStateOf<UpdateResult?>(null)

fun checkAppUpdate() {
viewModelScope.launch(Dispatchers.IO) {
runCatching {
val ret = AppUpdateChecker.checkUpdate()
if (ret.hasUpdate()) showUpdateDialog = ret
}.onFailure {
Log.e(TAG, "checkAppUpdate: ", it)
if (AppConfig.isAutoCheckUpdate.value)
viewModelScope.launch(Dispatchers.IO) {
runCatching {
val ret = AppUpdateChecker.checkUpdate()
if (ret.hasUpdate()) showUpdateDialog = ret
}.onFailure {
Log.e(TAG, "checkAppUpdate: ", it)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ fun AListConfigScreen(vm: AListConfigViewModel = viewModel()) {
}

val scope = rememberCoroutineScope()
LaunchedEffect(Unit) {

LaunchedEffect(vm.hashCode()) {
vm.init()
}
val cfg = vm.config

var address by remember { mutableStateOf("0.0.0.0") }
Scaffold(
Expand Down Expand Up @@ -165,7 +166,10 @@ fun AListConfigScreen(vm: AListConfigViewModel = viewModel()) {
.padding(end = 8.dp),
"HTTP",
value = httpPort,
onValueChange = { httpPort = it })
onValueChange = {
httpPort = it

})

PortEdit(
Modifier
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package com.github.jing332.alistandroid.ui.nav.config

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.github.jing332.alistandroid.model.alist.AListConfig
import com.github.jing332.alistandroid.model.alist.AListConfigManager
import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.launch

class AListConfigViewModel : ViewModel() {
var config by mutableStateOf(AListConfig())

fun init() {
viewModelScope.launch {
AListConfigManager.flowConfig().conflate().collect {
println("collect ${it.scheme}")
// context.toast(it.scheme.httpPort.toString())
config = it
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
package com.github.jing332.alistandroid.ui.nav.settings

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowCircleUp
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import com.github.jing332.alistandroid.config.AppConfig

@Composable
fun SettingsScreen() {
Column(Modifier.statusBarsPadding()) {
var checkUpdate by remember { AppConfig.isAutoCheckUpdate }
PreferenceSwitch(
title = { Text("自动检查更新") },
subTitle = { Text("打开程序主界面时从Github检查更新") },
checked = checkUpdate,
onCheckedChange = { checkUpdate = it },
icon = {
Icon(Icons.Default.ArrowCircleUp, contentDescription = null)
}
)

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package com.github.jing332.alistandroid.ui.nav.settings

import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material.ripple.rememberRipple
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Switch
import androidx.compose.material3.minimumInteractiveComponentSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
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.draw.clip
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import com.github.jing332.text_searcher.ui.widgets.LabelSlider

@Composable
internal fun PreferenceSwitch(
modifier: Modifier = Modifier,
title: @Composable ColumnScope.() -> Unit,
subTitle: @Composable ColumnScope.() -> Unit,
icon: @Composable () -> Unit = {},

checked: Boolean,
onCheckedChange: (Boolean) -> Unit
) {
Row(modifier = modifier
.minimumInteractiveComponentSize()
.clip(MaterialTheme.shapes.extraSmall)
.clickable(
interactionSource = remember { MutableInteractionSource() },
indication = rememberRipple()
) {
onCheckedChange(!checked)
}
.padding(8.dp)
) {
Column(Modifier.align(Alignment.CenterVertically)) {
icon();
}

Column(
Modifier
.weight(1f)
.align(Alignment.CenterVertically)
.padding(start = 8.dp)
) {
CompositionLocalProvider(LocalTextStyle provides MaterialTheme.typography.titleMedium) {
title()
}

CompositionLocalProvider(LocalTextStyle provides MaterialTheme.typography.titleSmall) {
subTitle()
}
}
Switch(checked = checked, onCheckedChange = onCheckedChange)
}
}

@Composable
internal fun BasePreferenceWidget(
modifier: Modifier = Modifier,
onClick: () -> Unit,
title: @Composable () -> Unit,
subTitle: @Composable () -> Unit,
icon: @Composable () -> Unit = {},
content: @Composable RowScope.() -> Unit,
) {
Row(modifier = modifier
.minimumInteractiveComponentSize()
.clip(MaterialTheme.shapes.extraSmall)
.clickable(
interactionSource = remember { MutableInteractionSource() },
indication = rememberRipple()
) {
onClick()
}
.padding(8.dp)
) {
icon()

Column(
Modifier
.weight(1f)
.align(Alignment.CenterVertically)
) {
CompositionLocalProvider(LocalTextStyle provides MaterialTheme.typography.titleLarge) {
title()
}

CompositionLocalProvider(LocalTextStyle provides MaterialTheme.typography.titleSmall) {
subTitle()
}
}

Row(Modifier.align(Alignment.CenterVertically)) {
content()
}
}
}

@Composable
internal fun PreferenceSlider(
title: @Composable () -> Unit,
subTitle: @Composable () -> Unit,
value: Float,
onValueChange: (Float) -> Unit,
valueRange: ClosedFloatingPointRange<Float> = 0f..1f,
steps: Int = 0,
label: @Composable (title: @Composable () -> Unit) -> Unit,
) {
PreferenceDialog(title = title, subTitle = subTitle, dialogContent = {
LabelSlider(
value = value,
onValueChange = onValueChange,
valueRange = valueRange,
steps = steps
) {
label(title)
}
},
endContent = {
label(title)
}
)
}

@Composable
internal fun PreferenceDialog(
modifier: Modifier = Modifier,
title: @Composable () -> Unit,
subTitle: @Composable () -> Unit,

dialogContent: @Composable ColumnScope.() -> Unit,
endContent: @Composable RowScope.() -> Unit = {},
) {
var showDialog by remember { mutableStateOf(false) }
if (showDialog) {
Dialog(onDismissRequest = { showDialog = false }) {
Surface(
modifier = Modifier.fillMaxWidth(),
tonalElevation = 4.dp,
shape = MaterialTheme.shapes.small,
) {
Column(Modifier.padding(8.dp)) {
title()
dialogContent()
}
}
}
}
BasePreferenceWidget(modifier, onClick = {
showDialog = true
}, title = title, subTitle = subTitle) {
endContent()
}
}

0 comments on commit 26a1563

Please sign in to comment.