Appearance based on system theme

This commit is contained in:
ryanzhao 2021-10-19 11:03:59 +11:00
parent 1a669feb1a
commit 02932bca93
39 changed files with 172 additions and 46 deletions

View File

@ -194,8 +194,8 @@ static NSTimeInterval launchStartedAt;
mainWindow.rootViewController = [LoadingViewController new];
[mainWindow makeKeyAndVisible];
LKAppMode appMode = [self getAppModeOrSystemDefault];
[self setCurrentAppMode:appMode];
LKAppMode appMode = [self getCurrentAppMode];
[self adaptAppMode:appMode];
if (@available(iOS 11, *)) {
// This must happen in appDidFinishLaunching or earlier to ensure we don't
@ -246,7 +246,7 @@ static NSTimeInterval launchStartedAt;
[self ensureRootViewController];
LKAppMode appMode = [self getCurrentAppMode];
[self setCurrentAppMode:appMode];
[self adaptAppMode:appMode];
[AppReadiness runNowOrWhenAppDidBecomeReady:^{
[self handleActivation];
@ -709,16 +709,10 @@ static NSTimeInterval launchStartedAt;
# pragma mark - App Mode
- (LKAppMode)getCurrentAppMode
{
return [NSUserDefaults.standardUserDefaults integerForKey:@"appMode"];
}
- (void)setCurrentAppMode:(LKAppMode)appMode
- (void)adaptAppMode:(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, *)) {
@ -735,9 +729,34 @@ static NSTimeInterval launchStartedAt;
break;
}
}
if (LKAppModeUtilities.isSystemDefault) {
if (@available(iOS 13.0, *)) {
window.overrideUserInterfaceStyle = UIUserInterfaceStyleUnspecified;
}
}
[NSNotificationCenter.defaultCenter postNotificationName:NSNotification.appModeChanged object:nil];
}
- (LKAppMode)getCurrentAppMode
{
LKAppMode appMode = [self getAppModeOrSystemDefault];
UIWindow *window = UIApplication.sharedApplication.keyWindow;
return appMode;
}
- (void)setCurrentAppMode:(LKAppMode)appMode
{
[NSUserDefaults.standardUserDefaults setInteger:appMode forKey:@"appMode"];
[self adaptAppMode:appMode];
}
- (void)setAppModeToSystemDefault
{
[NSUserDefaults.standardUserDefaults removeObjectForKey:@"appMode"];
LKAppMode appMode = [self getCurrentAppMode];
[self adaptAppMode:appMode];
}
# pragma mark - Other
- (void)handleDataNukeRequested:(NSNotification *)notification

View File

@ -1,23 +1,12 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "dark-theme-off-24@1x.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "dark-theme-off-24@2x.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "dark-theme-off-24@3x.png",
"scale" : "3x"
"filename" : "moon.pdf",
"idiom" : "universal"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
"author" : "xcode",
"version" : 1
}
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 472 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 994 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

View File

@ -1,23 +1,12 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "dark-theme-on-24@1x.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "dark-theme-on-24@2x.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "dark-theme-on-24@3x.png",
"scale" : "3x"
"filename" : "moon_fill.pdf",
"idiom" : "universal"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
"author" : "xcode",
"version" : 1
}
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 390 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 704 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.0 KiB

View File

