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

246 lines
11 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
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>()
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.rowHeight = UITableView.automaticDimension
2021-04-13 15:31:49 +02:00
tableView.separatorStyle = .singleLine
tableView.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
2021-04-12 10:31:53 +02:00
tableView.register(NotificationTableViewCell.self, forCellReuseIdentifier: String(describing: NotificationTableViewCell.self))
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))
tableView.tableFooterView = UIView()
2021-04-16 15:55:09 +02:00
tableView.estimatedRowHeight = UITableView.automaticDimension
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()
2021-04-19 11:00:51 +02:00
view.backgroundColor = Asset.Colors.Background.secondarySystemBackground.color
2021-04-12 10:31:53 +02:00
navigationItem.titleView = segmentControl
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)
}
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)
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-16 07:45:54 +02:00
guard let domain = viewModel.activeMastodonAuthenticationBox.value?.domain, let userID = viewModel.activeMastodonAuthenticationBox.value?.userID else {
return
}
2021-04-19 11:00:51 +02:00
if sender.selectedSegmentIndex == NotificationViewModel.NotificationSegment.EveryThing.rawValue {
2021-04-16 07:45:54 +02:00
viewModel.notificationPredicate.value = MastodonNotification.predicate(domain: domain, userID: userID)
} else {
2021-04-18 16:00:19 +02:00
viewModel.notificationPredicate.value = MastodonNotification.predicate(domain: domain, userID: userID, typeRaw: Mastodon.Entity.Notification.NotificationType.mention.rawValue)
}
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
}
}
}
2021-04-19 12:06:02 +02:00
extension NotificationViewController {
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-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 }
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 userAvatarDidPressed(notification: MastodonNotification) {
2021-04-15 04:16:30 +02:00
let viewModel = ProfileViewModel(context: context, optionalMastodonUser: notification.account)
DispatchQueue.main.async {
self.coordinator.present(scene: .profile(viewModel: viewModel), from: self, transition: .show)
}
}
2021-04-18 16:00:19 +02:00
2021-04-14 09:00:48 +02:00
func parent() -> UIViewController {
self
}
func notificationStatusTableViewCell(_ cell: NotificationStatusTableViewCell, statusView: StatusView, revealContentWarningButtonDidPressed button: UIButton) {
StatusProviderFacade.responseToStatusContentWarningRevealAction(dependency: self, cell: cell)
}
func notificationStatusTableViewCell(_ cell: NotificationStatusTableViewCell, statusView: StatusView, contentWarningOverlayViewDidPressed contentWarningOverlayView: ContentWarningOverlayView) {
StatusProviderFacade.responseToStatusContentWarningRevealAction(dependency: self, cell: cell)
}
func notificationStatusTableViewCell(_ cell: NotificationStatusTableViewCell, statusView: StatusView, playerContainerView: PlayerContainerView, contentWarningOverlayViewDidPressed contentWarningOverlayView: ContentWarningOverlayView) {
StatusProviderFacade.responseToStatusContentWarningRevealAction(dependency: self, cell: cell)
}
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)
}
}
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
}