Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS Dialog and Sheet Improvements #170

Merged
merged 3 commits into from
Sep 3, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.interop.LocalUIViewController
import androidx.compose.ui.window.DialogProperties
import com.mohamedrejeb.calf.core.InternalCalfApi

Expand All @@ -18,8 +19,11 @@ actual fun AdaptiveAlertDialog(
text: String,
properties: DialogProperties
) {
val alertDialogManager = remember {
val currentUIViewController = LocalUIViewController.current

val alertDialogManager = remember(currentUIViewController) {
AlertDialogManager(
parentUIViewController = currentUIViewController,
onConfirm = onConfirm,
onDismiss = onDismiss,
confirmText = confirmText,
Expand All @@ -40,11 +44,9 @@ actual fun AdaptiveAlertDialog(
alertDialogManager.properties = properties
}

LaunchedEffect(Unit) {
DisposableEffect(Unit) {
alertDialogManager.showAlertDialog()
}

DisposableEffect(Unit) {
onDispose {
alertDialogManager.dismiss()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ import platform.objc.sel_registerName
* @param title The title of the dialog.
* @param text The text of the dialog.
* @param properties The properties of the dialog.
* @param uiViewController The [UIViewController] that will present the dialog.
* @param parentUIViewController The [UIViewController] that will present the dialog.
* @constructor Creates an [AlertDialogManager] instance.
* @see [UIAlertController]
*/
@InternalCalfApi
class AlertDialogManager internal constructor(
private val parentUIViewController: UIViewController,
internal var onConfirm: () -> Unit,
internal var onDismiss: () -> Unit,
internal var confirmText: String,
Expand All @@ -43,11 +44,40 @@ class AlertDialogManager internal constructor(
*/
private var isAnimating = false

/**
* The ui view controller that is used to present the dialog.
*/
private val dialogUIViewController: UIViewController by lazy {
UIAlertController.alertControllerWithTitle(
title = title,
message = text,
preferredStyle = UIAlertControllerStyleAlert
).apply {
val confirmAction = UIAlertAction.actionWithTitle(
title = confirmText,
style = UIAlertActionStyleDefault,
handler = {
onConfirm()
}
)
addAction(confirmAction)

val cancelAction = UIAlertAction.actionWithTitle(
title = dismissText,
style = UIAlertActionStyleDestructive,
handler = {
onDismiss()
}
)
addAction(cancelAction)
}
}

/**
* Lambda that dismisses the dialog.
*/
private val onDismissLambda: (() -> Unit) = {
UIApplication.sharedApplication.keyWindow?.rootViewController?.dismissViewControllerAnimated(
dialogUIViewController.dismissViewControllerAnimated(
flag = true,
completion = {
isPresented = false
Expand Down Expand Up @@ -82,40 +112,16 @@ class AlertDialogManager internal constructor(
if (isPresented || isAnimating) return
isAnimating = true

val alertController = UIAlertController.alertControllerWithTitle(
title = title,
message = text,
preferredStyle = UIAlertControllerStyleAlert
)

val confirmAction = UIAlertAction.actionWithTitle(
title = confirmText,
style = UIAlertActionStyleDefault,
handler = {
onConfirm()
}
)
alertController.addAction(confirmAction)

val cancelAction = UIAlertAction.actionWithTitle(
title = dismissText,
style = UIAlertActionStyleDestructive,
handler = {
onDismiss()
}
)
alertController.addAction(cancelAction)

UIApplication.sharedApplication.keyWindow?.rootViewController?.presentViewController(
viewControllerToPresent = alertController,
parentUIViewController.presentViewController(
viewControllerToPresent = dialogUIViewController,
animated = true,
completion = {
isPresented = true
isAnimating = false

if (properties.dismissOnClickOutside) {
alertController.view.superview?.setUserInteractionEnabled(true)
alertController.view.superview?.addGestureRecognizer(
dialogUIViewController.view.superview?.setUserInteractionEnabled(true)
dialogUIViewController.view.superview?.addGestureRecognizer(
UITapGestureRecognizer(
target = this,
action = dismissPointer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ actual fun AdaptiveBottomSheet(
onDismiss = {
onDismissRequest()
},
confirmValueChange = adaptiveSheetState.confirmValueChange,
content = {
val sheetCompositionLocalContext = currentCompositionLocalContext

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.mohamedrejeb.calf.ui.sheet

import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.SheetValue
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.window.ComposeUIViewController
Expand All @@ -21,11 +23,13 @@ import platform.darwin.NSObject
* @param onDismiss The callback that is called when the bottom sheet is dismissed.
* @param content The Compose content of the bottom sheet.
*/
@OptIn(ExperimentalMaterial3Api::class)
internal class BottomSheetManager(
private val parentUIViewController: UIViewController,
private var isDark: Boolean,
private var containerColor: Color,
private var onDismiss: () -> Unit,
private val confirmValueChange: (SheetValue) -> Boolean,
private val content: @Composable () -> Unit
) {
private var isInitialized = false
Expand All @@ -48,7 +52,8 @@ internal class BottomSheetManager(
onDismiss = {
isPresented = false
onDismiss()
}
},
confirmValueChange = confirmValueChange,
)
}

Expand Down Expand Up @@ -133,12 +138,14 @@ internal class BottomSheetManager(
}
}

@OptIn(ExperimentalMaterial3Api::class)
class BottomSheetControllerDelegate(
private val onDismiss: () -> Unit
private val onDismiss: () -> Unit,
private val confirmValueChange: (SheetValue) -> Boolean,
) : NSObject(), UIAdaptivePresentationControllerDelegateProtocol {

override fun presentationControllerShouldDismiss(presentationController: UIPresentationController): Boolean {
return true
return confirmValueChange(SheetValue.Hidden)
}

override fun presentationControllerDidDismiss(presentationController: UIPresentationController) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ actual constructor(
internal val skipPartiallyExpanded: Boolean,
density: Density,
initialValue: SheetValue,
confirmValueChange: (SheetValue) -> Boolean,
internal val confirmValueChange: (SheetValue) -> Boolean,
skipHiddenState: Boolean,
) {
init {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,56 @@
package com.mohamedrejeb.calf.sample.screens

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.ime
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.systemBars
import androidx.compose.foundation.layout.windowInsetsPadding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.material.icons.filled.ArrowBackIosNew
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.SheetValue
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
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
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.mohamedrejeb.calf.ui.dialog.AdaptiveAlertDialog
import com.mohamedrejeb.calf.ui.sheet.AdaptiveBottomSheet
import com.mohamedrejeb.calf.ui.sheet.rememberAdaptiveSheetState
import com.mohamedrejeb.calf.ui.toggle.AdaptiveSwitch
import kotlinx.coroutines.launch

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun BottomSheetScreen(
navigateBack: () -> Unit
) {
var allowHide by remember { mutableStateOf(true) }
val scope = rememberCoroutineScope()
val sheetState = rememberAdaptiveSheetState()
val sheetState = rememberAdaptiveSheetState(
confirmValueChange = {
it != SheetValue.Hidden || allowHide
}
)
var openBottomSheet by rememberSaveable { mutableStateOf(false) }

Box(
Expand Down Expand Up @@ -62,6 +91,8 @@ fun BottomSheetScreen(
},
adaptiveSheetState = sheetState,
) {
var showDialog by remember { mutableStateOf(false) }

LazyColumn(
modifier = Modifier
.background(color = MaterialTheme.colorScheme.surface)
Expand All @@ -82,6 +113,45 @@ fun BottomSheetScreen(
}
}

item {
Row(
verticalAlignment = Alignment.CenterVertically,
) {
AdaptiveSwitch(
checked = allowHide,
onCheckedChange = {
allowHide = it
}
)
Text("Allow swipe to hide")
}
}

item {
Button(
onClick = {
showDialog = true
},
) {
Text("Show Alert Dialog")
}

if (showDialog) {
AdaptiveAlertDialog(
onConfirm = {
showDialog = false
},
onDismiss = {
showDialog = false
},
confirmText = "Ok",
dismissText = "Cancel",
title = "Alert Dialog",
text = "This is a native alert dialog from Calf",
)
}
}

items(100) {
Text(
text = "Item $it",
Expand Down