Browse Source

DI Event Store in Menubar Title Provider.

pull/113/head
Abhishek 3 years ago
parent
commit
0cd1739250
  1. 28
      Clocker/ClockerUnitTests/StandardMenubarHandlerTests.swift
  2. 2
      Clocker/Panel/ParentPanelController.swift
  3. 10
      Clocker/Preferences/Menu Bar/MenubarTitleProvider.swift
  4. 4
      Clocker/Preferences/Menu Bar/StatusItemHandler.swift

28
Clocker/ClockerUnitTests/StandardMenubarHandlerTests.swift

@ -19,7 +19,7 @@ class StandardMenubarHandlerTests: XCTestCase {
private func makeMockStore(with menubarMode: Int = 1) -> DataStore { private func makeMockStore(with menubarMode: Int = 1) -> DataStore {
// Wipe all timezones from UserDefaults // Wipe all timezones from UserDefaults
let defaults = UserDefaults(suiteName: "com.abhishek.Clocker.StandardMenubarHandlerTests")! let defaults = UserDefaults(suiteName: "com.abhishek.Clocker.StandardMenubarHandlerTests")!
defaults.set(menubarMode, forKey: CLMenubarCompactMode) defaults.set(menubarMode, forKey: CLMenubarCompactMode)
defaults.set(0, forKey: CLShowMeetingInMenubar) defaults.set(0, forKey: CLShowMeetingInMenubar)
XCTAssertNotEqual(defaults, UserDefaults.standard) XCTAssertNotEqual(defaults, UserDefaults.standard)
@ -67,7 +67,7 @@ class StandardMenubarHandlerTests: XCTestCase {
let store = makeMockStore() let store = makeMockStore()
// Wipe all timezones from UserDefaults // Wipe all timezones from UserDefaults
store.setTimezones(nil) store.setTimezones(nil)
let menubarHandler = MenubarTitleProvider(with: store) let menubarHandler = MenubarTitleProvider(with: store, eventStore: EventCenter.sharedCenter())
let emptyMenubarString = menubarHandler.titleForMenubar() let emptyMenubarString = menubarHandler.titleForMenubar()
// Returns early because DataStore.menubarTimezones is nil // Returns early because DataStore.menubarTimezones is nil
XCTAssertNil(emptyMenubarString) XCTAssertNil(emptyMenubarString)
@ -86,7 +86,7 @@ class StandardMenubarHandlerTests: XCTestCase {
func testWithEmptyMenubarTimezones() { func testWithEmptyMenubarTimezones() {
let store = makeMockStore() let store = makeMockStore()
store.setTimezones(nil) store.setTimezones(nil)
let menubarHandler = MenubarTitleProvider(with: store) let menubarHandler = MenubarTitleProvider(with: store, eventStore: EventCenter.sharedCenter())
XCTAssertNil(menubarHandler.titleForMenubar()) XCTAssertNil(menubarHandler.titleForMenubar())
} }
@ -98,7 +98,7 @@ class StandardMenubarHandlerTests: XCTestCase {
dataObject.isFavourite = 1 dataObject.isFavourite = 1
saveObject(object: dataObject, in: store) saveObject(object: dataObject, in: store)
let menubarHandler = MenubarTitleProvider(with: store) let menubarHandler = MenubarTitleProvider(with: store, eventStore: EventCenter.sharedCenter())
XCTAssertNil(menubarHandler.titleForMenubar()) XCTAssertNil(menubarHandler.titleForMenubar())
} }
@ -110,7 +110,7 @@ class StandardMenubarHandlerTests: XCTestCase {
dataObject.isFavourite = 1 dataObject.isFavourite = 1
saveObject(object: dataObject, in: store) saveObject(object: dataObject, in: store)
let menubarHandler = MenubarTitleProvider(with: store) let menubarHandler = MenubarTitleProvider(with: store, eventStore: EventCenter.sharedCenter())
XCTAssertNotNil(menubarHandler.titleForMenubar()) XCTAssertNotNil(menubarHandler.titleForMenubar())
} }
@ -122,7 +122,7 @@ class StandardMenubarHandlerTests: XCTestCase {
mockEvent.title = "Mock Title" mockEvent.title = "Mock Title"
mockEvent.startDate = Date().add(futureChunk) mockEvent.startDate = Date().add(futureChunk)
let menubarHandler = MenubarTitleProvider(with: store) let menubarHandler = MenubarTitleProvider(with: store, eventStore: EventCenter.sharedCenter())
XCTAssert(menubarHandler.format(event: mockEvent) == "Mock Title in 10m", XCTAssert(menubarHandler.format(event: mockEvent) == "Mock Title in 10m",
"Suffix \(menubarHandler.format(event: mockEvent)) doesn't match expectation") "Suffix \(menubarHandler.format(event: mockEvent)) doesn't match expectation")
} }
@ -135,7 +135,7 @@ class StandardMenubarHandlerTests: XCTestCase {
mockEvent.title = "Mock Title" mockEvent.title = "Mock Title"
mockEvent.startDate = Date().add(futureChunk) mockEvent.startDate = Date().add(futureChunk)
let menubarHandler = MenubarTitleProvider(with: store) let menubarHandler = MenubarTitleProvider(with: store, eventStore: EventCenter.sharedCenter())
XCTAssert(menubarHandler.format(event: mockEvent) == "Mock Title in 1m", XCTAssert(menubarHandler.format(event: mockEvent) == "Mock Title in 1m",
"Suffix \(menubarHandler.format(event: mockEvent)) doesn't match expectation") "Suffix \(menubarHandler.format(event: mockEvent)) doesn't match expectation")
} }
@ -148,7 +148,7 @@ class StandardMenubarHandlerTests: XCTestCase {
mockEvent.title = "Mock Title" mockEvent.title = "Mock Title"
mockEvent.startDate = Date().add(futureChunk) mockEvent.startDate = Date().add(futureChunk)
let menubarHandler = MenubarTitleProvider(with: store) let menubarHandler = MenubarTitleProvider(with: store, eventStore: EventCenter.sharedCenter())
XCTAssert(menubarHandler.format(event: mockEvent) == "Mock Title starts now.", XCTAssert(menubarHandler.format(event: mockEvent) == "Mock Title starts now.",
"Suffix \(menubarHandler.format(event: mockEvent)) doesn't match expectation") "Suffix \(menubarHandler.format(event: mockEvent)) doesn't match expectation")
} }
@ -160,7 +160,7 @@ class StandardMenubarHandlerTests: XCTestCase {
let mockEvent = EKEvent(eventStore: eventStore) let mockEvent = EKEvent(eventStore: eventStore)
mockEvent.startDate = Date().add(futureChunk) mockEvent.startDate = Date().add(futureChunk)
let menubarHandler = MenubarTitleProvider(with: store) let menubarHandler = MenubarTitleProvider(with: store, eventStore: EventCenter.sharedCenter())
XCTAssert(menubarHandler.format(event: mockEvent) == CLEmptyString, XCTAssert(menubarHandler.format(event: mockEvent) == CLEmptyString,
"Suffix \(menubarHandler.format(event: mockEvent)) doesn't match expectation") "Suffix \(menubarHandler.format(event: mockEvent)) doesn't match expectation")
} }
@ -173,7 +173,7 @@ class StandardMenubarHandlerTests: XCTestCase {
mockEvent.title = "Really long calendar event title that longer than the longest name" mockEvent.title = "Really long calendar event title that longer than the longest name"
mockEvent.startDate = Date().add(futureChunk) mockEvent.startDate = Date().add(futureChunk)
let menubarHandler = MenubarTitleProvider(with: store) let menubarHandler = MenubarTitleProvider(with: store, eventStore: EventCenter.sharedCenter())
XCTAssert(menubarHandler.format(event: mockEvent) == "Really long calendar event tit... starts now.", XCTAssert(menubarHandler.format(event: mockEvent) == "Really long calendar event tit... starts now.",
"Suffix \(menubarHandler.format(event: mockEvent)) doesn't match expectation") "Suffix \(menubarHandler.format(event: mockEvent)) doesn't match expectation")
} }
@ -190,7 +190,7 @@ class StandardMenubarHandlerTests: XCTestCase {
meetingURL: nil, meetingURL: nil,
attendeStatus: .accepted) attendeStatus: .accepted)
let menubarHandler = MenubarTitleProvider(with: store) let menubarHandler = MenubarTitleProvider(with: store, eventStore: EventCenter.sharedCenter())
let calendar = Calendar.autoupdatingCurrent let calendar = Calendar.autoupdatingCurrent
let events: [Date: [EventInfo]] = [calendar.startOfDay(for: Date()): [eventInfo]] let events: [Date: [EventInfo]] = [calendar.startOfDay(for: Date()): [eventInfo]]
let actualResult = try XCTUnwrap(menubarHandler.checkForUpcomingEvents(events, calendar: calendar)) let actualResult = try XCTUnwrap(menubarHandler.checkForUpcomingEvents(events, calendar: calendar))
@ -210,7 +210,7 @@ class StandardMenubarHandlerTests: XCTestCase {
meetingURL: nil, meetingURL: nil,
attendeStatus: .accepted) attendeStatus: .accepted)
let menubarHandler = MenubarTitleProvider(with: store) let menubarHandler = MenubarTitleProvider(with: store, eventStore: EventCenter.sharedCenter())
let calendar = Calendar.autoupdatingCurrent let calendar = Calendar.autoupdatingCurrent
let events: [Date: [EventInfo]] = [calendar.startOfDay(for: Date()): [eventInfo]] let events: [Date: [EventInfo]] = [calendar.startOfDay(for: Date()): [eventInfo]]
let actualResult = try XCTUnwrap(menubarHandler.checkForUpcomingEvents(events, calendar: calendar)) let actualResult = try XCTUnwrap(menubarHandler.checkForUpcomingEvents(events, calendar: calendar))
@ -230,7 +230,7 @@ class StandardMenubarHandlerTests: XCTestCase {
meetingURL: nil, meetingURL: nil,
attendeStatus: .accepted) attendeStatus: .accepted)
let menubarHandler = MenubarTitleProvider(with: store) let menubarHandler = MenubarTitleProvider(with: store, eventStore: EventCenter.sharedCenter())
let calendar = Calendar.autoupdatingCurrent let calendar = Calendar.autoupdatingCurrent
let events: [Date: [EventInfo]] = [calendar.startOfDay(for: Date()): [eventInfo]] let events: [Date: [EventInfo]] = [calendar.startOfDay(for: Date()): [eventInfo]]
XCTAssertNil(menubarHandler.checkForUpcomingEvents(events, calendar: calendar)) XCTAssertNil(menubarHandler.checkForUpcomingEvents(events, calendar: calendar))
@ -247,7 +247,7 @@ class StandardMenubarHandlerTests: XCTestCase {
meetingURL: nil, meetingURL: nil,
attendeStatus: .accepted) attendeStatus: .accepted)
let menubarHandler = MenubarTitleProvider(with: store) let menubarHandler = MenubarTitleProvider(with: store, eventStore: EventCenter.sharedCenter())
let calendar = Calendar.autoupdatingCurrent let calendar = Calendar.autoupdatingCurrent
let events: [Date: [EventInfo]] = [calendar.startOfDay(for: Date()): [eventInfo]] let events: [Date: [EventInfo]] = [calendar.startOfDay(for: Date()): [eventInfo]]
XCTAssertNil(menubarHandler.checkForUpcomingEvents(events, calendar: calendar)) XCTAssertNil(menubarHandler.checkForUpcomingEvents(events, calendar: calendar))

