Commit c1a15102 by Demid Merzlyakov

Remove access to UIApplication.shared in widgets shared with widget, since it's banned.

parent 46de2356
...@@ -32,6 +32,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { ...@@ -32,6 +32,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
private let appsFlyer = AppsFlyerLib.shared() private let appsFlyer = AppsFlyerLib.shared()
private let appsFlyerLog = Logger(componentName: "AppsFlyer") private let appsFlyerLog = Logger(componentName: "AppsFlyer")
private let log = Logger(componentName: "AppDelegate") private let log = Logger(componentName: "AppDelegate")
private let settings = Settings.shared
private enum FirstOpenSource: String { private enum FirstOpenSource: String {
case icon = "Icon" case icon = "Icon"
...@@ -40,6 +41,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate { ...@@ -40,6 +41,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
} }
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
settings.appDelegate = self
settings.delegate.add(delegate: self)
ThemeManager.refreshAppearance() ThemeManager.refreshAppearance()
UserDefaults.migrateUserDefaultsToAppGroupsIfNeeded() UserDefaults.migrateUserDefaultsToAppGroupsIfNeeded()
...@@ -116,17 +119,17 @@ class AppDelegate: UIResponder, UIApplicationDelegate { ...@@ -116,17 +119,17 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
#endif #endif
logAppLaunchEvents(launchOptions: launchOptions) logAppLaunchEvents(launchOptions: launchOptions)
if let widgetPromotionTriggerCount = Settings.shared.widgetPromotionTriggerCount { if let widgetPromotionTriggerCount = settings.widgetPromotionTriggerCount {
if widgetPromotionTriggerCount >= 0 { if widgetPromotionTriggerCount >= 0 {
Settings.shared.widgetPromotionTriggerCount = widgetPromotionTriggerCount - 1 settings.widgetPromotionTriggerCount = widgetPromotionTriggerCount - 1
} }
} }
else { else {
if CCPAHelper.shared.isNewUser { if CCPAHelper.shared.isNewUser {
Settings.shared.widgetPromotionTriggerCount = 2 settings.widgetPromotionTriggerCount = 2
} }
else { else {
Settings.shared.widgetPromotionTriggerCount = 0 settings.widgetPromotionTriggerCount = 0
} }
} }
return true return true
...@@ -167,8 +170,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate { ...@@ -167,8 +170,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
} }
private func logAppLaunchEvents(launchOptions: [UIApplication.LaunchOptionsKey: Any]?) { private func logAppLaunchEvents(launchOptions: [UIApplication.LaunchOptionsKey: Any]?) {
let settings = Settings.shared
if settings.firstOpenDate == nil && CCPAHelper.shared.isNewUser { if settings.firstOpenDate == nil && CCPAHelper.shared.isNewUser {
var firstOpenSource = FirstOpenSource.icon var firstOpenSource = FirstOpenSource.icon
if launchOptions?[.remoteNotification] != nil { if launchOptions?[.remoteNotification] != nil {
...@@ -183,7 +184,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { ...@@ -183,7 +184,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
} }
if settings.d3RetentionDate == nil { if settings.d3RetentionDate == nil {
if let userQualifiedDate = Settings.shared.userQualifiedDate { if let userQualifiedDate = settings.userQualifiedDate {
let timeSinceQualified = Date().timeIntervalSince(userQualifiedDate) let timeSinceQualified = Date().timeIntervalSince(userQualifiedDate)
let day = TimeInterval(3600 * 24) let day = TimeInterval(3600 * 24)
if timeSinceQualified >= 3 * day && timeSinceQualified < 6 * day { if timeSinceQualified >= 3 * day && timeSinceQualified < 6 * day {
...@@ -278,3 +279,35 @@ extension AppDelegate: DeepLinkDelegate { ...@@ -278,3 +279,35 @@ extension AppDelegate: DeepLinkDelegate {
router.open(url: url) router.open(url: url)
} }
} }
extension AppDelegate: SettingsDelegate {
func settingsDidChange() {
DispatchQueue.main.async {
// accessing appTheme right away causes a crash, because settingsDidChange is posted right after it was modified
if #available(iOS 13, *) {
switch self.settings.appTheme {
case .light:
UIApplication.shared.keyWindow?.overrideUserInterfaceStyle = .light
case .dark:
UIApplication.shared.keyWindow?.overrideUserInterfaceStyle = .dark
case .system:
UIApplication.shared.keyWindow?.overrideUserInterfaceStyle = .unspecified
}
}
}
}
}
extension AppDelegate: SettingsAppDelegate {
func openDeviceSettings() {
guard
let bundleIdentifier = Bundle.main.bundleIdentifier,
let appSettingsURL = URL(string: UIApplication.openSettingsURLString + bundleIdentifier)
else {
assert(false, "Failed to create settings URL from: \(UIApplication.openSettingsURLString)")
return
}
UIApplication.shared.open(appSettingsURL)
}
}
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
// //
import UIKit import UIKit
import OneWeatherCore
import OneWeatherAnalytics import OneWeatherAnalytics
class SettingsViewController: UIViewController { class SettingsViewController: UIViewController {
...@@ -173,7 +174,7 @@ extension SettingsViewController: UITableViewDelegate { ...@@ -173,7 +174,7 @@ extension SettingsViewController: UITableViewDelegate {
case .other: case .other:
switch settingsCellFactory.rowTypeAt(indexPath: indexPath) { switch settingsCellFactory.rowTypeAt(indexPath: indexPath) {
case .locationAccess: case .locationAccess:
UIApplication.openSettings() Settings.shared.openDeviceSettings()
default: default:
break break
} }
......
...@@ -57,7 +57,6 @@ ...@@ -57,7 +57,6 @@
CD615FC32655295C00B717DB /* Dimension+Name.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD615FB02655293100B717DB /* Dimension+Name.swift */; }; CD615FC32655295C00B717DB /* Dimension+Name.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD615FB02655293100B717DB /* Dimension+Name.swift */; };
CD615FC42655295C00B717DB /* UnitPressure+Atmosphere.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD615FB12655293100B717DB /* UnitPressure+Atmosphere.swift */; }; CD615FC42655295C00B717DB /* UnitPressure+Atmosphere.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD615FB12655293100B717DB /* UnitPressure+Atmosphere.swift */; };
CD615FC52655295C00B717DB /* CLAuthorizationStatus+Localized.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD615FB22655293100B717DB /* CLAuthorizationStatus+Localized.swift */; }; CD615FC52655295C00B717DB /* CLAuthorizationStatus+Localized.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD615FB22655293100B717DB /* CLAuthorizationStatus+Localized.swift */; };
CD615FC62655295C00B717DB /* UIApplication+Settings.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD615FB32655293100B717DB /* UIApplication+Settings.swift */; };
CD6C22F026677E0200D75659 /* PushNotificationsManagerProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD6C22EF26677E0200D75659 /* PushNotificationsManagerProtocol.swift */; }; CD6C22F026677E0200D75659 /* PushNotificationsManagerProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD6C22EF26677E0200D75659 /* PushNotificationsManagerProtocol.swift */; };
CD71B9C6265E629D00803DBB /* String+NewLine.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD71B9C5265E629D00803DBB /* String+NewLine.swift */; }; CD71B9C6265E629D00803DBB /* String+NewLine.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD71B9C5265E629D00803DBB /* String+NewLine.swift */; };
CD8E48A526651414008E7F8D /* NWSCurrentEventsReponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD8E48A426651414008E7F8D /* NWSCurrentEventsReponse.swift */; }; CD8E48A526651414008E7F8D /* NWSCurrentEventsReponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD8E48A426651414008E7F8D /* NWSCurrentEventsReponse.swift */; };
...@@ -151,7 +150,6 @@ ...@@ -151,7 +150,6 @@
CD615FB02655293100B717DB /* Dimension+Name.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Dimension+Name.swift"; sourceTree = "<group>"; }; CD615FB02655293100B717DB /* Dimension+Name.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Dimension+Name.swift"; sourceTree = "<group>"; };
CD615FB12655293100B717DB /* UnitPressure+Atmosphere.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UnitPressure+Atmosphere.swift"; sourceTree = "<group>"; }; CD615FB12655293100B717DB /* UnitPressure+Atmosphere.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UnitPressure+Atmosphere.swift"; sourceTree = "<group>"; };
CD615FB22655293100B717DB /* CLAuthorizationStatus+Localized.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CLAuthorizationStatus+Localized.swift"; sourceTree = "<group>"; }; CD615FB22655293100B717DB /* CLAuthorizationStatus+Localized.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CLAuthorizationStatus+Localized.swift"; sourceTree = "<group>"; };
CD615FB32655293100B717DB /* UIApplication+Settings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIApplication+Settings.swift"; sourceTree = "<group>"; };
CD615FCD265529DE00B717DB /* Settings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Settings.swift; sourceTree = "<group>"; }; CD615FCD265529DE00B717DB /* Settings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Settings.swift; sourceTree = "<group>"; };
CD615FCE265529DE00B717DB /* DefaultSettingsFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DefaultSettingsFactory.swift; sourceTree = "<group>"; }; CD615FCE265529DE00B717DB /* DefaultSettingsFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DefaultSettingsFactory.swift; sourceTree = "<group>"; };
CD615FCF265529DE00B717DB /* DefaultSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DefaultSettings.swift; sourceTree = "<group>"; }; CD615FCF265529DE00B717DB /* DefaultSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DefaultSettings.swift; sourceTree = "<group>"; };
...@@ -378,7 +376,6 @@ ...@@ -378,7 +376,6 @@
CD615FB02655293100B717DB /* Dimension+Name.swift */, CD615FB02655293100B717DB /* Dimension+Name.swift */,
CD615FB12655293100B717DB /* UnitPressure+Atmosphere.swift */, CD615FB12655293100B717DB /* UnitPressure+Atmosphere.swift */,
CD615FB22655293100B717DB /* CLAuthorizationStatus+Localized.swift */, CD615FB22655293100B717DB /* CLAuthorizationStatus+Localized.swift */,
CD615FB32655293100B717DB /* UIApplication+Settings.swift */,
CD71B9C5265E629D00803DBB /* String+NewLine.swift */, CD71B9C5265E629D00803DBB /* String+NewLine.swift */,
CE3A112626CD3CDE00D925C7 /* UserDefaults+OneWeather.swift */, CE3A112626CD3CDE00D925C7 /* UserDefaults+OneWeather.swift */,
); );
...@@ -670,7 +667,6 @@ ...@@ -670,7 +667,6 @@
CD615FC32655295C00B717DB /* Dimension+Name.swift in Sources */, CD615FC32655295C00B717DB /* Dimension+Name.swift in Sources */,
CD615FC42655295C00B717DB /* UnitPressure+Atmosphere.swift in Sources */, CD615FC42655295C00B717DB /* UnitPressure+Atmosphere.swift in Sources */,
CD615FC52655295C00B717DB /* CLAuthorizationStatus+Localized.swift in Sources */, CD615FC52655295C00B717DB /* CLAuthorizationStatus+Localized.swift in Sources */,
CD615FC62655295C00B717DB /* UIApplication+Settings.swift in Sources */,
CEFE85202694C4BC003C67D3 /* DefaultSmartText.swift in Sources */, CEFE85202694C4BC003C67D3 /* DefaultSmartText.swift in Sources */,
CD8E48A526651414008E7F8D /* NWSCurrentEventsReponse.swift in Sources */, CD8E48A526651414008E7F8D /* NWSCurrentEventsReponse.swift in Sources */,
CD427D19266F5DCE00B4350A /* ShortsSource.swift in Sources */, CD427D19266F5DCE00B4350A /* ShortsSource.swift in Sources */,
......
//
// UIApplication+Settings.swift
// 1Weather
//
// Created by Dmitry Stepanets on 02.04.2021.
//
import UIKit
public extension UIApplication {
static func openSettings() {
guard
let bundleIdentifier = Bundle.main.bundleIdentifier,
let appSettingsURL = URL(string: UIApplication.openSettingsURLString + bundleIdentifier)
else {
assert(false, "Failed to create settings URL from: \(UIApplication.openSettingsURLString)")
return
}
UIApplication.shared.open(appSettingsURL)
}
}
...@@ -174,7 +174,7 @@ internal class DeviceLocationMonitor: NSObject { ...@@ -174,7 +174,7 @@ internal class DeviceLocationMonitor: NSObject {
let alertGoToSettings = UIAlertController(title: "location.goToSettings.title".localized(), message: text, preferredStyle: .alert) let alertGoToSettings = UIAlertController(title: "location.goToSettings.title".localized(), message: text, preferredStyle: .alert)
let actionGoToSettings = UIAlertAction(title: "location.goToSettings.goToSettingsAction".localized(), style: .default) {(_) in let actionGoToSettings = UIAlertAction(title: "location.goToSettings.goToSettingsAction".localized(), style: .default) {(_) in
UIApplication.openSettings() Settings.shared.openDeviceSettings()
completion(.denied) completion(.denied)
} }
......
...@@ -12,6 +12,10 @@ public protocol SettingsDelegate: AnyObject { ...@@ -12,6 +12,10 @@ public protocol SettingsDelegate: AnyObject {
func settingsDidChange() func settingsDidChange()
} }
public protocol SettingsAppDelegate: AnyObject {
func openDeviceSettings()
}
public enum AppTheme: Int { public enum AppTheme: Int {
case light = 0 case light = 0
case dark case dark
...@@ -32,6 +36,7 @@ public enum AppTheme: Int { ...@@ -32,6 +36,7 @@ public enum AppTheme: Int {
public class Settings { public class Settings {
public static let shared = Settings() public static let shared = Settings()
public let delegate = MulticastDelegate<SettingsDelegate>() public let delegate = MulticastDelegate<SettingsDelegate>()
public weak var appDelegate: SettingsAppDelegate?
private init() {} private init() {}
@UserDefaultsBasicValue(key: "app_theme", userDefaults: UserDefaults.appDefaults) @UserDefaultsBasicValue(key: "app_theme", userDefaults: UserDefaults.appDefaults)
...@@ -43,24 +48,16 @@ public class Settings { ...@@ -43,24 +48,16 @@ public class Settings {
} }
set { set {
_appTheme = newValue.rawValue _appTheme = newValue.rawValue
if #available(iOS 13, *) {
switch newValue {
case .light:
UIApplication.shared.keyWindow?.overrideUserInterfaceStyle = .light
case .dark:
UIApplication.shared.keyWindow?.overrideUserInterfaceStyle = .dark
case .system:
UIApplication.shared.keyWindow?.overrideUserInterfaceStyle = .unspecified
}
}
Settings.shared.delegate.invoke { (delegate) in Settings.shared.delegate.invoke { (delegate) in
delegate.settingsDidChange() delegate.settingsDidChange()
} }
} }
} }
public func openDeviceSettings() {
appDelegate?.openDeviceSettings()
}
@UserDefaultsUnitValue(key: "temperature_type", userDefaults: UserDefaults.appDefaults) @UserDefaultsUnitValue(key: "temperature_type", userDefaults: UserDefaults.appDefaults)
public var temperatureType = DefaultSettingsFactory().getSettings().temperatureType public var temperatureType = DefaultSettingsFactory().getSettings().temperatureType
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment