Skip to content

Commit

Permalink
Fix sheet dismiss issue
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedRejeb committed Aug 7, 2024
1 parent aa945b2 commit a721534
Showing 1 changed file with 27 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import platform.darwin.NSObject
* @param content The Compose content of the bottom sheet.
*/
internal class BottomSheetManager(
private val parentUIViewController: UIViewController,
dark: Boolean,
private val onDismiss: () -> Unit,
private val content: @Composable () -> Unit
Expand All @@ -35,39 +36,37 @@ internal class BottomSheetManager(
*/
private var isAnimating = false

private var isDark = dark

/**
* The ui view controller that is used to present the bottom sheet.
* The presentation controller delegate that is used to detect when the bottom sheet is dismissed.
*/
private var bottomSheetUIViewController: UIViewController? = null

private var isDark = dark
private val presentationControllerDelegate by lazy {
BottomSheetControllerDelegate(
onDismiss = {
isPresented = false
onDismiss()
}
)
}

private fun initBottomSheetUIViewController(): UIViewController {
val uiViewController = ComposeUIViewController(content).apply {
/**
* The ui view controller that is used to present the bottom sheet.
*/
private val bottomSheetUIViewController: UIViewController by lazy {
ComposeUIViewController(content).apply {
modalPresentationStyle = UIModalPresentationPageSheet
modalTransitionStyle = UIModalTransitionStyleCoverVertical
presentationController?.delegate = BottomSheetControllerDelegate(
onDismiss = {
isPresented = false
onDismiss()
disposeBottomSheetUIViewController()
}
)
presentationController?.delegate = presentationControllerDelegate
applyTheme(isDark)
}

bottomSheetUIViewController = uiViewController

return uiViewController
}

private fun disposeBottomSheetUIViewController() {
bottomSheetUIViewController = null
}

fun applyTheme(dark: Boolean) {
isDark = dark
bottomSheetUIViewController?.applyTheme(dark)

if (isPresented)
bottomSheetUIViewController.applyTheme(dark)
}

/**
Expand All @@ -76,12 +75,12 @@ internal class BottomSheetManager(
fun show(
skipPartiallyExpanded: Boolean,
) {
val uiViewController = initBottomSheetUIViewController()
applyTheme(isDark)

if (isPresented || isAnimating) return
isAnimating = true

uiViewController.sheetPresentationController?.setDetents(
bottomSheetUIViewController.sheetPresentationController?.setDetents(
if (skipPartiallyExpanded)
listOf(
UISheetPresentationControllerDetent.largeDetent()
Expand All @@ -92,10 +91,10 @@ internal class BottomSheetManager(
UISheetPresentationControllerDetent.mediumDetent(),
)
)
uiViewController.sheetPresentationController?.prefersGrabberVisible = true
bottomSheetUIViewController.sheetPresentationController?.prefersGrabberVisible = true

UIApplication.sharedApplication.keyWindow?.rootViewController?.presentViewController(
viewControllerToPresent = uiViewController,
parentUIViewController.presentViewController(
viewControllerToPresent = bottomSheetUIViewController,
animated = true,
completion = {
isPresented = true
Expand All @@ -113,13 +112,12 @@ internal class BottomSheetManager(
if (!isPresented || isAnimating) return
isAnimating = true

bottomSheetUIViewController?.dismissViewControllerAnimated(
bottomSheetUIViewController.dismissViewControllerAnimated(
flag = true,
completion = {
isPresented = false
isAnimating = false
completion?.invoke()
disposeBottomSheetUIViewController()
}
)
}
Expand Down

0 comments on commit a721534

Please sign in to comment.