@ -0,0 +1,12 @@
{
"images" : [
{
"filename" : "moon_auto.pdf",
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

Binary file not shown.

View File

@ -572,3 +572,6 @@
/* Button text which opens the settings app */
"OPEN_SETTINGS_BUTTON" = "Settings";
"APN_Message" = "You've got a new message";
"system_mode_theme" = "System";
"dark_mode_theme" = "Dark";
"light_mode_theme" = "Light";

View File

@ -573,3 +573,6 @@
/* Button text which opens the settings app */
"OPEN_SETTINGS_BUTTON" = "Settings";
"APN_Message" = "You've got a new message";
"system_mode_theme" = "System";
"dark_mode_theme" = "Dark";
"light_mode_theme" = "Light";

View File

@ -572,3 +572,6 @@
/* Button text which opens the settings app */
"OPEN_SETTINGS_BUTTON" = "Settings";
"APN_Message" = "You've got a new message";
"system_mode_theme" = "System";
"dark_mode_theme" = "Dark";
"light_mode_theme" = "Light";

View File

@ -572,3 +572,6 @@
/* Button text which opens the settings app */
"OPEN_SETTINGS_BUTTON" = "Settings";
"APN_Message" = "You've got a new message";
"system_mode_theme" = "System";
"dark_mode_theme" = "Dark";
"light_mode_theme" = "Light";

View File

@ -572,3 +572,6 @@
/* Button text which opens the settings app */
"OPEN_SETTINGS_BUTTON" = "Settings";
"APN_Message" = "You've got a new message";
"system_mode_theme" = "System";
"dark_mode_theme" = "Dark";
"light_mode_theme" = "Light";

View File

@ -572,3 +572,6 @@
/* Button text which opens the settings app */
"OPEN_SETTINGS_BUTTON" = "Settings";
"APN_Message" = "You've got a new message";
"system_mode_theme" = "System";
"dark_mode_theme" = "Dark";
"light_mode_theme" = "Light";

View File

@ -572,3 +572,6 @@
/* Button text which opens the settings app */
"OPEN_SETTINGS_BUTTON" = "Settings";
"APN_Message" = "You've got a new message";
"system_mode_theme" = "System";
"dark_mode_theme" = "Dark";
"light_mode_theme" = "Light";

View File

@ -572,3 +572,6 @@
/* Button text which opens the settings app */
"OPEN_SETTINGS_BUTTON" = "Settings";
"APN_Message" = "You've got a new message";
"system_mode_theme" = "System";
"dark_mode_theme" = "Dark";
"light_mode_theme" = "Light";

View File

@ -572,3 +572,6 @@
/* Button text which opens the settings app */
"OPEN_SETTINGS_BUTTON" = "Settings";
"APN_Message" = "You've got a new message";
"system_mode_theme" = "System";
"dark_mode_theme" = "Dark";
"light_mode_theme" = "Light";

View File

@ -572,3 +572,6 @@
/* Button text which opens the settings app */
"OPEN_SETTINGS_BUTTON" = "Settings";
"APN_Message" = "You've got a new message";
"system_mode_theme" = "System";
"dark_mode_theme" = "Dark";
"light_mode_theme" = "Light";

View File

@ -572,3 +572,6 @@
/* Button text which opens the settings app */
"OPEN_SETTINGS_BUTTON" = "Settings";
"APN_Message" = "You've got a new message";
"system_mode_theme" = "System";
"dark_mode_theme" = "Dark";
"light_mode_theme" = "Light";

View File

@ -572,3 +572,6 @@
/* Button text which opens the settings app */
"OPEN_SETTINGS_BUTTON" = "Settings";
"APN_Message" = "You've got a new message";
"system_mode_theme" = "System";
"dark_mode_theme" = "Dark";
"light_mode_theme" = "Light";

View File

@ -572,3 +572,6 @@
/* Button text which opens the settings app */
"OPEN_SETTINGS_BUTTON" = "Settings";
"APN_Message" = "You've got a new message";
"system_mode_theme" = "System";
"dark_mode_theme" = "Dark";
"light_mode_theme" = "Light";

View File

@ -572,3 +572,6 @@
/* Button text which opens the settings app */
"OPEN_SETTINGS_BUTTON" = "Settings";
"APN_Message" = "You've got a new message";
"system_mode_theme" = "System";
"dark_mode_theme" = "Dark";
"light_mode_theme" = "Light";

View File

@ -572,3 +572,6 @@
/* Button text which opens the settings app */
"OPEN_SETTINGS_BUTTON" = "Settings";
"APN_Message" = "You've got a new message";
"system_mode_theme" = "System";
"dark_mode_theme" = "Dark";
"light_mode_theme" = "Light";

View File

@ -573,3 +573,6 @@
/* Button text which opens the settings app */
"OPEN_SETTINGS_BUTTON" = "Settings";
"APN_Message" = "You've got a new message";
"system_mode_theme" = "System";
"dark_mode_theme" = "Dark";
"light_mode_theme" = "Light";

View File

@ -572,3 +572,6 @@
/* Button text which opens the settings app */
"OPEN_SETTINGS_BUTTON" = "Settings";
"APN_Message" = "You've got a new message";
"system_mode_theme" = "System";
"dark_mode_theme" = "Dark";
"light_mode_theme" = "Light";

View File

@ -572,3 +572,6 @@
/* Button text which opens the settings app */
"OPEN_SETTINGS_BUTTON" = "Settings";
"APN_Message" = "You've got a new message";
"system_mode_theme" = "System";
"dark_mode_theme" = "Dark";
"light_mode_theme" = "Light";

View File

@ -572,3 +572,6 @@
/* Button text which opens the settings app */
"OPEN_SETTINGS_BUTTON" = "Settings";
"APN_Message" = "You've got a new message";
"system_mode_theme" = "System";
"dark_mode_theme" = "Dark";
"light_mode_theme" = "Light";

View File

@ -572,3 +572,6 @@
/* Button text which opens the settings app */
"OPEN_SETTINGS_BUTTON" = "Settings";
"APN_Message" = "You've got a new message";
"system_mode_theme" = "System";
"dark_mode_theme" = "Dark";
"light_mode_theme" = "Light";

View File

@ -572,3 +572,6 @@
/* Button text which opens the settings app */
"OPEN_SETTINGS_BUTTON" = "Settings";
"APN_Message" = "You've got a new message";
"system_mode_theme" = "System";
"dark_mode_theme" = "Dark";
"light_mode_theme" = "Light";

View File

@ -572,3 +572,6 @@
/* Button text which opens the settings app */
"OPEN_SETTINGS_BUTTON" = "Settings";
"APN_Message" = "You've got a new message";
"system_mode_theme" = "System";
"dark_mode_theme" = "Dark";
"light_mode_theme" = "Light";

View File

@ -303,7 +303,12 @@ final class SettingsVC : BaseVC, AvatarViewHelperDelegate {
closeButton.isAccessibilityElement = true
navigationItem.leftBarButtonItem = closeButton
if #available(iOS 13, *) { // Pre iOS 13 the user can't switch actively but the app still responds to system changes
let appModeIcon = isDarkMode ? #imageLiteral(resourceName: "ic_dark_theme_on").withTintColor(.white) : #imageLiteral(resourceName: "ic_dark_theme_off").withTintColor(.black)
let appModeIcon: UIImage
if isSystemDefault {
appModeIcon = isDarkMode ? #imageLiteral(resourceName: "ic_theme_auto").withTintColor(.white) : #imageLiteral(resourceName: "ic_theme_auto").withTintColor(.black)
} else {
appModeIcon = isDarkMode ? #imageLiteral(resourceName: "ic_dark_theme_on").withTintColor(.white) : #imageLiteral(resourceName: "ic_dark_theme_off").withTintColor(.black)
}
let appModeButton = UIButton()
appModeButton.setImage(appModeIcon, for: UIControl.State.normal)
appModeButton.tintColor = Colors.text
@ -404,8 +409,26 @@ final class SettingsVC : BaseVC, AvatarViewHelperDelegate {
}
@objc private func switchAppMode() {
let newAppMode: AppMode = isLightMode ? .dark : .light
AppModeManager.shared.setCurrentAppMode(to: newAppMode)
let alertVC = UIAlertController.init(title: nil, message: nil, preferredStyle: .actionSheet)
let systemModeAction = UIAlertAction.init(title: NSLocalizedString("system_mode_theme", comment: ""), style: .default) { _ in
AppModeManager.shared.setAppModeToSystemDefault()
}
alertVC.addAction(systemModeAction)
let darkModeAction = UIAlertAction.init(title: NSLocalizedString("dark_mode_theme", comment: ""), style: .default) { _ in
AppModeManager.shared.setCurrentAppMode(to: .dark)
}
alertVC.addAction(darkModeAction)
let lightModeAction = UIAlertAction.init(title: NSLocalizedString("light_mode_theme", comment: ""), style: .default) { _ in
AppModeManager.shared.setCurrentAppMode(to: .light)
}
alertVC.addAction(lightModeAction)
let cancelAction = UIAlertAction.init(title: NSLocalizedString("TXT_CANCEL_TITLE", comment: ""), style: .cancel) {_ in }
alertVC.addAction(cancelAction)
self.presentAlert(alertVC)
}
@objc private func showQRCode() {

View File

@ -26,6 +26,16 @@ class BaseVC : UIViewController {
setNeedsStatusBarAppearanceUpdate()
NotificationCenter.default.addObserver(self, selector: #selector(handleAppModeChangedNotification(_:)), name: .appModeChanged, object: nil)
}
internal func ensureWindowBackground() {
let appMode = AppModeManager.shared.currentAppMode
switch appMode {
case .light:
UIApplication.shared.delegate?.window??.backgroundColor = .white
case .dark:
UIApplication.shared.delegate?.window??.backgroundColor = .black
}
}
internal func setUpGradientBackground() {
hasGradient = true
@ -80,12 +90,13 @@ class BaseVC : UIViewController {
}
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
// TODO: Post an appModeChanged notification?
NotificationCenter.default.post(name: .appModeChanged, object: nil)
}
@objc internal func handleAppModeChangedNotification(_ notification: Notification) {
if hasGradient {
setUpGradientBackground() // Re-do the gradient
}
ensureWindowBackground()
}
}

View File

@ -173,6 +173,10 @@ final class ShareVC : UINavigationController, ShareViewDelegate, AppModeManagerD
return // Not applicable to share extensions
}
public func setAppModeToSystemDefault() {
return // Not applicable to share extensions
}
// MARK: Updating
private func showLockScreenOrMainContent() {
if OWSScreenLock.shared.isScreenLockEnabled() {

View File

@ -25,6 +25,10 @@ public final class AppModeManager : NSObject {
public func setCurrentAppMode(to appMode: AppMode) {
delegate.setCurrentAppMode(to: appMode)
}
public func setAppModeToSystemDefault() {
delegate.setAppModeToSystemDefault()
}
}
@objc(LKAppModeManagerDelegate)
@ -33,6 +37,7 @@ public protocol AppModeManagerDelegate {
func getCurrentAppMode() -> AppMode
@objc(setCurrentAppMode:)
func setCurrentAppMode(to appMode: AppMode)
func setAppModeToSystemDefault()
}
@objc(LKAppMode)
@ -40,6 +45,10 @@ public enum AppMode : Int {
case light, dark
}
public var isSystemDefault: Bool {
return !UserDefaults.standard.dictionaryRepresentation().keys.contains("appMode")
}
public var isLightMode: Bool {
return AppModeManager.shared.currentAppMode == .light
}
@ -49,6 +58,10 @@ public var isDarkMode: Bool {
}
@objc public final class LKAppModeUtilities : NSObject {
@objc public static var isSystemDefault: Bool {
return !UserDefaults.standard.dictionaryRepresentation().keys.contains("appMode")
}
@objc public static var isLightMode: Bool {
return AppModeManager.shared.currentAppMode == .light