Integrated the FeedViewModel view model into the FeedViewController view controller in the Feed framework.
This commit is contained in:
parent
6d5b416471
commit
c0c05b1faa
@ -6,6 +6,7 @@
|
||||
// Copyright © 2020 ING. All rights reserved.
|
||||
//
|
||||
|
||||
import Combine
|
||||
import UIKit
|
||||
|
||||
public class FeedViewController: UITableViewController {
|
||||
@ -13,6 +14,9 @@ public class FeedViewController: UITableViewController {
|
||||
// MARK: Constants
|
||||
private let viewModel: ViewModel = .init()
|
||||
|
||||
// MARK: Properties
|
||||
private var cancellables: Set<AnyCancellable> = []
|
||||
|
||||
// MARK: Initialisers
|
||||
public init() {
|
||||
super.init(style: .plain)
|
||||
@ -25,26 +29,54 @@ public class FeedViewController: UITableViewController {
|
||||
// MARK: UIViewController
|
||||
public override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
|
||||
tableView.register(ReviewCell.self, forCellReuseIdentifier: "cellId")
|
||||
tableView.rowHeight = 160
|
||||
|
||||
bindViewModel()
|
||||
|
||||
viewModel.fetch()
|
||||
}
|
||||
|
||||
// MARK: UITableViewDataSource
|
||||
public override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
||||
return 100
|
||||
public override func tableView(
|
||||
_ tableView: UITableView,
|
||||
numberOfRowsInSection section: Int
|
||||
) -> Int {
|
||||
viewModel.items.count
|
||||
}
|
||||
|
||||
public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
||||
let c = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as! ReviewCell
|
||||
c.update(item: randomReview())
|
||||
return c
|
||||
public override func tableView(
|
||||
_ tableView: UITableView,
|
||||
cellForRowAt indexPath: IndexPath
|
||||
) -> 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
|
||||
public override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
||||
tableView.deselectRow(at: indexPath, animated: true)
|
||||
let vc = DetailsViewController(review: randomReview())
|
||||
navigationController!.pushViewController(vc, animated: true)
|
||||
public override func tableView(
|
||||
_ tableView: UITableView,
|
||||
didSelectRowAt indexPath: IndexPath
|
||||
) {
|
||||
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 {
|
||||
|
||||
// MARK: Functions
|
||||
func randomReview() -> Review {
|
||||
let author = ["Dan Auerbach", "Bo Diddley", "Otis Rush", "Jimi Hendrix", "Albert King", "Buddy Guy", "Muddy Waters", "Eric Clapton"].randomElement()!
|
||||
let version = ["3.11", "3.12"].randomElement()!
|
||||
let rating = Int.random(in: 1...5)
|
||||
let title = ["Awesome app", "Could be better", "Gimme my money back!!", "Lemme tell you a story..."].randomElement()!
|
||||
let id = UUID().uuidString
|
||||
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,
|
||||
version: version,
|
||||
rating: rating,
|
||||
title: title,
|
||||
id: id,
|
||||
content: content)
|
||||
func bindViewModel() {
|
||||
viewModel.$loading
|
||||
.sink { loading in
|
||||
print("LOADING: \(loading)")
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
|
||||
viewModel.$loading
|
||||
.filter { $0 == false }
|
||||
.dropFirst()
|
||||
.receive(on: RunLoop.main)
|
||||
.sink { [weak self] _ in
|
||||
self?.tableView.reloadData()
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user