Tusker/Tusker/Screens/Notifications/NotificationsPageViewContro...

105 lines
3.2 KiB
Swift

//
// NotificationsPageViewController.swift
// Tusker
//
// Created by Shadowfacts on 9/13/19.
// Copyright © 2019 Shadowfacts. All rights reserved.
//
import UIKit
import Pachyderm
class NotificationsPageViewController: SegmentedPageViewController<NotificationsPageViewController.Page> {
weak var mastodonController: MastodonController!
var initialMode: NotificationsMode?
init(initialMode: NotificationsMode? = nil, mastodonController: MastodonController) {
self.initialMode = initialMode
self.mastodonController = mastodonController
super.init(pages: [.all, .mentions]) { page in
let vc = NotificationsTableViewController(allowedTypes: page.allowedTypes, mastodonController: mastodonController)
vc.title = page.title
vc.userActivity = page.userActivity(accountID: mastodonController.accountInfo!.id)
return vc
}
title = Page.all.title
tabBarItem.image = UIImage(systemName: "bell.fill")
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
selectMode(initialMode ?? Preferences.shared.defaultNotificationsMode)
}
func selectMode(_ mode: NotificationsMode) {
let page: Page
switch mode {
case .allNotifications:
page = .all
case .mentionsOnly:
page = .mentions
}
selectPage(page, animated: false)
}
enum Page: SegmentedPageViewControllerPage {
case all
case mentions
var title: String {
switch self {
case .all:
return NSLocalizedString("Notifications", comment: "notifications tab title")
case .mentions:
return NSLocalizedString("Mentions", comment: "mentions tab title")
}
}
var segmentedControlTitle: String {
title
}
var allowedTypes: [Pachyderm.Notification.Kind] {
switch self {
case .all:
var set = Set(Pachyderm.Notification.Kind.allCases)
set.remove(.unknown)
return Array(set)
case .mentions:
return [.mention]
}
}
func userActivity(accountID: String) -> NSUserActivity {
switch self {
case .all:
return UserActivityManager.checkNotificationsActivity(mode: .allNotifications, accountID: accountID)
case .mentions:
return UserActivityManager.checkNotificationsActivity(mode: .mentionsOnly, accountID: accountID)
}
}
}
}
extension NotificationsPageViewController: StateRestorableViewController {
func stateRestorationActivity() -> NSUserActivity? {
return currentPage.userActivity(accountID: mastodonController.accountInfo!.id)
}
func restoreActivity(_ activity: NSUserActivity) {
if let mode = UserActivityManager.getNotificationsMode(from: activity) {
selectMode(mode)
}
}
}