Skip to content

Commit

Permalink
💫 Update: Exp - AdaptiveModal
Browse files Browse the repository at this point in the history
Summary: Update experiment/test - `swift-programmatic-modal/AdaptiveModal`.
  • Loading branch information
dominicstop committed Jun 20, 2023
1 parent 4a3b652 commit e1c01dd
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,25 @@ class AdaptiveModalManager: NSObject {
private var layoutKeyboardValues: RNILayoutKeyboardValues?;

private var layoutValueContext: RNILayoutValueContext {
if let targetVC = self.targetViewController {
return .init(fromTargetViewController: targetVC) ?? .default;
};

if let targetView = self.targetView {
return .init(fromTargetView: targetView) ?? .default;
};
let context: RNILayoutValueContext? = {
if let targetVC = self.targetViewController {
return .init(
fromTargetViewController: targetVC,
keyboardValues: self.layoutKeyboardValues
);
};

if let targetView = self.targetView {
return .init(
fromTargetView: targetView,
keyboardValues: self.layoutKeyboardValues
);
};

return nil;
}();

return .default;
return context ?? .default;
};

// MARK: - Properties - Config Interpolation Points
Expand Down Expand Up @@ -608,6 +618,10 @@ class AdaptiveModalManager: NSObject {
self.modalAnimator = nil;
};

private func clearLayoutKeyboardValues(){
self.layoutKeyboardValues = nil;
};

private func removeObservers(){
let notificationNames = [
UIResponder.keyboardWillShowNotification,
Expand Down Expand Up @@ -669,6 +683,7 @@ class AdaptiveModalManager: NSObject {
private func cleanup() {
self.clearGestureValues();
self.clearAnimators();
self.clearLayoutKeyboardValues();

self.cleanupViews();
self.cleanupSnapPointOverride();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ struct RNILayoutKeyboardValues {

var animationDuration: CGFloat;
var animationCurve: UIView.AnimationCurve;

func computeKeyboardSize(relativeToView targetView: UIView) -> CGSize? {
guard let window = targetView.window else { return nil };

// Get keyboard height.
let keyboardFrameInView = targetView.convert(
self.frameEnd,
from: window
);

let intersection = targetView.bounds.intersection(keyboardFrameInView);

guard !intersection.isNull else { return nil };
return intersection.size;
};
};

extension RNILayoutKeyboardValues {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
import UIKit

public struct RNILayoutValueContext {

static let `default`: Self = .init(
targetRect: .zero,
windowSize: nil,
currentSize: nil,
safeAreaInsets: nil
safeAreaInsets: nil,
keyboardScreenRect: nil,
keyboardRelativeSize: nil
);

let targetRect: CGRect;
Expand All @@ -22,6 +25,9 @@ public struct RNILayoutValueContext {

let safeAreaInsets: UIEdgeInsets?;

let keyboardScreenRect: CGRect?;
let keyboardRelativeSize: CGSize?;

var targetSize: CGSize {
self.targetRect.size;
};
Expand All @@ -31,25 +37,46 @@ public struct RNILayoutValueContext {
};
};

// MARK: - Init
// ------------

extension RNILayoutValueContext {

init(
derivedFrom prev: Self,
targetView: UIView? = nil,
targetRect: CGRect? = nil,
windowSize: CGSize? = nil,
currentSize: CGSize? = nil,
safeAreaInsets: UIEdgeInsets? = nil
safeAreaInsets: UIEdgeInsets? = nil,
keyboardValues: RNILayoutKeyboardValues? = nil
) {

self.targetRect = targetRect ?? prev.targetRect;

self.windowSize = windowSize ?? prev.windowSize;
self.currentSize = currentSize ?? prev.currentSize;

self.safeAreaInsets = safeAreaInsets ?? prev.safeAreaInsets;
self.keyboardScreenRect = keyboardValues?.frameEnd ?? prev.keyboardScreenRect;

self.keyboardRelativeSize = {
guard let targetView = targetView,
let keyboardValues = keyboardValues
else {
return prev.keyboardRelativeSize;
};

let keyboardSize = keyboardValues.computeKeyboardSize(
relativeToView: targetView
);

return keyboardSize ?? prev.keyboardRelativeSize;
}();
};

init?(
fromTargetViewController targetVC: UIViewController,
keyboardValues: RNILayoutKeyboardValues? = nil,
currentSize: CGSize? = nil
) {
guard let targetView = targetVC.view else { return nil };
Expand All @@ -58,18 +85,28 @@ extension RNILayoutValueContext {
self.windowSize = targetView.window?.bounds.size;

self.safeAreaInsets = targetView.window?.safeAreaInsets;

self.currentSize = currentSize;

self.keyboardScreenRect = keyboardValues?.frameEnd;

self.keyboardRelativeSize =
keyboardValues?.computeKeyboardSize(relativeToView: targetView);
};

init?(
fromTargetView targetView: UIView,
keyboardValues: RNILayoutKeyboardValues? = nil,
currentSize: CGSize? = nil
) {
self.targetRect = targetView.frame;
self.windowSize = targetView.window?.bounds.size;

self.safeAreaInsets = targetView.window?.safeAreaInsets;
self.currentSize = currentSize;

self.keyboardScreenRect = keyboardValues?.frameEnd;

self.keyboardRelativeSize =
keyboardValues?.computeKeyboardSize(relativeToView: targetView);
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -432,11 +432,11 @@
88D017D72A1B302F004664D2 /* RNILayout.swift */,
88E8C01B2A23203E008C2FF8 /* RNILayoutPreset.swift */,
886AFCAA2A31FF40004AC9FB /* RNILayoutValue.swift */,
887AA0562A4253050089F517 /* RNILayoutKeyboardValues.swift */,
886AFCB02A325B6F004AC9FB /* RNILayoutValueContext.swift */,
886AFCAC2A3209D5004AC9FB /* RNILayoutValueMode.swift */,
886AFCAE2A320DED004AC9FB /* RNILayoutValuePercentTarget.swift */,
8849B6A62A3F7A7700A5F412 /* RNILayoutValue+StaticInit.swift */,
887AA0562A4253050089F517 /* RNILayoutKeyboardValues.swift */,
);
path = RNILayout;
sourceTree = "<group>";
Expand Down

0 comments on commit e1c01dd

Please sign in to comment.