Skip to content

Commit

Permalink
⭐️ Impl: RNIBlurView.blurEffectStyle.blurConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicstop committed Aug 17, 2024
1 parent dfb6b3e commit 69a9d38
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 73 deletions.
21 changes: 17 additions & 4 deletions ios/RNIBlurView/RNIBlurConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,23 @@ extension RNIBlurConfig: InitializableFromDictionary {
public init(fromDict dict: Dictionary<String, Any>) throws {
let mode: String = try dict.getValueFromDictionary(forKey: "mode");

let blurEffectStyle = try dict.getValueFromDictionary(
forKey: "blurEffectStyle",
type: UIBlurEffect.Style.self
);
let blurEffectStyle: UIBlurEffect.Style = try {
let rawValue: String =
try dict.getValueFromDictionary(forKey: "blurEffectStyle");

let blurStyle: UIBlurEffect.Style? = .init(fromString: rawValue);
guard let blurStyle = blurStyle else {
throw RNIVisualEffectViewError(
errorCode: .invalidValue,
extraDebugValues: [
"blurEffectStyle": rawValue,
"mode": mode,
]
);
};

return blurStyle;
}();

switch mode {
case "none":
Expand Down
121 changes: 52 additions & 69 deletions ios/RNIBlurView/RNIBlurViewDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public final class RNIBlurViewDelegate: UIView, RNIContentView {

public static var propKeyPathMap: Dictionary<String, PartialKeyPath<RNIBlurViewDelegate>> {
return [
"blurConfig": \.blurConfig,
"blurConfig": \.blurConfigProp,
];
};

Expand All @@ -26,10 +26,8 @@ public final class RNIBlurViewDelegate: UIView, RNIContentView {
// MARK: Properties
// ----------------

var _didSendEvents = false;


var visualEffectView: VisualEffectView?;
var _didSetup = false;
var blurView: VisualEffectBlurView?;

// MARK: - Properties - RNIContentViewDelegate
// -------------------------------------------
Expand All @@ -41,14 +39,32 @@ public final class RNIBlurViewDelegate: UIView, RNIContentView {

public var reactProps: NSDictionary = [:];

@objc
public var blurConfig: NSDictionary? {
public var blurConfig: RNIBlurConfig = .none;
@objc public var blurConfigProp: NSDictionary? {
willSet {
guard let newValue = newValue as? Dictionary<String, Any>,
let blurConfigNew = try? RNIBlurConfig(fromDict: newValue)
else {
blurConfig = .none;
return;
};

let blurConfigOld = self.blurConfig;
self.blurConfig = blurConfigNew;

print(
"RNIBlurViewDelegate.blurConfig",
"\n - willSet, newValue:", newValue?.description ?? "N/A",
"RNIBlurViewDelegate.blurConfigProp",
"\n - willSet, newValue:", newValue.description,
"\n - blurConfigOld:", String(describing: blurConfigOld),
"\n - blurConfigNew:", String(describing: blurConfigNew),
"\n"
);

guard blurConfigOld != blurConfigNew else {
return;
};


}
};

Expand All @@ -72,83 +88,45 @@ public final class RNIBlurViewDelegate: UIView, RNIContentView {
guard self.window != nil,
let parentReactView = self.parentReactView
else { return };

print(
"RNIBlurViewDelegate.didMoveToWindow",
"\n - reactProps:", self.reactProps.description,
"\n"
);

self._setupContent();
return;
};

func _setupContent(){
guard !self._didSetup else { return };
self._didSetup = true;

let imageConfig: ImageConfigGradient = ImageConfigGradient(
colors: [.black, .clear],
startPointPreset: .top,
endPointPreset: .bottom,
size: UIScreen.main.bounds.size
let blurView = try? VisualEffectBlurView(
blurEffectStyle: self.blurConfig.blurEffectStyle
);

let gradientImage = try! imageConfig.makeImage();

let visualEffectView = try! VisualEffectView(rawFilterTypes: []);
self.visualEffectView = visualEffectView;
guard let blurView = blurView else { return };
self.blurView = blurView;

self.addSubview(visualEffectView);
visualEffectView.translatesAutoresizingMaskIntoConstraints = false;
visualEffectView.shouldOnlyShowBgLayer = true;
self.addSubview(blurView);
blurView.translatesAutoresizingMaskIntoConstraints = false;

NSLayoutConstraint.activate([
visualEffectView.leadingAnchor.constraint(
blurView.leadingAnchor.constraint(
equalTo: self.leadingAnchor
),
visualEffectView.trailingAnchor.constraint(
blurView.trailingAnchor.constraint(
equalTo: self.trailingAnchor
),
visualEffectView.topAnchor.constraint(
blurView.topAnchor.constraint(
equalTo: self.topAnchor
),
visualEffectView.bottomAnchor.constraint(
blurView.bottomAnchor.constraint(
equalTo: self.bottomAnchor
),
]);

try! visualEffectView.setFiltersViaEffectDesc(
withFilterTypes: [
.variadicBlur(
radius: 0,
maskImage: gradientImage.cgImage,
shouldNormalizeEdges: true
),
.colorBlackAndWhite(amount: 0.1),
],
shouldImmediatelyApplyFilter: true
);

let tapGesture = UITapGestureRecognizer();
tapGesture.isEnabled = true;

tapGesture.addAction { _ in
try! visualEffectView.updateCurrentFiltersViaEffectDesc(
withFilterTypes: [
.variadicBlur(
radius: 16,
maskImage: gradientImage.cgImage,
shouldNormalizeEdges: true
),
]
);

UIView.animate(withDuration: 1) {
try! visualEffectView.applyRequestedFilterEffects();

} completion: { _ in
try! visualEffectView.updateCurrentFiltersViaEffectDesc(
withFilterTypes: [
.colorBlackAndWhite(amount: 0.75),
]
);
UIView.animate(withDuration: 1) {
try! visualEffectView.applyRequestedFilterEffects();
};
};
};

visualEffectView.addGestureRecognizer(tapGesture);
};
};

Expand All @@ -160,7 +138,12 @@ extension RNIBlurViewDelegate: RNIContentViewDelegate {
// --------------------

public func notifyOnInit(sender: RNIContentViewParentDelegate) {
self._setupContent();
print(
"RNIBlurViewDelegate.notifyOnInit",
"\n - reactProps:", self.reactProps.description,
"\n"
);
return;
};

public func notifyOnMountChildComponentView(
Expand Down

0 comments on commit 69a9d38

Please sign in to comment.