From 272af63da2c878182d137e6d7396a634f29d5631 Mon Sep 17 00:00:00 2001 From: Abhishek Banthia <8280282+n0shake@users.noreply.github.com> Date: Wed, 13 Apr 2022 15:04:43 -0400 Subject: [PATCH] Use DI for Data Store in Status Item Handler --- Clocker/AppDelegate.swift | 2 +- .../Menu Bar/StatusItemHandler.swift | 29 ++++++++++--------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/Clocker/AppDelegate.swift b/Clocker/AppDelegate.swift index c56f7e1..8bac137 100644 --- a/Clocker/AppDelegate.swift +++ b/Clocker/AppDelegate.swift @@ -115,7 +115,7 @@ open class AppDelegate: NSObject, NSApplicationDelegate { checkIfAppIsAlreadyOpen() // Install the menubar item! - statusBarHandler = StatusItemHandler() + statusBarHandler = StatusItemHandler(with: DataStore.shared()) if ProcessInfo.processInfo.arguments.contains(CLUITestingLaunchArgument) { FirebaseApp.configure() diff --git a/Clocker/Preferences/Menu Bar/StatusItemHandler.swift b/Clocker/Preferences/Menu Bar/StatusItemHandler.swift index f3055bb..e2d3cc8 100644 --- a/Clocker/Preferences/Menu Bar/StatusItemHandler.swift +++ b/Clocker/Preferences/Menu Bar/StatusItemHandler.swift @@ -22,7 +22,7 @@ class StatusItemHandler: NSObject { return statusItem }() - private var menubarTitleHandler = MenubarTitleProvider(with: DataStore.shared()) + private lazy var menubarTitleHandler = MenubarTitleProvider(with: self.store) private var statusContainerView: StatusContainerView? @@ -31,6 +31,8 @@ class StatusItemHandler: NSObject { private lazy var units: Set = Set([.era, .year, .month, .day, .hour, .minute]) private var userNotificationsDidChangeNotif: NSObjectProtocol? + + private let store: DataStore // Current State might be set twice when the user first launches an app. // First, when StatusItemHandler() is instantiated in AppDelegate @@ -63,7 +65,8 @@ class StatusItemHandler: NSObject { } } - override init() { + init(with dataStore: DataStore) { + self.store = dataStore super.init() setupStatusItem() @@ -74,10 +77,10 @@ class StatusItemHandler: NSObject { // Let's figure out the initial menubar state var menubarState = MenubarState.icon - let shouldTextBeDisplayed = DataStore.shared().menubarTimezones()?.isEmpty ?? true + let shouldTextBeDisplayed = store.menubarTimezones()?.isEmpty ?? true - if !shouldTextBeDisplayed || DataStore.shared().shouldDisplay(.showMeetingInMenubar) { - if DataStore.shared().shouldDisplay(.menubarCompactMode) { + if !shouldTextBeDisplayed || store.shouldDisplay(.showMeetingInMenubar) { + if store.shouldDisplay(.menubarCompactMode) { menubarState = .compactText } else { menubarState = .standardText @@ -138,7 +141,7 @@ class StatusItemHandler: NSObject { private func constructCompactView(with upcomingEventView: Bool = false) { statusContainerView = nil - let menubarTimezones = DataStore.shared().menubarTimezones() ?? [] + let menubarTimezones = store.menubarTimezones() ?? [] if menubarTimezones.isEmpty { currentState = .icon return @@ -155,7 +158,7 @@ class StatusItemHandler: NSObject { // Our icon is template, so it changes automatically; so is our standard status bar text // Only need to handle the compact mode! @objc func respondToInterfaceStyleChange() { - if DataStore.shared().shouldDisplay(.menubarCompactMode) { + if store.shouldDisplay(.menubarCompactMode) { updateCompactMenubar() } } @@ -199,11 +202,11 @@ class StatusItemHandler: NSObject { } private func shouldDisplaySecondsInMenubar() -> Bool { - let syncedTimezones = DataStore.shared().menubarTimezones() ?? [] + let syncedTimezones = store.menubarTimezones() ?? [] let timezonesSupportingSeconds = syncedTimezones.filter { data in if let timezoneObj = TimezoneData.customObject(from: data) { - return timezoneObj.shouldShowSeconds(DataStore.shared().timezoneFormat()) + return timezoneObj.shouldShowSeconds(store.timezoneFormat()) } return false } @@ -213,7 +216,7 @@ class StatusItemHandler: NSObject { private func calculateFireDate() -> Date? { let shouldDisplaySeconds = shouldDisplaySecondsInMenubar() - let menubarFavourites = DataStore.shared().menubarTimezones() + let menubarFavourites = store.menubarTimezones() if !units.contains(.second), shouldDisplaySeconds { units.insert(.second) @@ -298,9 +301,9 @@ class StatusItemHandler: NSObject { // Check if user is not showing // 1. Timezones // 2. Upcoming Event - let menubarFavourites = DataStore.shared().menubarTimezones() ?? [] + let menubarFavourites = store.menubarTimezones() ?? [] - if menubarFavourites.isEmpty, DataStore.shared().shouldDisplay(.showMeetingInMenubar) == false { + if menubarFavourites.isEmpty, store.shouldDisplay(.showMeetingInMenubar) == false { Logger.info("Invalidating menubar timer!") invalidation() @@ -347,7 +350,7 @@ class StatusItemHandler: NSObject { if let menubarTitle = menubarTitleHandler.titleForMenubar() { menubarText = menubarTitle - } else if DataStore.shared().shouldDisplay(.showMeetingInMenubar) { + } else if store.shouldDisplay(.showMeetingInMenubar) { // Don't have any meeting to show } else { // We have no favourites to display and no meetings to show.