Skip to content

Commit

Permalink
Merge pull request #298 from PatilShreyas/feature/confirmation-dialogs
Browse files Browse the repository at this point in the history
[ComposeApp] Show confirmation dialogs in the app
  • Loading branch information
PatilShreyas authored Nov 1, 2021
2 parents bae72db + 70e0fe0 commit 09d06fd
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,23 @@

package dev.shreyaspatil.noty.composeapp.component.dialog

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.runtime.Composable
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.graphics.Color
Expand Down Expand Up @@ -87,3 +96,82 @@ fun FailureDialog(failureMessage: String, onDismissed: () -> Unit = {}) {
}
}
}

@Composable
fun ConfirmationDialog(
title: String,
message: String,
onConfirmedYes: () -> Unit,
onConfirmedNo: () -> Unit,
onDismissed: () -> Unit
) {
var isDismissed by remember { mutableStateOf(false) }

if (!isDismissed) {
Dialog(onDismissRequest = {}) {
Surface {
Column(
horizontalAlignment = Alignment.Start,
modifier = Modifier.padding(
top = 16.dp,
bottom = 8.dp,
start = 16.dp,
end = 16.dp
)
) {
Text(
text = title,
color = MaterialTheme.colors.onSurface,
style = MaterialTheme.typography.body1,
fontWeight = FontWeight.Bold
)

Text(
text = message,
color = MaterialTheme.colors.onSurface,
style = MaterialTheme.typography.body2,
fontWeight = FontWeight.Normal,
modifier = Modifier.padding(vertical = 8.dp)
)

Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.End,
verticalAlignment = Alignment.CenterVertically
) {
TextButton(
onClick = {
onConfirmedYes()
isDismissed = true
},
modifier = Modifier
.padding(horizontal = 4.dp),

) {
Text(
style = typography.button,
color = MaterialTheme.colors.primary,
text = "Yes"
)
}
TextButton(
onClick = {
onConfirmedNo()
isDismissed = true
},
modifier = Modifier
.padding(horizontal = 4.dp),

) {
Text(
style = typography.button,
color = MaterialTheme.colors.primary,
text = "No"
)
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import dev.shreyaspatil.noty.composeapp.component.action.DeleteAction
import dev.shreyaspatil.noty.composeapp.component.action.ShareAction
import dev.shreyaspatil.noty.composeapp.component.action.ShareActionItem
import dev.shreyaspatil.noty.composeapp.component.action.ShareDropdown
import dev.shreyaspatil.noty.composeapp.component.dialog.ConfirmationDialog
import dev.shreyaspatil.noty.composeapp.component.dialog.FailureDialog
import dev.shreyaspatil.noty.composeapp.component.text.NoteField
import dev.shreyaspatil.noty.composeapp.component.text.NoteTitleField
Expand All @@ -79,9 +80,7 @@ fun NoteDetailsScreen(
viewModel: NoteDetailViewModel
) {

val focusRequester = remember {
FocusRequester()
}
val focusRequester = remember { FocusRequester() }
val context = LocalContext.current

val updateState = viewModel.updateNoteState.collectAsState(initial = null)
Expand All @@ -93,6 +92,17 @@ fun NoteDetailsScreen(
var titleText by remember { mutableStateOf(note.title) }
var noteText by remember { mutableStateOf(note.note) }
var captureNoteImageRequestKey: Int? by remember { mutableStateOf(null) }
var showDeleteNoteConfirmation by remember { mutableStateOf(false) }

if (showDeleteNoteConfirmation) {
ConfirmationDialog(
title = "Delete?",
message = "Sure want to delete this note?",
onConfirmedYes = { viewModel.deleteNote() },
onConfirmedNo = { showDeleteNoteConfirmation = false },
onDismissed = { showDeleteNoteConfirmation = false }
)
}

Scaffold(
modifier = Modifier
Expand Down Expand Up @@ -125,15 +135,11 @@ fun NoteDetailsScreen(
elevation = 0.dp,
actions = {
var dropdownExpanded by remember { mutableStateOf(false) }
DeleteAction(onClick = { viewModel.deleteNote() })
ShareAction(onClick = {
dropdownExpanded = true
})
DeleteAction(onClick = { showDeleteNoteConfirmation = true })
ShareAction(onClick = { dropdownExpanded = true })
ShareDropdown(
expanded = dropdownExpanded,
onDismissRequest = {
dropdownExpanded = false
},
onDismissRequest = { dropdownExpanded = false },
shareActions = listOf(
ShareActionItem(
label = "Text",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
Expand All @@ -48,6 +49,7 @@ import dev.shreyaspatil.noty.composeapp.component.NotesList
import dev.shreyaspatil.noty.composeapp.component.action.AboutAction
import dev.shreyaspatil.noty.composeapp.component.action.LogoutAction
import dev.shreyaspatil.noty.composeapp.component.action.ThemeSwitchAction
import dev.shreyaspatil.noty.composeapp.component.dialog.ConfirmationDialog
import dev.shreyaspatil.noty.composeapp.component.dialog.FailureDialog
import dev.shreyaspatil.noty.composeapp.navigation.NOTY_NAV_HOST_ROUTE
import dev.shreyaspatil.noty.composeapp.ui.Screen
Expand All @@ -73,6 +75,20 @@ fun NotesScreen(navController: NavHostController, viewModel: NotesViewModel) {

val isInDarkMode = isSystemInDarkTheme()

var showLogoutConfirmationDialog by remember { mutableStateOf(false) }

if (showLogoutConfirmationDialog) {
ConfirmationDialog(
title = "Logout?",
message = "Sure want to logout?",
onConfirmedYes = {
scope.launch { viewModel.clearUserSession() }
},
onConfirmedNo = { showLogoutConfirmationDialog = false },
onDismissed = { showLogoutConfirmationDialog = false }
)
}

Scaffold(
topBar = {
TopAppBar(
Expand All @@ -94,11 +110,7 @@ fun NotesScreen(navController: NavHostController, viewModel: NotesViewModel) {
Screen.About.route
)
}
LogoutAction(
onLogout = {
scope.launch { viewModel.clearUserSession() }
}
)
LogoutAction(onLogout = { showLogoutConfirmationDialog = true })
}
)
},
Expand Down

0 comments on commit 09d06fd

Please sign in to comment.