Kurdtvs-Live-Kurdish-TV-Kur.../Mastodon/Scene/Notification/NotificationViewController....

526 lines
22 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
//
2021-04-12 10:31:53 +02:00
import Combine
2021-04-14 09:00:48 +02:00
import CoreData
import CoreDataStack
2021-04-14 13:04:11 +02:00
import GameplayKit
2021-04-15 04:16:30 +02:00
import MastodonSDK
import OSLog
import UIKit
import ActiveLabel
import Meta
import MetaTextView
2021-02-23 09:45:00 +01:00
final class NotificationViewController: UIViewController, NeedsDependency {
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)
2021-04-18 16:00:19 +02:00
2021-04-12 10:31:53 +02:00
let segmentControl: UISegmentedControl = {
2021-04-15 04:16:30 +02:00
let control = UISegmentedControl(items: [L10n.Scene.Notification.Title.everything, L10n.Scene.Notification.Title.mentions])
2021-04-19 11:00:51 +02:00
control.selectedSegmentIndex = NotificationViewModel.NotificationSegment.EveryThing.rawValue
2021-04-12 10:31:53 +02:00
return control
}()
2021-04-18 16:00:19 +02:00
2021-04-12 10:31:53 +02:00
let tableView: UITableView = {
let tableView = ControlContainableTableView()
tableView.register(NotificationStatusTableViewCell.self, forCellReuseIdentifier: String(describing: NotificationStatusTableViewCell.self))
2021-04-16 07:45:54 +02:00
tableView.register(TimelineBottomLoaderTableViewCell.self, forCellReuseIdentifier: String(describing: TimelineBottomLoaderTableViewCell.self))
2021-04-16 15:55:09 +02:00
tableView.estimatedRowHeight = UITableView.automaticDimension
tableView.separatorStyle = .none
tableView.tableFooterView = UIView()
tableView.backgroundColor = .clear
2021-04-12 10:31:53 +02:00
return tableView
}()
2021-04-18 16:00:19 +02:00
2021-04-12 10:31:53 +02:00
let refreshControl = UIRefreshControl()
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)
segmentControl.translatesAutoresizingMaskIntoConstraints = false
2021-04-12 10:31:53 +02:00
navigationItem.titleView = segmentControl
NSLayoutConstraint.activate([
segmentControl.widthAnchor.constraint(equalToConstant: 287)
])
segmentControl.addTarget(self, action: #selector(NotificationViewController.segmentedControlValueChanged(_:)), for: .valueChanged)
2021-04-19 05:41:50 +02:00
tableView.translatesAutoresizingMaskIntoConstraints = false
2021-04-12 10:31:53 +02:00
view.addSubview(tableView)
2021-04-19 05:41:50 +02:00
NSLayoutConstraint.activate([
tableView.topAnchor.constraint(equalTo: view.topAnchor),
2021-04-12 10:31:53 +02:00
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])
2021-04-18 16:00:19 +02:00
2021-04-12 10:31:53 +02:00
tableView.refreshControl = refreshControl
refreshControl.addTarget(self, action: #selector(NotificationViewController.refreshControlValueChanged(_:)), for: .valueChanged)
2021-04-18 16:00:19 +02:00
2021-04-12 10:31:53 +02:00
tableView.delegate = self
viewModel.tableView = tableView
viewModel.contentOffsetAdjustableTimelineViewControllerDelegate = self
2021-04-14 09:00:48 +02:00
viewModel.setupDiffableDataSource(for: tableView, delegate: self, dependency: self)
2021-04-13 15:31:49 +02:00
viewModel.viewDidLoad.send()
2021-04-12 10:31:53 +02:00
// bind refresh control
viewModel.isFetchingLatestNotification
.receive(on: DispatchQueue.main)
.sink { [weak self] isFetching in
guard let self = self else { return }
if !isFetching {
UIView.animate(withDuration: 0.5) { [weak self] in
guard let self = self else { return }
self.refreshControl.endRefreshing()
}
}
}
.store(in: &disposeBag)
viewModel.dataSourceDidUpdated
.receive(on: RunLoop.main)
.sink { [weak self] in
guard let self = self else { return }
guard self.viewModel.needsScrollToTopAfterDataSourceUpdate else { return }
self.viewModel.needsScrollToTopAfterDataSourceUpdate = false
DispatchQueue.main.asyncAfter(deadline: .now() + 0.33) {
self.scrollToTop(animated: true)
}
}
.store(in: &disposeBag)
viewModel.selectedIndex
.removeDuplicates()
.receive(on: DispatchQueue.main)
.sink { [weak self] segment in
guard let self = self else { return }
self.segmentControl.selectedSegmentIndex = segment.rawValue
// trigger scroll-to-top after data reload
self.viewModel.needsScrollToTopAfterDataSourceUpdate = true
guard let domain = self.viewModel.activeMastodonAuthenticationBox.value?.domain, let userID = self.viewModel.activeMastodonAuthenticationBox.value?.userID else {
return
}
self.viewModel.needsScrollToTopAfterDataSourceUpdate = true
switch segment {
case .EveryThing:
self.viewModel.notificationPredicate.value = MastodonNotification.predicate(domain: domain, userID: userID)
case .Mentions:
self.viewModel.notificationPredicate.value = MastodonNotification.predicate(domain: domain, userID: userID, typeRaw: Mastodon.Entity.Notification.NotificationType.mention.rawValue)
}
}
.store(in: &disposeBag)
segmentControl.observe(\.selectedSegmentIndex, options: [.new]) { [weak self] segmentControl, _ in
guard let self = self else { return }
// scroll to top when select same segment
if segmentControl.selectedSegmentIndex == self.viewModel.selectedIndex.value.rawValue {
self.scrollToTop(animated: true)
}
}
.store(in: &observations)
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-04-18 16:00:19 +02:00
tableView.deselectRow(with: transitionCoordinator, animated: 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()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
if (self.viewModel.fetchedResultsController.fetchedObjects ?? []).count == 0 {
self.viewModel.loadLatestStateMachine.enter(NotificationViewModel.LoadLatestState.Loading.self)
}
}
}
2021-04-18 16:00:19 +02:00
2021-04-12 10:31:53 +02:00
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
coordinator.animate { _ in
// do nothing
} completion: { _ in
self.tableView.reloadData()
}
2021-02-23 09:45:00 +01:00
}
}
2021-04-12 10:31:53 +02:00
extension NotificationViewController {
@objc private func segmentedControlValueChanged(_ sender: UISegmentedControl) {
2021-04-15 04:16:30 +02:00
os_log("%{public}s[%{public}ld], %{public}s: select at index: %ld", (#file as NSString).lastPathComponent, #line, #function, sender.selectedSegmentIndex)
2021-04-18 16:00:19 +02:00
viewModel.selectedIndex.value = NotificationViewModel.NotificationSegment(rawValue: sender.selectedSegmentIndex)!
2021-04-12 10:31:53 +02:00
}
2021-04-18 16:00:19 +02:00
2021-04-12 10:31:53 +02:00
@objc private func refreshControlValueChanged(_ sender: UIRefreshControl) {
guard viewModel.loadLatestStateMachine.enter(NotificationViewModel.LoadLatestState.Loading.self) else {
sender.endRefreshing()
return
}
}
}
// MARK: - StatusTableViewControllerAspect
extension NotificationViewController: StatusTableViewControllerAspect { }
// MARK: - TableViewCellHeightCacheableContainer
extension NotificationViewController: TableViewCellHeightCacheableContainer {
var cellFrameCache: NSCache<NSNumber, NSValue> {
viewModel.cellFrameCache
}
func cacheTableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
guard let diffableDataSource = viewModel.diffableDataSource else { return }
guard let item = diffableDataSource.itemIdentifier(for: indexPath) else { return }
let key = item.hashValue
let frame = cell.frame
viewModel.cellFrameCache.setObject(NSValue(cgRect: frame), forKey: NSNumber(value: key))
}
func handleTableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
guard let diffableDataSource = viewModel.diffableDataSource else { return UITableView.automaticDimension }
guard let item = diffableDataSource.itemIdentifier(for: indexPath) else { return UITableView.automaticDimension }
guard let frame = viewModel.cellFrameCache.object(forKey: NSNumber(value: item.hashValue))?.cgRectValue else {
if case .bottomLoader = item {
return TimelineLoaderTableViewCell.cellHeight
} else {
return UITableView.automaticDimension
}
}
return ceil(frame.height)
}
}
2021-04-19 12:06:02 +02:00
2021-04-12 10:31:53 +02:00
// MARK: - UITableViewDelegate
2021-04-15 04:16:30 +02:00
2021-04-12 10:31:53 +02:00
extension NotificationViewController: UITableViewDelegate {
2021-04-19 12:06:02 +02:00
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let diffableDataSource = viewModel.diffableDataSource else { return }
guard let item = diffableDataSource.itemIdentifier(for: indexPath) else { return }
open(item: item)
}
}
extension NotificationViewController {
private func open(item: NotificationItem) {
switch item {
case .notification(let objectID, _):
let notification = context.managedObjectContext.object(with: objectID) as! MastodonNotification
2021-04-18 16:00:19 +02:00
if let status = notification.status {
let viewModel = ThreadViewModel(context: context, optionalStatus: status)
coordinator.present(scene: .thread(viewModel: viewModel), from: self, transition: .show)
} else {
2021-04-15 04:16:30 +02:00
let viewModel = ProfileViewModel(context: context, optionalMastodonUser: notification.account)
2021-04-18 16:00:19 +02:00
coordinator.present(scene: .profile(viewModel: viewModel), from: self, transition: .show)
}
default:
break
}
}
2021-04-18 16:00:19 +02:00
2021-04-14 14:02:41 +02:00
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
guard let diffableDataSource = viewModel.diffableDataSource else { return }
guard let item = diffableDataSource.itemIdentifier(for: indexPath) else { return }
switch item {
case .bottomLoader:
2021-04-15 04:16:30 +02:00
if !tableView.isDragging, !tableView.isDecelerating {
2021-04-14 14:02:41 +02:00
viewModel.loadoldestStateMachine.enter(NotificationViewModel.LoadOldestState.Loading.self)
}
default:
break
}
}
2021-04-12 10:31:53 +02:00
}
// MARK: - ContentOffsetAdjustableTimelineViewControllerDelegate
2021-04-15 04:16:30 +02:00
2021-04-12 10:31:53 +02:00
extension NotificationViewController: ContentOffsetAdjustableTimelineViewControllerDelegate {
func navigationBar() -> UINavigationBar? {
2021-04-15 04:16:30 +02:00
navigationController?.navigationBar
2021-04-12 10:31:53 +02:00
}
}
// MARK: - NotificationTableViewCellDelegate
2021-04-14 09:00:48 +02:00
extension NotificationViewController: NotificationTableViewCellDelegate {
func notificationStatusTableViewCell(_ cell: NotificationStatusTableViewCell, avatarImageViewDidPressed imageView: UIImageView) {
guard let diffableDataSource = viewModel.diffableDataSource else { return }
guard let indexPath = tableView.indexPath(for: cell) else { return }
guard let item = diffableDataSource.itemIdentifier(for: indexPath) else { return }
switch item {
case .notification(let objectID, _):
guard let notification = try? viewModel.fetchedResultsController.managedObjectContext.existingObject(with: objectID) as? MastodonNotification else { return }
let viewModel = ProfileViewModel(context: context, optionalMastodonUser: notification.account)
coordinator.present(scene: .profile(viewModel: viewModel), from: self, transition: .show)
default:
break
}
}
func notificationStatusTableViewCell(_ cell: NotificationStatusTableViewCell, authorNameLabelDidPressed label: ActiveLabel) {
guard let diffableDataSource = viewModel.diffableDataSource else { return }
guard let indexPath = tableView.indexPath(for: cell) else { return }
guard let item = diffableDataSource.itemIdentifier(for: indexPath) else { return }
switch item {
case .notification(let objectID, _):
guard let notification = try? viewModel.fetchedResultsController.managedObjectContext.existingObject(with: objectID) as? MastodonNotification else { return }
let viewModel = ProfileViewModel(context: context, optionalMastodonUser: notification.account)
coordinator.present(scene: .profile(viewModel: viewModel), from: self, transition: .show)
default:
break
}
}
func notificationTableViewCell(_ cell: NotificationStatusTableViewCell, notification: MastodonNotification, acceptButtonDidPressed button: UIButton) {
2021-04-27 10:54:23 +02:00
viewModel.acceptFollowRequest(notification: notification)
}
func notificationTableViewCell(_ cell: NotificationStatusTableViewCell, notification: MastodonNotification, rejectButtonDidPressed button: UIButton) {
2021-04-27 10:54:23 +02:00
viewModel.rejectFollowRequest(notification: notification)
}
2021-04-18 16:00:19 +02:00
func userNameLabelDidPressed(notification: MastodonNotification) {
let viewModel = CachedProfileViewModel(context: context, mastodonUser: notification.account)
DispatchQueue.main.async {
self.coordinator.present(scene: .profile(viewModel: viewModel), from: self, transition: .show)
}
}
2021-04-14 09:00:48 +02:00
func parent() -> UIViewController {
self
}
func notificationStatusTableViewCell(_ cell: NotificationStatusTableViewCell, statusView: StatusView, revealContentWarningButtonDidPressed button: UIButton) {
StatusProviderFacade.responseToStatusContentWarningRevealAction(provider: self, cell: cell)
}
func notificationStatusTableViewCell(_ cell: NotificationStatusTableViewCell, statusView: StatusView, contentWarningOverlayViewDidPressed contentWarningOverlayView: ContentWarningOverlayView) {
StatusProviderFacade.responseToStatusContentWarningRevealAction(provider: self, cell: cell)
}
func notificationStatusTableViewCell(_ cell: NotificationStatusTableViewCell, statusView: StatusView, playerContainerView: PlayerContainerView, contentWarningOverlayViewDidPressed contentWarningOverlayView: ContentWarningOverlayView) {
StatusProviderFacade.responseToStatusContentWarningRevealAction(provider: self, cell: cell)
}
func notificationStatusTableViewCell(_ cell: NotificationStatusTableViewCell, statusView: StatusView, metaText: MetaText, didSelectMeta meta: Meta) {
StatusProviderFacade.responseToStatusMetaTextAction(provider: self, cell: cell, metaText: metaText, didSelectMeta: meta)
}
2021-04-14 09:00:48 +02:00
}
2021-04-14 13:04:11 +02:00
// MARK: - UIScrollViewDelegate
2021-04-15 04:16:30 +02:00
2021-04-14 13:04:11 +02:00
extension NotificationViewController {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
handleScrollViewDidScroll(scrollView)
}
}
// MARK: - ScrollViewContainer
extension NotificationViewController: ScrollViewContainer {
var scrollView: UIScrollView { tableView }
func scrollToTop(animated: Bool) {
let indexPath = IndexPath(row: 0, section: 0)
guard viewModel.diffableDataSource?.itemIdentifier(for: indexPath) != nil else { return }
tableView.scrollToRow(at: indexPath, at: .top, animated: true)
}
}
2021-04-14 13:04:11 +02:00
extension NotificationViewController: LoadMoreConfigurableTableViewContainer {
2021-04-16 07:45:54 +02:00
typealias BottomLoaderTableViewCell = TimelineBottomLoaderTableViewCell
2021-04-14 13:04:11 +02:00
typealias LoadingState = NotificationViewModel.LoadOldestState.Loading
2021-04-15 04:16:30 +02:00
var loadMoreConfigurableTableView: UITableView { tableView }
var loadMoreConfigurableStateMachine: GKStateMachine { viewModel.loadoldestStateMachine }
2021-04-14 13:04:11 +02:00
}
extension NotificationViewController {
enum CategorySwitch: String, CaseIterable {
case showEverything
case showMentions
var title: String {
switch self {
case .showEverything: return L10n.Scene.Notification.Keyobard.showEverything
case .showMentions: return L10n.Scene.Notification.Keyobard.showMentions
}
}
// UIKeyCommand input
var input: String {
switch self {
case .showEverything: return "[" // + shift + command
case .showMentions: return "]" // + shift + command
}
}
var modifierFlags: UIKeyModifierFlags {
switch self {
case .showEverything: return [.shift, .command]
case .showMentions: 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 .showEverything:
viewModel.selectedIndex.value = .EveryThing
case .showMentions:
viewModel.selectedIndex.value = .Mentions
}
}
override var keyCommands: [UIKeyCommand]? {
return categorySwitchKeyCommands + navigationKeyCommands
}
}
extension NotificationViewController: TableViewControllerNavigateable {
func navigate(direction: TableViewNavigationDirection) {
if let indexPathForSelectedRow = tableView.indexPathForSelectedRow {
// navigate up/down on the current selected item
navigateToStatus(direction: direction, indexPath: indexPathForSelectedRow)
} else {
// set first visible item selected
navigateToFirstVisibleStatus()
}
}
private func navigateToStatus(direction: TableViewNavigationDirection, indexPath: IndexPath) {
guard let diffableDataSource = viewModel.diffableDataSource else { return }
let items = diffableDataSource.snapshot().itemIdentifiers
guard let selectedItem = diffableDataSource.itemIdentifier(for: indexPath),
let selectedItemIndex = items.firstIndex(of: selectedItem) else {
return
}
let _navigateToItem: NotificationItem? = {
var index = selectedItemIndex
while 0..<items.count ~= index {
index = {
switch direction {
case .up: return index - 1
case .down: return index + 1
}
}()
guard 0..<items.count ~= index else { return nil }
let item = items[index]
guard Self.validNavigateableItem(item) else { continue }
return item
}
return nil
}()
guard let item = _navigateToItem, let indexPath = diffableDataSource.indexPath(for: item) else { return }
let scrollPosition: UITableView.ScrollPosition = overrideNavigationScrollPosition ?? Self.navigateScrollPosition(tableView: tableView, indexPath: indexPath)
tableView.selectRow(at: indexPath, animated: true, scrollPosition: scrollPosition)
}
private func navigateToFirstVisibleStatus() {
guard let indexPathsForVisibleRows = tableView.indexPathsForVisibleRows else { return }
guard let diffableDataSource = viewModel.diffableDataSource else { return }
var visibleItems: [NotificationItem] = indexPathsForVisibleRows.sorted().compactMap { indexPath in
guard let item = diffableDataSource.itemIdentifier(for: indexPath) else { return nil }
guard Self.validNavigateableItem(item) else { return nil }
return item
}
if indexPathsForVisibleRows.first?.row != 0, visibleItems.count > 1 {
// drop first when visible not the first cell of table
visibleItems.removeFirst()
}
guard let item = visibleItems.first, let indexPath = diffableDataSource.indexPath(for: item) else { return }
let scrollPosition: UITableView.ScrollPosition = overrideNavigationScrollPosition ?? Self.navigateScrollPosition(tableView: tableView, indexPath: indexPath)
tableView.selectRow(at: indexPath, animated: true, scrollPosition: scrollPosition)
}
static func validNavigateableItem(_ item: NotificationItem) -> Bool {
switch item {
case .notification:
return true
default:
return false
}
}
func open() {
guard let indexPathForSelectedRow = tableView.indexPathForSelectedRow else { return }
guard let diffableDataSource = viewModel.diffableDataSource else { return }
guard let item = diffableDataSource.itemIdentifier(for: indexPathForSelectedRow) else { return }
open(item: item)
}
func navigateKeyCommandHandlerRelay(_ sender: UIKeyCommand) {
navigateKeyCommandHandler(sender)
}
}