Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: bump to ios 13 #32

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class ThemeControlModule(reactContext: ReactApplicationContext?) :
activity?.window?.decorView?.setBackgroundColor(appBackground)
}
}
promise.resolve(activity != null)
promise.resolve(activity?.window?.decorView != null)
}

@ReactMethod
Expand Down
4 changes: 2 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
82 changes: 33 additions & 49 deletions ios/RNThemeControl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,23 @@ + (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;
}


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
Expand All @@ -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]);
}

Expand Down Expand Up @@ -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<UIWindow *> *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<UIWindow *> *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 {
Expand Down
2 changes: 1 addition & 1 deletion react-native-theme-control.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
Loading