-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⭐️ Impl: Add
RNIModalPresentationState
Related: * `TODO:2023-03-31-18-33-34` - Unify/streamline/consolidate logic for invoking modal focus/blur. * `TODO:2023-03-31-18-34-23` - Impl. "modal presentation state" (i.e. `RNIModalPresentationState`).
- Loading branch information
1 parent
e4e454b
commit 24a668a
Showing
1 changed file
with
122 additions
and
0 deletions.
There are no files selected for viewing
122 changes: 122 additions & 0 deletions
122
ios/src_library/React Native/RNIModal/RNIModalPresentationState.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// | ||
// RNIModalPresentationState.swift | ||
// react-native-ios-modal | ||
// | ||
// Created by Dominic Go on 4/7/23. | ||
// | ||
|
||
import Foundation | ||
|
||
|
||
public enum RNIModalPresentationState: String { | ||
case INITIAL; | ||
|
||
case PRESENTING_PROGRAMMATIC; | ||
case PRESENTING_UNKNOWN; | ||
|
||
case PRESENTED; | ||
case PRESENTED_UNKNOWN; | ||
|
||
case DISMISSING_GESTURE; | ||
case DISMISSING_PROGRAMMATIC; | ||
case DISMISSING_UNKNOWN; | ||
|
||
case DISMISS_GESTURE_CANCELLING; | ||
|
||
case DISMISSED; | ||
|
||
// MARK: - Computed Properties | ||
// --------------------------- | ||
|
||
var isDismissing: Bool { | ||
switch self { | ||
case .DISMISSING_UNKNOWN, | ||
.DISMISSING_GESTURE, | ||
.DISMISSING_PROGRAMMATIC: | ||
return true; | ||
|
||
default: | ||
return false; | ||
}; | ||
}; | ||
|
||
var isDismissingKnown: Bool { | ||
self.isDismissing && self != .DISMISSING_UNKNOWN; | ||
}; | ||
|
||
var isPresenting: Bool { | ||
switch self { | ||
case .PRESENTING_PROGRAMMATIC, | ||
.PRESENTING_UNKNOWN, | ||
.DISMISS_GESTURE_CANCELLING: | ||
return true; | ||
|
||
default: | ||
return false; | ||
}; | ||
}; | ||
|
||
var isNotSpecific: Bool { | ||
switch self { | ||
case .PRESENTING_UNKNOWN, | ||
.DISMISSING_UNKNOWN: | ||
return true; | ||
|
||
default: | ||
return false; | ||
}; | ||
}; | ||
|
||
// MARK: - Computed Properties - Alias | ||
// ----------------------------------- | ||
|
||
var isDismissViaGestureCancelling: Bool { | ||
self == .DISMISS_GESTURE_CANCELLING; | ||
}; | ||
|
||
var isDismissingViaGesture: Bool { | ||
self == .DISMISSING_GESTURE | ||
}; | ||
|
||
var isPresented: Bool { | ||
self == .PRESENTED; | ||
}; | ||
|
||
var isDismissed: Bool { | ||
self == .DISMISSED; | ||
}; | ||
}; | ||
|
||
public struct RNIModalPresentationStateMachine { | ||
|
||
var state: RNIModalPresentationState = .INITIAL; | ||
var statePrev: RNIModalPresentationState = .INITIAL; | ||
|
||
var onDismissWillCancel: (() -> Void)?; | ||
var onDismissDidCancel: (() -> Void)?; | ||
|
||
mutating func set(state nextState: RNIModalPresentationState){ | ||
let prevState = self.state; | ||
self.statePrev = prevState; | ||
|
||
if prevState.isDismissingViaGesture && nextState.isPresenting { | ||
self.state = .DISMISS_GESTURE_CANCELLING; | ||
self.onDismissWillCancel?(); | ||
|
||
} else if prevState.isDismissViaGestureCancelling && nextState.isPresented { | ||
self.state = nextState; | ||
self.onDismissDidCancel?(); | ||
|
||
} else if !prevState.isNotSpecific && nextState.isNotSpecific { | ||
/// Do not over-write specific/"known state", with non-specific/"unknown | ||
/// state", e.g. | ||
/// | ||
/// * ✅: `PRESENTING_PROGRAMMATIC` -> `PRESENTING_UNKNOWN` | ||
/// * ❌: `DISMISSING_GESTURE` -> `DISMISSING_UNKNOWN` | ||
return; | ||
|
||
} else { | ||
self.state = nextState; | ||
}; | ||
}; | ||
}; |