Integrated the FeedViewModel view model into the FeedViewController view controller in the Feed framework.

This commit is contained in:
Javier Cicchelli 2024-03-18 23:23:24 +01:00
parent 6d5b416471
commit c0c05b1faa

View File

@ -6,6 +6,7 @@
// Copyright © 2020 ING. All rights reserved. // Copyright © 2020 ING. All rights reserved.
// //
import Combine
import UIKit import UIKit
public class FeedViewController: UITableViewController { public class FeedViewController: UITableViewController {
@ -13,6 +14,9 @@ public class FeedViewController: UITableViewController {
// MARK: Constants // MARK: Constants
private let viewModel: ViewModel = .init() private let viewModel: ViewModel = .init()
// MARK: Properties
private var cancellables: Set<AnyCancellable> = []
// MARK: Initialisers // MARK: Initialisers
public init() { public init() {
super.init(style: .plain) super.init(style: .plain)
@ -25,26 +29,54 @@ public class FeedViewController: UITableViewController {
// MARK: UIViewController // MARK: UIViewController
public override func viewDidLoad() { public override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
tableView.register(ReviewCell.self, forCellReuseIdentifier: "cellId") tableView.register(ReviewCell.self, forCellReuseIdentifier: "cellId")
tableView.rowHeight = 160 tableView.rowHeight = 160
bindViewModel()
viewModel.fetch()
} }
// MARK: UITableViewDataSource // MARK: UITableViewDataSource
public override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { public override func tableView(
return 100 _ tableView: UITableView,
numberOfRowsInSection section: Int
) -> Int {
viewModel.items.count
} }
public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { public override func tableView(
let c = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as! ReviewCell _ tableView: UITableView,
c.update(item: randomReview()) cellForRowAt indexPath: IndexPath
return c ) -> UITableViewCell {
guard
let cell = tableView.dequeueReusableCell(
withIdentifier: "cellId",
for: indexPath
) as? ReviewCell
else {
return .init()
}
cell.update(item: viewModel.items[indexPath.row])
return cell
} }
// MARK: UITableViewDelegate // MARK: UITableViewDelegate
public override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { public override func tableView(
tableView.deselectRow(at: indexPath, animated: true) _ tableView: UITableView,
let vc = DetailsViewController(review: randomReview()) didSelectRowAt indexPath: IndexPath
navigationController!.pushViewController(vc, animated: true) ) {
let details = DetailsViewController(review: viewModel.items[indexPath.row])
tableView.deselectRow(
at: indexPath,
animated: true
)
navigationController?.pushViewController(details, animated: true)
} }
} }
@ -53,19 +85,21 @@ public class FeedViewController: UITableViewController {
private extension FeedViewController { private extension FeedViewController {
// MARK: Functions // MARK: Functions
func randomReview() -> Review { func bindViewModel() {
let author = ["Dan Auerbach", "Bo Diddley", "Otis Rush", "Jimi Hendrix", "Albert King", "Buddy Guy", "Muddy Waters", "Eric Clapton"].randomElement()! viewModel.$loading
let version = ["3.11", "3.12"].randomElement()! .sink { loading in
let rating = Int.random(in: 1...5) print("LOADING: \(loading)")
let title = ["Awesome app", "Could be better", "Gimme my money back!!", "Lemme tell you a story..."].randomElement()! }
let id = UUID().uuidString .store(in: &cancellables)
let content = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
return Review(author: author, viewModel.$loading
version: version, .filter { $0 == false }
rating: rating, .dropFirst()
title: title, .receive(on: RunLoop.main)
id: id, .sink { [weak self] _ in
content: content) self?.tableView.reloadData()
}
.store(in: &cancellables)
} }
} }