From a9243d15050d3d0a42c72742ddf32c9063f0d946 Mon Sep 17 00:00:00 2001 From: Vojtech Novak Date: Wed, 29 May 2024 18:50:22 +0200 Subject: [PATCH] feat: bump to ios 13 --- .../reactnativetraining/ThemeControlModule.kt | 2 +- example/ios/Podfile.lock | 4 +- ios/RNThemeControl.mm | 82 ++++++++----------- react-native-theme-control.podspec | 2 +- 4 files changed, 37 insertions(+), 53 deletions(-) diff --git a/android/src/main/java/eu/reactnativetraining/ThemeControlModule.kt b/android/src/main/java/eu/reactnativetraining/ThemeControlModule.kt index 5f74401..72c666a 100644 --- a/android/src/main/java/eu/reactnativetraining/ThemeControlModule.kt +++ b/android/src/main/java/eu/reactnativetraining/ThemeControlModule.kt @@ -63,7 +63,7 @@ class ThemeControlModule(reactContext: ReactApplicationContext?) : activity?.window?.decorView?.setBackgroundColor(appBackground) } } - promise.resolve(activity != null) + promise.resolve(activity?.window?.decorView != null) } @ReactMethod diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index cb668b3..da6a2f6 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -1462,7 +1462,7 @@ SPEC CHECKSUMS: RCTTypeSafety: a11979ff0570d230d74de9f604f7d19692157bc4 React: 88794fad7f460349dbc9df8a274d95f37a009f5d React-callinvoker: 7a7023e34a55c89ea2aa62486bb3c1164ab0be0c - React-Codegen: fd3771c0d2852072d411bf8c4595dee3e4cd2662 + React-Codegen: 8df01834eb78febdd3c6d8fae28778e5f067a0be React-Core: 60075333bc22b5a793d3f62e207368b79bff2e64 React-CoreModules: 147c314d6b3b1e069c9ad64cbbbeba604854ff86 React-cxxreact: 5de27fd8bff4764acb2eac3ee66001e0e2b910e7 @@ -1484,7 +1484,7 @@ SPEC CHECKSUMS: react-native-menu: 5b03f66058c56eb9d81c6d4dd017e7d658f38588 react-native-safe-area-context: dcab599c527c2d7de2d76507a523d20a0b83823d react-native-segmented-control: b92809e9111013dfa266e1168ba366d62898d9a4 - react-native-theme-control: 2206d0f927ba2a8398b9e9f2770d98df354dd41d + react-native-theme-control: cafbf74d8270f4bb121e9fc141ad0ccc866dd6bd React-nativeconfig: b0073a590774e8b35192fead188a36d1dca23dec React-NativeModulesApple: df46ff3e3de5b842b30b4ca8a6caae6d7c8ab09f React-perflogger: 3d31e0d1e8ad891e43a09ac70b7b17a79773003a diff --git a/ios/RNThemeControl.mm b/ios/RNThemeControl.mm index 742693c..54d7c44 100644 --- a/ios/RNThemeControl.mm +++ b/ios/RNThemeControl.mm @@ -17,15 +17,13 @@ + (BOOL)requiresMainQueueSetup - (instancetype)init { self = [super init]; if (self != nil) { - if (@available(iOS 13.0, *)) { - if (![NSThread isMainThread]) { - RCTLogError(@"RNThemeControl: not inited on the main thread. This should not happen."); - self.cachedStyle = UIUserInterfaceStyleUnspecified; - return self; - } - UIUserInterfaceStyle current = UIApplication.sharedApplication.delegate.window.overrideUserInterfaceStyle; - self.cachedStyle = current; + if (!NSThread.isMainThread) { + RCTLogError(@"RNThemeControl: not inited on the main thread. This should not happen."); + self.cachedStyle = UIUserInterfaceStyleUnspecified; + return self; } + UIUserInterfaceStyle current = UIApplication.sharedApplication.delegate.window.overrideUserInterfaceStyle; + self.cachedStyle = current; } return self; } @@ -33,14 +31,9 @@ - (instancetype)init { RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSString *, getThemePreference) { - if (@available(iOS 13.0, *)) { - UIUserInterfaceStyle current = (UIUserInterfaceStyle) self.cachedStyle; - NSString* result = [RNThemeControl getRCTAppearanceOverride:current]; - if (result) { - return result; - } - } - return systemThemeName; + UIUserInterfaceStyle current = self.cachedStyle; + NSString* themePreferenceName = [RNThemeControl getRCTAppearanceOverride:current]; + return themePreferenceName ?: systemThemeName; } RCT_EXPORT_METHOD(setTheme:(NSString*) themeStyle @@ -49,21 +42,18 @@ - (instancetype)init { reject:(RCTPromiseRejectBlock)reject) { - if (@available(iOS 13.0, *)) { - NSString* patchedThemeStyle = [systemThemeName isEqualToString:themeStyle] ? @"unspecified" : themeStyle; - UIUserInterfaceStyle style = [RCTConvert UIUserInterfaceStyle:patchedThemeStyle]; - - BOOL shouldPersistTheme = options[@"persistTheme"] == nil || [options[@"persistTheme"] boolValue]; - if (shouldPersistTheme) { - [self persistTheme:style]; - } - - self.cachedStyle = style; + UIUserInterfaceStyle style = [systemThemeName isEqualToString:themeStyle] ? UIUserInterfaceStyleUnspecified : [RCTConvert UIUserInterfaceStyle:themeStyle]; + self.cachedStyle = style; - dispatch_async(dispatch_get_main_queue(), ^{ - [RNThemeControl forceTheme:style]; - }); + BOOL shouldPersistTheme = options[@"persistTheme"] == nil || [options[@"persistTheme"] boolValue]; + if (shouldPersistTheme) { + [self persistTheme:style]; } + + dispatch_async(dispatch_get_main_queue(), ^{ + // NOTE: technically, this part could be replaced with RN-JS appearance call + [RNThemeControl forceTheme:style]; + }); resolve([NSNull null]); } @@ -92,31 +82,25 @@ - (void) persistTheme: (UIUserInterfaceStyle) style { } + (UIUserInterfaceStyle) recoverApplicationTheme { - if (@available(iOS 13.0, *)) { - NSUserDefaults* defaults = NSUserDefaults.standardUserDefaults; - NSInteger recoveredInt = [defaults integerForKey:THEME_ENTRY_KEY]; - UIUserInterfaceStyle recoveredStyle = [RNThemeControl intToUIUserInterfaceStyle:recoveredInt]; - - BOOL doesHaveStyle = recoveredStyle != 0; - if (doesHaveStyle) { - [RNThemeControl forceTheme:recoveredStyle]; - } - return recoveredStyle; + NSUserDefaults* defaults = NSUserDefaults.standardUserDefaults; + NSInteger recoveredInt = [defaults integerForKey:THEME_ENTRY_KEY]; + UIUserInterfaceStyle recoveredStyle = [RNThemeControl intToUIUserInterfaceStyle:recoveredInt]; + + BOOL doesHaveStyle = recoveredStyle != UIUserInterfaceStyleUnspecified; + if (doesHaveStyle) { + [RNThemeControl forceTheme:recoveredStyle]; } - return UIUserInterfaceStyleUnspecified; + return recoveredStyle; } + (void) forceTheme: (UIUserInterfaceStyle) forcedStyle { - if (@available(iOS 13.0, *)) { - UIUserInterfaceStyle casted = [RNThemeControl intToUIUserInterfaceStyle:forcedStyle]; - NSArray *windows = RCTSharedApplication().windows; - for (UIWindow *window in windows) { - window.overrideUserInterfaceStyle = casted; - } - NSString* appearanceOverride = [RNThemeControl getRCTAppearanceOverride:casted]; - // TODO investigate more into why this call is needed - RCTOverrideAppearancePreference(appearanceOverride); + NSArray *windows = RCTSharedApplication().windows; + for (UIWindow *window in windows) { + window.overrideUserInterfaceStyle = forcedStyle; } + NSString* appearanceOverride = [RNThemeControl getRCTAppearanceOverride:forcedStyle]; + // TODO investigate more into why this call is needed + RCTOverrideAppearancePreference(appearanceOverride); } + (nullable NSString*) getRCTAppearanceOverride: (UIUserInterfaceStyle) style { diff --git a/react-native-theme-control.podspec b/react-native-theme-control.podspec index 1f0afa4..784e70a 100644 --- a/react-native-theme-control.podspec +++ b/react-native-theme-control.podspec @@ -10,7 +10,7 @@ Pod::Spec.new do |s| s.license = package["license"] s.authors = package["author"] - s.platforms = { :ios => "12.0" } + s.platforms = { :ios => "13.0" } s.source = { :git => "https://github.com/vonovak/react-native-theme-control.git", :tag => "#{s.version}" } s.source_files = "ios/**/*.{h,m,mm}"