Set notification-types (IOS-14)

This commit is contained in:
Nathan Mattes 2023-09-26 15:36:57 +02:00
parent fcb19b9734
commit ba0f689acd
7 changed files with 95 additions and 11 deletions

View File

@ -538,13 +538,15 @@ private extension SceneCoordinator {
viewController = activityViewController viewController = activityViewController
case .settings(let setting): case .settings(let setting):
guard let presentedOn = sender, guard let presentedOn = sender,
let accountName = authContext?.mastodonAuthenticationBox.authenticationRecord.object(in: appContext.managedObjectContext)?.username let accountName = authContext?.mastodonAuthenticationBox.authenticationRecord.object(in: appContext.managedObjectContext)?.username,
let authContext
else { return nil } else { return nil }
let settingsCoordinator = SettingsCoordinator(presentedOn: presentedOn, let settingsCoordinator = SettingsCoordinator(presentedOn: presentedOn,
accountName: accountName, accountName: accountName,
setting: setting, setting: setting,
appContext: appContext) appContext: appContext,
authContext: authContext)
settingsCoordinator.delegate = self settingsCoordinator.delegate = self
settingsCoordinator.start() settingsCoordinator.start()

View File

@ -3,7 +3,7 @@
import UIKit import UIKit
protocol NotificationSettingToggleCellDelegate: AnyObject { protocol NotificationSettingToggleCellDelegate: AnyObject {
func toggleValueChanged(_ tableViewCell: NotificationSettingTableViewToggleCell, alert: NotificationAlert, newValue: Bool)
} }
class NotificationSettingTableViewToggleCell: ToggleTableViewCell { class NotificationSettingTableViewToggleCell: ToggleTableViewCell {
@ -45,6 +45,8 @@ class NotificationSettingTableViewToggleCell: ToggleTableViewCell {
@objc @objc
func toggleValueChanged(_ sender: UISwitch) { func toggleValueChanged(_ sender: UISwitch) {
guard let alert else { return }
delegate?.toggleValueChanged(self, alert: alert, newValue: sender.isOn)
} }
} }

View File

@ -5,6 +5,7 @@ import CoreDataStack
import MastodonLocalization import MastodonLocalization
protocol NotificationSettingsViewControllerDelegate: AnyObject { protocol NotificationSettingsViewControllerDelegate: AnyObject {
func viewWillDisappear(_ viewController: UIViewController, viewModel: NotificationSettingsViewModel)
func showPolicyList(_ viewController: UIViewController, viewModel: NotificationSettingsViewModel) func showPolicyList(_ viewController: UIViewController, viewModel: NotificationSettingsViewModel)
} }
@ -88,6 +89,20 @@ class NotificationSettingsViewController: UIViewController {
tableViewDataSource?.apply(snapshot, animatingDifferences: false) tableViewDataSource?.apply(snapshot, animatingDifferences: false)
} }
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let snapshot = tableViewDataSource?.snapshot() {
tableViewDataSource?.applySnapshotUsingReloadData(snapshot)
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
delegate?.viewWillDisappear(self, viewModel: viewModel)
}
} }
extension NotificationSettingsViewController: UITableViewDelegate { extension NotificationSettingsViewController: UITableViewDelegate {
@ -107,5 +122,18 @@ extension NotificationSettingsViewController: UITableViewDelegate {
} }
extension NotificationSettingsViewController: NotificationSettingToggleCellDelegate { extension NotificationSettingsViewController: NotificationSettingToggleCellDelegate {
func toggleValueChanged(_ tableViewCell: NotificationSettingTableViewToggleCell, alert: NotificationAlert, newValue: Bool) {
switch alert {
case .mentionsAndReplies:
viewModel.notifyMentions = newValue
case .boosts:
viewModel.notifyBoosts = newValue
case .favorites:
viewModel.notifyFavorites = newValue
case .newFollowers:
viewModel.notifyNewFollowers = newValue
}
viewModel.updated = true
}
} }

View File

@ -64,15 +64,17 @@ class PolicySelectionViewController: UIViewController {
extension PolicySelectionViewController: UITableViewDelegate { extension PolicySelectionViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let newPolicy = sections[indexPath.section].entries[indexPath.row] let newPolicy = sections[indexPath.section].entries[indexPath.row]
viewModel.selectedPolicy = newPolicy viewModel.selectedPolicy = newPolicy
viewModel.updated = true
if let dataSource { if let dataSource {
dataSource.applySnapshotUsingReloadData(dataSource.snapshot()) dataSource.applySnapshotUsingReloadData(dataSource.snapshot())
} }
delegate?.newPolicySelected(self, newPolicy: newPolicy) delegate?.newPolicySelected(self, newPolicy: newPolicy)
tableView.deselectRow(at: indexPath, animated: true)
} }
} }

View File

