This commit is contained in:
nielsandriesse 2020-08-24 14:08:56 +10:00
parent 9299883caa
commit f3f55d3aee
4 changed files with 24 additions and 5 deletions

View File

@ -254,6 +254,9 @@ static NSTimeInterval launchStartedAt;
mainWindow.rootViewController = [LoadingViewController new];
[mainWindow makeKeyAndVisible];
LKAppMode appMode = [NSUserDefaults.standardUserDefaults integerForKey:@"appMode"];
[self setCurrentAppMode:appMode];
if (@available(iOS 11, *)) {
// This must happen in appDidFinishLaunching or earlier to ensure we don't
// miss notifications.
@ -308,6 +311,9 @@ static NSTimeInterval launchStartedAt;
[self ensureRootViewController];
LKAppMode appMode = [NSUserDefaults.standardUserDefaults integerForKey:@"appMode"];
[self setCurrentAppMode:appMode];
[AppReadiness runNowOrWhenAppDidBecomeReady:^{
[self handleActivation];
}];
@ -937,21 +943,24 @@ static NSTimeInterval launchStartedAt;
# pragma mark - App Mode
- (LKAppMode)getCurrentAppMode {
- (LKAppMode)getCurrentAppMode
{
UIWindow *window = UIApplication.sharedApplication.keyWindow;
if (window == nil) { return LKAppModeLight; }
UIUserInterfaceStyle userInterfaceStyle = window.traitCollection.userInterfaceStyle;
BOOL isLightMode = userInterfaceStyle == UIUserInterfaceStyleUnspecified || userInterfaceStyle == UIUserInterfaceStyleLight;
BOOL isLightMode = userInterfaceStyle == UIUserInterfaceStyleLight || userInterfaceStyle == UIUserInterfaceStyleUnspecified;
return isLightMode ? LKAppModeLight : LKAppModeDark;
}
- (void)setCurrentAppModeTo:(LKAppMode)appMode {
- (void)setCurrentAppMode:(LKAppMode)appMode
{
UIWindow *window = UIApplication.sharedApplication.keyWindow;
if (window == nil) { return; }
[NSUserDefaults.standardUserDefaults setInteger:appMode forKey:@"appMode"];
switch (appMode) {
case LKAppModeLight: {
if (@available(iOS 13.0, *)) {
window.overrideUserInterfaceStyle = UIUserInterfaceStyleUnspecified;
window.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
}
break;
}
@ -962,6 +971,7 @@ static NSTimeInterval launchStartedAt;
break;
}
}
[NSNotificationCenter.defaultCenter postNotificationName:NSNotification.appModeChanged object:nil];
}
# pragma mark - Other

View File

@ -327,7 +327,6 @@ final class SettingsVC : BaseVC, AvatarViewHelperDelegate {
@objc private func switchAppMode() {
let newAppMode: AppMode = isLightMode ? .dark : .light
AppModeManager.shared.setCurrentAppMode(to: newAppMode)
NotificationCenter.default.post(name: .appModeChanged, object: nil)
}
@objc private func showQRCode() {

View File

@ -30,6 +30,7 @@ public final class AppModeManager : NSObject {
public protocol AppModeManagerDelegate {
func getCurrentAppMode() -> AppMode
@objc(setCurrentAppMode:)
func setCurrentAppMode(to appMode: AppMode)
}

View File

@ -20,6 +20,10 @@ public enum LKUserDefaults {
public enum Double : Swift.String {
case lastDeviceTokenUpload = "lastDeviceTokenUploadTime"
}
public enum Int: Swift.String {
case appMode
}
public enum String {
case slaveDeviceName(Swift.String)
@ -53,6 +57,11 @@ public extension UserDefaults {
get { return self.double(forKey: double.rawValue) }
set { set(newValue, forKey: double.rawValue) }
}
public subscript(int: LKUserDefaults.Int) -> Int {
get { return self.integer(forKey: int.rawValue) }
set { set(newValue, forKey: int.rawValue) }
}
public subscript(string: LKUserDefaults.String) -> String? {
get { return self.string(forKey: string.key) }