package com.vgmlr.jamb
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.SystemBarStyle
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ExposedDropdownMenuBox
import androidx.compose.material3.ExposedDropdownMenuDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MenuAnchorType
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.OutlinedTextFieldDefaults
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import kotlinx.coroutines.launch
import androidx.compose.foundation.layout.RowScope
class JambSettings : ComponentActivity() {
private lateinit var prefs: JambPrefs
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge(statusBarStyle = SystemBarStyle.dark(android.graphics.Color.TRANSPARENT))
prefs = JambPrefs(this)
setContent { JambTheme { SettingsScreen() } }
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun SettingsScreen() {
val scope = rememberCoroutineScope()
var awake by remember { mutableStateOf(JambPrefs.AWAKE_NONE) }
LaunchedEffect(Unit) { awake = prefs.getKeepAwake() }
Scaffold(
topBar = {
TopAppBar(
title = { Text("Settings", fontSize = 18.sp, color = JambColors.Title) },
navigationIcon = {
IconButton(onClick = { finish() }) {
Icon(Icons.AutoMirrored.Filled.ArrowBack, "Back", tint = JambColors.Title)
}
},
colors = TopAppBarDefaults.topAppBarColors(containerColor = JambColors.Primary)
)
},
contentWindowInsets = WindowInsets(0, 0, 0, 0),
containerColor = JambColors.Background
) { padding ->
Column(
modifier = Modifier
.fillMaxSize()
.padding(padding)
.background(JambColors.Background)
.padding(16.dp)
) {
Row(
modifier = Modifier.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically
) {
Box(modifier = Modifier.width(140.dp)) {
Text(
"Keep Awake",
color = JambColors.Title.copy(alpha = 0.6f),
fontSize = 16.sp,
fontWeight = FontWeight.Bold
)
}
AwakeDropdown(awake) { picked ->
awake = picked
scope.launch { prefs.setKeepAwake(picked) }
}
}
}
}
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun RowScope.AwakeDropdown(value: String, onPick: (String) -> Unit) {
val options = listOf(
JambPrefs.AWAKE_NONE to "None",
JambPrefs.AWAKE_CHARGING to "When Charging",
JambPrefs.AWAKE_ALWAYS to "Always"
)
var expanded by remember { mutableStateOf(false) }
val label = options.firstOrNull { it.first == value }?.second ?: "None"
ExposedDropdownMenuBox(
expanded = expanded,
onExpandedChange = { expanded = it },
modifier = Modifier.weight(1f)
) {
OutlinedTextField(
value = label,
onValueChange = {},
readOnly = true,
singleLine = true,
trailingIcon = { ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded) },
modifier = Modifier.menuAnchor(MenuAnchorType.PrimaryNotEditable).fillMaxWidth(),
colors = OutlinedTextFieldDefaults.colors(
focusedTextColor = JambColors.Title,
unfocusedTextColor = JambColors.Title,
focusedBorderColor = JambColors.Title.copy(alpha = 0.5f),
unfocusedBorderColor = JambColors.Title.copy(alpha = 0.2f),
cursorColor = JambColors.Title
)
)
ExposedDropdownMenu(expanded = expanded, onDismissRequest = { expanded = false }) {
options.forEach { (v, text) ->
DropdownMenuItem(
text = { Text(text) },
onClick = { onPick(v); expanded = false }
)
}
}
}
}
}