Don't duck Compose screen when the draft is empty

See #369
This commit is contained in:
Shadowfacts 2023-05-04 18:40:00 -04:00
parent 346888db41
commit 99b74559da
3 changed files with 26 additions and 8 deletions

View File

@ -10,7 +10,7 @@ import UIKit
public protocol DuckableViewController: UIViewController {
var duckableDelegate: DuckableViewControllerDelegate? { get set }
func duckableViewControllerShouldDuck() -> Bool
func duckableViewControllerShouldDuck() -> DuckAttemptAction
func duckableViewControllerMayAttemptToDuck()
@ -20,7 +20,7 @@ public protocol DuckableViewController: UIViewController {
}
extension DuckableViewController {
public func duckableViewControllerShouldDuck() -> Bool { true }
public func duckableViewControllerShouldDuck() -> DuckAttemptAction { .duck }
public func duckableViewControllerMayAttemptToDuck() {}
public func duckableViewControllerWillAnimateDuck(withDuration duration: CGFloat, afterDelay delay: CGFloat) {}
public func duckableViewControllerDidFinishAnimatingDuck() {}
@ -30,6 +30,12 @@ public protocol DuckableViewControllerDelegate: AnyObject {
func duckableViewControllerWillDismiss(animated: Bool)
}
public enum DuckAttemptAction {
case duck
case dismiss
case block
}
extension UIViewController {
@available(iOS 16.0, *)
public func presentDuckable(_ viewController: DuckableViewController, animated: Bool, isDucked: Bool = false) -> Bool {

View File

@ -135,14 +135,18 @@ public class DuckableContainerViewController: UIViewController, DuckableViewCont
guard case .presentingDucked(let viewController, isFirstPresentation: _) = state else {
return
}
guard viewController.duckableViewControllerShouldDuck() else {
viewController.sheetPresentationController!.selectedDetentIdentifier = .large
return
}
switch viewController.duckableViewControllerShouldDuck() {
case .duck:
let placeholder = createPlaceholderForDuckedViewController(viewController)
state = .ducked(viewController, placeholder: placeholder)
configureChildForDuckedPlaceholder()
dismiss(animated: true)
case .block:
viewController.sheetPresentationController!.selectedDetentIdentifier = .large
case .dismiss:
duckableViewControllerWillDismiss(animated: true)
dismiss(animated: true)
}
}
private func configureChildForDuckedPlaceholder() {

View File

@ -138,6 +138,14 @@ class ComposeHostingController: UIHostingController<ComposeHostingController.Vie
// MARK: Duckable
func duckableViewControllerShouldDuck() -> DuckAttemptAction {
if controller.draft.hasContent {
return .duck
} else {
return .dismiss
}
}
func duckableViewControllerWillAnimateDuck(withDuration duration: CGFloat, afterDelay delay: CGFloat) {
controller.deleteDraftOnDisappear = false