Added the "viewStatusPublisher" property to the LocationsListViewModeling protocol and binded this property to the LocationsListViewController view controller.

This commit is contained in:
Javier Cicchelli 2023-04-12 13:46:51 +02:00
parent 39ec206454
commit f718210180
3 changed files with 37 additions and 5 deletions

View File

@ -6,12 +6,17 @@
// Copyright © 2023 Röck+Cöde. All rights reserved.
//
import Combine
import Foundation
protocol LocationsListViewModeling: AnyObject {
// MARK: Properties
var coordinator: LocationsListCoordination? { get set }
var viewStatusPublisher: Published<LocationsListViewStatus>.Publisher { get }
// MARK: Functions
func openAddLocation()

View File

@ -6,6 +6,7 @@
// Copyright © 2023 Röck+Cöde. All rights reserved.
//
import Combine
import Core
import UIKit
@ -15,6 +16,8 @@ class LocationsListViewController: BaseViewController {
private let viewModel: LocationsListViewModeling
private var cancellables: Set<AnyCancellable> = []
// MARK: Outlets
private lazy var error = ErrorMessageView()
@ -46,6 +49,7 @@ class LocationsListViewController: BaseViewController {
setupBar()
setupView()
bindViewModel()
}
}
@ -72,12 +76,7 @@ private extension LocationsListViewController {
view.addSubview(table)
view.addSubview(error)
view.addSubview(loading)
error.isHidden = true
error.onRetry = { print("RETRY BUTTON PRESSED!") }
loading.isHidden = true
NSLayoutConstraint.activate([
error.widthAnchor.constraint(equalToConstant: 300),
view.centerXAnchor.constraint(equalTo: error.centerXAnchor),
@ -91,6 +90,19 @@ private extension LocationsListViewController {
])
}
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
}
.store(in: &cancellables)
}
@objc func addLocationPressed() {
viewModel.openAddLocation()
}

View File

@ -15,6 +15,8 @@ class LocationsListViewModel: ObservableObject {
weak var coordinator: LocationsListCoordination?
@Published private var viewStatus: LocationsListViewStatus = .initialised
// MARK: Initialisers
init(coordinator: LocationsListCoordination) {
@ -27,6 +29,10 @@ class LocationsListViewModel: ObservableObject {
extension LocationsListViewModel: LocationsListViewModeling {
// MARK: Properties
var viewStatusPublisher: Published<LocationsListViewStatus>.Publisher { $viewStatus }
// MARK: Functions
func openAddLocation() {
@ -34,3 +40,12 @@ extension LocationsListViewModel: LocationsListViewModeling {
}
}
// MARK: - Enumerations
enum LocationsListViewStatus {
case initialised
case loading
case loaded
case error
}