Skip to content

Commit

Permalink
Add pull to refresh to Followers-list
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitschlag committed Oct 25, 2023
1 parent e187dbe commit d9d9a3c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 17 deletions.
5 changes: 2 additions & 3 deletions Mastodon/Coordinator/SceneCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -464,9 +464,8 @@ private extension SceneCoordinator {
_viewController.viewModel = viewModel
viewController = _viewController
case .follower(let viewModel):
let _viewController = FollowerListViewController()
_viewController.viewModel = viewModel
viewController = _viewController
let followerListViewController = FollowerListViewController(viewModel: viewModel, coordinator: self, context: appContext)
viewController = followerListViewController
case .following(let viewModel):
let followingListViewController = FollowingListViewController(viewModel: viewModel, coordinator: self, context: appContext)
viewController = followingListViewController
Expand Down
53 changes: 42 additions & 11 deletions Mastodon/Scene/Profile/Follower/FollowerListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,53 @@ import Combine
import MastodonCore
import MastodonUI
import MastodonLocalization
import CoreDataStack

final class FollowerListViewController: UIViewController, NeedsDependency {

weak var context: AppContext! { willSet { precondition(!isViewLoaded) } }
weak var coordinator: SceneCoordinator! { willSet { precondition(!isViewLoaded) } }
weak var context: AppContext!
weak var coordinator: SceneCoordinator!

var disposeBag = Set<AnyCancellable>()
var viewModel: FollowerListViewModel!
var viewModel: FollowerListViewModel

lazy var tableView: UITableView = {
let tableView = UITableView()
let tableView: UITableView
let refreshControl: UIRefreshControl

init(viewModel: FollowerListViewModel, coordinator: SceneCoordinator, context: AppContext) {

self.context = context
self.coordinator = coordinator
self.viewModel = viewModel

tableView = UITableView()
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.register(UserTableViewCell.self, forCellReuseIdentifier: String(describing: UserTableViewCell.self))
tableView.register(TimelineBottomLoaderTableViewCell.self, forCellReuseIdentifier: String(describing: TimelineBottomLoaderTableViewCell.self))
tableView.register(TimelineFooterTableViewCell.self, forCellReuseIdentifier: String(describing: TimelineFooterTableViewCell.self))
tableView.rowHeight = UITableView.automaticDimension
tableView.separatorStyle = .none
tableView.backgroundColor = .clear
return tableView
}()



refreshControl = UIRefreshControl()
tableView.refreshControl = refreshControl

super.init(nibName: nil, bundle: nil)

title = L10n.Scene.Following.title

view.backgroundColor = .secondarySystemBackground

view.addSubview(tableView)
tableView.pinToParent()
tableView.delegate = self
tableView.refreshControl?.addTarget(self, action: #selector(FollowingListViewController.refresh(_:)), for: .valueChanged)

viewModel.tableView = tableView

refreshControl.addTarget(self, action: #selector(FollowerListViewController.refresh(_:)), for: .valueChanged)
}

required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
}

extension FollowerListViewController {
Expand Down Expand Up @@ -82,7 +107,13 @@ extension FollowerListViewController {

tableView.deselectRow(with: transitionCoordinator, animated: animated)
}


//MARK: - Actions

@objc
func refresh(_ sender: UIRefreshControl) {
viewModel.stateMachine.enter(FollowerListViewModel.State.Reloading.self)
}
}

// MARK: - AuthContextProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import Foundation
import GameplayKit
import MastodonSDK
import MastodonCore
import CoreDataStack

extension FollowerListViewModel {
class State: GKState {
Expand Down Expand Up @@ -98,6 +97,12 @@ extension FollowerListViewModel.State {
return false
}
}

override func didEnter(from previousState: GKState?) {
super.didEnter(from: previousState)

viewModel?.tableView?.refreshControl?.endRefreshing()
}
}

class Loading: FollowerListViewModel.State {
Expand Down Expand Up @@ -188,6 +193,8 @@ extension FollowerListViewModel.State {

override func didEnter(from previousState: GKState?) {
super.didEnter(from: previousState)

viewModel?.tableView?.refreshControl?.endRefreshing()
}
}
}
8 changes: 6 additions & 2 deletions Mastodon/Scene/Profile/Follower/FollowerListViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ final class FollowerListViewModel {
let authContext: AuthContext
@Published var accounts: [Mastodon.Entity.Account]
@Published var relationships: [Mastodon.Entity.Relationship]
let listBatchFetchViewModel = ListBatchFetchViewModel()

let listBatchFetchViewModel: ListBatchFetchViewModel

@Published var domain: String?
@Published var userID: String?


var tableView: UITableView?

// output
var diffableDataSource: UITableViewDiffableDataSource<UserSection, UserItem>?
private(set) lazy var stateMachine: GKStateMachine = {
Expand Down Expand Up @@ -53,5 +56,6 @@ final class FollowerListViewModel {
self.userID = userID
self.accounts = []
self.relationships = []
self.listBatchFetchViewModel = ListBatchFetchViewModel()
}
}

0 comments on commit d9d9a3c

Please sign in to comment.