From 152fef23ddfe44f51596566176df154f50a28b5e Mon Sep 17 00:00:00 2001 From: Alex Mazanov Date: Sun, 23 Oct 2022 09:47:28 -0400 Subject: [PATCH] Add support for multiple cron schedules --- README.md | 7 +++++++ SwiftBar/Plugin/ExecutablePlugin.swift | 11 +++++++++-- SwiftBar/Plugin/PluginMetadata.swift | 4 ++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 68d3e84..e050f3b 100644 --- a/README.md +++ b/README.md @@ -212,6 +212,13 @@ A special tag can be used as an alternative to refresh interval defined in plugi 01,16,31,46 * * * * ``` +You can configure multiple schedules, using the sepparator `|`: + +``` +1 * * * *|2 * * * * +``` + + #### Other Parameters * `#true` - refreshes plugin on click, before presenting the menu diff --git a/SwiftBar/Plugin/ExecutablePlugin.swift b/SwiftBar/Plugin/ExecutablePlugin.swift index 0958b65..c331a2a 100644 --- a/SwiftBar/Plugin/ExecutablePlugin.swift +++ b/SwiftBar/Plugin/ExecutablePlugin.swift @@ -30,6 +30,8 @@ class ExecutablePlugin: Plugin { Timer.TimerPublisher(interval: updateInterval, runLoop: .main, mode: .default) } + var cronTimer: Timer? + var cancellable: Set = [] let prefs = PreferencesStore.shared @@ -67,11 +69,16 @@ class ExecutablePlugin: Plugin { refresh() } + // this function called each time plugin updated(manual or scheduled) func enableTimer() { // handle cron scheduled plugins if let nextDate = metadata?.nextDate { - let timer = Timer(fireAt: nextDate, interval: 0, target: self, selector: #selector(scheduledContentUpdate), userInfo: nil, repeats: false) - RunLoop.main.add(timer, forMode: .common) + print("Time: \(nextDate)") + cronTimer?.invalidate() + cronTimer = Timer(fireAt: nextDate, interval: 0, target: self, selector: #selector(scheduledContentUpdate), userInfo: nil, repeats: false) + if let cronTimer { + RunLoop.main.add(cronTimer, forMode: .common) + } return } guard cancellable.isEmpty else { return } diff --git a/SwiftBar/Plugin/PluginMetadata.swift b/SwiftBar/Plugin/PluginMetadata.swift index 96eb76f..ecad5cd 100644 --- a/SwiftBar/Plugin/PluginMetadata.swift +++ b/SwiftBar/Plugin/PluginMetadata.swift @@ -74,8 +74,8 @@ class PluginMetadata: ObservableObject { } var nextDate: Date? { - guard let cron = try? SwifCron(schedule) else { return nil } - return try? cron.next() + // parse schedule string and return the minimum date + schedule.components(separatedBy: "|").compactMap { try? SwifCron($0).next() }.reduce(Date.distantFuture, min) } init(name: String = "", version: String = "", author: String = "", github: String = "", desc: String = "", previewImageURL: URL? = nil, dependencies: [String] = [], aboutURL: URL? = nil, dropTypes: [String] = [], schedule: String = "", type: PluginType = .Executable, hideAbout: Bool = false, hideRunInTerminal: Bool = false, hideLastUpdated: Bool = false, hideDisablePlugin: Bool = false, hideSwiftBar: Bool = false, environment: [String: String] = [:], runInBash: Bool = true, refreshOnOpen: Bool = false, useTrailingStreamSeparator: Bool = false) {