Skip to content

Commit

Permalink
⭐️ Impl: UIBlurEffect+Init
Browse files Browse the repository at this point in the history
Related:
* TODO:2023-03-04-06-34-28 - Library Native Cleanup
* TODO:2023-03-22-13-18-14 - Refactor: Move `fromString` to enum init

Summary:
* Impl. `UIBlurEffect+Init`.
* Remove `/UIBlurEffect+Helpers`.
* Replace `UIBlurEffect+Helpers` + `UIBlurEffect.Style.fromString` usage w/  `UIBlurEffect+Init` `UIBlurEffect.Style` + `UIBlurEffect.Style.init`.
  • Loading branch information
dominicstop committed Mar 24, 2023
1 parent 11b4240 commit fb89e8f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
//
// UIBlurEffect+Helpers.swift
// nativeUIModulesTest
// UIBlurEffect+Init.swift
// react-native-ios-modal
//
// Created by Dominic Go on 6/26/20.
// Created by Dominic Go on 3/23/23.
//

import Foundation

extension UIBlurEffect.Style: CaseIterable {
extension UIBlurEffect.Style: CaseIterable, CustomStringConvertible {

/// The available `UIBlurEffect.Style` that can be used based on the current
/// platform version
///
public static var availableStyles: [UIBlurEffect.Style] {
var styles: [UIBlurEffect.Style] = [
.light,
Expand Down Expand Up @@ -45,49 +49,84 @@ extension UIBlurEffect.Style: CaseIterable {
return styles;
};

// MARK: - CaseIterable
// --------------------

public static var allCases: [UIBlurEffect.Style] {
return self.availableStyles;
};

func stringDescription() -> String {
// MARK: - CustomStringConvertible
// -------------------------------

/// Note:2023-03-23-23-14-57
///
/// * `UIBlurEffect.Style` is an objc enum, and as such, it's actually raw
/// value internally is `Int`.
///
/// * As such, `String(describing:)` a ` UIBlurEffect.Style` enum value
/// outputs an `Int`.
///
/// * Because of this, we have to manually map out the enum values to a string
/// representation.
///
public var description: String {
switch self {
// Adaptable Styles
case .systemUltraThinMaterial: return "systemUltraThinMaterial";
case .systemThinMaterial : return "systemThinMaterial";
case .systemMaterial : return "systemMaterial";
case .systemThickMaterial : return "systemThickMaterial";
case .systemChromeMaterial : return "systemChromeMaterial";

// Light Styles
case .systemMaterialLight : return "systemMaterialLight";
case .systemThinMaterialLight : return "systemThinMaterialLight";
case .systemUltraThinMaterialLight: return "systemUltraThinMaterialLight";
case .systemThickMaterialLight : return "systemThickMaterialLight";
case .systemChromeMaterialLight : return "systemChromeMaterialLight";

// Dark Styles
// Dark Styles
case .systemChromeMaterialDark : return "systemChromeMaterialDark";
case .systemMaterialDark : return "systemMaterialDark";
case .systemThickMaterialDark : return "systemThickMaterialDark";
case .systemThinMaterialDark : return "systemThinMaterialDark";
case .systemUltraThinMaterialDark: return "systemUltraThinMaterialDark";

// Additional Styles
case .regular : return "regular";
case .prominent : return "prominent";
case .light : return "light";
case .extraLight: return "extraLight";
case .dark : return "dark";

@unknown default: return "";
};
};

static func fromString(_ string: String) -> UIBlurEffect.Style? {
return self.allCases.first{ $0.stringDescription() == string };
};
// MARK: - Init
// ------------

static func fromString(_ string: NSString) -> UIBlurEffect.Style? {
return self.fromString(string as String);
init?(string: String){

/// Note:2023-03-23-23-21-21
///
/// * Normally, a simple `switch` + `case "foo": self = foo` would suffice,
/// (especially since it's O(1) access), but the usable enum values depend
/// on the platform version.
///
/// * The useable enums are stored in `availableStyles`, and is used to
/// communicate to JS the available enum values.
///
/// * As such, we might as well re-use `availableStyles` for the parsing
/// logic (even if it's less efficient).
///
let style = Self.allCases.first{
$0.description == string
};

guard let style = style else { return nil };
self = style;
};
};

6 changes: 3 additions & 3 deletions ios/src_library/React Native/RNIModalView/RNIModalView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,9 @@ class RNIModalView: UIView, RNIModalPresentation {
return .systemThinMaterial;
}();

// TODO:2023-03-22-13-18-14 - Refactor: Move `fromString` to enum init
guard let blurStyle = UIBlurEffect.Style.fromString(self.modalBGBlurEffectStyle)
else {
guard let blurStyle = UIBlurEffect.Style(
string: self.modalBGBlurEffectStyle as String
) else {
#if DEBUG
print(
"RNIModalView - synthesizedModalBGBlurEffectStyle: Unsupported "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ class RNIModalViewController: UIViewController {
let blurEffectStyle = self.blurEffectStyle else { return };

#if DEBUG
print("RNIModalViewController, didSet blurEffectStyle: \(blurEffectStyle.stringDescription())");
print(
"RNIModalViewController, didSet blurEffectStyle: "
+ "'\(blurEffectStyle.description)'"
);
#endif

self.updateBackgroundBlur();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class RNIModalViewManager: RCTViewManager {
@objc override func constantsToExport() -> [AnyHashable : Any]! {
return [
"availableBlurEffectStyles": UIBlurEffect.Style
.availableStyles.map { $0.stringDescription() },
.availableStyles.map { $0.description },

"availablePresentationStyles": UIModalPresentationStyle
.availableStyles.map { $0.stringDescription() },
Expand Down

0 comments on commit fb89e8f

Please sign in to comment.