2019-10-03 02:42:16 +02:00
|
|
|
//
|
|
|
|
// NotificationManager.swift
|
|
|
|
// NetNewsWire
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 10/2/19.
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Account
|
|
|
|
import Articles
|
|
|
|
import UserNotifications
|
|
|
|
|
|
|
|
final class UserNotificationManager: NSObject {
|
|
|
|
|
|
|
|
override init() {
|
|
|
|
super.init()
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(accountDidDownloadArticles(_:)), name: .AccountDidDownloadArticles, object: nil)
|
2019-10-04 18:20:57 +02:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(statusesDidChange(_:)), name: .StatusesDidChange, object: nil)
|
2019-10-03 02:42:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc func accountDidDownloadArticles(_ note: Notification) {
|
|
|
|
guard let articles = note.userInfo?[Account.UserInfoKey.newArticles] as? Set<Article> else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for article in articles {
|
2019-11-15 03:11:41 +01:00
|
|
|
if !article.status.read, let webFeed = article.webFeed, webFeed.isNotifyAboutNewArticles ?? false {
|
|
|
|
sendNotification(webFeed: webFeed, article: article)
|
2019-10-03 02:42:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-04 18:20:57 +02:00
|
|
|
@objc func statusesDidChange(_ note: Notification) {
|
2020-01-11 20:51:13 +01:00
|
|
|
guard let articleIDs = note.userInfo?[Account.UserInfoKey.articleIDs] as? Set<String>, !articleIDs.isEmpty else {
|
2019-10-04 18:20:57 +02:00
|
|
|
return
|
|
|
|
}
|
2020-01-11 20:51:13 +01:00
|
|
|
let identifiers = articleIDs.map { "articleID:\($0)" }
|
2019-10-04 18:20:57 +02:00
|
|
|
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: identifiers)
|
|
|
|
}
|
|
|
|
|
2019-10-03 02:42:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private extension UserNotificationManager {
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
private func sendNotification(webFeed: WebFeed, article: Article) {
|
2019-10-03 02:42:16 +02:00
|
|
|
let content = UNMutableNotificationContent()
|
2019-10-03 16:53:21 +02:00
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
content.title = webFeed.nameForDisplay
|
2019-10-20 09:28:00 +02:00
|
|
|
content.body = ArticleStringFormatter.truncatedTitle(article)
|
2019-10-03 16:53:21 +02:00
|
|
|
if content.body.isEmpty {
|
2019-10-20 09:28:00 +02:00
|
|
|
content.body = ArticleStringFormatter.truncatedSummary(article)
|
2019-10-03 16:53:21 +02:00
|
|
|
}
|
|
|
|
|
2019-10-03 02:42:16 +02:00
|
|
|
content.sound = UNNotificationSound.default
|
2019-11-14 22:06:32 +01:00
|
|
|
content.userInfo = [UserInfoKey.articlePath: article.pathUserInfo]
|
2019-10-03 16:53:21 +02:00
|
|
|
|
2019-10-03 02:42:16 +02:00
|
|
|
let request = UNNotificationRequest.init(identifier: "articleID:\(article.articleID)", content: content, trigger: nil)
|
|
|
|
UNUserNotificationCenter.current().add(request)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|