Skip to content

Commit

Permalink
⭐️ Impl: ModalSheetPresentationStateMachine
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicstop committed Sep 27, 2024
1 parent 60424ef commit 5bb4fe8
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
110 changes: 110 additions & 0 deletions ios/Temp/ModalSheetPresentationStateMachine.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//
// ModalSheetPresentationStateMachine.swift
// react-native-ios-modal
//
// Created by Dominic Go on 9/27/24.
//

import UIKit
import DGSwiftUtilities


public class ModalSheetPresentationStateMachine {

public var prevState: ModalSheetState?;
public var currentState: ModalSheetState = .dismissed;

public var didPresent = false;
public var didDismissAfterPresented = false;

public var eventDelegates:
MulticastDelegate<ModalSheetStateEventNotifiable> = .init();

public func setStateExplicit(nextState: ModalSheetState){
let prevState = self.prevState;
let currentState = self.currentState;

self.prevState = self.currentState;
self.currentState = nextState;

switch (prevState, currentState, nextState) {
case (_, let currentState, let nextState)
where !currentState.isPresented && nextState.isPresented:

self.didPresent = true;

case (_, let currentState, let nextState) where
!currentState.isPresented && nextState.isPresented:

self.didDismissAfterPresented = true;

default:
break;
};
};

public func setState(nextState: ModalSheetState){
switch (self.prevState, self.currentState, nextState) {

default:
break;
};

self.setStateExplicit(nextState: nextState);
};

public func reset(){
self.prevState = nil;
self.currentState = .dismissed;

self.didPresent = false;
self.didDismissAfterPresented = false;
};
};

extension ModalSheetPresentationStateMachine: ViewControllerLifecycleNotifiable {

public func notifyOnViewDidLoad(sender: UIViewController) {
// no-op
};

public func notifyOnViewWillAppear(
sender: UIViewController,
isAnimated: Bool
) {

self.setState(nextState: .presenting);
};

public func notifyOnViewIsAppearing(
sender: UIViewController,
isAnimated: Bool
) {

self.setState(nextState: .presenting);
};

public func notifyOnViewDidAppear(
sender: UIViewController,
isAnimated: Bool
) {

self.setState(nextState: .presented);
};

public func notifyOnViewWillDisappear(
sender: UIViewController,
isAnimated: Bool
) {

self.setState(nextState: .dismissing);
};

public func notifyOnViewDidDisappear(
sender: UIViewController,
isAnimated: Bool
) {

self.setState(nextState: .dismissed);
};
};
5 changes: 5 additions & 0 deletions ios/Temp/ModalSheetViewControllerLifecycleNotifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ open class ModalSheetViewControllerLifecycleNotifier: ViewControllerLifecycleNot

public weak var sheetGesture: UIPanGestureRecognizer?;
public weak var sheetDropShadowView: UIView?;

public var sheetPresentationStateMachine:
ModalSheetPresentationStateMachine = .init();

// MARK: - View Controller Lifecycle
// ---------------------------------
Expand Down Expand Up @@ -56,6 +59,8 @@ open class ModalSheetViewControllerLifecycleNotifier: ViewControllerLifecycleNot
self.presentationControllerDelegateProxy = self;
};

self.lifecycleEventDelegates.add(self.sheetPresentationStateMachine);

presentationController.delegate = self;
self._didSetup = true;
};
Expand Down

0 comments on commit 5bb4fe8

Please sign in to comment.