FollowerListViewController.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // FollowerListViewController.swift
  3. // Mastodon
  4. //
  5. // Created by Cirno MainasuK on 2021-11-1.
  6. //
  7. import os.log
  8. import UIKit
  9. import GameplayKit
  10. import Combine
  11. import MastodonCore
  12. import MastodonUI
  13. import MastodonLocalization
  14. final class FollowerListViewController: UIViewController, NeedsDependency {
  15. let logger = Logger(subsystem: "FollowerListViewController", category: "ViewController")
  16. weak var context: AppContext! { willSet { precondition(!isViewLoaded) } }
  17. weak var coordinator: SceneCoordinator! { willSet { precondition(!isViewLoaded) } }
  18. var disposeBag = Set<AnyCancellable>()
  19. var viewModel: FollowerListViewModel!
  20. lazy var tableView: UITableView = {
  21. let tableView = UITableView()
  22. tableView.register(UserTableViewCell.self, forCellReuseIdentifier: String(describing: UserTableViewCell.self))
  23. tableView.register(TimelineBottomLoaderTableViewCell.self, forCellReuseIdentifier: String(describing: TimelineBottomLoaderTableViewCell.self))
  24. tableView.register(TimelineFooterTableViewCell.self, forCellReuseIdentifier: String(describing: TimelineFooterTableViewCell.self))
  25. tableView.rowHeight = UITableView.automaticDimension
  26. tableView.separatorStyle = .none
  27. tableView.backgroundColor = .clear
  28. return tableView
  29. }()
  30. deinit {
  31. os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
  32. }
  33. }
  34. extension FollowerListViewController {
  35. override func viewDidLoad() {
  36. super.viewDidLoad()
  37. title = L10n.Scene.Follower.title
  38. view.backgroundColor = ThemeService.shared.currentTheme.value.secondarySystemBackgroundColor
  39. ThemeService.shared.currentTheme
  40. .receive(on: DispatchQueue.main)
  41. .sink { [weak self] theme in
  42. guard let self = self else { return }
  43. self.view.backgroundColor = theme.secondarySystemBackgroundColor
  44. }
  45. .store(in: &disposeBag)
  46. tableView.translatesAutoresizingMaskIntoConstraints = false
  47. view.addSubview(tableView)
  48. NSLayoutConstraint.activate([
  49. tableView.topAnchor.constraint(equalTo: view.topAnchor),
  50. tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
  51. tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
  52. tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
  53. ])
  54. tableView.delegate = self
  55. viewModel.setupDiffableDataSource(
  56. tableView: tableView,
  57. userTableViewCellDelegate: self
  58. )
  59. // setup batch fetch
  60. viewModel.listBatchFetchViewModel.setup(scrollView: tableView)
  61. viewModel.listBatchFetchViewModel.shouldFetch
  62. .receive(on: DispatchQueue.main)
  63. .sink { [weak self] _ in
  64. guard let self = self else { return }
  65. self.viewModel.stateMachine.enter(FollowerListViewModel.State.Loading.self)
  66. }
  67. .store(in: &disposeBag)
  68. // trigger user timeline loading
  69. Publishers.CombineLatest(
  70. viewModel.$domain.removeDuplicates(),
  71. viewModel.$userID.removeDuplicates()
  72. )
  73. .receive(on: DispatchQueue.main)
  74. .sink { [weak self] _ in
  75. guard let self = self else { return }
  76. self.viewModel.stateMachine.enter(FollowerListViewModel.State.Reloading.self)
  77. }
  78. .store(in: &disposeBag)
  79. }
  80. override func viewWillAppear(_ animated: Bool) {
  81. super.viewWillAppear(animated)
  82. tableView.deselectRow(with: transitionCoordinator, animated: animated)
  83. }
  84. }
  85. // MARK: - AuthContextProvider
  86. extension FollowerListViewController: AuthContextProvider {
  87. var authContext: AuthContext { viewModel.authContext }
  88. }
  89. // MARK: - UITableViewDelegate
  90. extension FollowerListViewController: UITableViewDelegate, AutoGenerateTableViewDelegate {
  91. // sourcery:inline:FollowerListViewController.AutoGenerateTableViewDelegate
  92. // Generated using Sourcery
  93. // DO NOT EDIT
  94. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  95. aspectTableView(tableView, didSelectRowAt: indexPath)
  96. }
  97. // sourcery:end
  98. }
  99. // MARK: - UserTableViewCellDelegate
  100. extension FollowerListViewController: UserTableViewCellDelegate { }