Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 53 additions & 4 deletions app/src/main/java/com/example/printer/ui/ConfigurationField.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.example.printer.ui

import android.net.Uri
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
Expand All @@ -10,6 +13,8 @@ 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.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
Expand Down Expand Up @@ -157,6 +162,15 @@ fun ConfigurationField(
}

FieldType.FILE -> {
val filePickerLauncher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.GetContent()
) { uri: Uri? ->
uri?.let {
val filePath = it.toString()
onValueChange(filePath)
}
}

OutlinedTextField(
value = currentValue?.toString() ?: field.defaultValue?.toString() ?: "",
onValueChange = { onValueChange(it) },
Expand All @@ -166,7 +180,9 @@ fun ConfigurationField(
},
trailingIcon = {
TextButton(
onClick = { /* TODO: File picker */ }
onClick = {
filePickerLauncher.launch("*/*")
}
) {
Text("Browse")
}
Expand All @@ -175,14 +191,31 @@ fun ConfigurationField(
}

FieldType.COLOR -> {
val colorString = currentValue?.toString() ?: field.defaultValue?.toString() ?: "#000000"
val parsedColor = remember(colorString) {
try {
parseHexColor(colorString)
} catch (e: Exception) {
Color.Gray
}
}

Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalAlignment = Alignment.CenterVertically
) {
OutlinedTextField(
value = currentValue?.toString() ?: field.defaultValue?.toString() ?: "#000000",
onValueChange = { onValueChange(it) },
value = colorString,
onValueChange = {
val trimmed = it.trim()
if (trimmed.startsWith("#") || trimmed.matches(Regex("^[0-9A-Fa-f]{6}$"))) {
val formatted = if (trimmed.startsWith("#")) trimmed else "#$trimmed"
onValueChange(formatted)
} else if (trimmed.isEmpty()) {
onValueChange("#000000")
}
},
modifier = Modifier.weight(1f),
placeholder = {
Text("#RRGGBB")
Expand All @@ -192,12 +225,28 @@ fun ConfigurationField(
modifier = Modifier
.size(40.dp)
.background(
color = androidx.compose.ui.graphics.Color.Gray, // TODO: Parse color
color = parsedColor,
shape = RoundedCornerShape(8.dp)
)
)
}
}
}
}
}

/**
* Parses a hex color string (e.g., "#RRGGBB" or "RRGGBB") into a Color object
*/
private fun parseHexColor(hex: String): Color {
val cleanHex = hex.trim().removePrefix("#")
if (cleanHex.length != 6) {
throw IllegalArgumentException("Invalid hex color format: $hex")
}

val r = cleanHex.substring(0, 2).toInt(16)
val g = cleanHex.substring(2, 4).toInt(16)
val b = cleanHex.substring(4, 6).toInt(16)

return Color(r, g, b)
}
Loading