diff --git a/IceCubesApp/App/AppRouteur.swift b/IceCubesApp/App/AppRouteur.swift index 70b02ee7..ecebb215 100644 --- a/IceCubesApp/App/AppRouteur.swift +++ b/IceCubesApp/App/AppRouteur.swift @@ -5,7 +5,9 @@ import Env import Status import DesignSystem import Lists +import AppAccount +@MainActor extension View { func withAppRouteur() -> some View { self.navigationDestination(for: RouteurDestinations.self) { destination in @@ -39,23 +41,41 @@ extension View { switch destination { case let .replyToStatusEditor(status): StatusEditorView(mode: .replyTo(status: status)) + .withEnvironments() case let .newStatusEditor(visibility): StatusEditorView(mode: .new(vivibilty: visibility)) + .withEnvironments() case let .editStatusEditor(status): StatusEditorView(mode: .edit(status: status)) + .withEnvironments() case let .quoteStatusEditor(status): StatusEditorView(mode: .quote(status: status)) + .withEnvironments() case let .mentionStatusEditor(account, visibility): StatusEditorView(mode: .mention(account: account, visibility: visibility)) + .withEnvironments() case let .listEdit(list): ListEditView(list: list) + .withEnvironments() case let .listAddAccount(account): ListAddAccountView(account: account) + .withEnvironments() case .addAccount: AddAccountView() + .withEnvironments() case .addRemoteLocalTimeline: AddRemoteTimelineView() + .withEnvironments() } } } + + func withEnvironments() -> some View { + self + .environmentObject(CurrentAccount.shared) + .environmentObject(UserPreferences.shared) + .environmentObject(CurrentInstance.shared) + .environmentObject(Theme.shared) + .environmentObject(AppAccountsManager.shared) + } } diff --git a/IceCubesApp/App/IceCubesApp.swift b/IceCubesApp/App/IceCubesApp.swift index d1109877..d8d9792d 100644 --- a/IceCubesApp/App/IceCubesApp.swift +++ b/IceCubesApp/App/IceCubesApp.swift @@ -15,12 +15,12 @@ struct IceCubesApp: App { @Environment(\.scenePhase) private var scenePhase @StateObject private var appAccountsManager = AppAccountsManager.shared - @StateObject private var currentInstance = CurrentInstance() - @StateObject private var currentAccount = CurrentAccount() - @StateObject private var userPreferences = UserPreferences() + @StateObject private var currentInstance = CurrentInstance.shared + @StateObject private var currentAccount = CurrentAccount.shared + @StateObject private var userPreferences = UserPreferences.shared @StateObject private var watcher = StreamWatcher() @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 selectSidebarItem: Tab? = .timeline diff --git a/IceCubesNotifications/NotificationService.swift b/IceCubesNotifications/NotificationService.swift index 36cbef40..599d1729 100644 --- a/IceCubesNotifications/NotificationService.swift +++ b/IceCubesNotifications/NotificationService.swift @@ -54,7 +54,7 @@ class NotificationService: UNNotificationServiceExtension { bestAttemptContent.userInfo["plaintext"] = plaintextData bestAttemptContent.sound = UNNotificationSound.init(named: UNNotificationSoundName(rawValue: "glass.wav")) - let preferences = UserPreferences() + let preferences = UserPreferences.shared preferences.pushNotificationsCount += 1 bestAttemptContent.badge = .init(integerLiteral: preferences.pushNotificationsCount) diff --git a/IceCubesShareExtension/ShareViewController.swift b/IceCubesShareExtension/ShareViewController.swift index 4f09d865..aa27dc39 100644 --- a/IceCubesShareExtension/ShareViewController.swift +++ b/IceCubesShareExtension/ShareViewController.swift @@ -14,18 +14,18 @@ class ShareViewController: UIViewController { super.viewDidLoad() let client = AppAccountsManager.shared.currentClient - let account = CurrentAccount() - let instance = CurrentInstance() + let account = CurrentAccount.shared + let instance = CurrentInstance.shared account.setClient(client: client) instance.setClient(client: client) let colorScheme = traitCollection.userInterfaceStyle - let theme = Theme() + let theme = Theme.shared theme.setColor(withName: colorScheme == .dark ? .iceCubeDark : .iceCubeLight) if let item = extensionContext?.inputItems.first as? NSExtensionItem { if let attachments = item.attachments { let view = StatusEditorView(mode: .shareExtension(items: attachments)) - .environmentObject(UserPreferences()) + .environmentObject(UserPreferences.shared) .environmentObject(client) .environmentObject(account) .environmentObject(theme) diff --git a/Packages/DesignSystem/Sources/DesignSystem/Theme.swift b/Packages/DesignSystem/Sources/DesignSystem/Theme.swift index ac25fd78..4421fcac 100644 --- a/Packages/DesignSystem/Sources/DesignSystem/Theme.swift +++ b/Packages/DesignSystem/Sources/DesignSystem/Theme.swift @@ -80,7 +80,9 @@ public class Theme: ObservableObject { private var cancellables = Set() - public init() { + public static let shared = Theme() + + private init() { selectedSet = storedSet // If theme is never set before set the default store. This should only execute once after install. diff --git a/Packages/Env/Sources/Env/CurrentAccount.swift b/Packages/Env/Sources/Env/CurrentAccount.swift index f877abc5..d0c31ba7 100644 --- a/Packages/Env/Sources/Env/CurrentAccount.swift +++ b/Packages/Env/Sources/Env/CurrentAccount.swift @@ -10,7 +10,9 @@ public class CurrentAccount: ObservableObject { private var client: Client? - public init() { } + static public let shared = CurrentAccount() + + private init() { } public func setClient(client: Client) { self.client = client diff --git a/Packages/Env/Sources/Env/CurrentInstance.swift b/Packages/Env/Sources/Env/CurrentInstance.swift index b0c956d6..7aa1b498 100644 --- a/Packages/Env/Sources/Env/CurrentInstance.swift +++ b/Packages/Env/Sources/Env/CurrentInstance.swift @@ -8,9 +8,9 @@ public class CurrentInstance: ObservableObject { private var client: Client? - public init() { - - } + static public let shared = CurrentInstance() + + private init() { } public func setClient(client: Client) { self.client = client diff --git a/Packages/Env/Sources/Env/UserPreferences.swift b/Packages/Env/Sources/Env/UserPreferences.swift index 98c9a58b..1676f03c 100644 --- a/Packages/Env/Sources/Env/UserPreferences.swift +++ b/Packages/Env/Sources/Env/UserPreferences.swift @@ -6,6 +6,7 @@ import Network @MainActor public class UserPreferences: ObservableObject { public static let sharedDefault = UserDefaults.init(suiteName: "group.icecubesapps") + public static let shared = UserPreferences() private var client: Client? @@ -24,7 +25,7 @@ public class UserPreferences: ObservableObject { @Published public var serverPreferences: ServerPreferences? - public init() { } + private init() { } public func setClient(client: Client) { self.client = client