Implemented the filter navigation button with its respective filter menu for the FeedListViewController view controller in the Feed framework.

This commit is contained in:
Javier Cicchelli 2024-03-20 21:24:02 +01:00
parent 656c0ee5ff
commit 7990076212

View File

@ -7,6 +7,8 @@
// //
import Combine import Combine
import Foundation
import ReviewsFoundationKit
import ReviewsUIKit import ReviewsUIKit
import SwiftUI import SwiftUI
import UIKit import UIKit
@ -19,6 +21,50 @@ public class FeedListViewController: UITableViewController {
// MARK: Properties // MARK: Properties
private var cancellables: Set<AnyCancellable> = [] private var cancellables: Set<AnyCancellable> = []
// MARK: Outlets
private lazy var filterButton = {
let allStars = UIAction(
title: FilterOption.all.text,
image: .init(systemName: FilterOption.all.icon)
) { [weak self] _ in
self?.viewModel.filter = .all
}
return UIBarButtonItem(
title: NSLocalizedString(
.Key.Navigation.Button.filter,
bundle: .module,
comment: .empty
),
image: .Icon.filter,
primaryAction: nil,
menu: .init(
title: NSLocalizedString(
.Key.Menu.filter,
bundle: .module,
comment: .empty
),
image: UIImage.Icon.star,
children: [allStars, filterStarMenu]
)
)
}()
private lazy var filterStarMenu = {
UIMenu(
options: .displayInline,
children: {
FilterOption.allCases.map { option -> UIAction in
.init(title: option.text,
image: .init(systemName: option.icon)
) { [weak self] _ in
self?.viewModel.filter = option
}
}
}()
)
}()
// MARK: Initialisers // MARK: Initialisers
public init(configuration: Configuration = .init()) { public init(configuration: Configuration = .init()) {
self.viewModel = .init(configuration: configuration) self.viewModel = .init(configuration: configuration)
@ -30,6 +76,11 @@ public class FeedListViewController: UITableViewController {
fatalError("init(coder:) has not been implemented") fatalError("init(coder:) has not been implemented")
} }
// MARK: Computed
var items: [Review] {
viewModel.items
}
// MARK: UIViewController // MARK: UIViewController
public override func viewDidLoad() { public override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
@ -60,11 +111,11 @@ public class FeedListViewController: UITableViewController {
cell.contentConfiguration = { cell.contentConfiguration = {
if #available(iOS 16.0, *) { if #available(iOS 16.0, *) {
UIHostingConfiguration { UIHostingConfiguration {
FeedItemCell(viewModel.items[indexPath.row]) FeedItemCell(items[indexPath.row])
} }
} else { } else {
HostingConfiguration { HostingConfiguration {
FeedItemCell(viewModel.items[indexPath.row]) FeedItemCell(items[indexPath.row])
} }
} }
}() }()
@ -77,7 +128,7 @@ public class FeedListViewController: UITableViewController {
_ tableView: UITableView, _ tableView: UITableView,
didSelectRowAt indexPath: IndexPath didSelectRowAt indexPath: IndexPath
) { ) {
let details = FeedItemViewController(viewModel.items[indexPath.row]) let details = FeedItemViewController(items[indexPath.row])
tableView.deselectRow( tableView.deselectRow(
at: indexPath, at: indexPath,
@ -94,15 +145,18 @@ private extension FeedListViewController {
// MARK: Functions // MARK: Functions
func bindViewModel() { func bindViewModel() {
viewModel.$loading viewModel.$isFilterEnabled
.sink { loading in .removeDuplicates()
print("LOADING: \(loading)") .receive(on: RunLoop.main)
.sink { [weak self] enabled in
self?.filterButton.isEnabled = enabled
} }
.store(in: &cancellables) .store(in: &cancellables)
viewModel.$loading viewModel.$isLoading
.dropFirst() .dropFirst()
.filter { $0 == false } .filter { $0 == false }
.removeDuplicates()
.receive(on: RunLoop.main) .receive(on: RunLoop.main)
.sink { [weak self] _ in .sink { [weak self] _ in
self?.tableView.reloadData() self?.tableView.reloadData()
@ -118,7 +172,12 @@ private extension FeedListViewController {
navigationController?.navigationBar.prefersLargeTitles = true navigationController?.navigationBar.prefersLargeTitles = true
navigationController?.navigationBar.isTranslucent = true navigationController?.navigationBar.isTranslucent = true
navigationItem.title = "Latest reviews" navigationItem.rightBarButtonItem = filterButton
navigationItem.title = NSLocalizedString(
.Key.Navigation.title,
bundle: .module,
comment: .empty
)
} }
} }
@ -151,11 +210,24 @@ private extension String {
enum Cell { enum Cell {
static let feedItem = "FeedItemCell" static let feedItem = "FeedItemCell"
} }
enum Key {
enum Menu {
static let filter = "common.filter.menu.title.text"
}
enum Navigation {
static let title = "view.feed-list.navigation-bar.title.text"
enum Button {
static let filter = "view.feed-list.navigation-bar.button.filter-list.text"
}
}
}
} }
// MARK: - Previews // MARK: - Previews
#if DEBUG #if DEBUG
import ReviewsFoundationKit
import ReviewsiTunesKit import ReviewsiTunesKit
@available(iOS 17.0, *) @available(iOS 17.0, *)