mastodon-ios/Mastodon/Scene/Notification/NotificationViewController....

250 lines
8.5 KiB
Swift
Raw Normal View History

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
//
import os.log
2021-04-15 04:16:30 +02:00
import UIKit
import Combine
import MastodonAsset
import MastodonLocalization
import Tabman
import Pageboy
2021-02-23 09:45:00 +01:00
final class NotificationViewController: TabmanViewController, NeedsDependency {
let logger = Logger(subsystem: "NotificationViewController", category: "ViewController")
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>()
var observations = Set<NSKeyValueObservation>()
private(set) lazy var viewModel = NotificationViewModel(context: context)
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
}
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()
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)
setupSegmentedControl(scopes: viewModel.scopes)
pageSegmentedControl.translatesAutoresizingMaskIntoConstraints = false
navigationItem.titleView = pageSegmentedControl
2021-04-19 05:41:50 +02:00
NSLayoutConstraint.activate([
pageSegmentedControl.widthAnchor.constraint(greaterThanOrEqualToConstant: 287)
2021-04-12 10:31:53 +02:00
])
pageSegmentedControl.addTarget(self, action: #selector(NotificationViewController.pageSegmentedControlValueChanged(_:)), for: .valueChanged)
2021-04-18 16:00:19 +02:00
dataSource = viewModel
viewModel.$viewControllers
2021-04-12 10:31:53 +02:00
.receive(on: DispatchQueue.main)
.sink { [weak self] viewControllers in
guard let self = self else { return }
self.reloadData()
self.bounces = viewControllers.count > 1
}
.store(in: &disposeBag)
viewModel.viewControllers = viewModel.scopes.map { scope in
createViewController(for: scope)
}
viewModel.$currentPageIndex
.receive(on: DispatchQueue.main)
.sink { [weak self] currentPageIndex in
guard let self = self else { return }
if self.pageSegmentedControl.selectedSegmentIndex != currentPageIndex {
self.pageSegmentedControl.selectedSegmentIndex = currentPageIndex
}
}
.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)
// aspectViewWillAppear(animated)
// fetch latest notification when scroll position is within half screen height to prevent list reload
// if tableView.contentOffset.y < view.frame.height * 0.5 {
// viewModel.loadLatestStateMachine.enter(NotificationViewModel.LoadLatestState.Loading.self)
// }
2021-04-27 11:27:03 +02:00
2021-04-12 10:31:53 +02:00
// needs trigger manually after onboarding dismiss
setNeedsStatusBarAppearanceUpdate()
2021-04-12 10:31:53 +02:00
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// reset notification count
context.notificationService.clearNotificationCountForActiveUser()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// reset notification count
context.notificationService.clearNotificationCountForActiveUser()
2021-04-12 10:31:53 +02:00
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
// aspectViewDidDisappear(animated)
2021-02-23 09:45:00 +01:00
}
}
2021-04-12 10:31:53 +02:00
extension NotificationViewController {
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
}
// set initial selection
guard !pageSegmentedControl.isSelected else { return }
if viewModel.currentPageIndex < pageSegmentedControl.numberOfSegments {
pageSegmentedControl.selectedSegmentIndex = viewModel.currentPageIndex
} else {
pageSegmentedControl.selectedSegmentIndex = 0
}
}
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
}
}
extension NotificationViewController {
@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
}
}
// MARK: - ScrollViewContainer
extension NotificationViewController: ScrollViewContainer {
var scrollView: UIScrollView? {
guard let viewController = currentViewController as? NotificationTimelineViewController else {
return nil
}
return viewController.scrollView
}
}
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
}
}