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

Handling "is already presenting" error #115

Merged
merged 1 commit into from
Mar 5, 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
6 changes: 6 additions & 0 deletions Nivelir.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@
C027115E25F60A5100ECC57C /* AnyStackScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0270EE925F4A61F00ECC57C /* AnyStackScreen.swift */; };
C027116325F60A5600ECC57C /* AnyTabsScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0270EF025F4A63400ECC57C /* AnyTabsScreen.swift */; };
C027116825F60A5600ECC57C /* AnyTabsScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0270EF025F4A63400ECC57C /* AnyTabsScreen.swift */; };
C04C26E22B970FB90086F4E4 /* ScreenContainerAlreadyPresentingError.swift in Sources */ = {isa = PBXBuildFile; fileRef = C04C26E12B970FB90086F4E4 /* ScreenContainerAlreadyPresentingError.swift */; };
C04C26E32B970FB90086F4E4 /* ScreenContainerAlreadyPresentingError.swift in Sources */ = {isa = PBXBuildFile; fileRef = C04C26E12B970FB90086F4E4 /* ScreenContainerAlreadyPresentingError.swift */; };
C052C7B52822D9E100132875 /* ScreenObservation.swift in Sources */ = {isa = PBXBuildFile; fileRef = C052C7B42822D9E100132875 /* ScreenObservation.swift */; };
C052C7B62822D9E100132875 /* ScreenObservation.swift in Sources */ = {isa = PBXBuildFile; fileRef = C052C7B42822D9E100132875 /* ScreenObservation.swift */; };
C052C7CD2823A90F00132875 /* ScreenObserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = C052C7CC2823A90F00132875 /* ScreenObserver.swift */; };
Expand Down Expand Up @@ -729,6 +731,7 @@
C027107A25F50FE300ECC57C /* DefaultScreenIterator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DefaultScreenIterator.swift; sourceTree = "<group>"; };
C027108A25F5120300ECC57C /* ScreenIterationPredicate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScreenIterationPredicate.swift; sourceTree = "<group>"; };
C027109E25F51C7F00ECC57C /* ScreenIterationResult.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScreenIterationResult.swift; sourceTree = "<group>"; };
C04C26E12B970FB90086F4E4 /* ScreenContainerAlreadyPresentingError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScreenContainerAlreadyPresentingError.swift; sourceTree = "<group>"; };
C052C7B42822D9E100132875 /* ScreenObservation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScreenObservation.swift; sourceTree = "<group>"; };
C052C7CC2823A90F00132875 /* ScreenObserver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScreenObserver.swift; sourceTree = "<group>"; };
C052C7CF2823A94500132875 /* ScreenObserverPredicate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScreenObserverPredicate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2243,6 +2246,7 @@
C0238F2C27A9477000148E91 /* ScreenCanceledError.swift */,
C0F9A47425FD61EB00DAE060 /* ScreenContainerNotFoundError.swift */,
C088605B26B9737C00CA0A77 /* ScreenContainerNotSupportedError.swift */,
C04C26E12B970FB90086F4E4 /* ScreenContainerAlreadyPresentingError.swift */,
C0F9A47325FD61EB00DAE060 /* ScreenContainerTypeMismatchError.swift */,
C0AADA2F25EED85C0087439E /* ScreenError.swift */,
);
Expand Down Expand Up @@ -2633,6 +2637,7 @@
C052C7CD2823A90F00132875 /* ScreenObserver.swift in Sources */,
C0885F9826B6BA0100CA0A77 /* ScreenPresentingAction.swift in Sources */,
C0885F7626B6BA0100CA0A77 /* ScreenTryResolution.swift in Sources */,
C04C26E22B970FB90086F4E4 /* ScreenContainerAlreadyPresentingError.swift in Sources */,
C0885FB026B6C0A500CA0A77 /* ScreenStackPopPredicate.swift in Sources */,
C0885F7026B6BA0100CA0A77 /* ScreenRefreshableContainer.swift in Sources */,
C0270E5F25F3EC7200ECC57C /* DefaultScreenLogger.swift in Sources */,
Expand Down Expand Up @@ -3152,6 +3157,7 @@
C0F9A47625FD61EB00DAE060 /* ScreenContainerTypeMismatchError.swift in Sources */,
C089D8E92A0B18C800B6400C /* KeyboardHandler.swift in Sources */,
C078FD482861A1DA0008FC6E /* ProgressSuccessIndicatorView.swift in Sources */,
C04C26E32B970FB90086F4E4 /* ScreenContainerAlreadyPresentingError.swift in Sources */,
C0270E9125F4245800ECC57C /* ScreenDecorator.swift in Sources */,
C078FD722861A1DA0008FC6E /* ProgressEmptyHeader.swift in Sources */,
C0238ED127A40F6C00148E91 /* DictionaryNonConformingFloatDecodingStrategy.swift in Sources */,
Expand Down
4 changes: 4 additions & 0 deletions Sources/Addons/ActionSheet/ScreenShowActionSheetAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ public struct ScreenShowActionSheetAction<Container: UIViewController>: ScreenAc
) {
navigator.logInfo("Presenting \(actionSheet) on \(type(of: container))")

guard container.presented == nil else {
return completion(.containerAlreadyPresenting(container, for: self))
}

let alertContainer = makeAlertContainer()

switch UIDevice.current.userInterfaceIdiom {
Expand Down
4 changes: 4 additions & 0 deletions Sources/Addons/Alert/ScreenShowAlertAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ public struct ScreenShowAlertAction<Container: UIViewController>: ScreenAction {
) {
navigator.logInfo("Presenting \(alert) on \(type(of: container))")

guard container.presented == nil else {
return completion(.containerAlreadyPresenting(container, for: self))
}

let alertContainer = makeAlertContainer()

container.present(alertContainer, animated: animated) {
Expand Down
4 changes: 4 additions & 0 deletions Sources/Addons/MediaPicker/ScreenShowMediaPickerAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ public struct ScreenShowMediaPickerAction<Container: UIViewController>: ScreenAc
) {
navigator.logInfo("Presenting \(mediaPicker) on \(type(of: container))")

guard container.presented == nil else {
return completion(.containerAlreadyPresenting(container, for: self))
}

requestAccessIfNeeded { authorized in
guard authorized else {
return completion(.mediaPickerSourceAccessDenied(for: self))
Expand Down
4 changes: 4 additions & 0 deletions Sources/Addons/Safari/ScreenShowSafariAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public struct ScreenShowSafariAction<Container: UIViewController>: ScreenAction
) {
navigator.logInfo("Presenting \(safari) on \(type(of: container))")

guard container.presented == nil else {
return completion(.containerAlreadyPresenting(container, for: self))
}

guard safari.url.scheme.map({ ["http", "https"].contains($0) }) ?? false else {
return completion(.invalidSafariURL(for: self))
}
Expand Down
4 changes: 4 additions & 0 deletions Sources/Addons/Sharing/ScreenShareAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ public struct ScreenShareAction<Container: UIViewController>: ScreenAction {
) {
navigator.logInfo("Sharing \(sharing.items) on \(type(of: container))")

guard container.presented == nil else {
return completion(.containerAlreadyPresenting(container, for: self))
}

let activitiesContainer = makeActivitiesContainer(navigator: navigator)

switch UIDevice.current.userInterfaceIdiom {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public struct ScreenShowStoreProductAction<Container: UIViewController>: ScreenA
) {
navigator.logInfo("Presenting \(storeProduct) on \(type(of: container))")

guard container.presented == nil else {
return completion(.containerAlreadyPresenting(container, for: self))
}

guard let storeProductID = Int(storeProduct.itemID).map(NSNumber.init(value:)) else {
return completion(.invalidStoreProductID(for: self))
}
Expand Down
4 changes: 4 additions & 0 deletions Sources/Screen/Actions/Modal/ScreenPresentAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public struct ScreenPresentAction<
) {
navigator.logInfo("Presenting \(screen) on \(type(of: container))")

guard container.presented == nil else {
return completion(.containerAlreadyPresenting(container, for: self))
}

let presented = screen.build(navigator: navigator)

container.present(presented, animated: animated) {
Expand Down
46 changes: 46 additions & 0 deletions Sources/Screen/Errors/ScreenContainerAlreadyPresentingError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import Foundation

/// The container is already presenting another container.
///
/// This error occurs whenever an action fails to present a modal container on a container
/// which is already presenting another container.
public struct ScreenContainerAlreadyPresentingError: ScreenError {

public var description: String {
"""
The container \(container) is already presenting another container for:
\(trigger)
"""
}

/// Container that is already presenting another screen.
public let container: ScreenContainer

/// The action that caused the error.
public let trigger: Any

/// Creates an error.
///
/// - Parameters:
/// - container: Container that is already presenting another screen.
/// - trigger: The action that caused the error.
public init(container: ScreenContainer, for trigger: Any) {
self.container = container
self.trigger = trigger
}
}

extension Result where Failure == Error {

internal static func containerAlreadyPresenting(
_ container: ScreenContainer,
for trigger: Any
) -> Self {
.failure(
ScreenContainerAlreadyPresentingError(
container: container,
for: trigger
)
)
}
}
Loading