Skip to content

Commit

Permalink
🛠 Refactor: Cleanup RNIModalViewController
Browse files Browse the repository at this point in the history
Related to: TODO:2023-03-04-06-34-28 - Library Native Cleanup

Summary: Rename properties, re-arrange code, and re-write + simplify to be more readable.
  • Loading branch information
dominicstop committed Mar 17, 2023
1 parent 269eda4 commit 73520a6
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 80 deletions.
6 changes: 1 addition & 5 deletions ios/src_library/React Native/RNIModalView/RNIModalView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,7 @@ class RNIModalView: UIView {
};

/// unique identifier for this modal
@objc var modalID: NSString? = nil {
willSet {
self.modalVC?.modalID = newValue;
}
};
@objc var modalID: NSString? = nil;

/// disable swipe gesture recognizer for this modal
@objc var enableSwipeGesture: Bool = true {
Expand Down
185 changes: 111 additions & 74 deletions ios/src_library/React Native/RNIModalView/RNIModalViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,37 @@ class RNIModalViewController: UIViewController {
// MARK: - Properties
// ------------------

var lastViewFrame: CGRect?;
var prevViewFrame: CGRect?;
var boundsDidChangeBlock: ((CGRect) -> Void)?;

var modalID: NSString? = nil;
weak var modalViewRef: RNIModalView?;

var isBGTransparent: Bool = true {
didSet {
guard oldValue != self.isBGTransparent else { return };
self.setBGTransparent();
self.setBGBlur();
self.updateBackgroundTransparency();
self.updateBackgroundBlur();
}
};

var isBGBlurred: Bool = true {
didSet {
guard oldValue != self.isBGBlurred else { return };
self.setBGBlur();
self.updateBackgroundBlur();
}
};

var blurEffectView : UIView? = nil;
// MARK: - Properties - Computed
// -----------------------------

var modalID: String? {
self.modalViewRef?.modalID as? String
};

// MARK: - Properties - Views
// --------------------------

var blurEffectView: UIVisualEffectView? = nil;

var blurEffectStyle: UIBlurEffect.Style? {
didSet {
Expand All @@ -48,16 +57,29 @@ class RNIModalViewController: UIViewController {
print("RNIModalViewController, didSet blurEffectStyle: \(blurEffectStyle.stringDescription())");
#endif

self.setBGBlur();
self.updateBackgroundBlur();
}
};

var reactView: UIView? {
didSet {
guard let nextView = reactView else { return };

if oldValue?.reactTag != nextView.reactTag {
self.view.addSubview(nextView);
let didChange =
oldValue?.reactTag != self.reactView?.reactTag;

let shouldRemoveOldView =
oldValue != nil && didChange;

if shouldRemoveOldView,
let oldView = oldValue {

oldView.removeFromSuperview();
};

if didChange,
let newView = self.reactView {

self.view.addSubview(newView);
};
}
};
Expand All @@ -69,7 +91,7 @@ class RNIModalViewController: UIViewController {
super.viewDidLoad();

#if DEBUG
print("RNIModalViewController, viewDidLoad");
print("RNIModalViewController - viewDidLoad");
#endif

// setup vc's view
Expand All @@ -79,42 +101,36 @@ class RNIModalViewController: UIViewController {
return view;
}();

self.setBGTransparent();
self.setBGBlur();
self.updateBackgroundTransparency();
self.updateBackgroundBlur();

if let boundsDidChangeBlock = self.boundsDidChangeBlock {
boundsDidChangeBlock(self.view.bounds);
};
self.boundsDidChangeBlock?(self.view.bounds)
};

override func viewDidLayoutSubviews(){
super.viewDidLayoutSubviews();

guard let boundsDidChangeBlock = self.boundsDidChangeBlock else {
#if DEBUG
print("RNIModalViewController, viewDidLayoutSubviews: guard check failed");
#endif
return;
};

#if DEBUG
print("RNIModalViewController, viewDidLayoutSubviews");
#endif

let didChangeViewFrame: Bool = !(
lastViewFrame?.equalTo(self.view.frame) ?? false
);
let didChangeViewFrame: Bool = {
guard let lastViewFrame = self.prevViewFrame else { return true };
return !lastViewFrame.equalTo(self.view.frame);
}();

if didChangeViewFrame {
if didChangeViewFrame,
let boundsDidChangeBlock = self.boundsDidChangeBlock {

boundsDidChangeBlock(self.view.bounds);
self.lastViewFrame = self.view.frame;
self.prevViewFrame = self.view.frame;

#if DEBUG
print("RNIModalViewController - viewDidLayoutSubviews - boundsDidChangeBlock");
#endif
};
};

// MARK: - Private Functions
// -------------------------

private func setBGTransparent(){
private func updateBackgroundTransparency(){
self.view.backgroundColor = {
if self.isBGTransparent {
return .none;
Expand All @@ -126,55 +142,76 @@ class RNIModalViewController: UIViewController {

#if DEBUG
print(
"RNIModalViewController, setBGTransparent"
"RNIModalViewController - updateBackgroundTransparency"
+ " - set backgroundColor: \(self.isBGTransparent)"
);
#endif
};

private func setBGBlur(){
guard self.isViewLoaded else { return };

if self.isBGBlurred && self.isBGTransparent {
// 1) either blur or update the bg
guard let blurEffectStyle = self.blurEffectStyle else { return };

if let blurEffectView = self.blurEffectView as? UIVisualEffectView {
// 1A) since blurEffectView has already been init/set, we are just
// gonna update the existing UIBlurEffect
blurEffectView.effect = UIBlurEffect(style: blurEffectStyle);

#if DEBUG
print("RNIModalViewController, setBGBlur: update blurEffectStyle");
#endif

} else {
// 1B) we are gonna set the blurEffectStyle so init blurEffectView
// if it hasn't been set yet
self.blurEffectView = {
let blurEffect = UIBlurEffect(style: blurEffectStyle);
let blurEffectView = UIVisualEffectView(effect: blurEffect);

blurEffectView.frame = self.view.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight];

self.view.insertSubview(blurEffectView, at: 0);
return blurEffectView;
}();

#if DEBUG
print("RNIModalViewController, setBGBlur: init. blurEffectView");
#endif
};

} else {
// 2) bg is not transparent or blurred so remove bg blur
self.removeBlur();
};
private func initBackgroundBlur(){
guard let blurEffectStyle = self.blurEffectStyle else { return };

let blurEffect = UIBlurEffect(style: blurEffectStyle);

let blurEffectView = UIVisualEffectView(effect: blurEffect);
self.blurEffectView = blurEffectView;

blurEffectView.frame = self.view.bounds;
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight];

self.view.insertSubview(blurEffectView, at: 0);

#if DEBUG
print(
"RNIModalViewController - initBackgroundBlur"
+ " - set blurEffectStyle: \(blurEffect)"
);
#endif
};

private func removeBlur(){
private func removeBackgroundBlur(){
self.blurEffectView?.removeFromSuperview();
self.blurEffectView = nil;
};

private func updateBackgroundBlur(){
guard self.isViewLoaded,
let blurEffectStyle = self.blurEffectStyle
else { return };

/// bg is transparent, and blur is enabled
let shouldUpdateBackgroundBlur =
self.isBGTransparent && self.isBGBlurred ;

/// bg is not transparent or blurred
let shouldRemoveBackgroundBlur =
!self.isBGTransparent || !self.isBGBlurred;

let shouldInitBackgroundBlur =
self.blurEffectView == nil && shouldUpdateBackgroundBlur;

// 01 - Init background blur first if needed
if shouldInitBackgroundBlur {
self.initBackgroundBlur();
};

if shouldUpdateBackgroundBlur,
let blurEffectView = self.blurEffectView {

// 02-A - Update background blur
blurEffectView.effect = UIBlurEffect(style: blurEffectStyle);

#if DEBUG
print(
"RNIModalViewController - updateBackgroundBlur"
+ " - Set blur effect: \(blurEffectStyle)"
);
#endif

} else if shouldRemoveBackgroundBlur {
// 02-B - background is not transparent or blurred so remove
// background blur
self.removeBackgroundBlur();
};
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ extension RNIModalViewModule {
let rootVC = navVC.viewControllers.first,
let modalVC = rootVC as? RNIModalViewController,
// check if this is the modal we want to dismiss
modalVC.modalID == modalID {
modalVC.modalID == modalID as String {

let completion = {
// modal dismissed
Expand Down

0 comments on commit 73520a6

Please sign in to comment.