Skip to content

Commit

Permalink
Merge pull request #736 from splendo/bugfix/735-action-sheet-ipad-fix
Browse files Browse the repository at this point in the history
Fix AlertPresenter crashing on iPad when displaying ACTION_LIST
  • Loading branch information
Daeda88 authored Nov 1, 2023
2 parents 63233a7 + 804d701 commit b402826
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions alerts/src/iosMain/kotlin/AlertPresenter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,26 @@ import platform.UIKit.UIAlertControllerStyle
import platform.UIKit.UIAlertControllerStyleActionSheet
import platform.UIKit.UIAlertControllerStyleAlert
import platform.UIKit.UIControlEventEditingChanged
import platform.UIKit.UIPopoverPresentationController
import platform.UIKit.UIPopoverPresentationControllerDelegateProtocol
import platform.UIKit.UITextField
import platform.UIKit.UIView
import platform.UIKit.UIViewController
import platform.UIKit.popoverPresentationController
import platform.darwin.NSObject
import platform.objc.sel_registerName

/**
* A [BaseAlertPresenter] for presenting an [Alert].
* @param alert The [Alert] being presented.
* @param parent The [UIViewController] to present the [Alert]
* @param delegateBuilder Method that creates a [UIPopoverPresentationControllerDelegateProtocol].
* This allows for presentation of [Alert.Style.ACTION_LIST] on iPad.
*/
actual class AlertPresenter(
private val alert: Alert,
private val parent: UIViewController,
private val delegateBuilder: (Alert) -> UIPopoverPresentationControllerDelegateProtocol,
) : BaseAlertPresenter(alert) {

/** Ref to alert's [UITextField] of type [Alert.Style.TEXT_INPUT] */
Expand Down Expand Up @@ -84,8 +92,24 @@ actual class AlertPresenter(
/**
* A [BaseAlertPresenter.Builder] for creating an [AlertPresenter]
* @param viewController The [UIViewController] to present any [AlertPresenter] built using this builder.
* @param delegateBuilder Method that creates a [UIPopoverPresentationControllerDelegateProtocol] for an [Alert].
* This allows for presentation of [Alert.Style.ACTION_LIST] on iPad.
*/
actual class Builder(private val viewController: UIViewController) : BaseAlertPresenter.Builder() {
actual class Builder(
private val viewController: UIViewController,
private val delegateBuilder: (Alert) -> UIPopoverPresentationControllerDelegateProtocol,
) : BaseAlertPresenter.Builder() {

/**
* Constructor that returns a [DefaultUIPopoverPresentationControllerDelegateProtocol] when a presented [AlertPresenter] requires a [UIPopoverPresentationControllerDelegateProtocol].
* @param viewController The [UIViewController] to present any [AlertPresenter] built using this builder.
*/
constructor(
viewController: UIViewController,
) : this(
viewController,
{ DefaultUIPopoverPresentationControllerDelegateProtocol(viewController.view) },
)

/**
* Creates an [AlertPresenter]
Expand All @@ -94,7 +118,15 @@ actual class AlertPresenter(
* @param coroutineScope The [CoroutineScope] managing the alert lifecycle.
* @return The created [AlertPresenter]
*/
actual override fun create(alert: Alert, coroutineScope: CoroutineScope) = AlertPresenter(alert, viewController)
actual override fun create(alert: Alert, coroutineScope: CoroutineScope) = AlertPresenter(alert, viewController, delegateBuilder)
}

class DefaultUIPopoverPresentationControllerDelegateProtocol(private val sourceView: UIView) : NSObject(), UIPopoverPresentationControllerDelegateProtocol {
override fun prepareForPopoverPresentation(popoverPresentationController: UIPopoverPresentationController) {
popoverPresentationController.sourceView = sourceView
popoverPresentationController.sourceRect = sourceView.bounds
popoverPresentationController.permittedArrowDirections = 0UL
}
}

override fun dismissAlert(animated: Boolean) {
Expand Down Expand Up @@ -143,6 +175,7 @@ actual class AlertPresenter(
}
}
}.run {
popoverPresentationController?.setDelegate(delegateBuilder(alert))
parent.presentViewController(this, animated, completion)
}
}
Expand Down

0 comments on commit b402826

Please sign in to comment.