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

168 lines
6.4 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 UIKit
2021-04-12 10:31:53 +02:00
import Combine
import OSLog
2021-04-14 09:00:48 +02:00
import CoreData
import CoreDataStack
import MastodonSDK
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-12 10:31:53 +02:00
var disposeBag = Set<AnyCancellable>()
private(set) lazy var viewModel = NotificationViewModel(context: context, coordinator: coordinator)
let segmentControl: UISegmentedControl = {
let control = UISegmentedControl(items: [L10n.Scene.Notification.Title.everything,L10n.Scene.Notification.Title.mentions])
control.selectedSegmentIndex = 0
return control
}()
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))
2021-04-13 15:31:49 +02:00
tableView.register(SearchBottomLoader.self, forCellReuseIdentifier: String(describing: SearchBottomLoader.self))
tableView.tableFooterView = UIView()
2021-04-12 10:31:53 +02:00
return tableView
}()
let refreshControl = UIRefreshControl()
2021-02-23 09:45:00 +01:00
}
extension NotificationViewController {
override func viewDidLoad() {
super.viewDidLoad()
2021-04-12 10:31:53 +02:00
view.backgroundColor = Asset.Colors.Background.searchResult.color
navigationItem.titleView = segmentControl
segmentControl.addTarget(self, action: #selector(NotificationViewController.segmentedControlValueChanged(_:)), for: .valueChanged)
2021-04-12 10:31:53 +02:00
view.addSubview(tableView)
tableView.constrain([
tableView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])
tableView.refreshControl = refreshControl
refreshControl.addTarget(self, action: #selector(NotificationViewController.refreshControlValueChanged(_:)), for: .valueChanged)
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)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
2021-02-23 09:45:00 +01: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)
}
}
}
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) {
os_log("%{public}s[%{public}ld], %{public}s: select at index: %ld", ((#file as NSString).lastPathComponent), #line, #function, sender.selectedSegmentIndex)
guard let domain = viewModel.activeMastodonAuthenticationBox.value?.domain else {
return
}
if sender.selectedSegmentIndex == 0 {
viewModel.notificationPredicate.value = MastodonNotification.predicate(domain: domain)
} else {
viewModel.notificationPredicate.value = MastodonNotification.predicate(domain: domain, type: Mastodon.Entity.Notification.NotificationType.mention.rawValue)
}
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: - UITableViewDelegate
extension NotificationViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 68
}
}
// MARK: - ContentOffsetAdjustableTimelineViewControllerDelegate
extension NotificationViewController: ContentOffsetAdjustableTimelineViewControllerDelegate {
func navigationBar() -> UINavigationBar? {
return navigationController?.navigationBar
}
}
2021-04-14 09:00:48 +02:00
extension NotificationViewController: NotificationTableViewCellDelegate {
func parent() -> UIViewController {
self
}
}
2021-04-12 10:31:53 +02:00
//// MARK: - UIScrollViewDelegate
//extension NotificationViewController {
// func scrollViewDidScroll(_ scrollView: UIScrollView) {
// handleScrollViewDidScroll(scrollView)
// }
//}
//
//extension NotificationViewController: LoadMoreConfigurableTableViewContainer {
// typealias BottomLoaderTableViewCell = SearchBottomLoader
// typealias LoadingState = NotificationViewController.LoadOldestState.Loading
// var loadMoreConfigurableTableView: UITableView { return tableView }
// var loadMoreConfigurableStateMachine: GKStateMachine { return viewModel.loadoldestStateMachine }
//}