NetNewsWire/Multiplatform/Shared/Inspector/InspectorModel.swift
Stuart Breckenridge 4d227e7e2c
Inspector tweaks
- [x] Account Inspector should have an Active toggle (see iOS)
 - [x] Feed Inspector Home Page should open the link in browser when tapped
- [x] Feed Inspector Home Page should have a browser icon (see iOS)
- [x] Feed Inspector Home Page URL should line wrap
- [x] Feed Inspector Feed URL should be copiable (currently this is a long press on iOS)
- [x] Feed Inspector Feed URL should line wrap
- [x] Inspector should use inset style on iOS
2020-07-19 07:57:25 +08:00

120 lines
3.0 KiB
Swift

//
// InspectorModel.swift
// NetNewsWire
//
// Created by Stuart Breckenridge on 18/7/20.
// Copyright © 2020 Ranchero Software. All rights reserved.
//
import Foundation
import UserNotifications
import RSCore
import Account
#if os(macOS)
import AppKit
#else
import UIKit
#endif
class InspectorModel: ObservableObject {
// Global Inspector Variables
@Published var editedName: String = ""
@Published var shouldUpdate: Bool = false
// Account Inspector Variables
@Published var notificationSettings: UNNotificationSettings?
@Published var notifyAboutNewArticles: Bool = false {
didSet {
updateNotificationSettings()
}
}
@Published var alwaysShowReaderView: Bool = false {
didSet {
selectedWebFeed?.isArticleExtractorAlwaysOn = alwaysShowReaderView
}
}
@Published var accountIsActive: Bool = false {
didSet {
selectedAccount?.isActive = accountIsActive
}
}
@Published var showHomePage: Bool = false // iOS only
// Private Variables
private let centre = UNUserNotificationCenter.current()
private var selectedWebFeed: WebFeed?
private var selectedFolder: Folder?
private var selectedAccount: Account?
init() {
getNotificationSettings()
}
func getNotificationSettings() {
centre.getNotificationSettings { (settings) in
DispatchQueue.main.async {
self.notificationSettings = settings
if settings.authorizationStatus == .authorized {
#if os(macOS)
NSApplication.shared.registerForRemoteNotifications()
#else
UIApplication.shared.registerForRemoteNotifications()
#endif
}
}
}
}
func configure(with feed: WebFeed) {
selectedWebFeed = feed
notifyAboutNewArticles = selectedWebFeed?.isNotifyAboutNewArticles ?? false
alwaysShowReaderView = selectedWebFeed?.isArticleExtractorAlwaysOn ?? false
editedName = feed.nameForDisplay
}
func configure(with folder: Folder) {
selectedFolder = folder
editedName = folder.nameForDisplay
}
func configure(with account: Account) {
selectedAccount = account
editedName = account.nameForDisplay
accountIsActive = account.isActive
}
func updateNotificationSettings() {
guard let feed = selectedWebFeed,
let settings = notificationSettings
else { return }
if settings.authorizationStatus == .denied {
notifyAboutNewArticles = false
} else if settings.authorizationStatus == .authorized {
feed.isNotifyAboutNewArticles = notifyAboutNewArticles
} else {
UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .sound, .alert]) { [weak self] (granted, error) in
self?.updateNotificationSettings()
if granted {
DispatchQueue.main.async {
self?.selectedWebFeed!.isNotifyAboutNewArticles = self?.notifyAboutNewArticles
#if os(macOS)
NSApplication.shared.registerForRemoteNotifications()
#else
UIApplication.shared.registerForRemoteNotifications()
#endif
}
} else {
DispatchQueue.main.async {
self?.notifyAboutNewArticles = false
}
}
}
}
}
}