Skip to content

Commit

Permalink
Merge pull request #79 from winebarrel/impl_remove_notify
Browse files Browse the repository at this point in the history
Implement remove notification
  • Loading branch information
winebarrel authored Jan 14, 2025
2 parents ca3a61b + 5fca7d9 commit 100ff52
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
7 changes: 5 additions & 2 deletions Pulse/GitHubAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ struct PullRequest: Identifiable {
typealias PullRequests = [PullRequest]

func - (left: PullRequests, right: PullRequests) -> PullRequests {
let rightIDs = right.map { $0.id }
return left.filter { !rightIDs.contains($0.id) }
let rightTups = right.map { ($0.id, $0.reviewResult, $0.checkResult) }
return left.filter {
let tup = ($0.id, $0.reviewResult, $0.checkResult)
return !rightTups.contains { $0 == tup }
}
}

actor GitHubAPI {
Expand Down
11 changes: 10 additions & 1 deletion Pulse/Notification.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import UserNotifications

enum Notification {
private static let bundleIdentifier = Bundle.main.bundleIdentifier!

static func notify(id: String, title: String, body: String, url: String) async {
let userNotificationCenter = UNUserNotificationCenter.current()

Expand All @@ -10,7 +12,14 @@ enum Notification {
content.userInfo = ["url": url]
content.sound = UNNotificationSound.default

let req = UNNotificationRequest(identifier: "\(Bundle.main.bundleIdentifier!).\(id)", content: content, trigger: nil)
let req = UNNotificationRequest(identifier: "\(bundleIdentifier).\(id)", content: content, trigger: nil)
try? await userNotificationCenter.add(req)
}

static func remove(ids: [String]) {
let userNotificationCenter = UNUserNotificationCenter.current()
let idsWithBundleId = ids.map { "\(bundleIdentifier).\($0)" }
userNotificationCenter.removeDeliveredNotifications(
withIdentifiers: idsWithBundleId)
}
}
5 changes: 5 additions & 0 deletions Pulse/PullRequestModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class PullRequestModel: ObservableObject {
if !newSettled.isEmpty {
notify(newSettled)
}

if !pending.isEmpty {
let ids = pending.map { $0.id }
Notification.remove(ids: ids)
}
} catch let githubError as GitHubError {
error = githubError
status = .error
Expand Down

0 comments on commit 100ff52

Please sign in to comment.