mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-10 17:08:39 +00:00
Seperated button view and event source logic
This commit is contained in:
parent
8b5c2f10b7
commit
a89c9506f3
@ -12,7 +12,6 @@ import EventKit
|
|||||||
|
|
||||||
class UpNextBarItem: CustomButtonTouchBarItem {
|
class UpNextBarItem: CustomButtonTouchBarItem {
|
||||||
private let activity: NSBackgroundActivityScheduler // Update scheduler
|
private let activity: NSBackgroundActivityScheduler // Update scheduler
|
||||||
private let eventStore = EKEventStore() //
|
|
||||||
private let df = DateFormatter()
|
private let df = DateFormatter()
|
||||||
private let buttonTemplate = "🗓 %@ - %@ "
|
private let buttonTemplate = "🗓 %@ - %@ "
|
||||||
|
|
||||||
@ -22,8 +21,7 @@ class UpNextBarItem: CustomButtonTouchBarItem {
|
|||||||
private var nthEvent: Int
|
private var nthEvent: Int
|
||||||
|
|
||||||
// State
|
// State
|
||||||
private var hasPermission: Bool = false
|
private var eventSources : [IUpNextSource] = []
|
||||||
|
|
||||||
|
|
||||||
/// <#Description#>
|
/// <#Description#>
|
||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
@ -33,45 +31,22 @@ class UpNextBarItem: CustomButtonTouchBarItem {
|
|||||||
/// - to: Relative to current time, how far forward we search for events in hours
|
/// - to: Relative to current time, how far forward we search for events in hours
|
||||||
/// - nthEvent: Which event to show (1 is first, 2 is second, and so on)
|
/// - nthEvent: Which event to show (1 is first, 2 is second, and so on)
|
||||||
init(identifier: NSTouchBarItem.Identifier, interval: TimeInterval, from: Double, to: Double, nthEvent: Int) {
|
init(identifier: NSTouchBarItem.Identifier, interval: TimeInterval, from: Double, to: Double, nthEvent: Int) {
|
||||||
|
// Initialise member properties
|
||||||
activity = NSBackgroundActivityScheduler(identifier: "\(identifier.rawValue).updateCheck")
|
activity = NSBackgroundActivityScheduler(identifier: "\(identifier.rawValue).updateCheck")
|
||||||
activity.interval = interval
|
pastSearchCutoff = from * 360
|
||||||
self.pastSearchCutoff = from
|
futureSearchCutoff = to * 360
|
||||||
self.futureSearchCutoff = to
|
|
||||||
self.nthEvent = nthEvent
|
self.nthEvent = nthEvent
|
||||||
self.df.dateFormat = "HH:mm"
|
df.dateFormat = "HH:mm"
|
||||||
|
// Error handling
|
||||||
if (nthEvent <= 0) {
|
if (nthEvent <= 0) {
|
||||||
fatalError("Error on UpNext bar item. nthEvent property must be greater than 0.")
|
fatalError("Error on UpNext bar item. nthEvent property must be greater than 0.")
|
||||||
}
|
}
|
||||||
|
// Init super
|
||||||
super.init(identifier: identifier, title: " ")
|
super.init(identifier: identifier, title: " ")
|
||||||
let authorizationStatus = EKEventStore.authorizationStatus(for: EKEntityType.event)
|
// Add event sources
|
||||||
switch authorizationStatus {
|
self.eventSources.append(UpNextCalenderSource())
|
||||||
case .notDetermined:
|
|
||||||
print("notDetermined")
|
|
||||||
case .restricted:
|
|
||||||
print("restricted")
|
|
||||||
case .denied:
|
|
||||||
print("denied")
|
|
||||||
case .authorized:
|
|
||||||
print("authorizded")
|
|
||||||
default:
|
|
||||||
print("Unkown EKEventStore authorization status")
|
|
||||||
}
|
|
||||||
eventStore.requestAccess(to: .event){ granted, error in
|
|
||||||
self.hasPermission = granted;
|
|
||||||
if(!granted) {
|
|
||||||
NSLog("Error: MTMR UpNextBarWidget not given calendar access.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
self.updateView()
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
tapClosure = { [weak self] in self?.gotoAppleCalendar() }
|
|
||||||
|
|
||||||
|
|
||||||
// Start activity to update view
|
// Start activity to update view
|
||||||
|
activity.interval = interval
|
||||||
activity.repeats = true
|
activity.repeats = true
|
||||||
activity.qualityOfService = .utility
|
activity.qualityOfService = .utility
|
||||||
activity.schedule { (completion: NSBackgroundActivityScheduler.CompletionHandler) in
|
activity.schedule { (completion: NSBackgroundActivityScheduler.CompletionHandler) in
|
||||||
@ -79,7 +54,6 @@ class UpNextBarItem: CustomButtonTouchBarItem {
|
|||||||
completion(NSBackgroundActivityScheduler.Result.finished)
|
completion(NSBackgroundActivityScheduler.Result.finished)
|
||||||
}
|
}
|
||||||
updateView()
|
updateView()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
required init?(coder _: NSCoder) {
|
required init?(coder _: NSCoder) {
|
||||||
@ -87,10 +61,6 @@ class UpNextBarItem: CustomButtonTouchBarItem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func updateView() {
|
func updateView() {
|
||||||
if (!self.hasPermission) {
|
|
||||||
self.title = "🗓 No permissions"
|
|
||||||
return
|
|
||||||
}
|
|
||||||
var upcomingEvents = self.getUpcomingEvents()
|
var upcomingEvents = self.getUpcomingEvents()
|
||||||
upcomingEvents.sort(by: {$0.startDate.compare($1.startDate) == .orderedAscending})
|
upcomingEvents.sort(by: {$0.startDate.compare($1.startDate) == .orderedAscending})
|
||||||
for event in upcomingEvents {
|
for event in upcomingEvents {
|
||||||
@ -102,34 +72,71 @@ class UpNextBarItem: CustomButtonTouchBarItem {
|
|||||||
let startDateString = self.df.string(for: event.startDate)
|
let startDateString = self.df.string(for: event.startDate)
|
||||||
print("TITLE: " + title + " STARTDATE: " + (startDateString ?? "nil"))
|
print("TITLE: " + title + " STARTDATE: " + (startDateString ?? "nil"))
|
||||||
|
|
||||||
DispatchQueue.main.async {
|
print("SHOW")
|
||||||
print("SHOW")
|
self.image = nil
|
||||||
self.image = nil
|
self.title = String(format: self.buttonTemplate, title, startDateString ?? "No time")
|
||||||
self.title = String(format: self.buttonTemplate, title, startDateString ?? "No time")
|
// self.view.widthAnchor.constraint(lessThanOrEqualToConstant: 1000 as CGFloat).isActive = true
|
||||||
self.view.isHidden = false
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// Do not display any event
|
// Do not display any event
|
||||||
DispatchQueue.main.async {
|
print("HIDE " + String(upcomingEvents.count) + " " + String(self.nthEvent) + " " + String(upcomingEvents.count > self.nthEvent))
|
||||||
print("HIDE " + String(upcomingEvents.count) + " " + String(self.nthEvent) + " " + String(upcomingEvents.count > self.nthEvent))
|
self.image = nil
|
||||||
self.image = nil
|
self.title = ""
|
||||||
self.title = ""
|
// self.setWidth(value: 0)
|
||||||
self.view.isHidden = true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getUpcomingEvents() -> [UpNextEventModel] {
|
func getUpcomingEvents() -> [UpNextEventModel] {
|
||||||
var upcomingEvents: [UpNextEventModel] = []
|
var upcomingEvents: [UpNextEventModel] = []
|
||||||
|
|
||||||
NSLog("Getting calendar events...")
|
|
||||||
// Calculate the range we're going to search for events in
|
// Calculate the range we're going to search for events in
|
||||||
let dateLowerBounds = Date(timeIntervalSinceNow: self.pastSearchCutoff * 360)
|
let dateLowerBounds = Date(timeIntervalSinceNow: self.pastSearchCutoff)
|
||||||
let dateUpperBounds = Date(timeIntervalSinceNow: self.futureSearchCutoff * 360)
|
let dateUpperBounds = Date(timeIntervalSinceNow: self.futureSearchCutoff)
|
||||||
|
|
||||||
|
// Get all events from all sources
|
||||||
|
for eventSource in self.eventSources {
|
||||||
|
if (eventSource.hasPermission) {
|
||||||
|
let events = eventSource.getUpcomingEvents(dateLowerBounds: dateLowerBounds, dateUpperBounds: dateUpperBounds)
|
||||||
|
upcomingEvents.append(contentsOf: events)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return upcomingEvents
|
||||||
|
}
|
||||||
|
|
||||||
|
func gotoAppleCalendar() {
|
||||||
|
print("CLICK")
|
||||||
|
NSWorkspace.shared.open(URL(fileURLWithPath: "/Applications/Calendar.app"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Model for events to be displayed in dock
|
||||||
|
struct UpNextEventModel {
|
||||||
|
var title: String
|
||||||
|
var startDate: Date
|
||||||
|
}
|
||||||
|
// Interface for any event source
|
||||||
|
protocol IUpNextSource {
|
||||||
|
var hasPermission : Bool { get }
|
||||||
|
init()
|
||||||
|
func getUpcomingEvents(dateLowerBounds: Date, dateUpperBounds: Date) -> [UpNextEventModel]
|
||||||
|
}
|
||||||
|
|
||||||
|
class UpNextCalenderSource : IUpNextSource {
|
||||||
|
public var hasPermission: Bool = false
|
||||||
|
private let eventStore = EKEventStore() //
|
||||||
|
required init() {
|
||||||
|
eventStore.requestAccess(to: .event){ granted, error in
|
||||||
|
self.hasPermission = granted;
|
||||||
|
if(!granted) {
|
||||||
|
NSLog("Error: MTMR UpNextBarWidget not given calendar access.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public func getUpcomingEvents(dateLowerBounds: Date, dateUpperBounds: Date) -> [UpNextEventModel] {
|
||||||
|
NSLog("Getting calendar events...")
|
||||||
|
var upcomingEvents: [UpNextEventModel] = []
|
||||||
let calendars = self.eventStore.calendars(for: .event)
|
let calendars = self.eventStore.calendars(for: .event)
|
||||||
|
|
||||||
|
|
||||||
for calendar in calendars {
|
for calendar in calendars {
|
||||||
|
|
||||||
let predicate = self.eventStore.predicateForEvents(withStart: dateLowerBounds, end: dateUpperBounds, calendars: [calendar])
|
let predicate = self.eventStore.predicateForEvents(withStart: dateLowerBounds, end: dateUpperBounds, calendars: [calendar])
|
||||||
@ -143,13 +150,4 @@ class UpNextBarItem: CustomButtonTouchBarItem {
|
|||||||
print("Found " + String(upcomingEvents.count) + " events.")
|
print("Found " + String(upcomingEvents.count) + " events.")
|
||||||
return upcomingEvents
|
return upcomingEvents
|
||||||
}
|
}
|
||||||
|
|
||||||
func gotoAppleCalendar() {
|
|
||||||
print("CLICK")
|
|
||||||
NSWorkspace.shared.open(URL(fileURLWithPath: "/Applications/Photos.app"))
|
|
||||||
}}
|
|
||||||
|
|
||||||
struct UpNextEventModel {
|
|
||||||
var title: String
|
|
||||||
var startDate: Date
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user