diff --git a/example/ios/Podfile b/example/ios/Podfile index a3f20e9..04e7a0a 100644 --- a/example/ios/Podfile +++ b/example/ios/Podfile @@ -1,5 +1,6 @@ require_relative '../node_modules/react-native/scripts/react_native_pods' require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules' +ENV['RCT_NEW_ARCH_ENABLED'] = "1" platform :ios, min_ios_version_supported prepare_react_native_project! diff --git a/ios/FabricLookComponent.swift b/ios/FabricLookComponent.swift new file mode 100644 index 0000000..c982cd4 --- /dev/null +++ b/ios/FabricLookComponent.swift @@ -0,0 +1,36 @@ +// +// FabricLookComponent.swift +// react-native-fabric-look +// +// Created by Yusuf Zeren on 20.05.2023. +// + +import UIKit + +@objc(FabricLookComponent) +public class FabricLookComponent: UIView { + + + @objc public var color: String = "" { + didSet { + self.backgroundColor = hexStringToUIColor(hexColor: color) + } + } + + + func hexStringToUIColor(hexColor: String) -> UIColor { + let stringScanner = Scanner(string: hexColor) + + if(hexColor.hasPrefix("#")) { + stringScanner.scanLocation = 1 + } + var color: UInt32 = 0 + stringScanner.scanHexInt32(&color) + + let r = CGFloat(Int(color >> 16) & 0x000000FF) + let g = CGFloat(Int(color >> 8) & 0x000000FF) + let b = CGFloat(Int(color) & 0x000000FF) + + return UIColor(red: r / 255.0, green: g / 255.0, blue: b / 255.0, alpha: 1) + } +} diff --git a/ios/FabricLookView.h b/ios/FabricLookView.h index 8ccd114..4605197 100644 --- a/ios/FabricLookView.h +++ b/ios/FabricLookView.h @@ -2,6 +2,7 @@ #ifdef RCT_NEW_ARCH_ENABLED #import #import +#import #ifndef FabricLookViewNativeComponent_h #define FabricLookViewNativeComponent_h diff --git a/ios/FabricLookView.mm b/ios/FabricLookView.mm index 96c4a0d..c956c03 100644 --- a/ios/FabricLookView.mm +++ b/ios/FabricLookView.mm @@ -1,13 +1,17 @@ #ifdef RCT_NEW_ARCH_ENABLED #import "FabricLookView.h" +#import +#import + + #import #import #import #import #import "RCTFabricComponentsPlugins.h" -#import "Utils.h" +#import "react-native-fabric-look-Swift.h" using namespace facebook::react; @@ -16,9 +20,15 @@ @interface FabricLookView () @end @implementation FabricLookView { - UIView * _view; + FabricLookComponent * _view; +} + ++ (BOOL)requiresMainQueueSetup +{ + return NO; } + + (ComponentDescriptorProvider)componentDescriptorProvider { return concreteComponentDescriptorProvider(); @@ -30,7 +40,7 @@ - (instancetype)initWithFrame:(CGRect)frame static const auto defaultProps = std::make_shared(); _props = defaultProps; - _view = [[UIView alloc] init]; + _view = [[FabricLookComponent alloc] init]; self.contentView = _view; } @@ -44,8 +54,7 @@ - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const & const auto &newViewProps = *std::static_pointer_cast(props); if (oldViewProps.color != newViewProps.color) { - NSString * colorToConvert = [[NSString alloc] initWithUTF8String: newViewProps.color.c_str()]; - [_view setBackgroundColor: [Utils hexStringToColor:colorToConvert]]; + [_view setColor:RCTNSStringFromString(newViewProps.color)]; } [super updateProps:props oldProps:oldProps]; diff --git a/ios/FabricLookViewManager.mm b/ios/FabricLookViewManager.mm index c111ed6..48ca572 100644 --- a/ios/FabricLookViewManager.mm +++ b/ios/FabricLookViewManager.mm @@ -1,23 +1,7 @@ #import -#import -#import "RCTBridge.h" -#import "Utils.h" -@interface FabricLookViewManager : RCTViewManager -@end - -@implementation FabricLookViewManager - -RCT_EXPORT_MODULE(FabricLookView) - -- (UIView *)view -{ - return [[UIView alloc] init]; -} +@interface RCT_EXTERN_MODULE(FabricLookViewManager, RCTViewManager) -RCT_CUSTOM_VIEW_PROPERTY(color, NSString, UIView) -{ - [view setBackgroundColor: [Utils hexStringToColor:json]]; -} +RCT_EXPORT_VIEW_PROPERTY(color, NSString) @end diff --git a/ios/FabricLookViewManager.swift b/ios/FabricLookViewManager.swift new file mode 100644 index 0000000..47be6bd --- /dev/null +++ b/ios/FabricLookViewManager.swift @@ -0,0 +1,28 @@ +// +// FabricLookViewManager.swift +// react-native-fabric-look +// +// Created by Yusuf Zeren on 20.05.2023. +// + +import React +import UIKit + +@objc(FabricLookViewManager) +public class FabricLookViewManager: RCTViewManager { + var myView: FabricLookComponent? + + + public override func view() -> (FabricLookComponent) { + myView = FabricLookComponent() + return myView! + } + + func methodQueue() -> DispatchQueue { + return bridge.uiManager.methodQueue + } + @objc public override static func requiresMainQueueSetup() -> Bool { + return false + } + +} diff --git a/ios/Utils.h b/ios/Utils.h deleted file mode 100644 index 1be34db..0000000 --- a/ios/Utils.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef Utils_h -#define Utils_h - -@interface Utils : NSObject -+ hexStringToColor:(NSString *)stringToConvert; -@end - -#endif /* Utils_h */ diff --git a/ios/Utils.m b/ios/Utils.m deleted file mode 100644 index bc9f8b9..0000000 --- a/ios/Utils.m +++ /dev/null @@ -1,26 +0,0 @@ -#import -#import "Utils.h" -#import - -@implementation Utils - -+ hexStringToColor:(NSString *)stringToConvert -{ - NSString *noHashString = [stringToConvert stringByReplacingOccurrencesOfString:@"#" withString:@""]; - NSScanner *stringScanner = [NSScanner scannerWithString:noHashString]; - - unsigned hex; - if (![stringScanner scanHexInt:&hex]) return nil; - int r = (hex >> 16) & 0xFF; - int g = (hex >> 8) & 0xFF; - int b = (hex) & 0xFF; - - return [UIColor colorWithRed:r / 255.0f green:g / 255.0f blue:b / 255.0f alpha:1.0f]; -} - -+ (id)alloc { - [NSException raise:@"Cannot be instantiated!" format:@"Static class 'Utils' cannot be instantiated!"]; - return nil; -} - -@end diff --git a/react-native-fabric-look-Bridging-Header.h b/react-native-fabric-look-Bridging-Header.h new file mode 100644 index 0000000..1b2cb5d --- /dev/null +++ b/react-native-fabric-look-Bridging-Header.h @@ -0,0 +1,4 @@ +// +// Use this file to import your target's public headers that you would like to expose to Swift. +// + diff --git a/react-native-fabric-look.podspec b/react-native-fabric-look.podspec index b47e747..a157d11 100644 --- a/react-native-fabric-look.podspec +++ b/react-native-fabric-look.podspec @@ -11,26 +11,27 @@ Pod::Spec.new do |s| s.license = package["license"] s.authors = package["author"] - s.platforms = { :ios => "11.0" } + s.platforms = { :ios => "12.0" } s.source = { :git => "https://github.com/ysfzrn/react-native-fabric-look.git", :tag => "#{s.version}" } - s.source_files = "ios/**/*.{h,m,mm}" - - s.dependency "React-Core" + s.source_files = "ios/**/*.{h,m,mm,swift}" # Don't install the dependencies when we run `pod install` in the old architecture. - if ENV['RCT_NEW_ARCH_ENABLED'] == '1' then - s.compiler_flags = folly_compiler_flags + " -DRCT_NEW_ARCH_ENABLED=1" - s.pod_target_xcconfig = { - "HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)/boost\"", - "OTHER_CPLUSPLUSFLAGS" => "-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1", - "CLANG_CXX_LANGUAGE_STANDARD" => "c++17" - } - s.dependency "React-RCTFabric" - s.dependency "React-Codegen" - s.dependency "RCT-Folly" - s.dependency "RCTRequired" - s.dependency "RCTTypeSafety" - s.dependency "ReactCommon/turbomodule/core" - end + if ENV['RCT_NEW_ARCH_ENABLED'] == '1' then + s.compiler_flags = folly_compiler_flags + " -DRCT_NEW_ARCH_ENABLED=1" + s.pod_target_xcconfig = { + "DEFINES_MODULE" => "YES", + #"HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)/boost\"", + #"OTHER_CPLUSPLUSFLAGS" => "-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1", + #"CLANG_CXX_LANGUAGE_STANDARD" => "c++17", + "SWIFT_OBJC_INTERFACE_HEADER_NAME" => "react-native-fabric-look-Swift.h", + "OTHER_SWIFT_FLAGS" => "-DNATIVE_LIST_PACKAGE_NEW_ARCH_ENABLED" + } + else + s.pod_target_xcconfig = { + "DEFINES_MODULE" => "YES", + "SWIFT_OBJC_INTERFACE_HEADER_NAME" => "react-native-fabric-look-Swift.h" + } + end + install_modules_dependencies(s) end