From 2cffe91e0bba8e7ff3f67d89a03392a421178196 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sat, 20 Aug 2022 21:55:06 +0100 Subject: [PATCH] ios: choose accent color (#956) --- apps/ios/Shared/AppDelegate.swift | 20 ++++++++++ .../UserSettings/AppearanceSettings.swift | 40 +++++++++++++++++++ .../Views/UserSettings/SettingsView.swift | 8 +++- 3 files changed, 67 insertions(+), 1 deletion(-) diff --git a/apps/ios/Shared/AppDelegate.swift b/apps/ios/Shared/AppDelegate.swift index b06fac9ee0..068693461b 100644 --- a/apps/ios/Shared/AppDelegate.swift +++ b/apps/ios/Shared/AppDelegate.swift @@ -80,6 +80,16 @@ class AppDelegate: NSObject, UIApplicationDelegate { terminateChat() } + func application(_ application: UIApplication, + configurationForConnecting connectingSceneSession: UISceneSession, + options: UIScene.ConnectionOptions) -> UISceneConfiguration { + let configuration = UISceneConfiguration(name: nil, sessionRole: connectingSceneSession.role) + if connectingSceneSession.role == .windowApplication { + configuration.delegateClass = SceneDelegate.self + } + return configuration + } + private func receiveMessages(_ completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { let complete = BGManager.shared.completionHandler { logger.debug("AppDelegate: completed BGManager.receiveMessages") @@ -89,3 +99,13 @@ class AppDelegate: NSObject, UIApplicationDelegate { BGManager.shared.receiveMessages(complete) } } + +class SceneDelegate: NSObject, ObservableObject, UIWindowSceneDelegate { + var window: UIWindow? + + func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { + guard let windowScene = scene as? UIWindowScene else { return } + window = windowScene.keyWindow + window?.tintColor = UIColor(cgColor: getUIAccentColorDefault()) + } +} diff --git a/apps/ios/Shared/Views/UserSettings/AppearanceSettings.swift b/apps/ios/Shared/Views/UserSettings/AppearanceSettings.swift index 1c0558b86b..98aa97fec7 100644 --- a/apps/ios/Shared/Views/UserSettings/AppearanceSettings.swift +++ b/apps/ios/Shared/Views/UserSettings/AppearanceSettings.swift @@ -8,9 +8,13 @@ import SwiftUI +let defaultAccentColor = CGColor.init(red: 0, green: 0.533, blue: 1, alpha: 1) + struct AppearanceSettings: View { + @EnvironmentObject var sceneDelegate: SceneDelegate @State private var iconLightTapped = false @State private var iconDarkTapped = false + @State private var uiTintColor = getUIAccentColorDefault() var body: some View { VStack{ @@ -22,6 +26,23 @@ struct AppearanceSettings: View { updateAppIcon(image: "icon-dark", icon: "DarkAppIcon", tapped: $iconDarkTapped) } } + + Section { + ColorPicker("Accent color", selection: $uiTintColor, supportsOpacity: false) + } header: { + Text("Colors") + } footer: { + Button { + uiTintColor = defaultAccentColor + setUIAccentColorDefault(defaultAccentColor) + } label: { + Text("Reset colors").font(.callout) + } + } + .onChange(of: uiTintColor) { _ in + sceneDelegate.window?.tintColor = UIColor(cgColor: uiTintColor) + setUIAccentColorDefault(uiTintColor) + } } } } @@ -44,6 +65,25 @@ struct AppearanceSettings: View { } } +func getUIAccentColorDefault() -> CGColor { + let defs = UserDefaults.standard + return CGColor( + red: defs.double(forKey: DEFAULT_ACCENT_COLOR_RED), + green: defs.double(forKey: DEFAULT_ACCENT_COLOR_GREEN), + blue: defs.double(forKey: DEFAULT_ACCENT_COLOR_BLUE), + alpha: 1 + ) +} + +func setUIAccentColorDefault(_ color: CGColor) { + if let cs = color.components { + let defs = UserDefaults.standard + defs.set(cs[0], forKey: DEFAULT_ACCENT_COLOR_RED) + defs.set(cs[1], forKey: DEFAULT_ACCENT_COLOR_GREEN) + defs.set(cs[2], forKey: DEFAULT_ACCENT_COLOR_BLUE) + } +} + struct AppearanceSettings_Previews: PreviewProvider { static var previews: some View { AppearanceSettings() diff --git a/apps/ios/Shared/Views/UserSettings/SettingsView.swift b/apps/ios/Shared/Views/UserSettings/SettingsView.swift index dd2a592c4a..bd87e77ec2 100644 --- a/apps/ios/Shared/Views/UserSettings/SettingsView.swift +++ b/apps/ios/Shared/Views/UserSettings/SettingsView.swift @@ -26,6 +26,9 @@ let DEFAULT_CHAT_ARCHIVE_NAME = "chatArchiveName" let DEFAULT_CHAT_ARCHIVE_TIME = "chatArchiveTime" let DEFAULT_CHAT_V3_DB_MIGRATION = "chatV3DBMigration" let DEFAULT_DEVELOPER_TOOLS = "developerTools" +let DEFAULT_ACCENT_COLOR_RED = "accentColorRed" +let DEFAULT_ACCENT_COLOR_GREEN = "accentColorGreen" +let DEFAULT_ACCENT_COLOR_BLUE = "accentColorBlue" let appDefaults: [String: Any] = [ DEFAULT_SHOW_LA_NOTICE: false, @@ -36,7 +39,10 @@ let appDefaults: [String: Any] = [ DEFAULT_PRIVACY_LINK_PREVIEWS: true, DEFAULT_EXPERIMENTAL_CALLS: false, DEFAULT_CHAT_V3_DB_MIGRATION: "offer", - DEFAULT_DEVELOPER_TOOLS: false + DEFAULT_DEVELOPER_TOOLS: false, + DEFAULT_ACCENT_COLOR_RED: 0.000, + DEFAULT_ACCENT_COLOR_GREEN: 0.533, + DEFAULT_ACCENT_COLOR_BLUE: 1.000 ] private var indent: CGFloat = 36