shim/app/src/main/kotlin/com/vgmlr/shim/ShimSettings.kt (8.3 kb)
Modified: 02:29:52 66 026 (20 May 026) - 4 Days Ago
Download
package com.vgmlr.shim

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.enableEdgeToEdge
import androidx.activity.SystemBarStyle
import androidx.activity.compose.setContent
import androidx.core.graphics.toColorInt
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.foundation.rememberScrollState
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.foundation.border
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class ShimSettings : ComponentActivity() {
    private lateinit var themeManager: ShimThemeManager
    private val btnShape = RoundedCornerShape(7.dp)
    private val btnColors = @Composable { ButtonDefaults.buttonColors(containerColor = Color.Gray) }
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge(statusBarStyle = SystemBarStyle.dark(android.graphics.Color.TRANSPARENT))
        themeManager = ShimThemeManager(this)

        setContent {
            ShimTheme(themeManager) {
                SettingsScreen()
            }
        }
    }

    @OptIn(ExperimentalMaterial3Api::class)
    @Composable
    fun SettingsScreen() {
        var bgColor by remember { mutableStateOf(themeManager.hexBgColor) }
        var textColor by remember { mutableStateOf(themeManager.hexTextColor) }
        var fontSize by remember { mutableStateOf(themeManager.fontSize.floatValue.toString()) }
        var isMonospace by remember { mutableStateOf(themeManager.isMonospace.value) }
        var hashColor by remember { mutableStateOf(themeManager.hexHashColor) }
        var lineHeight by remember { mutableStateOf(themeManager.lineHeight.floatValue.toString()) }
        var lineSpacing by remember { mutableStateOf(themeManager.lineSpacing.floatValue.toString()) }

        var saveText by remember { mutableStateOf("Save") }
        val scope = rememberCoroutineScope()

        Scaffold(
            topBar = {
                TopAppBar(
                    title = { Text("Settings", fontSize = 18.sp, color = Color.White) },
                    navigationIcon = {
                        IconButton(onClick = { finish() }) {
                            Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = "Back", tint = Color.White)
                        }
                    },
                    
                    colors = TopAppBarDefaults.topAppBarColors(containerColor = Color(0xFF486860))
                )
            },
            contentWindowInsets = WindowInsets(0, 0, 0, 0),
            containerColor = themeManager.backgroundColor.value
        ) { padding ->
            Column(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(padding)
                    .imePadding()
                    .background(themeManager.backgroundColor.value)
                    .padding(16.dp)
                    .verticalScroll(rememberScrollState())
            ) {
                
                SettingItem("Ground Color", bgColor, true) { bgColor = it }
                SettingItem("Font Color", textColor, true) { textColor = it }
                SettingItem("Hash Tag Color", hashColor, true) { hashColor = it }
                Spacer(modifier = Modifier.height(10.dp))
                Row(
                    modifier = Modifier.fillMaxWidth().padding(vertical = 1.dp),
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Switch(
                        checked = isMonospace,
                        onCheckedChange = { isMonospace = it },
                        colors = SwitchDefaults.colors(
                            checkedThumbColor = Color.Gray,
                            checkedTrackColor = Color.Transparent,
                            checkedBorderColor = Color.Gray,
                            uncheckedThumbColor = Color.Gray,
                            uncheckedTrackColor = Color.Transparent,
                            uncheckedBorderColor = Color.Gray.copy(alpha = 0.5f)
                        )
                    )
                    Spacer(modifier = Modifier.width(12.dp))
                    Text("Monospace", color = themeManager.textColor.value.copy(alpha = 0.6f), fontSize = 17.sp)
                }
                SettingItem("Font Size", fontSize, false) { fontSize = it }
                SettingItem("Line Height", lineHeight, false) { lineHeight = it }
                SettingItem("Line Spacing", lineSpacing, false) { lineSpacing = it }

                Spacer(modifier = Modifier.height(24.dp))

                Button(
                    onClick = {
                        themeManager.saveTheme(
                            bgColor,
                            textColor,
                            hashColor,
                            isMonospace,
                            fontSize,
                            lineHeight,
                            lineSpacing
                        )
                        scope.launch {
                            saveText = "Saved"
                            delay(2000)
                            saveText = "Save"
                        }
                    },
                    modifier = Modifier.fillMaxWidth().height(48.dp),
                    colors = btnColors(),
                    shape = btnShape
                ) {
                    Text(
                        text = saveText,
                        color = Color.Black,
                        fontSize = 16.sp
                    )
                }
            }
        }
    }

    @Composable
    fun SettingItem(label: String, value: String, isColor: Boolean, onValueChange: (String) -> Unit) {
        val labelCol = themeManager.textColor.value.copy(alpha = 0.6f)
        val bCol = themeManager.textColor.value.copy(alpha = 0.2f)

        Column(modifier = Modifier.padding(bottom = 5.dp)) {
            Text(label, color = labelCol, fontSize = 12.sp, fontWeight = FontWeight.Bold, modifier = Modifier.padding(top = 8.dp, bottom = 2.dp))

            Row(verticalAlignment = Alignment.CenterVertically) {
                if (isColor) {
                    val previewColor = try { Color((if (value.startsWith("#")) value else "#$value").toColorInt()) } catch (_: Exception) { Color.Gray }
                    Box(
                        modifier = Modifier
                            .size(56.dp)
                            .background(previewColor, RoundedCornerShape(4.dp))
                            .border(1.dp, bCol, RoundedCornerShape(4.dp))
                    )
                    Spacer(Modifier.width(12.dp))
                }
                OutlinedTextField(
                    value = value,
                    onValueChange = onValueChange,
                    modifier = Modifier.weight(1f),
                    colors = OutlinedTextFieldDefaults.colors(
                        focusedTextColor = themeManager.textColor.value,
                        unfocusedTextColor = themeManager.textColor.value,
                        focusedBorderColor = themeManager.textColor.value.copy(alpha = 0.5f),
                        unfocusedBorderColor = themeManager.textColor.value.copy(alpha = 0.2f),
                        cursorColor = themeManager.textColor.value
                    ),
                    placeholder = {
                        Text(
                            when(label) {
                                "Font Size" -> "15.5"
                                "Line Height" -> "1.35"
                                "Line Spacing" -> "0.2"
                                else -> "#000000"
                            },
                            color = labelCol
                        )
                    },
                    singleLine = true,
                )
            }
        }
    }
}