2021-02-23 09:45:00 +01:00
|
|
|
//
|
|
|
|
// NotificationViewController.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
2021-04-12 10:31:53 +02:00
|
|
|
// Created by sxiaojian on 2021/4/12.
|
2021-02-23 09:45:00 +01:00
|
|
|
//
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
import os.log
|
2021-04-15 04:16:30 +02:00
|
|
|
import UIKit
|
2022-01-27 14:23:39 +01:00
|
|
|
import Combine
|
|
|
|
import MastodonAsset
|
|
|
|
import MastodonLocalization
|
|
|
|
import Tabman
|
|
|
|
import Pageboy
|
2021-02-23 09:45:00 +01:00
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
final class NotificationViewController: TabmanViewController, NeedsDependency {
|
|
|
|
|
|
|
|
let logger = Logger(subsystem: "NotificationViewController", category: "ViewController")
|
2021-11-02 07:28:21 +01:00
|
|
|
|
2021-02-23 09:45:00 +01:00
|
|
|
weak var context: AppContext! { willSet { precondition(!isViewLoaded) } }
|
|
|
|
weak var coordinator: SceneCoordinator! { willSet { precondition(!isViewLoaded) } }
|
2021-04-18 16:00:19 +02:00
|
|
|
|
2021-04-12 10:31:53 +02:00
|
|
|
var disposeBag = Set<AnyCancellable>()
|
2021-06-22 13:58:20 +02:00
|
|
|
var observations = Set<NSKeyValueObservation>()
|
|
|
|
|
2021-04-14 11:37:58 +02:00
|
|
|
private(set) lazy var viewModel = NotificationViewModel(context: context)
|
2021-11-02 07:28:21 +01:00
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
let pageSegmentedControl = UISegmentedControl()
|
|
|
|
|
|
|
|
override func pageboyViewController(
|
|
|
|
_ pageboyViewController: PageboyViewController,
|
|
|
|
didScrollToPageAt index: TabmanViewController.PageIndex,
|
|
|
|
direction: PageboyViewController.NavigationDirection,
|
|
|
|
animated: Bool
|
|
|
|
) {
|
|
|
|
super.pageboyViewController(
|
|
|
|
pageboyViewController,
|
|
|
|
didScrollToPageAt: index,
|
|
|
|
direction: direction,
|
|
|
|
animated: animated
|
|
|
|
)
|
|
|
|
|
|
|
|
viewModel.currentPageIndex = index
|
|
|
|
}
|
2021-09-30 08:19:15 +02:00
|
|
|
|
|
|
|
deinit {
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
|
|
|
|
}
|
2021-02-23 09:45:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extension NotificationViewController {
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
2021-07-06 12:00:39 +02:00
|
|
|
|
|
|
|
view.backgroundColor = ThemeService.shared.currentTheme.value.secondarySystemBackgroundColor
|
2021-07-05 10:07:17 +02:00
|
|
|
ThemeService.shared.currentTheme
|
|
|
|
.receive(on: RunLoop.main)
|
|
|
|
.sink { [weak self] theme in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.view.backgroundColor = theme.secondarySystemBackgroundColor
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
setupSegmentedControl(scopes: viewModel.scopes)
|
|
|
|
pageSegmentedControl.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
navigationItem.titleView = pageSegmentedControl
|
2021-04-19 05:41:50 +02:00
|
|
|
NSLayoutConstraint.activate([
|
2022-01-27 14:23:39 +01:00
|
|
|
pageSegmentedControl.widthAnchor.constraint(greaterThanOrEqualToConstant: 287)
|
2021-04-12 10:31:53 +02:00
|
|
|
])
|
2022-01-27 14:23:39 +01:00
|
|
|
pageSegmentedControl.addTarget(self, action: #selector(NotificationViewController.pageSegmentedControlValueChanged(_:)), for: .valueChanged)
|
2021-04-18 16:00:19 +02:00
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
dataSource = viewModel
|
|
|
|
viewModel.$viewControllers
|
2021-04-12 10:31:53 +02:00
|
|
|
.receive(on: DispatchQueue.main)
|
2022-01-27 14:23:39 +01:00
|
|
|
.sink { [weak self] viewControllers in
|
2021-06-22 12:28:27 +02:00
|
|
|
guard let self = self else { return }
|
2022-01-27 14:23:39 +01:00
|
|
|
self.reloadData()
|
|
|
|
self.bounces = viewControllers.count > 1
|
|
|
|
|
2021-06-22 12:28:27 +02:00
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2021-05-21 11:18:03 +02:00
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
viewModel.viewControllers = viewModel.scopes.map { scope in
|
|
|
|
createViewController(for: scope)
|
|
|
|
}
|
|
|
|
|
|
|
|
viewModel.$currentPageIndex
|
2021-05-21 11:18:03 +02:00
|
|
|
.receive(on: DispatchQueue.main)
|
2022-01-27 14:23:39 +01:00
|
|
|
.sink { [weak self] currentPageIndex in
|
2021-05-21 11:18:03 +02:00
|
|
|
guard let self = self else { return }
|
2022-01-27 14:23:39 +01:00
|
|
|
if self.pageSegmentedControl.selectedSegmentIndex != currentPageIndex {
|
|
|
|
self.pageSegmentedControl.selectedSegmentIndex = currentPageIndex
|
2021-05-21 11:18:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2021-04-12 10:31:53 +02:00
|
|
|
}
|
2021-04-18 16:00:19 +02:00
|
|
|
|
2021-04-12 10:31:53 +02:00
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
super.viewWillAppear(animated)
|
2021-11-02 07:28:21 +01:00
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
// aspectViewWillAppear(animated)
|
2021-04-20 07:18:27 +02:00
|
|
|
|
2021-07-09 13:13:09 +02:00
|
|
|
// fetch latest notification when scroll position is within half screen height to prevent list reload
|
2022-01-27 14:23:39 +01:00
|
|
|
// if tableView.contentOffset.y < view.frame.height * 0.5 {
|
|
|
|
// viewModel.loadLatestStateMachine.enter(NotificationViewModel.LoadLatestState.Loading.self)
|
|
|
|
// }
|
2021-07-02 08:01:53 +02:00
|
|
|
|
2021-04-27 11:27:03 +02:00
|
|
|
|
2021-04-12 10:31:53 +02:00
|
|
|
// needs trigger manually after onboarding dismiss
|
2022-02-16 12:47:51 +01:00
|
|
|
setNeedsStatusBarAppearanceUpdate()
|
2021-04-12 10:31:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override func viewDidAppear(_ animated: Bool) {
|
|
|
|
super.viewDidAppear(animated)
|
|
|
|
|
2022-02-10 09:43:07 +01:00
|
|
|
// reset notification count
|
|
|
|
context.notificationService.clearNotificationCountForActiveUser()
|
2021-09-16 10:30:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override func viewWillDisappear(_ animated: Bool) {
|
|
|
|
super.viewWillDisappear(animated)
|
|
|
|
|
2022-02-10 09:43:07 +01:00
|
|
|
// reset notification count
|
|
|
|
context.notificationService.clearNotificationCountForActiveUser()
|
2021-04-12 10:31:53 +02:00
|
|
|
}
|
2021-11-02 07:28:21 +01:00
|
|
|
|
|
|
|
override func viewDidDisappear(_ animated: Bool) {
|
|
|
|
super.viewDidDisappear(animated)
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
// aspectViewDidDisappear(animated)
|
2021-02-23 09:45:00 +01:00
|
|
|
}
|
|
|
|
}
|
2021-04-12 10:31:53 +02:00
|
|
|
|
|
|
|
extension NotificationViewController {
|
2022-01-27 14:23:39 +01:00
|
|
|
private func setupSegmentedControl(scopes: [NotificationTimelineViewModel.Scope]) {
|
|
|
|
pageSegmentedControl.removeAllSegments()
|
|
|
|
for (i, scope) in scopes.enumerated() {
|
|
|
|
pageSegmentedControl.insertSegment(withTitle: scope.title, at: i, animated: false)
|
2021-04-12 10:31:53 +02:00
|
|
|
}
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
// set initial selection
|
|
|
|
guard !pageSegmentedControl.isSelected else { return }
|
|
|
|
if viewModel.currentPageIndex < pageSegmentedControl.numberOfSegments {
|
|
|
|
pageSegmentedControl.selectedSegmentIndex = viewModel.currentPageIndex
|
|
|
|
} else {
|
|
|
|
pageSegmentedControl.selectedSegmentIndex = 0
|
2021-11-02 07:28:21 +01:00
|
|
|
}
|
2021-05-21 10:52:47 +02:00
|
|
|
}
|
2021-11-02 07:28:21 +01:00
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
private func createViewController(for scope: NotificationTimelineViewModel.Scope) -> UIViewController {
|
|
|
|
let viewController = NotificationTimelineViewController()
|
|
|
|
viewController.context = context
|
|
|
|
viewController.coordinator = coordinator
|
|
|
|
viewController.viewModel = NotificationTimelineViewModel(
|
|
|
|
context: context,
|
|
|
|
scope: scope
|
|
|
|
)
|
|
|
|
return viewController
|
2021-07-12 13:20:54 +02:00
|
|
|
}
|
2021-05-21 10:52:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extension NotificationViewController {
|
2022-01-27 14:23:39 +01:00
|
|
|
@objc private func pageSegmentedControlValueChanged(_ sender: UISegmentedControl) {
|
|
|
|
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public)")
|
|
|
|
|
|
|
|
let index = sender.selectedSegmentIndex
|
|
|
|
scrollToPage(.at(index: index), animated: true, completion: nil)
|
2021-04-12 10:31:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-14 07:55:00 +01:00
|
|
|
// MARK: - ScrollViewContainer
|
|
|
|
extension NotificationViewController: ScrollViewContainer {
|
|
|
|
var scrollView: UIScrollView? {
|
|
|
|
guard let viewController = currentViewController as? NotificationTimelineViewController else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return viewController.scrollView
|
|
|
|
}
|
|
|
|
}
|
2022-02-16 12:47:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
extension NotificationViewController {
|
|
|
|
|
|
|
|
enum CategorySwitch: String, CaseIterable {
|
|
|
|
case everything
|
|
|
|
case mentions
|
|
|
|
|
|
|
|
var title: String {
|
|
|
|
switch self {
|
|
|
|
case .everything: return L10n.Scene.Notification.Keyobard.showEverything
|
|
|
|
case .mentions: return L10n.Scene.Notification.Keyobard.showMentions
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UIKeyCommand input
|
|
|
|
var input: String {
|
|
|
|
switch self {
|
|
|
|
case .everything: return "[" // + shift + command
|
|
|
|
case .mentions: return "]" // + shift + command
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var modifierFlags: UIKeyModifierFlags {
|
|
|
|
switch self {
|
|
|
|
case .everything: return [.shift, .command]
|
|
|
|
case .mentions: return [.shift, .command]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var propertyList: Any {
|
|
|
|
return rawValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var categorySwitchKeyCommands: [UIKeyCommand] {
|
|
|
|
CategorySwitch.allCases.map { category in
|
|
|
|
UIKeyCommand(
|
|
|
|
title: category.title,
|
|
|
|
image: nil,
|
|
|
|
action: #selector(NotificationViewController.showCategory(_:)),
|
|
|
|
input: category.input,
|
|
|
|
modifierFlags: category.modifierFlags,
|
|
|
|
propertyList: category.propertyList,
|
|
|
|
alternates: [],
|
|
|
|
discoverabilityTitle: nil,
|
|
|
|
attributes: [],
|
|
|
|
state: .off
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func showCategory(_ sender: UIKeyCommand) {
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
|
|
|
|
guard let rawValue = sender.propertyList as? String,
|
|
|
|
let category = CategorySwitch(rawValue: rawValue)
|
|
|
|
else { return }
|
|
|
|
|
|
|
|
switch category {
|
|
|
|
case .everything:
|
|
|
|
scrollToPage(.first, animated: true, completion: nil)
|
|
|
|
case .mentions:
|
|
|
|
scrollToPage(.last, animated: true, completion: nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override var keyCommands: [UIKeyCommand]? {
|
|
|
|
return categorySwitchKeyCommands
|
|
|
|
}
|
|
|
|
}
|