-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f92826a
commit f759db9
Showing
4 changed files
with
172 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...in/kotlin/com/sunnychung/application/multiplatform/hellohttp/ux/CustomHttpMethodDialog.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.sunnychung.application.multiplatform.hellohttp.ux | ||
|
||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.defaultMinSize | ||
import androidx.compose.foundation.layout.padding | ||
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.setValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.focus.FocusRequester | ||
import androidx.compose.ui.focus.focusRequester | ||
import androidx.compose.ui.input.key.Key | ||
import androidx.compose.ui.input.key.KeyEventType | ||
import androidx.compose.ui.input.key.key | ||
import androidx.compose.ui.input.key.onPreviewKeyEvent | ||
import androidx.compose.ui.input.key.type | ||
import androidx.compose.ui.unit.dp | ||
import com.sunnychung.application.multiplatform.hellohttp.AppContext | ||
import com.sunnychung.application.multiplatform.hellohttp.model.isValidHttpMethod | ||
import com.sunnychung.application.multiplatform.hellohttp.ux.local.LocalColor | ||
|
||
@Composable | ||
fun CustomHttpMethodDialog(isEnabled: Boolean, onDismiss: () -> Unit, onConfirm: (String) -> Unit) { | ||
val colours = LocalColor.current | ||
val errorMessageVM = AppContext.ErrorMessagePromptViewModel | ||
var text by remember { mutableStateOf("") } | ||
|
||
fun onDone() { | ||
if (!text.isValidHttpMethod()) { | ||
errorMessageVM.showErrorMessage("Provided input is not a valid HTTP method.") | ||
} else { | ||
onConfirm(text) | ||
} | ||
} | ||
|
||
MainWindowDialog( | ||
key = "CustomHttpMethodDialog", | ||
isEnabled = isEnabled, | ||
onDismiss = onDismiss) { | ||
|
||
val focusRequester = remember { FocusRequester() } | ||
|
||
Column(horizontalAlignment = Alignment.CenterHorizontally) { | ||
AppTextFieldWithPlaceholder( | ||
value = text, | ||
onValueChange = { | ||
if (it.isEmpty() || it.isValidHttpMethod()) { | ||
text = it | ||
} | ||
}, | ||
placeholder = { | ||
AppText( | ||
text = "HTTP method name", | ||
color = colours.placeholder | ||
) | ||
}, | ||
singleLine = true, | ||
modifier = Modifier.focusRequester(focusRequester) | ||
.onPreviewKeyEvent { | ||
if (it.key == Key.Enter && it.type == KeyEventType.KeyDown) { | ||
onDone() | ||
true | ||
} else { | ||
false | ||
} | ||
} | ||
.defaultMinSize(minWidth = 200.dp), | ||
) | ||
AppTextButton( | ||
text = "Done", | ||
onClick = { onDone() }, | ||
modifier = Modifier.padding(top = 4.dp), | ||
) | ||
} | ||
|
||
LaunchedEffect(Unit) { | ||
focusRequester.requestFocus() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters