From cd0a1b0a6829e9aeb98a7883de0259c903e8c262 Mon Sep 17 00:00:00 2001 From: Max Goedjen Date: Thu, 12 Nov 2020 23:48:56 -0800 Subject: [PATCH] Fixignoring bugs --- Brief/Updater.swift | 65 ++++++++++++++----- SecretAgent/Notifier.swift | 2 +- .../Preview Content/PreviewUpdater.swift | 4 +- Secretive/Views/ContentView.swift | 10 ++- Secretive/Views/NoticeView.swift | 16 +++++ 5 files changed, 74 insertions(+), 23 deletions(-) diff --git a/Brief/Updater.swift b/Brief/Updater.swift index f9f67c6..7935772 100644 --- a/Brief/Updater.swift +++ b/Brief/Updater.swift @@ -4,7 +4,8 @@ import Combine public protocol UpdaterProtocol: ObservableObject { var update: Release? { get } - + func ignore(release: Release) + } public class Updater: ObservableObject, UpdaterProtocol { @@ -41,26 +42,15 @@ extension Updater { func evaluate(release: Release) { guard !userIgnored(release: release) else { return } - let latestVersion = semVer(from: release.name) - let currentVersion = semVer(from: Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String) - for (latest, current) in zip(latestVersion, currentVersion) { - if latest > current { - DispatchQueue.main.async { - self.update = release - } - return + let latestVersion = SemVer(release.name) + let currentVersion = SemVer(Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String) + if latestVersion > currentVersion { + DispatchQueue.main.async { + self.update = release } } } - func semVer(from stringVersion: String) -> [Int] { - var split = stringVersion.split(separator: ".").compactMap { Int($0) } - while split.count < 3 { - split.append(0) - } - return split - } - func userIgnored(release: Release) -> Bool { guard !release.critical else { return false } return defaults.bool(forKey: release.name) @@ -71,6 +61,38 @@ extension Updater { } } +struct SemVer { + + let versionNumbers: [Int] + + init(_ version: String) { + // Betas have the format 1.2.3_beta1 + let strippedBeta = version.split(separator: "_").first! + var split = strippedBeta.split(separator: ".").compactMap { Int($0) } + while split.count < 3 { + split.append(0) + } + versionNumbers = split + } + +} + +extension SemVer: Comparable { + + static func < (lhs: SemVer, rhs: SemVer) -> Bool { + for (latest, current) in zip(lhs.versionNumbers, rhs.versionNumbers) { + if latest < current { + return true + } else if latest > current { + return false + } + } + return false + } + + +} + extension Updater { enum Constants { @@ -93,11 +115,18 @@ public struct Release: Codable { } +extension Release: Identifiable { + + public var id: String { + html_url.absoluteString + } + +} extension Release { public var critical: Bool { - return body.contains(Constants.securityContent) + body.contains(Constants.securityContent) } } diff --git a/SecretAgent/Notifier.swift b/SecretAgent/Notifier.swift index cbfce20..f40706e 100644 --- a/SecretAgent/Notifier.swift +++ b/SecretAgent/Notifier.swift @@ -13,7 +13,7 @@ class Notifier { let updateAction = UNNotificationAction(identifier: Constants.updateActionIdentitifier, title: "Update", options: []) let ignoreAction = UNNotificationAction(identifier: Constants.ignoreActionIdentitifier, title: "Ignore", options: []) let updateCategory = UNNotificationCategory(identifier: Constants.updateCategoryIdentitifier, actions: [updateAction, ignoreAction], intentIdentifiers: [], options: []) - let criticalUpdateCategory = UNNotificationCategory(identifier: Constants.updateCategoryIdentitifier, actions: [updateAction], intentIdentifiers: [], options: []) + let criticalUpdateCategory = UNNotificationCategory(identifier: Constants.criticalUpdateCategoryIdentitifier, actions: [updateAction], intentIdentifiers: [], options: []) UNUserNotificationCenter.current().setNotificationCategories([updateCategory, criticalUpdateCategory]) UNUserNotificationCenter.current().delegate = notificationDelegate } diff --git a/Secretive/Preview Content/PreviewUpdater.swift b/Secretive/Preview Content/PreviewUpdater.swift index 2adb988..4973d25 100644 --- a/Secretive/Preview Content/PreviewUpdater.swift +++ b/Secretive/Preview Content/PreviewUpdater.swift @@ -16,7 +16,9 @@ class PreviewUpdater: UpdaterProtocol { self.update = Release(name: "10.10.10", html_url: URL(string: "https://example.com")!, body: "Critical Security Update") } } - + + func ignore(release: Release) { + } } extension PreviewUpdater { diff --git a/Secretive/Views/ContentView.swift b/Secretive/Views/ContentView.swift index 7ce9325..1228f3d 100644 --- a/Secretive/Views/ContentView.swift +++ b/Secretive/Views/ContentView.swift @@ -87,9 +87,13 @@ struct ContentView some View { diff --git a/Secretive/Views/NoticeView.swift b/Secretive/Views/NoticeView.swift index dceba20..869836d 100644 --- a/Secretive/Views/NoticeView.swift +++ b/Secretive/Views/NoticeView.swift @@ -7,12 +7,28 @@ struct NoticeView: View { let severity: Severity let actionTitle: String? let action: (() -> Void)? + let secondaryActionTitle: String? + let secondaryAction: (() -> Void)? + + public init(text: String, severity: NoticeView.Severity, actionTitle: String?, action: (() -> Void)?, secondaryActionTitle: String? = nil, secondaryAction: (() -> Void)? = nil) { + self.text = text + self.severity = severity + self.actionTitle = actionTitle + self.action = action + self.secondaryActionTitle = secondaryActionTitle + self.secondaryAction = secondaryAction + } var body: some View { HStack { Text(text).bold() Spacer() if action != nil { + if secondaryAction != nil { + Button(action: secondaryAction!) { + Text(secondaryActionTitle!) + } + } Button(action: action!) { Text(actionTitle!) }