@ -4,6 +4,8 @@ import UIKit
import AuthenticationServices import AuthenticationServices
import MastodonCore import MastodonCore
import CoreDataStack import CoreDataStack
import MastodonSDK
import Combine
protocol SettingsCoordinatorDelegate: AnyObject { protocol SettingsCoordinatorDelegate: AnyObject {
func logout(_ settingsCoordinator: SettingsCoordinator) func logout(_ settingsCoordinator: SettingsCoordinator)
@ -22,12 +24,15 @@ class SettingsCoordinator: NSObject, Coordinator {
let setting: Setting let setting: Setting
let appContext: AppContext let appContext: AppContext
let authContext: AuthContext
var disposeBag = Set<AnyCancellable>()
init(presentedOn: UIViewController, accountName: String, setting: Setting, appContext: AppContext) { init(presentedOn: UIViewController, accountName: String, setting: Setting, appContext: AppContext, authContext: AuthContext) {
self.presentedOn = presentedOn self.presentedOn = presentedOn
navigationController = UINavigationController() navigationController = UINavigationController()
self.setting = setting self.setting = setting
self.appContext = appContext self.appContext = appContext
self.authContext = authContext
settingsViewController = SettingsViewController(accountName: accountName) settingsViewController = SettingsViewController(accountName: accountName)
} }
@ -131,11 +136,50 @@ extension SettingsCoordinator: NotificationSettingsViewControllerDelegate {
navigationController.pushViewController(policyListViewController, animated: true) navigationController.pushViewController(policyListViewController, animated: true)
} }
func viewWillDisappear(_ viewController: UIViewController, viewModel: NotificationSettingsViewModel) {
guard viewModel.updated else { return }
let authenticationBox = authContext.mastodonAuthenticationBox
guard let subscription = setting.activeSubscription,
setting.domain == authenticationBox.domain,
setting.userID == authenticationBox.userID,
let legacyViewModel = appContext.notificationService.dequeueNotificationViewModel(mastodonAuthenticationBox: authenticationBox), let deviceToken = appContext.notificationService.deviceToken.value else { return }
let queryData = Mastodon.API.Subscriptions.QueryData(
policy: viewModel.selectedPolicy.subscriptionPolicy,
alerts: Mastodon.API.Subscriptions.QueryData.Alerts(
favourite: viewModel.notifyFavorites,
follow: viewModel.notifyNewFollowers,
reblog: viewModel.notifyBoosts,
mention: viewModel.notifyMentions,
poll: subscription.alert.poll
)
)
let query = legacyViewModel.createSubscribeQuery(
deviceToken: deviceToken,
queryData: queryData,
mastodonAuthenticationBox: authenticationBox
)
appContext.apiService.createSubscription(
subscriptionObjectID: subscription.objectID,
query: query,
mastodonAuthenticationBox: authenticationBox
).sink(receiveCompletion: { completion in
print(completion)
}, receiveValue: { output in
print(output)
})
.store(in: &disposeBag)
}
} }
//MARK: - PolicySelectionViewControllerDelegate //MARK: - PolicySelectionViewControllerDelegate
extension SettingsCoordinator: PolicySelectionViewControllerDelegate { extension SettingsCoordinator: PolicySelectionViewControllerDelegate {
func newPolicySelected(_ viewController: PolicySelectionViewController, newPolicy: NotificationPolicy) { func newPolicySelected(_ viewController: PolicySelectionViewController, newPolicy: NotificationPolicy) {
//TODO: Send to backend etc. self.setting.activeSubscription?.policyRaw = newPolicy.subscriptionPolicy.rawValue
try? self.appContext.managedObjectContext.save()
} }
} }

View File

@ -13,7 +13,7 @@ import MastodonSDK
extension APIService { extension APIService {
func createSubscription( public func createSubscription(
subscriptionObjectID: NSManagedObjectID, subscriptionObjectID: NSManagedObjectID,
query: Mastodon.API.Subscriptions.CreateSubscriptionQuery, query: Mastodon.API.Subscriptions.CreateSubscriptionQuery,
mastodonAuthenticationBox: MastodonAuthenticationBox mastodonAuthenticationBox: MastodonAuthenticationBox
@ -34,6 +34,12 @@ extension APIService {
assertionFailure() assertionFailure()
return return
} }
subscription.alert.update(favourite: response.value.alerts.favourite)
subscription.alert.update(reblog: response.value.alerts.reblog)
subscription.alert.update(follow: response.value.alerts.follow)
subscription.alert.update(mention: response.value.alerts.mention)
subscription.endpoint = response.value.endpoint subscription.endpoint = response.value.endpoint
subscription.serverKey = response.value.serverKey subscription.serverKey = response.value.serverKey
subscription.userToken = authorization.accessToken subscription.userToken = authorization.accessToken

View File

@ -127,7 +127,7 @@ extension NotificationService {
extension NotificationService { extension NotificationService {
func dequeueNotificationViewModel( public func dequeueNotificationViewModel(
mastodonAuthenticationBox: MastodonAuthenticationBox mastodonAuthenticationBox: MastodonAuthenticationBox
) -> NotificationViewModel? { ) -> NotificationViewModel? {
var _notificationSubscription: NotificationViewModel? var _notificationSubscription: NotificationViewModel?
@ -281,7 +281,7 @@ extension NotificationService {
} }
extension NotificationService.NotificationViewModel { extension NotificationService.NotificationViewModel {
func createSubscribeQuery( public func createSubscribeQuery(
deviceToken: Data, deviceToken: Data,
queryData: Mastodon.API.Subscriptions.QueryData, queryData: Mastodon.API.Subscriptions.QueryData,
mastodonAuthenticationBox: MastodonAuthenticationBox mastodonAuthenticationBox: MastodonAuthenticationBox