2
Clocker/Panel/ParentPanelController.swift

@ -584,7 +584,7 @@ class ParentPanelController: NSWindowController {
Logger.log(object: nil, for: "Deleted Timezone Through Swipe") Logger.log(object: nil, for: "Deleted Timezone Through Swipe")
} }
private lazy var menubarTitleHandler = MenubarTitleProvider(with: DataStore.shared()) private lazy var menubarTitleHandler = MenubarTitleProvider(with: DataStore.shared(), eventStore: EventCenter.sharedCenter())
@objc func updateTime() { @objc func updateTime() {
let store = DataStore.shared() let store = DataStore.shared()

10
Clocker/Preferences/Menu Bar/MenubarTitleProvider.swift

@ -7,16 +7,18 @@ import EventKit
class MenubarTitleProvider: NSObject { class MenubarTitleProvider: NSObject {
private let store: DataStore private let store: DataStore
private let eventCenter: EventCenter
init(with dataStore: DataStore) { init(with dataStore: DataStore, eventStore: EventCenter) {
store = dataStore store = dataStore
eventCenter = eventStore
super.init() super.init()
} }
func titleForMenubar() -> String? { func titleForMenubar() -> String? {
let eventCenter = EventCenter.sharedCenter().eventsForDate let filteredEvents = eventCenter.eventsForDate
let autoupdatingCalendar = EventCenter.sharedCenter().autoupdatingCalendar let autoupdatingCalendar = eventCenter.autoupdatingCalendar
if let nextEvent = checkForUpcomingEvents(eventCenter, calendar: autoupdatingCalendar) { if let nextEvent = checkForUpcomingEvents(filteredEvents, calendar: autoupdatingCalendar) {
return nextEvent return nextEvent
} }

4
Clocker/Preferences/Menu Bar/StatusItemHandler.swift

@ -22,7 +22,7 @@ class StatusItemHandler: NSObject {
return statusItem return statusItem
}() }()
private lazy var menubarTitleHandler = MenubarTitleProvider(with: self.store) private lazy var menubarTitleHandler = MenubarTitleProvider(with: self.store, eventStore: EventCenter.sharedCenter())
private var statusContainerView: StatusContainerView? private var statusContainerView: StatusContainerView?
@ -380,7 +380,7 @@ class StatusItemHandler: NSObject {
let filteredEvents = EventCenter.sharedCenter().filteredEvents let filteredEvents = EventCenter.sharedCenter().filteredEvents
let calendar = EventCenter.sharedCenter().autoupdatingCalendar let calendar = EventCenter.sharedCenter().autoupdatingCalendar
let checkForUpcomingEvents = menubarTitleHandler.checkForUpcomingEvents(filteredEvents, calendar: calendar) let checkForUpcomingEvents = menubarTitleHandler.checkForUpcomingEvents(filteredEvents, calendar: calendar)
constructCompactView(with: checkForUpcomingEvents != nil) constructCompactView(with: checkForUpcomingEvents != nil)
updateMenubar() updateMenubar()
} }

Loading…
Cancel
Save