[Framework] Pull to refresh in the Feed List (#13)
This PR contains the work done to implement the pull to refresh into the `FeedListViewController` view controller. Reviewed-on: rock-n-code/app-reviews#13 Co-authored-by: Javier Cicchelli <javier@rock-n-code.com> Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
This commit was merged in pull request #13.
This commit is contained in:
@@ -51,6 +51,24 @@ public class FeedListViewController: UITableViewController {
|
||||
)
|
||||
)
|
||||
}()
|
||||
|
||||
private lazy var pullControl = {
|
||||
let control = UIRefreshControl()
|
||||
|
||||
control.attributedTitle = .init(string: NSLocalizedString(
|
||||
.Key.PullToRefresh.title,
|
||||
bundle: .module,
|
||||
comment: .empty
|
||||
))
|
||||
|
||||
control.addTarget(
|
||||
self,
|
||||
action: #selector(refresh(_:)),
|
||||
for: .valueChanged
|
||||
)
|
||||
|
||||
return control
|
||||
}()
|
||||
|
||||
// MARK: Initialisers
|
||||
public init(configuration: Configuration = .init()) {
|
||||
@@ -73,6 +91,7 @@ public class FeedListViewController: UITableViewController {
|
||||
super.viewDidLoad()
|
||||
|
||||
setNavigationBar()
|
||||
setView()
|
||||
registerTableCells()
|
||||
bindViewModel()
|
||||
|
||||
@@ -103,20 +122,28 @@ public class FeedListViewController: UITableViewController {
|
||||
_ tableView: UITableView,
|
||||
didSelectRowAt indexPath: IndexPath
|
||||
) {
|
||||
let details = FeedItemViewController(items[indexPath.row])
|
||||
|
||||
guard let item = viewModel.item(for: indexPath.row) else { return }
|
||||
|
||||
tableView.deselectRow(
|
||||
at: indexPath,
|
||||
animated: true
|
||||
)
|
||||
|
||||
navigationController?.pushViewController(details, animated: true)
|
||||
|
||||
navigationController?.pushViewController(
|
||||
FeedItemViewController(item),
|
||||
animated: true
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
private extension FeedListViewController {
|
||||
|
||||
// MARK: Actions
|
||||
@objc func refresh(_ sender: AnyObject) {
|
||||
self.viewModel.fetch()
|
||||
}
|
||||
|
||||
// MARK: Functions
|
||||
func bindViewModel() {
|
||||
@@ -139,10 +166,15 @@ private extension FeedListViewController {
|
||||
viewModel.$isLoading
|
||||
.dropFirst()
|
||||
.filter { $0 == false }
|
||||
.removeDuplicates()
|
||||
.receive(on: RunLoop.main)
|
||||
.sink { [weak self] _ in
|
||||
self?.pullControl.endRefreshing()
|
||||
self?.tableView.reloadData()
|
||||
self?.tableView.scrollToRow(
|
||||
at: .init(row: 0, section: 0),
|
||||
at: .middle,
|
||||
animated: true
|
||||
)
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
}
|
||||
@@ -194,7 +226,7 @@ private extension FeedListViewController {
|
||||
|
||||
return cell
|
||||
}
|
||||
|
||||
|
||||
func registerTableCells() {
|
||||
FeedItemCell.register(in: tableView)
|
||||
TopWordsCell.register(in: tableView)
|
||||
@@ -212,6 +244,10 @@ private extension FeedListViewController {
|
||||
)
|
||||
}
|
||||
|
||||
func setView() {
|
||||
tableView.refreshControl = pullControl
|
||||
}
|
||||
|
||||
func updateFilterMenu(_ option: FilterOption) {
|
||||
filterButton
|
||||
.menu?
|
||||
@@ -267,6 +303,10 @@ private extension String {
|
||||
static let filter = "view.feed-list.navigation-bar.button.filter-list.text"
|
||||
}
|
||||
}
|
||||
|
||||
enum PullToRefresh {
|
||||
static let title = "view.feed-list.pull-to-refresh.title.text"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user