Skip to content

Commit

Permalink
SettingPage: 设置游戏下载源
Browse files Browse the repository at this point in the history
  • Loading branch information
purofle committed Dec 31, 2023
1 parent 96cd21c commit e9e05ca
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.github.purofle.nmsl.utils.os.OS
import kotlin.io.path.createDirectories
import kotlin.io.path.readText
import kotlin.io.path.writeText
import kotlin.reflect.KProperty

object Config {
private val configPath by lazy {
Expand All @@ -18,4 +19,12 @@ object Config {
}

fun readConfig(): NmslConfig = configPath.readText().toJsonObject()

operator fun getValue(nothing: Nothing?, property: KProperty<*>): NmslConfig {
return readConfig()
}

operator fun setValue(nothing: Nothing?, property: KProperty<*>, nmslConfig: NmslConfig) {
createConfig(nmslConfig)
}
}
86 changes: 85 additions & 1 deletion src/main/kotlin/com/github/purofle/nmsl/pages/SettingPage.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,93 @@
package com.github.purofle.nmsl.pages

import androidx.compose.runtime.Composable
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowDropDown
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.DpOffset
import androidx.compose.ui.unit.dp
import com.github.purofle.nmsl.config.Config
import com.github.purofle.nmsl.config.LauncherConfig
import com.github.purofle.nmsl.download.DownloadProvider

class SettingPage : Page {
@Composable
override fun render() {

var config by Config
var gameSource by remember { mutableStateOf("${config.launcherConfig.provider}") }

Scaffold {
LazyColumn {
item {
SettingSelectItem(
title = "游戏资源下载源",
items = DownloadProvider.providers.keys.map {
SettingItem("${it}") {
gameSource = "${it}"
config = config.copy(launcherConfig = LauncherConfig(provider = it))
}
},
defaultItem = gameSource
)
}
}
}
}
}

data class SettingItem(
val title: String,
val onClick: () -> Unit
)

@Composable
fun SettingSelectItem(
title: String,
items: List<SettingItem>,
defaultItem: String
) {

var expanded by remember { mutableStateOf(false) }

Column(modifier = Modifier.padding(12.dp)) {
Text(title)
Spacer(modifier = Modifier.height(6.dp))
Row(
modifier = Modifier
.height(32.dp)
.background(MaterialTheme.colorScheme.secondaryContainer, RoundedCornerShape(2.dp))
.clickable { expanded = !expanded },
verticalAlignment = Alignment.CenterVertically
) {
Text(defaultItem, modifier = Modifier.padding(start = 12.dp))
Icon(
imageVector = Icons.Default.ArrowDropDown,
contentDescription = null,
modifier = Modifier.padding(start = 48.dp, end = 12.dp)
)
}
}
DropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false },
offset = DpOffset(12.dp, 0.dp)
) {
items.forEach {
DropdownMenuItem(
text = { Text(it.title) },
onClick = {
expanded = false
it.onClick()
}
)
}
}
}

0 comments on commit e9e05ca

Please sign in to comment.