[Framework] Pull to refresh in the Feed List #13
@ -108,6 +108,18 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"view.feed-list.pull-to-refresh.title.text" : {
|
||||
"comment" : "The title for the Pull To Refresh control associated to the table in the Feed List view.",
|
||||
"extractionState" : "manual",
|
||||
"localizations" : {
|
||||
"en" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "Pull to fetch the latest reviews"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"version" : "1.0"
|
||||
|
@ -82,7 +82,10 @@ extension FeedListViewController {
|
||||
topWords($0).map(TopWord.init)
|
||||
}
|
||||
|
||||
items = reviewsAll
|
||||
items = filter == .all
|
||||
? reviewsAll
|
||||
: reviewsFiltered[filter] ?? []
|
||||
|
||||
isFilterEnabled = !items.isEmpty
|
||||
} catch {
|
||||
// TODO: handle this error gracefully.
|
||||
|
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user