Fix EnvironmentObject related crash on macOS + iPad

This commit is contained in:
Thomas Ricouard 2023-01-17 07:39:13 +01:00
parent 382ebcf8f7
commit dd6483dd37
8 changed files with 40 additions and 15 deletions

View file

@ -5,7 +5,9 @@ import Env
import Status import Status
import DesignSystem import DesignSystem
import Lists import Lists
import AppAccount
@MainActor
extension View { extension View {
func withAppRouteur() -> some View { func withAppRouteur() -> some View {
self.navigationDestination(for: RouteurDestinations.self) { destination in self.navigationDestination(for: RouteurDestinations.self) { destination in
@ -39,23 +41,41 @@ extension View {
switch destination { switch destination {
case let .replyToStatusEditor(status): case let .replyToStatusEditor(status):
StatusEditorView(mode: .replyTo(status: status)) StatusEditorView(mode: .replyTo(status: status))
.withEnvironments()
case let .newStatusEditor(visibility): case let .newStatusEditor(visibility):
StatusEditorView(mode: .new(vivibilty: visibility)) StatusEditorView(mode: .new(vivibilty: visibility))
.withEnvironments()
case let .editStatusEditor(status): case let .editStatusEditor(status):
StatusEditorView(mode: .edit(status: status)) StatusEditorView(mode: .edit(status: status))
.withEnvironments()
case let .quoteStatusEditor(status): case let .quoteStatusEditor(status):
StatusEditorView(mode: .quote(status: status)) StatusEditorView(mode: .quote(status: status))
.withEnvironments()
case let .mentionStatusEditor(account, visibility): case let .mentionStatusEditor(account, visibility):
StatusEditorView(mode: .mention(account: account, visibility: visibility)) StatusEditorView(mode: .mention(account: account, visibility: visibility))
.withEnvironments()
case let .listEdit(list): case let .listEdit(list):
ListEditView(list: list) ListEditView(list: list)
.withEnvironments()
case let .listAddAccount(account): case let .listAddAccount(account):
ListAddAccountView(account: account) ListAddAccountView(account: account)
.withEnvironments()
case .addAccount: case .addAccount:
AddAccountView() AddAccountView()
.withEnvironments()
case .addRemoteLocalTimeline: case .addRemoteLocalTimeline:
AddRemoteTimelineView() AddRemoteTimelineView()
.withEnvironments()
} }
} }
} }
func withEnvironments() -> some View {
self
.environmentObject(CurrentAccount.shared)
.environmentObject(UserPreferences.shared)
.environmentObject(CurrentInstance.shared)
.environmentObject(Theme.shared)
.environmentObject(AppAccountsManager.shared)
}
} }

View file

@ -15,12 +15,12 @@ struct IceCubesApp: App {
@Environment(\.scenePhase) private var scenePhase @Environment(\.scenePhase) private var scenePhase
@StateObject private var appAccountsManager = AppAccountsManager.shared @StateObject private var appAccountsManager = AppAccountsManager.shared
@StateObject private var currentInstance = CurrentInstance() @StateObject private var currentInstance = CurrentInstance.shared
@StateObject private var currentAccount = CurrentAccount() @StateObject private var currentAccount = CurrentAccount.shared
@StateObject private var userPreferences = UserPreferences() @StateObject private var userPreferences = UserPreferences.shared
@StateObject private var watcher = StreamWatcher() @StateObject private var watcher = StreamWatcher()
@StateObject private var quickLook = QuickLook() @StateObject private var quickLook = QuickLook()
@StateObject private var theme = Theme() @StateObject private var theme = Theme.shared
@State private var selectedTab: Tab = .timeline @State private var selectedTab: Tab = .timeline
@State private var selectSidebarItem: Tab? = .timeline @State private var selectSidebarItem: Tab? = .timeline

View file

@ -54,7 +54,7 @@ class NotificationService: UNNotificationServiceExtension {
bestAttemptContent.userInfo["plaintext"] = plaintextData bestAttemptContent.userInfo["plaintext"] = plaintextData
bestAttemptContent.sound = UNNotificationSound.init(named: UNNotificationSoundName(rawValue: "glass.wav")) bestAttemptContent.sound = UNNotificationSound.init(named: UNNotificationSoundName(rawValue: "glass.wav"))
let preferences = UserPreferences() let preferences = UserPreferences.shared
preferences.pushNotificationsCount += 1 preferences.pushNotificationsCount += 1
bestAttemptContent.badge = .init(integerLiteral: preferences.pushNotificationsCount) bestAttemptContent.badge = .init(integerLiteral: preferences.pushNotificationsCount)

View file

@ -14,18 +14,18 @@ class ShareViewController: UIViewController {
super.viewDidLoad() super.viewDidLoad()
let client = AppAccountsManager.shared.currentClient let client = AppAccountsManager.shared.currentClient
let account = CurrentAccount() let account = CurrentAccount.shared
let instance = CurrentInstance() let instance = CurrentInstance.shared
account.setClient(client: client) account.setClient(client: client)
instance.setClient(client: client) instance.setClient(client: client)
let colorScheme = traitCollection.userInterfaceStyle let colorScheme = traitCollection.userInterfaceStyle
let theme = Theme() let theme = Theme.shared
theme.setColor(withName: colorScheme == .dark ? .iceCubeDark : .iceCubeLight) theme.setColor(withName: colorScheme == .dark ? .iceCubeDark : .iceCubeLight)
if let item = extensionContext?.inputItems.first as? NSExtensionItem { if let item = extensionContext?.inputItems.first as? NSExtensionItem {
if let attachments = item.attachments { if let attachments = item.attachments {
let view = StatusEditorView(mode: .shareExtension(items: attachments)) let view = StatusEditorView(mode: .shareExtension(items: attachments))
.environmentObject(UserPreferences()) .environmentObject(UserPreferences.shared)
.environmentObject(client) .environmentObject(client)
.environmentObject(account) .environmentObject(account)
.environmentObject(theme) .environmentObject(theme)

View file

@ -80,7 +80,9 @@ public class Theme: ObservableObject {
private var cancellables = Set<AnyCancellable>() private var cancellables = Set<AnyCancellable>()
public init() { public static let shared = Theme()
private init() {
selectedSet = storedSet selectedSet = storedSet
// If theme is never set before set the default store. This should only execute once after install. // If theme is never set before set the default store. This should only execute once after install.

View file

@ -10,7 +10,9 @@ public class CurrentAccount: ObservableObject {
private var client: Client? private var client: Client?
public init() { } static public let shared = CurrentAccount()
private init() { }
public func setClient(client: Client) { public func setClient(client: Client) {
self.client = client self.client = client

View file

@ -8,9 +8,9 @@ public class CurrentInstance: ObservableObject {
private var client: Client? private var client: Client?
public init() { static public let shared = CurrentInstance()
} private init() { }
public func setClient(client: Client) { public func setClient(client: Client) {
self.client = client self.client = client

View file

@ -6,6 +6,7 @@ import Network
@MainActor @MainActor
public class UserPreferences: ObservableObject { public class UserPreferences: ObservableObject {
public static let sharedDefault = UserDefaults.init(suiteName: "group.icecubesapps") public static let sharedDefault = UserDefaults.init(suiteName: "group.icecubesapps")
public static let shared = UserPreferences()
private var client: Client? private var client: Client?
@ -24,7 +25,7 @@ public class UserPreferences: ObservableObject {
@Published public var serverPreferences: ServerPreferences? @Published public var serverPreferences: ServerPreferences?
public init() { } private init() { }
public func setClient(client: Client) { public func setClient(client: Client) {
self.client = client self.client = client