-
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.
Merge pull request #31 from opatry/add-task-list-button
Add task list button
- Loading branch information
Showing
7 changed files
with
196 additions
and
74 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
108 changes: 108 additions & 0 deletions
108
tasks-app-shared/src/commonMain/kotlin/net/opatry/tasks/app/ui/component/EditTextDialog.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,108 @@ | ||
/* | ||
* Copyright (c) 2024 Olivier Patry | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining | ||
* a copy of this software and associated documentation files (the "Software"), | ||
* to deal in the Software without restriction, including without limitation | ||
* the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
* and/or sell copies of the Software, and to permit persons to whom the Software | ||
* is furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE | ||
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package net.opatry.tasks.app.ui.component | ||
|
||
import androidx.compose.animation.AnimatedVisibility | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material3.AlertDialogDefaults | ||
import androidx.compose.material3.Button | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.OutlinedTextField | ||
import androidx.compose.material3.Surface | ||
import androidx.compose.material3.Text | ||
import androidx.compose.material3.TextButton | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.derivedStateOf | ||
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.unit.dp | ||
import androidx.compose.ui.window.Dialog | ||
import androidx.compose.ui.window.DialogProperties | ||
|
||
@Composable | ||
fun EditTextDialog( | ||
onDismissRequest: () -> Unit, | ||
validateLabel: String, | ||
onValidate: (String) -> Unit, | ||
dialogTitle: String? = null, | ||
initialText: String = "", | ||
allowBlank: Boolean = true, | ||
) { | ||
Dialog( | ||
onDismissRequest = onDismissRequest, | ||
properties = DialogProperties(dismissOnBackPress = false, dismissOnClickOutside = false) | ||
) { | ||
var newTitle by remember { mutableStateOf(initialText) } | ||
val hasError by remember(newTitle, allowBlank) { | ||
derivedStateOf { | ||
!allowBlank && newTitle.isBlank() | ||
} | ||
} | ||
|
||
Surface( | ||
shape = MaterialTheme.shapes.large, | ||
tonalElevation = AlertDialogDefaults.TonalElevation | ||
) { | ||
Column(Modifier.padding(24.dp), verticalArrangement = Arrangement.spacedBy(16.dp)) { | ||
if (dialogTitle != null) { | ||
Text(dialogTitle, style = MaterialTheme.typography.titleLarge) | ||
} | ||
OutlinedTextField( | ||
newTitle, | ||
onValueChange = { newTitle = it }, | ||
maxLines = 1, | ||
supportingText = if (allowBlank) null else { | ||
{ | ||
AnimatedVisibility(visible = hasError) { | ||
Text("Text cannot be empty") | ||
} | ||
} | ||
}, | ||
isError = hasError, | ||
) | ||
Row( | ||
Modifier.align(Alignment.End), | ||
verticalAlignment = Alignment.CenterVertically, | ||
horizontalArrangement = Arrangement.spacedBy(8.dp) | ||
) { | ||
TextButton(onClick = onDismissRequest) { | ||
Text("Cancel") | ||
} | ||
Button( | ||
onClick = { onValidate(newTitle) }, | ||
enabled = allowBlank || !hasError | ||
) { | ||
Text(validateLabel) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.