Browse Source

Use DI for Data Store in Status Item Handler

pull/113/head
Abhishek Banthia 3 years ago
parent
commit
272af63da2
  1. 2
      Clocker/AppDelegate.swift
  2. 29
      Clocker/Preferences/Menu Bar/StatusItemHandler.swift

2
Clocker/AppDelegate.swift

@ -115,7 +115,7 @@ open class AppDelegate: NSObject, NSApplicationDelegate {
checkIfAppIsAlreadyOpen() checkIfAppIsAlreadyOpen()
// Install the menubar item! // Install the menubar item!
statusBarHandler = StatusItemHandler() statusBarHandler = StatusItemHandler(with: DataStore.shared())
if ProcessInfo.processInfo.arguments.contains(CLUITestingLaunchArgument) { if ProcessInfo.processInfo.arguments.contains(CLUITestingLaunchArgument) {
FirebaseApp.configure() FirebaseApp.configure()

29
Clocker/Preferences/Menu Bar/StatusItemHandler.swift

@ -22,7 +22,7 @@ class StatusItemHandler: NSObject {
return statusItem return statusItem
}() }()
private var menubarTitleHandler = MenubarTitleProvider(with: DataStore.shared()) private lazy var menubarTitleHandler = MenubarTitleProvider(with: self.store)
private var statusContainerView: StatusContainerView? private var statusContainerView: StatusContainerView?
@ -32,6 +32,8 @@ class StatusItemHandler: NSObject {
private var userNotificationsDidChangeNotif: NSObjectProtocol? private var userNotificationsDidChangeNotif: NSObjectProtocol?
private let store: DataStore
// Current State might be set twice when the user first launches an app. // Current State might be set twice when the user first launches an app.
// First, when StatusItemHandler() is instantiated in AppDelegate // First, when StatusItemHandler() is instantiated in AppDelegate
// Second, when AppDelegate.fetchLocalTimezone() is called triggering a customLabel didSet. // Second, when AppDelegate.fetchLocalTimezone() is called triggering a customLabel didSet.
@ -63,7 +65,8 @@ class StatusItemHandler: NSObject {
} }
} }
override init() { init(with dataStore: DataStore) {
self.store = dataStore
super.init() super.init()
setupStatusItem() setupStatusItem()
@ -74,10 +77,10 @@ class StatusItemHandler: NSObject {
// Let's figure out the initial menubar state // Let's figure out the initial menubar state
var menubarState = MenubarState.icon var menubarState = MenubarState.icon
let shouldTextBeDisplayed = DataStore.shared().menubarTimezones()?.isEmpty ?? true let shouldTextBeDisplayed = store.menubarTimezones()?.isEmpty ?? true
if !shouldTextBeDisplayed || DataStore.shared().shouldDisplay(.showMeetingInMenubar) { if !shouldTextBeDisplayed || store.shouldDisplay(.showMeetingInMenubar) {
if DataStore.shared().shouldDisplay(.menubarCompactMode) { if store.shouldDisplay(.menubarCompactMode) {
menubarState = .compactText menubarState = .compactText
} else { } else {
menubarState = .standardText menubarState = .standardText
@ -138,7 +141,7 @@ class StatusItemHandler: NSObject {
private func constructCompactView(with upcomingEventView: Bool = false) { private func constructCompactView(with upcomingEventView: Bool = false) {
statusContainerView = nil statusContainerView = nil
let menubarTimezones = DataStore.shared().menubarTimezones() ?? [] let menubarTimezones = store.menubarTimezones() ?? []
if menubarTimezones.isEmpty { if menubarTimezones.isEmpty {
currentState = .icon currentState = .icon
return return
@ -155,7 +158,7 @@ class StatusItemHandler: NSObject {
// Our icon is template, so it changes automatically; so is our standard status bar text // Our icon is template, so it changes automatically; so is our standard status bar text
// Only need to handle the compact mode! // Only need to handle the compact mode!
@objc func respondToInterfaceStyleChange() { @objc func respondToInterfaceStyleChange() {
if DataStore.shared().shouldDisplay(.menubarCompactMode) { if store.shouldDisplay(.menubarCompactMode) {
updateCompactMenubar() updateCompactMenubar()
} }
} }
@ -199,11 +202,11 @@ class StatusItemHandler: NSObject {
} }
private func shouldDisplaySecondsInMenubar() -> Bool { private func shouldDisplaySecondsInMenubar() -> Bool {
let syncedTimezones = DataStore.shared().menubarTimezones() ?? [] let syncedTimezones = store.menubarTimezones() ?? []
let timezonesSupportingSeconds = syncedTimezones.filter { data in let timezonesSupportingSeconds = syncedTimezones.filter { data in
if let timezoneObj = TimezoneData.customObject(from: data) { if let timezoneObj = TimezoneData.customObject(from: data) {
return timezoneObj.shouldShowSeconds(DataStore.shared().timezoneFormat()) return timezoneObj.shouldShowSeconds(store.timezoneFormat())
} }
return false return false
} }
@ -213,7 +216,7 @@ class StatusItemHandler: NSObject {
private func calculateFireDate() -> Date? { private func calculateFireDate() -> Date? {
let shouldDisplaySeconds = shouldDisplaySecondsInMenubar() let shouldDisplaySeconds = shouldDisplaySecondsInMenubar()
let menubarFavourites = DataStore.shared().menubarTimezones() let menubarFavourites = store.menubarTimezones()
if !units.contains(.second), shouldDisplaySeconds { if !units.contains(.second), shouldDisplaySeconds {
units.insert(.second) units.insert(.second)
@ -298,9 +301,9 @@ class StatusItemHandler: NSObject {
// Check if user is not showing // Check if user is not showing
// 1. Timezones // 1. Timezones
// 2. Upcoming Event // 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!") Logger.info("Invalidating menubar timer!")
invalidation() invalidation()
@ -347,7 +350,7 @@ class StatusItemHandler: NSObject {
if let menubarTitle = menubarTitleHandler.titleForMenubar() { if let menubarTitle = menubarTitleHandler.titleForMenubar() {
menubarText = menubarTitle menubarText = menubarTitle
} else if DataStore.shared().shouldDisplay(.showMeetingInMenubar) { } else if store.shouldDisplay(.showMeetingInMenubar) {
// Don't have any meeting to show // Don't have any meeting to show
} else { } else {
// We have no favourites to display and no meetings to show. // We have no favourites to display and no meetings to show.

Loading…
Cancel
Save