2020-07-21 12:29:54 +02:00
|
|
|
//
|
|
|
|
// AppDelegate.swift
|
2024-03-18 17:07:33 +01:00
|
|
|
// ReviewsFeed
|
2020-07-21 12:29:54 +02:00
|
|
|
//
|
|
|
|
// Created by Dmitrii Ivanov on 21/07/2020.
|
|
|
|
// Copyright © 2020 ING. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2024-03-18 23:23:24 +01:00
|
|
|
import Combine
|
2024-03-19 01:59:18 +01:00
|
|
|
import ReviewsUIKit
|
|
|
|
import SwiftUI
|
2020-07-21 12:29:54 +02:00
|
|
|
import UIKit
|
|
|
|
|
2024-03-16 00:28:59 +00:00
|
|
|
public class FeedViewController: UITableViewController {
|
2020-07-21 12:29:54 +02:00
|
|
|
|
2024-03-18 17:07:33 +01:00
|
|
|
// MARK: Constants
|
|
|
|
private let viewModel: ViewModel = .init()
|
|
|
|
|
2024-03-18 23:23:24 +01:00
|
|
|
// MARK: Properties
|
|
|
|
private var cancellables: Set<AnyCancellable> = []
|
|
|
|
|
2024-03-18 17:07:33 +01:00
|
|
|
// MARK: Initialisers
|
2024-03-16 00:28:59 +00:00
|
|
|
public init() {
|
|
|
|
super.init(style: .plain)
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
2024-03-18 17:07:33 +01:00
|
|
|
// MARK: UIViewController
|
2024-03-16 00:28:59 +00:00
|
|
|
public override func viewDidLoad() {
|
2020-07-21 12:29:54 +02:00
|
|
|
super.viewDidLoad()
|
2024-03-18 23:23:24 +01:00
|
|
|
|
2024-03-19 01:59:18 +01:00
|
|
|
tableView.register(
|
|
|
|
UITableViewCell.self,
|
|
|
|
forCellReuseIdentifier: .Cell.feedItem
|
|
|
|
)
|
|
|
|
|
2024-03-18 23:23:24 +01:00
|
|
|
bindViewModel()
|
|
|
|
|
|
|
|
viewModel.fetch()
|
2020-07-21 12:29:54 +02:00
|
|
|
}
|
2024-03-18 17:07:33 +01:00
|
|
|
|
|
|
|
// MARK: UITableViewDataSource
|
2024-03-18 23:23:24 +01:00
|
|
|
public override func tableView(
|
|
|
|
_ tableView: UITableView,
|
|
|
|
numberOfRowsInSection section: Int
|
|
|
|
) -> Int {
|
|
|
|
viewModel.items.count
|
2020-07-21 12:29:54 +02:00
|
|
|
}
|
2024-03-18 17:07:33 +01:00
|
|
|
|
2024-03-18 23:23:24 +01:00
|
|
|
public override func tableView(
|
|
|
|
_ tableView: UITableView,
|
|
|
|
cellForRowAt indexPath: IndexPath
|
|
|
|
) -> UITableViewCell {
|
2024-03-19 01:59:18 +01:00
|
|
|
guard let cell = tableView.dequeueReusableCell(withIdentifier: .Cell.feedItem) else {
|
2024-03-18 23:23:24 +01:00
|
|
|
return .init()
|
|
|
|
}
|
|
|
|
|
2024-03-19 01:59:18 +01:00
|
|
|
cell.contentConfiguration = {
|
|
|
|
if #available(iOS 16.0, *) {
|
|
|
|
UIHostingConfiguration {
|
|
|
|
FeedItem(viewModel.items[indexPath.row])
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
HostingConfiguration {
|
|
|
|
FeedItem(viewModel.items[indexPath.row])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2024-03-18 23:23:24 +01:00
|
|
|
|
|
|
|
return cell
|
2020-07-21 12:29:54 +02:00
|
|
|
}
|
|
|
|
|
2024-03-18 17:07:33 +01:00
|
|
|
// MARK: UITableViewDelegate
|
2024-03-18 23:23:24 +01:00
|
|
|
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)
|
2020-07-21 12:29:54 +02:00
|
|
|
}
|
|
|
|
|
2024-03-18 17:07:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Helpers
|
|
|
|
private extension FeedViewController {
|
|
|
|
|
|
|
|
// MARK: Functions
|
2024-03-18 23:23:24 +01:00
|
|
|
func bindViewModel() {
|
|
|
|
viewModel.$loading
|
|
|
|
.sink { loading in
|
|
|
|
print("LOADING: \(loading)")
|
|
|
|
}
|
|
|
|
.store(in: &cancellables)
|
|
|
|
|
|
|
|
viewModel.$loading
|
|
|
|
.dropFirst()
|
2024-03-19 01:59:18 +01:00
|
|
|
.filter { $0 == false }
|
2024-03-18 23:23:24 +01:00
|
|
|
.receive(on: RunLoop.main)
|
|
|
|
.sink { [weak self] _ in
|
|
|
|
self?.tableView.reloadData()
|
|
|
|
}
|
|
|
|
.store(in: &cancellables)
|
2020-07-21 12:29:54 +02:00
|
|
|
}
|
2024-03-18 17:07:33 +01:00
|
|
|
|
2020-07-21 12:29:54 +02:00
|
|
|
}
|
2024-03-19 01:59:18 +01:00
|
|
|
|
|
|
|
// MARK: - String+Constants
|
|
|
|
private extension String {
|
|
|
|
enum Cell {
|
|
|
|
static let feedItem = "FeedItemCell"
|
|
|
|
}
|
|
|
|
}
|