@@ -4,8 +4,13 @@ import androidx.compose.foundation.layout.Column
44import androidx.compose.foundation.layout.Row
55import androidx.compose.foundation.layout.fillMaxWidth
66import androidx.compose.foundation.layout.padding
7+ import androidx.compose.foundation.layout.wrapContentHeight
8+ import androidx.compose.foundation.layout.wrapContentWidth
79import androidx.compose.material3.AlertDialog
10+ import androidx.compose.material3.DropdownMenuItem
811import androidx.compose.material3.ExperimentalMaterial3Api
12+ import androidx.compose.material3.ExposedDropdownMenuBox
13+ import androidx.compose.material3.ExposedDropdownMenuDefaults
914import androidx.compose.material3.MaterialTheme
1015import androidx.compose.material3.Surface
1116import androidx.compose.material3.Switch
@@ -23,7 +28,8 @@ import androidx.compose.ui.Alignment
2328import androidx.compose.ui.Modifier
2429import androidx.compose.ui.platform.LocalLifecycleOwner
2530import androidx.compose.ui.res.stringResource
26- import androidx.compose.ui.unit.Dp
31+ import androidx.compose.ui.text.TextStyle
32+ import androidx.compose.ui.unit.dp
2733import androidx.compose.ui.unit.sp
2834import androidx.lifecycle.Lifecycle
2935import androidx.lifecycle.LifecycleEventObserver
@@ -49,7 +55,7 @@ fun OnLifecycleEvent(onEvent: (owner: LifecycleOwner, event: Lifecycle.Event) ->
4955
5056@Composable
5157fun HeaderText (text : String ) {
52- Row (modifier = Modifier .padding(top = Dp ( 20f ) , bottom = Dp ( 12f ) )) {
58+ Row (modifier = Modifier .padding(top = 20 .dp , bottom = 12 .dp )) {
5359 Text (text = text, style = MaterialTheme .typography.labelMedium, color = MaterialTheme .colorScheme.onSurfaceVariant)
5460 }
5561}
@@ -64,25 +70,117 @@ fun BooleanPropertyView(
6470 onClick : ((Boolean ) -> Unit )? = null,
6571) {
6672 if (toggled == null ) {
67- Column (modifier = Modifier .padding(top = Dp ( 12f ) , bottom = Dp ( 12f ) )) {
68- Text (text = label, fontSize = 18 .sp, modifier = Modifier .padding(bottom = Dp ( 4f ) ))
73+ Column (modifier = Modifier .padding(top = 12 .dp , bottom = 12 .dp )) {
74+ Text (text = label, fontSize = 18 .sp, modifier = Modifier .padding(bottom = 4 .dp ))
6975 Text (text = stringResource(R .string.unknown), fontSize = 14 .sp, color = MaterialTheme .colorScheme.outline)
7076 }
7177 return
7278 }
7379 if (onClick != null ) {
74- Row (verticalAlignment = Alignment .CenterVertically , modifier = Modifier .padding(top = Dp ( 12f ) , bottom = Dp ( 12f ) )) {
80+ Row (verticalAlignment = Alignment .CenterVertically , modifier = Modifier .padding(top = 12 .dp , bottom = 12 .dp )) {
7581 Text (text = label, modifier = Modifier .weight(1F ), fontSize = 18 .sp)
7682 Switch (checked = toggled, enabled = enabled, onCheckedChange = onClick)
7783 }
7884 } else {
79- Column (modifier = Modifier .padding(top = Dp ( 12f ) , bottom = Dp ( 12f ) )) {
80- Text (text = label, fontSize = 18 .sp, modifier = Modifier .padding(bottom = Dp ( 4f ) ))
85+ Column (modifier = Modifier .padding(top = 12 .dp , bottom = 12 .dp )) {
86+ Text (text = label, fontSize = 18 .sp, modifier = Modifier .padding(bottom = 4 .dp ))
8187 Text (text = if (toggled) { trueLabel } else { falseLabel }, fontSize = 14 .sp, color = MaterialTheme .colorScheme.outline)
8288 }
8389 }
8490}
8591
92+ @OptIn(ExperimentalMaterial3Api ::class )
93+ @Composable
94+ fun UserAgentPropertyView (label : String , value : String? , onUpdate : ((String ) -> Unit )? = null) {
95+ val labels = arrayOf(stringResource(R .string.default_), stringResource(R .string.lgu))
96+ val values = arrayOf(stringResource(R .string.ua_default), stringResource(R .string.ua_lgu))
97+
98+ var typedText by rememberSaveable { mutableStateOf(" " ) }
99+ var openTextEditDialog by rememberSaveable { mutableStateOf(false ) }
100+ var dropdownExpanded by rememberSaveable { mutableStateOf(false ) }
101+ var selectedIndex by rememberSaveable { mutableStateOf(if (values.contains(value)) values.indexOf(value) else 0 ) }
102+
103+ if (onUpdate != null ) {
104+ if (openTextEditDialog) {
105+ AlertDialog (
106+ onDismissRequest = {
107+ // Dismiss the dialog when the user clicks outside the dialog or on the back
108+ // button. If you want to disable that functionality, simply use an empty
109+ // onDismissRequest.
110+ openTextEditDialog = false
111+ },
112+ ) {
113+ Surface (
114+ modifier = Modifier
115+ .wrapContentWidth()
116+ .wrapContentHeight(),
117+ shape = MaterialTheme .shapes.large,
118+ ) {
119+ Column (modifier = Modifier .padding(all = 16 .dp)) {
120+ Text (text = stringResource(R .string.update_value), style = MaterialTheme .typography.titleLarge, modifier = Modifier .padding(bottom = 24 .dp))
121+ ExposedDropdownMenuBox (
122+ expanded = dropdownExpanded,
123+ onExpandedChange = { dropdownExpanded = ! dropdownExpanded },
124+ modifier = Modifier .padding(bottom = 8 .dp),
125+ ) {
126+ TextField (
127+ // The `menuAnchor` modifier must be passed to the text field for correctness.
128+ modifier = Modifier .menuAnchor().wrapContentWidth(),
129+ readOnly = true ,
130+ value = if (values[selectedIndex] == typedText) labels[selectedIndex] else " Custom" ,
131+ onValueChange = {},
132+ label = { Text (" Presets" ) },
133+ trailingIcon = { ExposedDropdownMenuDefaults .TrailingIcon (expanded = dropdownExpanded) },
134+ colors = ExposedDropdownMenuDefaults .textFieldColors(),
135+ )
136+ ExposedDropdownMenu (
137+ expanded = dropdownExpanded,
138+ onDismissRequest = { dropdownExpanded = false },
139+ ) {
140+ labels.forEachIndexed { i, label ->
141+ DropdownMenuItem (
142+ text = { Text (text = label) },
143+ onClick = {
144+ typedText = values[i]
145+ selectedIndex = i
146+ dropdownExpanded = false
147+ },
148+ contentPadding = ExposedDropdownMenuDefaults .ItemContentPadding ,
149+ )
150+ }
151+ }
152+ }
153+ TextField (textStyle = TextStyle (fontSize = 14 .sp), value = typedText, onValueChange = { typedText = it })
154+ Row (modifier = Modifier .align(Alignment .End )) {
155+ TextButton (
156+ onClick = {
157+ onUpdate(typedText)
158+ openTextEditDialog = false
159+ },
160+ ) {
161+ Text (stringResource(R .string.confirm))
162+ }
163+ TextButton (
164+ onClick = {
165+ openTextEditDialog = false
166+ },
167+ ) {
168+ Text (stringResource(R .string.dismiss))
169+ }
170+ }
171+ }
172+ }
173+ }
174+ }
175+ }
176+ ClickablePropertyView (label = label, value = value) {
177+ if (value != null ) {
178+ typedText = value
179+ openTextEditDialog = true
180+ }
181+ }
182+ }
183+
86184@OptIn(ExperimentalMaterial3Api ::class )
87185@Composable
88186fun StringPropertyView (label : String , value : String? , onUpdate : ((String ) -> Unit )? = null) {
@@ -136,22 +234,22 @@ fun StringPropertyView(label: String, value: String?, onUpdate: ((String) -> Uni
136234@Composable
137235fun ClickablePropertyView (label : String , value : String? , onClick : (() -> Unit )? = null) {
138236 if (value == null ) {
139- Column (modifier = Modifier .padding(top = Dp ( 12f ) , bottom = Dp ( 12f ) )) {
140- Text (text = label, modifier = Modifier .padding(bottom = Dp ( 4f ) ))
237+ Column (modifier = Modifier .padding(top = 12 .dp , bottom = 12 .dp )) {
238+ Text (text = label, modifier = Modifier .padding(bottom = 4 .dp ))
141239 Text (text = stringResource(R .string.unknown), color = MaterialTheme .colorScheme.outline, fontSize = 14f .sp)
142240 }
143241 return
144242 }
145243 if (onClick != null ) {
146244 Surface (onClick = onClick, modifier = Modifier .fillMaxWidth()) {
147- Column (modifier = Modifier .padding(top = Dp ( 12f ) , bottom = Dp ( 12f ) )) {
148- Text (text = label, modifier = Modifier .padding(bottom = Dp ( 4f ) ), fontSize = 18 .sp)
245+ Column (modifier = Modifier .padding(top = 12 .dp , bottom = 12 .dp )) {
246+ Text (text = label, modifier = Modifier .padding(bottom = 4 .dp ), fontSize = 18 .sp)
149247 Text (text = value, color = MaterialTheme .colorScheme.outline, fontSize = 14f .sp)
150248 }
151249 }
152250 } else {
153- Column (modifier = Modifier .padding(top = Dp ( 12f ) , bottom = Dp ( 12f ) )) {
154- Text (text = label, modifier = Modifier .padding(bottom = Dp ( 4f ) ))
251+ Column (modifier = Modifier .padding(top = 12 .dp , bottom = 12 .dp )) {
252+ Text (text = label, modifier = Modifier .padding(bottom = 4 .dp ))
155253 Text (text = value, color = MaterialTheme .colorScheme.outline, fontSize = 14f .sp)
156254 }
157255 }
0 commit comments