Skip to content

Commit

Permalink
Fixignoring bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
maxgoedjen committed Nov 13, 2020
1 parent 8bbf489 commit cd0a1b0
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 23 deletions.
65 changes: 47 additions & 18 deletions Brief/Updater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import Combine
public protocol UpdaterProtocol: ObservableObject {

var update: Release? { get }

func ignore(release: Release)

}

public class Updater: ObservableObject, UpdaterProtocol {
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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)
}

}
Expand Down
2 changes: 1 addition & 1 deletion SecretAgent/Notifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 3 additions & 1 deletion Secretive/Preview Content/PreviewUpdater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 7 additions & 3 deletions Secretive/Views/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,13 @@ struct ContentView<UpdaterType: UpdaterProtocol, AgentStatusCheckerType: AgentSt
severity = .advisory
text = "Update Available"
}
return AnyView(NoticeView(text: text, severity: severity, actionTitle: "Update") {
NSWorkspace.shared.open(update.html_url)
})
let action = {
_ = NSWorkspace.shared.open(update.html_url)
}
let ignoreAction = {
updater.ignore(release: update)
}
return AnyView(NoticeView(text: text, severity: severity, actionTitle: "Update", action: action, secondaryActionTitle: "Ignore", secondaryAction: ignoreAction))
}

func agentNotice() -> some View {
Expand Down
16 changes: 16 additions & 0 deletions Secretive/Views/NoticeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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!)
}
Expand Down

0 comments on commit cd0a1b0

Please sign in to comment.