2023-04-11 22:14:40 +00:00
|
|
|
//
|
|
|
|
// LocationsListViewController.swift
|
|
|
|
// Locations
|
|
|
|
//
|
|
|
|
// Created by Javier Cicchelli on 08/04/2023.
|
|
|
|
// Copyright © 2023 Röck+Cöde. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2023-04-12 13:46:51 +02:00
|
|
|
import Combine
|
2023-04-11 22:14:40 +00:00
|
|
|
import Core
|
|
|
|
import UIKit
|
|
|
|
|
|
|
|
class LocationsListViewController: BaseViewController {
|
|
|
|
|
|
|
|
// MARK: Properties
|
|
|
|
|
2023-04-12 12:50:26 +02:00
|
|
|
private let viewModel: LocationsListViewModeling
|
|
|
|
|
2023-04-12 13:46:51 +02:00
|
|
|
private var cancellables: Set<AnyCancellable> = []
|
|
|
|
|
2023-04-12 12:50:26 +02:00
|
|
|
// MARK: Outlets
|
|
|
|
|
|
|
|
private lazy var error = ErrorMessageView()
|
|
|
|
private lazy var loading = LoadingSpinnerView()
|
|
|
|
private lazy var table = {
|
|
|
|
let table = UITableView(frame: .zero, style: .plain)
|
|
|
|
|
2023-04-12 16:37:46 +02:00
|
|
|
table.dataSource = self
|
|
|
|
table.delegate = self
|
2023-04-12 12:50:26 +02:00
|
|
|
table.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
|
|
|
|
return table
|
|
|
|
}()
|
2023-04-11 22:14:40 +00:00
|
|
|
|
|
|
|
// MARK: Initialisers
|
|
|
|
|
|
|
|
init(viewModel: LocationsListViewModeling) {
|
|
|
|
self.viewModel = viewModel
|
|
|
|
|
|
|
|
super.init()
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: UIViewController
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
2023-04-12 12:50:26 +02:00
|
|
|
|
|
|
|
setupBar()
|
|
|
|
setupView()
|
2023-04-12 13:46:51 +02:00
|
|
|
bindViewModel()
|
2023-04-12 16:37:46 +02:00
|
|
|
|
|
|
|
viewModel.loadLocations()
|
2023-04-11 22:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-04-12 16:37:46 +02:00
|
|
|
// MARK: - UITableViewDataSource
|
|
|
|
|
|
|
|
extension LocationsListViewController: UITableViewDataSource {
|
|
|
|
|
|
|
|
// MARK: Functions
|
|
|
|
|
|
|
|
func numberOfSections(in tableView: UITableView) -> Int {
|
|
|
|
viewModel.numberOfSectionsInData
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(
|
|
|
|
_ tableView: UITableView,
|
|
|
|
numberOfRowsInSection section: Int
|
|
|
|
) -> Int {
|
|
|
|
viewModel.numberOfDataItems(in: section)
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(
|
|
|
|
_ tableView: UITableView,
|
|
|
|
cellForRowAt indexPath: IndexPath
|
|
|
|
) -> UITableViewCell {
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: "cellID", for: indexPath)
|
|
|
|
let entity = viewModel.dataItem(at: indexPath)
|
|
|
|
|
|
|
|
cell.textLabel?.text = entity.name
|
|
|
|
|
|
|
|
return cell
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - UITableViewDelegate
|
|
|
|
|
|
|
|
extension LocationsListViewController: UITableViewDelegate {}
|
|
|
|
|
2023-04-11 22:14:40 +00:00
|
|
|
// MARK: - Helpers
|
|
|
|
|
|
|
|
private extension LocationsListViewController {
|
|
|
|
|
|
|
|
// MARK: Functions
|
|
|
|
|
2023-04-12 12:50:26 +02:00
|
|
|
func setupBar() {
|
|
|
|
navigationController?.navigationBar.prefersLargeTitles = true
|
|
|
|
navigationController?.navigationBar.tintColor = .red
|
|
|
|
navigationItem.rightBarButtonItem = .init(
|
|
|
|
title: "Add",
|
|
|
|
style: .plain,
|
|
|
|
target: self,
|
|
|
|
action: #selector(addLocationPressed)
|
|
|
|
)
|
|
|
|
title = "Locations"
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupView() {
|
|
|
|
view.addSubview(table)
|
|
|
|
view.addSubview(error)
|
|
|
|
view.addSubview(loading)
|
2023-04-12 16:37:46 +02:00
|
|
|
|
|
|
|
error.onRetry = {
|
|
|
|
self.viewModel.loadLocations()
|
|
|
|
}
|
|
|
|
|
|
|
|
table.register(UITableViewCell.self, forCellReuseIdentifier: "cellID")
|
|
|
|
|
2023-04-12 12:50:26 +02:00
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
error.widthAnchor.constraint(equalToConstant: 300),
|
|
|
|
view.centerXAnchor.constraint(equalTo: error.centerXAnchor),
|
|
|
|
view.centerYAnchor.constraint(equalTo: error.centerYAnchor),
|
|
|
|
view.centerXAnchor.constraint(equalTo: loading.centerXAnchor),
|
|
|
|
view.centerYAnchor.constraint(equalTo: loading.centerYAnchor),
|
|
|
|
view.bottomAnchor.constraint(equalTo: table.bottomAnchor),
|
|
|
|
view.leadingAnchor.constraint(equalTo: table.leadingAnchor),
|
|
|
|
view.topAnchor.constraint(equalTo: table.topAnchor),
|
|
|
|
view.trailingAnchor.constraint(equalTo: table.trailingAnchor),
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
2023-04-12 13:46:51 +02:00
|
|
|
func bindViewModel() {
|
|
|
|
viewModel
|
|
|
|
.viewStatusPublisher
|
|
|
|
.receive(on: RunLoop.main)
|
|
|
|
.sink { viewStatus in
|
|
|
|
self.navigationItem.rightBarButtonItem?.isEnabled = viewStatus == .loaded
|
|
|
|
self.error.isHidden = viewStatus != .error
|
|
|
|
self.loading.isHidden = viewStatus != .loading
|
|
|
|
self.table.isHidden = viewStatus != .loaded
|
2023-04-12 16:37:46 +02:00
|
|
|
|
|
|
|
if viewStatus == .loaded {
|
|
|
|
self.table.reloadData()
|
|
|
|
}
|
2023-04-12 13:46:51 +02:00
|
|
|
}
|
|
|
|
.store(in: &cancellables)
|
|
|
|
}
|
|
|
|
|
2023-04-11 22:14:40 +00:00
|
|
|
@objc func addLocationPressed() {
|
|
|
|
viewModel.openAddLocation()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|