mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-11 09:28:38 +00:00
Added the ability to view multiple events
This commit is contained in:
parent
a12568368c
commit
42d6c077f2
@ -10,18 +10,18 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import EventKit
|
import EventKit
|
||||||
|
|
||||||
class UpNextBarItem: CustomButtonTouchBarItem {
|
class UpNextBarItem: NSCustomTouchBarItem {
|
||||||
|
// Dependencies
|
||||||
|
private let scrollView = NSScrollView()
|
||||||
private let activity: NSBackgroundActivityScheduler // Update scheduler
|
private let activity: NSBackgroundActivityScheduler // Update scheduler
|
||||||
private let df = DateFormatter()
|
private var eventSources : [IUpNextSource] = []
|
||||||
private let buttonTemplate = "🗓 %@ - %@ "
|
private var items: [UpNextItem] = []
|
||||||
|
|
||||||
// Settings
|
// Settings
|
||||||
private var futureSearchCutoff: Double
|
private var futureSearchCutoff: Double
|
||||||
private var pastSearchCutoff: Double
|
private var pastSearchCutoff: Double
|
||||||
private var nthEvent: Int
|
private var nthEvent: Int
|
||||||
|
private var widthConstraint: NSLayoutConstraint?
|
||||||
// State
|
|
||||||
private var eventSources : [IUpNextSource] = []
|
|
||||||
|
|
||||||
/// <#Description#>
|
/// <#Description#>
|
||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
@ -33,16 +33,17 @@ class UpNextBarItem: CustomButtonTouchBarItem {
|
|||||||
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
|
// Initialise member properties
|
||||||
activity = NSBackgroundActivityScheduler(identifier: "\(identifier.rawValue).updateCheck")
|
activity = NSBackgroundActivityScheduler(identifier: "\(identifier.rawValue).updateCheck")
|
||||||
pastSearchCutoff = from * 360
|
pastSearchCutoff = from * 3600
|
||||||
futureSearchCutoff = to * 360
|
futureSearchCutoff = to * 3600
|
||||||
self.nthEvent = nthEvent
|
self.nthEvent = nthEvent
|
||||||
df.dateFormat = "HH:mm"
|
UpNextItem.df.dateFormat = "HH:mm"
|
||||||
// Error handling
|
// 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
|
// Init super
|
||||||
super.init(identifier: identifier, title: " ")
|
super.init(identifier: identifier)
|
||||||
|
view = scrollView
|
||||||
// Add event sources
|
// Add event sources
|
||||||
self.eventSources.append(UpNextCalenderSource())
|
self.eventSources.append(UpNextCalenderSource())
|
||||||
// Start activity to update view
|
// Start activity to update view
|
||||||
@ -54,38 +55,47 @@ class UpNextBarItem: CustomButtonTouchBarItem {
|
|||||||
completion(NSBackgroundActivityScheduler.Result.finished)
|
completion(NSBackgroundActivityScheduler.Result.finished)
|
||||||
}
|
}
|
||||||
updateView()
|
updateView()
|
||||||
|
|
||||||
|
let upperBoundsDate = Date(timeIntervalSinceNow: futureSearchCutoff)
|
||||||
|
NSLog("Searching up to \(upperBoundsDate)")
|
||||||
}
|
}
|
||||||
|
|
||||||
required init?(coder _: NSCoder) {
|
required init?(coder _: NSCoder) {
|
||||||
fatalError("init(coder:) has not been implemented")
|
fatalError("init(coder:) has not been implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateView() {
|
private func updateView() {
|
||||||
|
items = []
|
||||||
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})
|
||||||
|
var index = 1
|
||||||
for event in upcomingEvents {
|
for event in upcomingEvents {
|
||||||
print("\(event.title) - \(event.startDate)")
|
// Create UpNextItem
|
||||||
|
let item = UpNextItem(event: event)
|
||||||
|
item.isBordered = false;
|
||||||
|
items.append(item)
|
||||||
|
// Check if should display any more
|
||||||
|
if (index == self.nthEvent) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
index += 1
|
||||||
|
}
|
||||||
|
self.reloadData()
|
||||||
}
|
}
|
||||||
if (upcomingEvents.count >= self.nthEvent) {
|
|
||||||
let event = upcomingEvents[self.nthEvent-1]
|
|
||||||
let title = event.title
|
|
||||||
let startDateString = self.df.string(for: event.startDate)
|
|
||||||
print("TITLE: " + title + " STARTDATE: " + (startDateString ?? "nil"))
|
|
||||||
|
|
||||||
print("SHOW")
|
private func reloadData() {
|
||||||
self.image = nil
|
NSLog("Displaying \(items.count) items...")
|
||||||
self.title = String(format: self.buttonTemplate, title, startDateString ?? "No time")
|
let stackView = NSStackView(views: items.compactMap { $0.view })
|
||||||
// self.view.widthAnchor.constraint(lessThanOrEqualToConstant: 1000 as CGFloat).isActive = true
|
DispatchQueue.main.async {
|
||||||
} else {
|
stackView.spacing = 1
|
||||||
// Do not display any event
|
stackView.orientation = .horizontal
|
||||||
print("HIDE " + String(upcomingEvents.count) + " " + String(self.nthEvent) + " " + String(upcomingEvents.count > self.nthEvent))
|
let visibleRect = self.scrollView.documentVisibleRect
|
||||||
self.image = nil
|
self.scrollView.documentView = stackView
|
||||||
self.title = ""
|
stackView.scroll(visibleRect.origin)
|
||||||
// self.setWidth(value: 0)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getUpcomingEvents() -> [UpNextEventModel] {
|
private func getUpcomingEvents() -> [UpNextEventModel] {
|
||||||
var upcomingEvents: [UpNextEventModel] = []
|
var upcomingEvents: [UpNextEventModel] = []
|
||||||
|
|
||||||
// Calculate the range we're going to search for events in
|
// Calculate the range we're going to search for events in
|
||||||
@ -108,11 +118,52 @@ class UpNextBarItem: CustomButtonTouchBarItem {
|
|||||||
NSWorkspace.shared.open(URL(fileURLWithPath: "/Applications/Calendar.app"))
|
NSWorkspace.shared.open(URL(fileURLWithPath: "/Applications/Calendar.app"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class UpNextItem : CustomButtonTouchBarItem {
|
||||||
|
static public let df = DateFormatter()
|
||||||
|
|
||||||
|
init(event: UpNextEventModel) {
|
||||||
|
let identifier = UpNextItem.getIdentifier(event: event)
|
||||||
|
let title = UpNextItem.getTitle(event: event)
|
||||||
|
super.init(identifier: NSTouchBarItem.Identifier(rawValue: identifier), title: title)
|
||||||
|
}
|
||||||
|
|
||||||
|
required init?(coder _: NSCoder) {
|
||||||
|
fatalError("init(coder:) has not been implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
private static func getTitle(event: UpNextEventModel) -> String {
|
||||||
|
var title = ""
|
||||||
|
let startDateString = UpNextItem.df.string(for: event.startDate)
|
||||||
|
switch event.sourceType {
|
||||||
|
case .iCalendar:
|
||||||
|
title = String.init(format: "🗓 %@ - %@ ", event.title, startDateString!)
|
||||||
|
}
|
||||||
|
return title
|
||||||
|
}
|
||||||
|
|
||||||
|
private static func getIdentifier(event: UpNextEventModel) -> String {
|
||||||
|
var identifier : String
|
||||||
|
switch event.sourceType {
|
||||||
|
case .iCalendar:
|
||||||
|
identifier = "com.mtmr.iCalendarEvent"
|
||||||
|
}
|
||||||
|
return identifier + "." + event.title
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum UpNextSourceType {
|
||||||
|
case iCalendar
|
||||||
|
}
|
||||||
|
|
||||||
// Model for events to be displayed in dock
|
// Model for events to be displayed in dock
|
||||||
struct UpNextEventModel {
|
struct UpNextEventModel {
|
||||||
var title: String
|
let title: String
|
||||||
var startDate: Date
|
let startDate: Date
|
||||||
|
let sourceType: UpNextSourceType
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Interface for any event source
|
// Interface for any event source
|
||||||
protocol IUpNextSource {
|
protocol IUpNextSource {
|
||||||
var hasPermission : Bool { get }
|
var hasPermission : Bool { get }
|
||||||
@ -122,7 +173,7 @@ protocol IUpNextSource {
|
|||||||
|
|
||||||
class UpNextCalenderSource : IUpNextSource {
|
class UpNextCalenderSource : IUpNextSource {
|
||||||
public var hasPermission: Bool = false
|
public var hasPermission: Bool = false
|
||||||
private let eventStore = EKEventStore() //
|
private var eventStore = EKEventStore()
|
||||||
required init() {
|
required init() {
|
||||||
eventStore.requestAccess(to: .event){ granted, error in
|
eventStore.requestAccess(to: .event){ granted, error in
|
||||||
self.hasPermission = granted;
|
self.hasPermission = granted;
|
||||||
@ -134,6 +185,7 @@ class UpNextCalenderSource : IUpNextSource {
|
|||||||
}
|
}
|
||||||
public func getUpcomingEvents(dateLowerBounds: Date, dateUpperBounds: Date) -> [UpNextEventModel] {
|
public func getUpcomingEvents(dateLowerBounds: Date, dateUpperBounds: Date) -> [UpNextEventModel] {
|
||||||
NSLog("Getting calendar events...")
|
NSLog("Getting calendar events...")
|
||||||
|
eventStore = EKEventStore()
|
||||||
var upcomingEvents: [UpNextEventModel] = []
|
var upcomingEvents: [UpNextEventModel] = []
|
||||||
let calendars = self.eventStore.calendars(for: .event)
|
let calendars = self.eventStore.calendars(for: .event)
|
||||||
|
|
||||||
@ -143,7 +195,7 @@ class UpNextCalenderSource : IUpNextSource {
|
|||||||
|
|
||||||
let events = self.eventStore.events(matching: predicate)
|
let events = self.eventStore.events(matching: predicate)
|
||||||
for event in events {
|
for event in events {
|
||||||
upcomingEvents.append(UpNextEventModel(title: event.title, startDate: event.startDate))
|
upcomingEvents.append(UpNextEventModel(title: event.title, startDate: event.startDate, sourceType: UpNextSourceType.iCalendar))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user