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 {
private let appsFlyer = AppsFlyerLib.shared()
private let appsFlyerLog = Logger(componentName: "AppsFlyer")
private let log = Logger(componentName: "AppDelegate")
private let settings = Settings.shared
private enum FirstOpenSource: String {
case icon = "Icon"
......@@ -40,6 +41,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
settings.appDelegate = self
settings.delegate.add(delegate: self)
ThemeManager.refreshAppearance()
UserDefaults.migrateUserDefaultsToAppGroupsIfNeeded()
......@@ -116,17 +119,17 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
#endif
logAppLaunchEvents(launchOptions: launchOptions)
if let widgetPromotionTriggerCount = Settings.shared.widgetPromotionTriggerCount {
if let widgetPromotionTriggerCount = settings.widgetPromotionTriggerCount {
if widgetPromotionTriggerCount >= 0 {
Settings.shared.widgetPromotionTriggerCount = widgetPromotionTriggerCount - 1
settings.widgetPromotionTriggerCount = widgetPromotionTriggerCount - 1
}
}
else {
if CCPAHelper.shared.isNewUser {
Settings.shared.widgetPromotionTriggerCount = 2
settings.widgetPromotionTriggerCount = 2
}
else {
Settings.shared.widgetPromotionTriggerCount = 0
settings.widgetPromotionTriggerCount = 0
}
}
return true
......@@ -167,8 +170,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}
private func logAppLaunchEvents(launchOptions: [UIApplication.LaunchOptionsKey: Any]?) {
let settings = Settings.shared
if settings.firstOpenDate == nil && CCPAHelper.shared.isNewUser {
var firstOpenSource = FirstOpenSource.icon
if launchOptions?[.remoteNotification] != nil {
......@@ -183,7 +184,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}
if settings.d3RetentionDate == nil {
if let userQualifiedDate = Settings.shared.userQualifiedDate {
if let userQualifiedDate = settings.userQualifiedDate {
let timeSinceQualified = Date().timeIntervalSince(userQualifiedDate)
let day = TimeInterval(3600 * 24)
if timeSinceQualified >= 3 * day && timeSinceQualified < 6 * day {
......@@ -278,3 +279,35 @@ extension AppDelegate: DeepLinkDelegate {
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 @@
//
import UIKit
import OneWeatherCore
import OneWeatherAnalytics
class SettingsViewController: UIViewController {
......@@ -173,7 +174,7 @@ extension SettingsViewController: UITableViewDelegate {
case .other:
switch settingsCellFactory.rowTypeAt(indexPath: indexPath) {
case .locationAccess:
UIApplication.openSettings()
Settings.shared.openDeviceSettings()
default:
break
}
......
......@@ -57,7 +57,6 @@
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 */; };
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 */; };
CD71B9C6265E629D00803DBB /* String+NewLine.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD71B9C5265E629D00803DBB /* String+NewLine.swift */; };
CD8E48A526651414008E7F8D /* NWSCurrentEventsReponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD8E48A426651414008E7F8D /* NWSCurrentEventsReponse.swift */; };
......@@ -151,7 +150,6 @@
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>"; };
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>"; };
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>"; };
......@@ -378,7 +376,6 @@
CD615FB02655293100B717DB /* Dimension+Name.swift */,
CD615FB12655293100B717DB /* UnitPressure+Atmosphere.swift */,
CD615FB22655293100B717DB /* CLAuthorizationStatus+Localized.swift */,
CD615FB32655293100B717DB /* UIApplication+Settings.swift */,
CD71B9C5265E629D00803DBB /* String+NewLine.swift */,
CE3A112626CD3CDE00D925C7 /* UserDefaults+OneWeather.swift */,
);
......@@ -670,7 +667,6 @@
CD615FC32655295C00B717DB /* Dimension+Name.swift in Sources */,
CD615FC42655295C00B717DB /* UnitPressure+Atmosphere.swift in Sources */,
CD615FC52655295C00B717DB /* CLAuthorizationStatus+Localized.swift in Sources */,
CD615FC62655295C00B717DB /* UIApplication+Settings.swift in Sources */,
CEFE85202694C4BC003C67D3 /* DefaultSmartText.swift in Sources */,
CD8E48A526651414008E7F8D /* NWSCurrentEventsReponse.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 {
let alertGoToSettings = UIAlertController(title: "location.goToSettings.title".localized(), message: text, preferredStyle: .alert)
let actionGoToSettings = UIAlertAction(title: "location.goToSettings.goToSettingsAction".localized(), style: .default) {(_) in
UIApplication.openSettings()
Settings.shared.openDeviceSettings()
completion(.denied)
}
......
......@@ -12,6 +12,10 @@ public protocol SettingsDelegate: AnyObject {
func settingsDidChange()
}
public protocol SettingsAppDelegate: AnyObject {
func openDeviceSettings()
}
public enum AppTheme: Int {
case light = 0
case dark
......@@ -32,6 +36,7 @@ public enum AppTheme: Int {
public class Settings {
public static let shared = Settings()
public let delegate = MulticastDelegate<SettingsDelegate>()
public weak var appDelegate: SettingsAppDelegate?
private init() {}
@UserDefaultsBasicValue(key: "app_theme", userDefaults: UserDefaults.appDefaults)
......@@ -43,24 +48,16 @@ public class Settings {
}
set {
_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
delegate.settingsDidChange()
}
}
}
public func openDeviceSettings() {
appDelegate?.openDeviceSettings()
}
@UserDefaultsUnitValue(key: "temperature_type", userDefaults: UserDefaults.appDefaults)
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