From 3dba1de84e88f23a4911ec1391a33ae577b3bfa7 Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Wed, 12 Apr 2023 09:49:31 +0200 Subject: [PATCH 01/13] Implemented the LoadingSpinnerView custom view. --- .../View Components/LoadingSpinnerView.swift | 87 +++++++++++++++++++ DeepLinking.xcodeproj/project.pbxproj | 12 +++ 2 files changed, 99 insertions(+) create mode 100644 Apps/Locations/Sources/View Components/LoadingSpinnerView.swift diff --git a/Apps/Locations/Sources/View Components/LoadingSpinnerView.swift b/Apps/Locations/Sources/View Components/LoadingSpinnerView.swift new file mode 100644 index 0000000..c6865e2 --- /dev/null +++ b/Apps/Locations/Sources/View Components/LoadingSpinnerView.swift @@ -0,0 +1,87 @@ +// +// LoadingSpinnerView.swift +// Locations +// +// Created by Javier Cicchelli on 12/04/2023. +// Copyright © 2023 Röck+Cöde. All rights reserved. +// + +import UIKit + +class LoadingSpinnerView: UIView { + + // MARK: Outlets + + lazy var stack: UIStackView = { + let stack = UIStackView(frame: .zero) + + stack.alignment = .center + stack.axis = .vertical + stack.distribution = .fill + stack.spacing = 8 + stack.translatesAutoresizingMaskIntoConstraints = false + + return stack + }() + + lazy var spinner: UIActivityIndicatorView = { + let spinner = UIActivityIndicatorView(style: .large) + + spinner.translatesAutoresizingMaskIntoConstraints = false + + spinner.startAnimating() + + return spinner + }() + + lazy var label: UILabel = { + let label = UILabel() + + label.translatesAutoresizingMaskIntoConstraints = false + label.font = .preferredFont(forTextStyle: .headline) + label.text = "Loading..." + label.textAlignment = .center + label.numberOfLines = 0 + label.lineBreakMode = .byWordWrapping + + return label + }() + + // MARK: Initialisers + + init() { + super.init(frame: .zero) + + setupView() + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + +} + +// MARK: - Helpers + +private extension LoadingSpinnerView { + + // MARK: Functions + + func setupView() { + backgroundColor = .clear + translatesAutoresizingMaskIntoConstraints = false + + stack.addArrangedSubview(spinner) + stack.addArrangedSubview(label) + + addSubview(stack) + + NSLayoutConstraint.activate([ + topAnchor.constraint(equalTo: stack.topAnchor), + leadingAnchor.constraint(equalTo: stack.leadingAnchor), + trailingAnchor.constraint(equalTo: stack.trailingAnchor), + bottomAnchor.constraint(equalTo: stack.bottomAnchor), + ]) + } + +} diff --git a/DeepLinking.xcodeproj/project.pbxproj b/DeepLinking.xcodeproj/project.pbxproj index 33b73ae..2be6b99 100644 --- a/DeepLinking.xcodeproj/project.pbxproj +++ b/DeepLinking.xcodeproj/project.pbxproj @@ -10,6 +10,7 @@ 02031EBF29E5F949003C108C /* LocationsAddViewModeling.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02031EBE29E5F949003C108C /* LocationsAddViewModeling.swift */; }; 02031EC629E5FEE4003C108C /* BaseViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02031EC529E5FEE4003C108C /* BaseViewController.swift */; }; 02031EC929E60B29003C108C /* DependencyService+Keys.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02031EC829E60B29003C108C /* DependencyService+Keys.swift */; }; + 02031EE829E68D9B003C108C /* LoadingSpinnerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02031EE729E68D9B003C108C /* LoadingSpinnerView.swift */; }; 46C3B7C629E5BF1500F8F57C /* LocationsListCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46C3B7C529E5BF1500F8F57C /* LocationsListCoordinator.swift */; }; 46C3B7CB29E5CD3200F8F57C /* LocationsListViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46C3B7CA29E5CD3200F8F57C /* LocationsListViewModel.swift */; }; 46C3B7CF29E5D00E00F8F57C /* LocationsAddViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46C3B7CE29E5D00E00F8F57C /* LocationsAddViewModel.swift */; }; @@ -124,6 +125,7 @@ 02031EBE29E5F949003C108C /* LocationsAddViewModeling.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationsAddViewModeling.swift; sourceTree = ""; }; 02031EC529E5FEE4003C108C /* BaseViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BaseViewController.swift; sourceTree = ""; }; 02031EC829E60B29003C108C /* DependencyService+Keys.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "DependencyService+Keys.swift"; sourceTree = ""; }; + 02031EE729E68D9B003C108C /* LoadingSpinnerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoadingSpinnerView.swift; sourceTree = ""; }; 46C3B7C529E5BF1500F8F57C /* LocationsListCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationsListCoordinator.swift; sourceTree = ""; }; 46C3B7CA29E5CD3200F8F57C /* LocationsListViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationsListViewModel.swift; sourceTree = ""; }; 46C3B7CE29E5D00E00F8F57C /* LocationsAddViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationsAddViewModel.swift; sourceTree = ""; }; @@ -174,6 +176,14 @@ path = Extensions; sourceTree = ""; }; + 02031EE629E68D7A003C108C /* View Components */ = { + isa = PBXGroup; + children = ( + 02031EE729E68D9B003C108C /* LoadingSpinnerView.swift */, + ); + path = "View Components"; + sourceTree = ""; + }; 0276C96029E5F5DC000B62AF /* Protocols */ = { isa = PBXGroup; children = ( @@ -304,6 +314,7 @@ 46C3B7C429E5BEE900F8F57C /* Coordinators */, 46C3B7C929E5CB8F00F8F57C /* Screens */, 02031EC429E5FEB1003C108C /* View Controllers */, + 02031EE629E68D7A003C108C /* View Components */, ); path = Sources; sourceTree = ""; @@ -521,6 +532,7 @@ 46C3B7CF29E5D00E00F8F57C /* LocationsAddViewModel.swift in Sources */, 02031EC929E60B29003C108C /* DependencyService+Keys.swift in Sources */, 46C3B7D129E5D06D00F8F57C /* LocationsAddViewController.swift in Sources */, + 02031EE829E68D9B003C108C /* LoadingSpinnerView.swift in Sources */, 46C3B7CB29E5CD3200F8F57C /* LocationsListViewModel.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; -- 2.47.1 From c91cbbe7dc1ca9cc41d8ee9fbcc7f2f5ebcbb5e2 Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Wed, 12 Apr 2023 12:31:10 +0200 Subject: [PATCH 02/13] Implemented the ErrorMessageView custom view. --- .../View Components/ErrorMessageView.swift | 124 ++++++++++++++++++ .../View Components/LoadingSpinnerView.swift | 21 +-- DeepLinking.xcodeproj/project.pbxproj | 4 + 3 files changed, 139 insertions(+), 10 deletions(-) create mode 100644 Apps/Locations/Sources/View Components/ErrorMessageView.swift diff --git a/Apps/Locations/Sources/View Components/ErrorMessageView.swift b/Apps/Locations/Sources/View Components/ErrorMessageView.swift new file mode 100644 index 0000000..d161f77 --- /dev/null +++ b/Apps/Locations/Sources/View Components/ErrorMessageView.swift @@ -0,0 +1,124 @@ +// +// ErrorMessageView.swift +// Locations +// +// Created by Javier Cicchelli on 12/04/2023. +// Copyright © 2023 Röck+Cöde. All rights reserved. +// + +import UIKit + +class ErrorMessageView: UIView { + + // MARK: Typealiases + + typealias OnRetryClosure = () -> Void + + // MARK: Properties + + var onRetry: OnRetryClosure? + + // MARK: Outlets + + private lazy var stack: UIStackView = { + let stack = UIStackView() + + stack.alignment = .center + stack.axis = .vertical + stack.distribution = .fill + stack.spacing = 32 + stack.translatesAutoresizingMaskIntoConstraints = false + + return stack + }() + + private lazy var title = { + let title = UILabel() + + title.font = .preferredFont(forTextStyle: .largeTitle) + title.numberOfLines = 0 + title.lineBreakMode = .byWordWrapping + title.text = "Some error title goes in here..." + title.textAlignment = .center + title.translatesAutoresizingMaskIntoConstraints = false + + return title + }() + + private lazy var message = { + let message = UILabel() + + message.font = .preferredFont(forTextStyle: .body) + message.lineBreakMode = .byWordWrapping + message.numberOfLines = 0 + message.text = "Some long, descriptive, explanatory error message goes in here..." + message.textAlignment = .center + message.textColor = .secondaryLabel + message.translatesAutoresizingMaskIntoConstraints = false + + return message + }() + + private lazy var retry = { + let retry = UIButton() + + retry.backgroundColor = .red + retry.translatesAutoresizingMaskIntoConstraints = false + retry.layer.borderColor = UIColor.red.cgColor + retry.layer.borderWidth = 1 + retry.layer.cornerRadius = 5 + retry.titleLabel?.font = .preferredFont(forTextStyle: .headline) + + retry.addTarget(self, action: #selector(retryPressed), for: .touchUpInside) + retry.setTitle("Try again", for: .normal) + + return retry + }() + + // MARK: Initialisers + + init() { + super.init(frame: .zero) + + setupView() + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + +} + +// MARK: - Helpers + +private extension ErrorMessageView { + + // MARK: Functions + + func setupView() { + stack.addArrangedSubview(title) + stack.addArrangedSubview(message) + stack.addArrangedSubview(retry) + stack.setCustomSpacing(160, after: message) + + backgroundColor = .clear + translatesAutoresizingMaskIntoConstraints = false + + addSubview(stack) + + NSLayoutConstraint.activate([ + retry.heightAnchor.constraint(equalToConstant: 44), + retry.leadingAnchor.constraint(equalTo: stack.leadingAnchor), + retry.trailingAnchor.constraint(equalTo: stack.trailingAnchor), + bottomAnchor.constraint(equalTo: stack.bottomAnchor), + leadingAnchor.constraint(equalTo: stack.leadingAnchor), + topAnchor.constraint(equalTo: stack.topAnchor), + trailingAnchor.constraint(equalTo: stack.trailingAnchor) + ]) + } + + @objc func retryPressed() { + onRetry?() + } + +} diff --git a/Apps/Locations/Sources/View Components/LoadingSpinnerView.swift b/Apps/Locations/Sources/View Components/LoadingSpinnerView.swift index c6865e2..461613b 100644 --- a/Apps/Locations/Sources/View Components/LoadingSpinnerView.swift +++ b/Apps/Locations/Sources/View Components/LoadingSpinnerView.swift @@ -12,8 +12,8 @@ class LoadingSpinnerView: UIView { // MARK: Outlets - lazy var stack: UIStackView = { - let stack = UIStackView(frame: .zero) + private lazy var stack = { + let stack = UIStackView() stack.alignment = .center stack.axis = .vertical @@ -24,7 +24,7 @@ class LoadingSpinnerView: UIView { return stack }() - lazy var spinner: UIActivityIndicatorView = { + private lazy var spinner = { let spinner = UIActivityIndicatorView(style: .large) spinner.translatesAutoresizingMaskIntoConstraints = false @@ -34,15 +34,16 @@ class LoadingSpinnerView: UIView { return spinner }() - lazy var label: UILabel = { + private lazy var label = { let label = UILabel() - label.translatesAutoresizingMaskIntoConstraints = false + label.font = .preferredFont(forTextStyle: .headline) - label.text = "Loading..." - label.textAlignment = .center label.numberOfLines = 0 label.lineBreakMode = .byWordWrapping + label.text = "Loading..." + label.textAlignment = .center + label.translatesAutoresizingMaskIntoConstraints = false return label }() @@ -68,12 +69,12 @@ private extension LoadingSpinnerView { // MARK: Functions func setupView() { - backgroundColor = .clear - translatesAutoresizingMaskIntoConstraints = false - stack.addArrangedSubview(spinner) stack.addArrangedSubview(label) + backgroundColor = .clear + translatesAutoresizingMaskIntoConstraints = false + addSubview(stack) NSLayoutConstraint.activate([ diff --git a/DeepLinking.xcodeproj/project.pbxproj b/DeepLinking.xcodeproj/project.pbxproj index 2be6b99..f83395d 100644 --- a/DeepLinking.xcodeproj/project.pbxproj +++ b/DeepLinking.xcodeproj/project.pbxproj @@ -11,6 +11,7 @@ 02031EC629E5FEE4003C108C /* BaseViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02031EC529E5FEE4003C108C /* BaseViewController.swift */; }; 02031EC929E60B29003C108C /* DependencyService+Keys.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02031EC829E60B29003C108C /* DependencyService+Keys.swift */; }; 02031EE829E68D9B003C108C /* LoadingSpinnerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02031EE729E68D9B003C108C /* LoadingSpinnerView.swift */; }; + 02031EEA29E6B495003C108C /* ErrorMessageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02031EE929E6B495003C108C /* ErrorMessageView.swift */; }; 46C3B7C629E5BF1500F8F57C /* LocationsListCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46C3B7C529E5BF1500F8F57C /* LocationsListCoordinator.swift */; }; 46C3B7CB29E5CD3200F8F57C /* LocationsListViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46C3B7CA29E5CD3200F8F57C /* LocationsListViewModel.swift */; }; 46C3B7CF29E5D00E00F8F57C /* LocationsAddViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46C3B7CE29E5D00E00F8F57C /* LocationsAddViewModel.swift */; }; @@ -126,6 +127,7 @@ 02031EC529E5FEE4003C108C /* BaseViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BaseViewController.swift; sourceTree = ""; }; 02031EC829E60B29003C108C /* DependencyService+Keys.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "DependencyService+Keys.swift"; sourceTree = ""; }; 02031EE729E68D9B003C108C /* LoadingSpinnerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoadingSpinnerView.swift; sourceTree = ""; }; + 02031EE929E6B495003C108C /* ErrorMessageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ErrorMessageView.swift; sourceTree = ""; }; 46C3B7C529E5BF1500F8F57C /* LocationsListCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationsListCoordinator.swift; sourceTree = ""; }; 46C3B7CA29E5CD3200F8F57C /* LocationsListViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationsListViewModel.swift; sourceTree = ""; }; 46C3B7CE29E5D00E00F8F57C /* LocationsAddViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationsAddViewModel.swift; sourceTree = ""; }; @@ -180,6 +182,7 @@ isa = PBXGroup; children = ( 02031EE729E68D9B003C108C /* LoadingSpinnerView.swift */, + 02031EE929E6B495003C108C /* ErrorMessageView.swift */, ); path = "View Components"; sourceTree = ""; @@ -526,6 +529,7 @@ 46EB331B29E1CE04001D5EAF /* AppDelegate.swift in Sources */, 02031EBF29E5F949003C108C /* LocationsAddViewModeling.swift in Sources */, 46C3B7DE29E5ED2E00F8F57C /* LocationsAddCoordinator.swift in Sources */, + 02031EEA29E6B495003C108C /* ErrorMessageView.swift in Sources */, 46C3B7DC29E5ED2300F8F57C /* LocationsAddCoordination.swift in Sources */, 46C3B7D829E5E55000F8F57C /* LocationsListCoordination.swift in Sources */, 46C3B7D629E5E50500F8F57C /* LocationsListViewModeling.swift in Sources */, -- 2.47.1 From 39ec2064542515d9c65c494ec31f1318245d5fdd Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Wed, 12 Apr 2023 12:50:26 +0200 Subject: [PATCH 03/13] Implemented the outlets of the LocationsListViewController view controller. --- .../LocationsListViewController.swift | 60 ++++++++++++++++--- 1 file changed, 51 insertions(+), 9 deletions(-) diff --git a/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewController.swift b/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewController.swift index cc35c26..3868d89 100644 --- a/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewController.swift +++ b/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewController.swift @@ -13,7 +13,19 @@ class LocationsListViewController: BaseViewController { // MARK: Properties - var viewModel: LocationsListViewModeling + private let viewModel: LocationsListViewModeling + + // MARK: Outlets + + private lazy var error = ErrorMessageView() + private lazy var loading = LoadingSpinnerView() + private lazy var table = { + let table = UITableView(frame: .zero, style: .plain) + + table.translatesAutoresizingMaskIntoConstraints = false + + return table + }() // MARK: Initialisers @@ -31,14 +43,9 @@ class LocationsListViewController: BaseViewController { override func viewDidLoad() { super.viewDidLoad() - - navigationItem.rightBarButtonItem = UIBarButtonItem( - title: "Add", - style: .plain, - target: self, - action: #selector(addLocationPressed) - ) - title = "Locations" + + setupBar() + setupView() } } @@ -49,6 +56,41 @@ private extension LocationsListViewController { // MARK: Functions + 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) + + 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), + 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), + ]) + } + @objc func addLocationPressed() { viewModel.openAddLocation() } -- 2.47.1 From f7182101805eecf163395695bf2a49eadc50ebf6 Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Wed, 12 Apr 2023 13:46:51 +0200 Subject: [PATCH 04/13] Added the "viewStatusPublisher" property to the LocationsListViewModeling protocol and binded this property to the LocationsListViewController view controller. --- .../LocationsListViewModeling.swift | 5 +++++ .../LocationsListViewController.swift | 22 ++++++++++++++----- .../LocationsListViewModel.swift | 15 +++++++++++++ 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/Apps/Locations/Sources/Protocols/ViewModeling/LocationsListViewModeling.swift b/Apps/Locations/Sources/Protocols/ViewModeling/LocationsListViewModeling.swift index 90deeab..40691a3 100644 --- a/Apps/Locations/Sources/Protocols/ViewModeling/LocationsListViewModeling.swift +++ b/Apps/Locations/Sources/Protocols/ViewModeling/LocationsListViewModeling.swift @@ -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.Publisher { get } + // MARK: Functions func openAddLocation() diff --git a/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewController.swift b/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewController.swift index 3868d89..9681ec5 100644 --- a/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewController.swift +++ b/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewController.swift @@ -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 = [] + // 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() } diff --git a/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewModel.swift b/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewModel.swift index c3644fa..6b4f55d 100644 --- a/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewModel.swift +++ b/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewModel.swift @@ -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.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 +} -- 2.47.1 From 543417744b7b5e16342be205479ce7311020411b Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Wed, 12 Apr 2023 14:48:27 +0200 Subject: [PATCH 05/13] Turned off the Location entity automatic code generation from the Model core data model in the Persistence library. --- .../Entities/Location+CoreDataClass.swift | 21 +++++++++++ .../Location+CoreDataProperties.swift | 36 +++++++++++++++++++ .../Locations.xcdatamodel/contents | 2 +- 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 Apps/Locations/Libraries/Sources/Persistence/Entities/Location+CoreDataClass.swift create mode 100644 Apps/Locations/Libraries/Sources/Persistence/Entities/Location+CoreDataProperties.swift diff --git a/Apps/Locations/Libraries/Sources/Persistence/Entities/Location+CoreDataClass.swift b/Apps/Locations/Libraries/Sources/Persistence/Entities/Location+CoreDataClass.swift new file mode 100644 index 0000000..2478f9e --- /dev/null +++ b/Apps/Locations/Libraries/Sources/Persistence/Entities/Location+CoreDataClass.swift @@ -0,0 +1,21 @@ +// +// Location+CoreDataClass.swift +// Locations +// +// Created by Javier Cicchelli on 12/04/2023. +// Copyright © 2023 Röck+Cöde. All rights reserved. +// +// + +import Foundation +import CoreData + +@objc(Location) +public class Location: NSManagedObject { + convenience init(context: NSManagedObjectContext) { + self.init( + entity: .entity(forEntityName: "Location", in: context)!, + insertInto: context + ) + } +} diff --git a/Apps/Locations/Libraries/Sources/Persistence/Entities/Location+CoreDataProperties.swift b/Apps/Locations/Libraries/Sources/Persistence/Entities/Location+CoreDataProperties.swift new file mode 100644 index 0000000..e7776ee --- /dev/null +++ b/Apps/Locations/Libraries/Sources/Persistence/Entities/Location+CoreDataProperties.swift @@ -0,0 +1,36 @@ +// +// Location+CoreDataProperties.swift +// Locations +// +// Created by Javier Cicchelli on 12/04/2023. +// Copyright © 2023 Röck+Cöde. All rights reserved. +// +// + +import Foundation +import CoreData + +extension Location { + + @nonobjc public class func fetchRequest() -> NSFetchRequest { + return NSFetchRequest(entityName: "Location") + } + + @NSManaged public var createdAt: Date + @NSManaged public var latitude: Float + @NSManaged public var longitude: Float + @NSManaged public var name: String? + @NSManaged public var source: LocationSource + +} + +// MARK: - Identifiable + +extension Location: Identifiable {} + +// MARK: - Enumerations + +@objc public enum LocationSource: Int16 { + case remote = 0 + case local +} diff --git a/Apps/Locations/Libraries/Sources/Persistence/Model.xcdatamodeld/Locations.xcdatamodel/contents b/Apps/Locations/Libraries/Sources/Persistence/Model.xcdatamodeld/Locations.xcdatamodel/contents index bd27564..cae680f 100644 --- a/Apps/Locations/Libraries/Sources/Persistence/Model.xcdatamodeld/Locations.xcdatamodel/contents +++ b/Apps/Locations/Libraries/Sources/Persistence/Model.xcdatamodeld/Locations.xcdatamodel/contents @@ -1,6 +1,6 @@ - + -- 2.47.1 From 4f315d7bfb0219c096956c96bcbcd30336c5bd88 Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Wed, 12 Apr 2023 15:14:02 +0200 Subject: [PATCH 06/13] Implemented the "allLocations()" static function in the NSFetchRequest+Location extension. --- .../Extensions/NSFetchRequest+Location.swift | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Apps/Locations/Libraries/Sources/Persistence/Extensions/NSFetchRequest+Location.swift diff --git a/Apps/Locations/Libraries/Sources/Persistence/Extensions/NSFetchRequest+Location.swift b/Apps/Locations/Libraries/Sources/Persistence/Extensions/NSFetchRequest+Location.swift new file mode 100644 index 0000000..acca57e --- /dev/null +++ b/Apps/Locations/Libraries/Sources/Persistence/Extensions/NSFetchRequest+Location.swift @@ -0,0 +1,27 @@ +// +// NSFetchRequest+Location.swift +// Persistence +// +// Created by Javier Cicchelli on 12/04/2023. +// Copyright © 2023 Röck+Cöde. All rights reserved. +// + +import CoreData + +public extension NSFetchRequest where ResultType == Location { + + // MARK: Functions + + static func allLocations() -> NSFetchRequest { + let request = Location.fetchRequest() + + request.sortDescriptors = [ + .init(keyPath: \Location.source, ascending: true), + .init(keyPath: \Location.createdAt, ascending: true) + ] + request.resultType = .managedObjectResultType + + return request + } + +} -- 2.47.1 From 8c50ce3653e0681534b33bba09459a916425f121 Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Wed, 12 Apr 2023 15:22:30 +0200 Subject: [PATCH 07/13] Implemented the LoadRemoteLocationsUseCase use case. --- .../LoadRemoteLocationsUseCase.swift | 75 +++++++++++++++++++ DeepLinking.xcodeproj/project.pbxproj | 12 +++ 2 files changed, 87 insertions(+) create mode 100644 Apps/Locations/Sources/Use Cases/LoadRemoteLocationsUseCase.swift diff --git a/Apps/Locations/Sources/Use Cases/LoadRemoteLocationsUseCase.swift b/Apps/Locations/Sources/Use Cases/LoadRemoteLocationsUseCase.swift new file mode 100644 index 0000000..e64bd01 --- /dev/null +++ b/Apps/Locations/Sources/Use Cases/LoadRemoteLocationsUseCase.swift @@ -0,0 +1,75 @@ +// +// LoadRemoteLocationsUseCase.swift +// Locations +// +// Created by Javier Cicchelli on 12/04/2023. +// Copyright © 2023 Röck+Cöde. All rights reserved. +// + +import CoreData +import Dependency +import Persistence +import Remote + +struct LoadRemoteLocationsUseCase { + + // MARK: Properties + + private let persistence: PersistenceService + private let remoteService: RemoteService + + // MARK: Initialisers + + init( + persistence: PersistenceService, + remoteService: RemoteService + ) { + self.persistence = persistence + self.remoteService = remoteService + } + + func callAsFunction() async throws { + let context = persistence.makeTaskContext() + let fetchRequest = NSFetchRequest.allLocations() + + try await context.perform { + let localLocations = try context.fetch(fetchRequest) + + localLocations + .filter { $0.source == .remote } + .forEach(context.delete) + } + + let remoteLocations = try await remoteService.getLocations() + + _ = remoteLocations + .map { + let entity = Persistence.Location(context: context) + + entity.createdAt = .now + entity.name = $0.name + entity.latitude = $0.latitude + entity.longitude = $0.longitude + entity.source = .remote + + return entity + } + + persistence.save(context: context) + } + +} + +// MARK: - LoadRemoteLocationsUseCase+Initialisers + +extension LoadRemoteLocationsUseCase { + init() { + @Dependency(\.persistence) var persistence + @Dependency(\.remote) var remote + + self.init( + persistence: persistence, + remoteService: remote + ) + } +} diff --git a/DeepLinking.xcodeproj/project.pbxproj b/DeepLinking.xcodeproj/project.pbxproj index f83395d..f6a330a 100644 --- a/DeepLinking.xcodeproj/project.pbxproj +++ b/DeepLinking.xcodeproj/project.pbxproj @@ -12,6 +12,7 @@ 02031EC929E60B29003C108C /* DependencyService+Keys.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02031EC829E60B29003C108C /* DependencyService+Keys.swift */; }; 02031EE829E68D9B003C108C /* LoadingSpinnerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02031EE729E68D9B003C108C /* LoadingSpinnerView.swift */; }; 02031EEA29E6B495003C108C /* ErrorMessageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02031EE929E6B495003C108C /* ErrorMessageView.swift */; }; + 4656CBC229E6D33C00600EE6 /* LoadRemoteLocationsUseCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4656CBC129E6D33C00600EE6 /* LoadRemoteLocationsUseCase.swift */; }; 46C3B7C629E5BF1500F8F57C /* LocationsListCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46C3B7C529E5BF1500F8F57C /* LocationsListCoordinator.swift */; }; 46C3B7CB29E5CD3200F8F57C /* LocationsListViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46C3B7CA29E5CD3200F8F57C /* LocationsListViewModel.swift */; }; 46C3B7CF29E5D00E00F8F57C /* LocationsAddViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46C3B7CE29E5D00E00F8F57C /* LocationsAddViewModel.swift */; }; @@ -128,6 +129,7 @@ 02031EC829E60B29003C108C /* DependencyService+Keys.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "DependencyService+Keys.swift"; sourceTree = ""; }; 02031EE729E68D9B003C108C /* LoadingSpinnerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoadingSpinnerView.swift; sourceTree = ""; }; 02031EE929E6B495003C108C /* ErrorMessageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ErrorMessageView.swift; sourceTree = ""; }; + 4656CBC129E6D33C00600EE6 /* LoadRemoteLocationsUseCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoadRemoteLocationsUseCase.swift; sourceTree = ""; }; 46C3B7C529E5BF1500F8F57C /* LocationsListCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationsListCoordinator.swift; sourceTree = ""; }; 46C3B7CA29E5CD3200F8F57C /* LocationsListViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationsListViewModel.swift; sourceTree = ""; }; 46C3B7CE29E5D00E00F8F57C /* LocationsAddViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationsAddViewModel.swift; sourceTree = ""; }; @@ -214,6 +216,14 @@ path = Coordination; sourceTree = ""; }; + 4656CBC029E6D31800600EE6 /* Use Cases */ = { + isa = PBXGroup; + children = ( + 4656CBC129E6D33C00600EE6 /* LoadRemoteLocationsUseCase.swift */, + ); + path = "Use Cases"; + sourceTree = ""; + }; 46C3B7C429E5BEE900F8F57C /* Coordinators */ = { isa = PBXGroup; children = ( @@ -318,6 +328,7 @@ 46C3B7C929E5CB8F00F8F57C /* Screens */, 02031EC429E5FEB1003C108C /* View Controllers */, 02031EE629E68D7A003C108C /* View Components */, + 4656CBC029E6D31800600EE6 /* Use Cases */, ); path = Sources; sourceTree = ""; @@ -533,6 +544,7 @@ 46C3B7DC29E5ED2300F8F57C /* LocationsAddCoordination.swift in Sources */, 46C3B7D829E5E55000F8F57C /* LocationsListCoordination.swift in Sources */, 46C3B7D629E5E50500F8F57C /* LocationsListViewModeling.swift in Sources */, + 4656CBC229E6D33C00600EE6 /* LoadRemoteLocationsUseCase.swift in Sources */, 46C3B7CF29E5D00E00F8F57C /* LocationsAddViewModel.swift in Sources */, 02031EC929E60B29003C108C /* DependencyService+Keys.swift in Sources */, 46C3B7D129E5D06D00F8F57C /* LocationsAddViewController.swift in Sources */, -- 2.47.1 From 94d905ffc3fd2303bc42016e2a61a678ee817693 Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Wed, 12 Apr 2023 16:32:34 +0200 Subject: [PATCH 08/13] Defined the properties and functions to load and to retrieve data from the Persistence stack. --- .../Protocols/ViewModeling/LocationsListViewModeling.swift | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Apps/Locations/Sources/Protocols/ViewModeling/LocationsListViewModeling.swift b/Apps/Locations/Sources/Protocols/ViewModeling/LocationsListViewModeling.swift index 40691a3..aa3a71f 100644 --- a/Apps/Locations/Sources/Protocols/ViewModeling/LocationsListViewModeling.swift +++ b/Apps/Locations/Sources/Protocols/ViewModeling/LocationsListViewModeling.swift @@ -8,6 +8,7 @@ import Combine import Foundation +import Persistence protocol LocationsListViewModeling: AnyObject { @@ -16,9 +17,13 @@ protocol LocationsListViewModeling: AnyObject { var coordinator: LocationsListCoordination? { get set } var viewStatusPublisher: Published.Publisher { get } + var numberOfSectionsInData: Int { get } // MARK: Functions func openAddLocation() + func loadLocations() + func numberOfDataItems(in section: Int) -> Int + func dataItem(at indexPath: IndexPath) -> Location } -- 2.47.1 From 985e8ffe8ea9d9774738cba64534bd44bea260b3 Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Wed, 12 Apr 2023 16:34:28 +0200 Subject: [PATCH 09/13] Implemented the "loadLocations()" function in the LocationsListViewModel view model. --- .../LocationsListViewModel.swift | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewModel.swift b/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewModel.swift index 6b4f55d..7b68701 100644 --- a/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewModel.swift +++ b/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewModel.swift @@ -6,10 +6,17 @@ // Copyright © 2023 Röck+Cöde. All rights reserved. // +import CoreData import Combine -import Core +import Dependency +import Foundation +import Persistence class LocationsListViewModel: ObservableObject { + + // MARK: Dependencies + + @Dependency(\.persistence) private var persistence // MARK: Properties @@ -17,6 +24,15 @@ class LocationsListViewModel: ObservableObject { @Published private var viewStatus: LocationsListViewStatus = .initialised + private lazy var fetchedResultsController = NSFetchedResultsController( + fetchRequest: NSFetchRequest.allLocations(), + managedObjectContext: persistence.container.viewContext, + sectionNameKeyPath: nil, + cacheName: nil + ) + + private let loadRemoteLocations = LoadRemoteLocationsUseCase() + // MARK: Initialisers init(coordinator: LocationsListCoordination) { @@ -39,6 +55,22 @@ extension LocationsListViewModel: LocationsListViewModeling { coordinator?.openAddLocation() } + func loadLocations() { + Task { + do { + viewStatus = .loading + + try await loadRemoteLocations() + + try fetchedResultsController.performFetch() + + viewStatus = .loaded + } catch { + viewStatus = .error + } + } + } + } // MARK: - Enumerations -- 2.47.1 From ea9fea98b34ed1a227e83cb966cc3fa657200159 Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Wed, 12 Apr 2023 16:35:57 +0200 Subject: [PATCH 10/13] Implemented the "numberOfSectionsInData" property and the "numberOfDataItems(in: )" and "dataItem(at: )" functions in the LocationsListViewModel view model. --- .../LocationsList/LocationsListViewModel.swift | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewModel.swift b/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewModel.swift index 7b68701..ebf3e9d 100644 --- a/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewModel.swift +++ b/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewModel.swift @@ -48,6 +48,7 @@ extension LocationsListViewModel: LocationsListViewModeling { // MARK: Properties var viewStatusPublisher: Published.Publisher { $viewStatus } + var numberOfSectionsInData: Int { fetchedResultsController.sections?.count ?? 0 } // MARK: Functions @@ -71,6 +72,21 @@ extension LocationsListViewModel: LocationsListViewModeling { } } + func numberOfDataItems(in section: Int) -> Int { + guard + let sections = fetchedResultsController.sections, + sections.endIndex > section + else { + return 0 + } + + return sections[section].numberOfObjects + } + + func dataItem(at indexPath: IndexPath) -> Location { + fetchedResultsController.object(at: indexPath) + } + } // MARK: - Enumerations -- 2.47.1 From 51e4c64a11398c6a550a5df5e9ae11a1e564a585 Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Wed, 12 Apr 2023 16:37:46 +0200 Subject: [PATCH 11/13] Conformed the LocationsListViewController view controller to the UITableViewDataSource and the UITableViewDelegate protocols. --- .../LocationsListViewController.swift | 51 ++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewController.swift b/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewController.swift index 9681ec5..6410a46 100644 --- a/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewController.swift +++ b/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewController.swift @@ -25,6 +25,8 @@ class LocationsListViewController: BaseViewController { private lazy var table = { let table = UITableView(frame: .zero, style: .plain) + table.dataSource = self + table.delegate = self table.translatesAutoresizingMaskIntoConstraints = false return table @@ -50,10 +52,47 @@ class LocationsListViewController: BaseViewController { setupBar() setupView() bindViewModel() + + viewModel.loadLocations() } } +// 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 {} + // MARK: - Helpers private extension LocationsListViewController { @@ -76,7 +115,13 @@ private extension LocationsListViewController { view.addSubview(table) view.addSubview(error) view.addSubview(loading) - error.onRetry = { print("RETRY BUTTON PRESSED!") } + + error.onRetry = { + self.viewModel.loadLocations() + } + + table.register(UITableViewCell.self, forCellReuseIdentifier: "cellID") + NSLayoutConstraint.activate([ error.widthAnchor.constraint(equalToConstant: 300), view.centerXAnchor.constraint(equalTo: error.centerXAnchor), @@ -99,6 +144,10 @@ private extension LocationsListViewController { self.error.isHidden = viewStatus != .error self.loading.isHidden = viewStatus != .loading self.table.isHidden = viewStatus != .loaded + + if viewStatus == .loaded { + self.table.reloadData() + } } .store(in: &cancellables) } -- 2.47.1 From 63f476157fbc6985cd3d14c02f7f23f0f2a5d7e3 Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Wed, 12 Apr 2023 18:35:20 +0200 Subject: [PATCH 12/13] Implemented the LocationViewCell cell. --- .../View Components/ErrorMessageView.swift | 72 +++--- .../View Components/LoadingSpinnerView.swift | 19 +- .../View Components/LocationViewCell.swift | 221 ++++++++++++++++++ DeepLinking.xcodeproj/project.pbxproj | 4 + 4 files changed, 270 insertions(+), 46 deletions(-) create mode 100644 Apps/Locations/Sources/View Components/LocationViewCell.swift diff --git a/Apps/Locations/Sources/View Components/ErrorMessageView.swift b/Apps/Locations/Sources/View Components/ErrorMessageView.swift index d161f77..c250ffd 100644 --- a/Apps/Locations/Sources/View Components/ErrorMessageView.swift +++ b/Apps/Locations/Sources/View Components/ErrorMessageView.swift @@ -33,46 +33,46 @@ class ErrorMessageView: UIView { }() private lazy var title = { - let title = UILabel() + let label = UILabel() - title.font = .preferredFont(forTextStyle: .largeTitle) - title.numberOfLines = 0 - title.lineBreakMode = .byWordWrapping - title.text = "Some error title goes in here..." - title.textAlignment = .center - title.translatesAutoresizingMaskIntoConstraints = false + label.font = .preferredFont(forTextStyle: .largeTitle) + label.numberOfLines = 0 + label.lineBreakMode = .byWordWrapping + label.text = "Some error title goes in here..." + label.textAlignment = .center + label.translatesAutoresizingMaskIntoConstraints = false - return title + return label }() private lazy var message = { - let message = UILabel() + let label = UILabel() - message.font = .preferredFont(forTextStyle: .body) - message.lineBreakMode = .byWordWrapping - message.numberOfLines = 0 - message.text = "Some long, descriptive, explanatory error message goes in here..." - message.textAlignment = .center - message.textColor = .secondaryLabel - message.translatesAutoresizingMaskIntoConstraints = false + label.font = .preferredFont(forTextStyle: .body) + label.lineBreakMode = .byWordWrapping + label.numberOfLines = 0 + label.text = "Some long, descriptive, explanatory error message goes in here..." + label.textAlignment = .center + label.textColor = .secondaryLabel + label.translatesAutoresizingMaskIntoConstraints = false - return message + return label }() private lazy var retry = { - let retry = UIButton() + let button = UIButton() - retry.backgroundColor = .red - retry.translatesAutoresizingMaskIntoConstraints = false - retry.layer.borderColor = UIColor.red.cgColor - retry.layer.borderWidth = 1 - retry.layer.cornerRadius = 5 - retry.titleLabel?.font = .preferredFont(forTextStyle: .headline) + button.backgroundColor = .red + button.translatesAutoresizingMaskIntoConstraints = false + button.layer.borderColor = UIColor.red.cgColor + button.layer.borderWidth = 1 + button.layer.cornerRadius = 5 + button.titleLabel?.font = .preferredFont(forTextStyle: .headline) - retry.addTarget(self, action: #selector(retryPressed), for: .touchUpInside) - retry.setTitle("Try again", for: .normal) + button.addTarget(self, action: #selector(retryPressed), for: .touchUpInside) + button.setTitle("Try again", for: .normal) - return retry + return button }() // MARK: Initialisers @@ -96,24 +96,24 @@ private extension ErrorMessageView { // MARK: Functions func setupView() { - stack.addArrangedSubview(title) - stack.addArrangedSubview(message) - stack.addArrangedSubview(retry) - stack.setCustomSpacing(160, after: message) - backgroundColor = .clear translatesAutoresizingMaskIntoConstraints = false addSubview(stack) + stack.addArrangedSubview(title) + stack.addArrangedSubview(message) + stack.addArrangedSubview(retry) + stack.setCustomSpacing(160, after: message) + NSLayoutConstraint.activate([ - retry.heightAnchor.constraint(equalToConstant: 44), - retry.leadingAnchor.constraint(equalTo: stack.leadingAnchor), - retry.trailingAnchor.constraint(equalTo: stack.trailingAnchor), bottomAnchor.constraint(equalTo: stack.bottomAnchor), leadingAnchor.constraint(equalTo: stack.leadingAnchor), topAnchor.constraint(equalTo: stack.topAnchor), - trailingAnchor.constraint(equalTo: stack.trailingAnchor) + trailingAnchor.constraint(equalTo: stack.trailingAnchor), + retry.heightAnchor.constraint(equalToConstant: 44), + retry.leadingAnchor.constraint(equalTo: stack.leadingAnchor), + retry.trailingAnchor.constraint(equalTo: stack.trailingAnchor), ]) } diff --git a/Apps/Locations/Sources/View Components/LoadingSpinnerView.swift b/Apps/Locations/Sources/View Components/LoadingSpinnerView.swift index 461613b..b5d3f4b 100644 --- a/Apps/Locations/Sources/View Components/LoadingSpinnerView.swift +++ b/Apps/Locations/Sources/View Components/LoadingSpinnerView.swift @@ -25,19 +25,18 @@ class LoadingSpinnerView: UIView { }() private lazy var spinner = { - let spinner = UIActivityIndicatorView(style: .large) + let activity = UIActivityIndicatorView(style: .large) - spinner.translatesAutoresizingMaskIntoConstraints = false + activity.translatesAutoresizingMaskIntoConstraints = false - spinner.startAnimating() + activity.startAnimating() - return spinner + return activity }() - private lazy var label = { + private lazy var title = { let label = UILabel() - - + label.font = .preferredFont(forTextStyle: .headline) label.numberOfLines = 0 label.lineBreakMode = .byWordWrapping @@ -69,14 +68,14 @@ private extension LoadingSpinnerView { // MARK: Functions func setupView() { - stack.addArrangedSubview(spinner) - stack.addArrangedSubview(label) - backgroundColor = .clear translatesAutoresizingMaskIntoConstraints = false addSubview(stack) + stack.addArrangedSubview(spinner) + stack.addArrangedSubview(title) + NSLayoutConstraint.activate([ topAnchor.constraint(equalTo: stack.topAnchor), leadingAnchor.constraint(equalTo: stack.leadingAnchor), diff --git a/Apps/Locations/Sources/View Components/LocationViewCell.swift b/Apps/Locations/Sources/View Components/LocationViewCell.swift new file mode 100644 index 0000000..39a700c --- /dev/null +++ b/Apps/Locations/Sources/View Components/LocationViewCell.swift @@ -0,0 +1,221 @@ +// +// LocationViewCell.swift +// Locations +// +// Created by Javier Cicchelli on 12/04/2023. +// Copyright © 2023 Röck+Cöde. All rights reserved. +// + +import UIKit + +class LocationViewCell: UITableViewCell { + + static let identifier = "LocationViewCell" + + // MARK: Outlets + + private lazy var icon = { + let view = UIImageView() + + view.contentMode = .top + view.tintColor = .red + + return view + }() + + private lazy var latitudeTitle = { + let label = UILabel() + + label.font = .preferredFont(forTextStyle: .subheadline) + label.numberOfLines = 1 + label.text = "• Latitude" + label.textAlignment = .natural + label.textColor = .secondaryLabel + label.translatesAutoresizingMaskIntoConstraints = false + + return label + }() + + private lazy var latitudeValue = { + let label = UILabel() + + label.font = .preferredFont(forTextStyle: .subheadline) + label.numberOfLines = 1 + label.textAlignment = .natural + label.textColor = .secondaryLabel + label.translatesAutoresizingMaskIntoConstraints = false + + return label + }() + + private lazy var longitudeTitle = { + let label = UILabel() + + label.font = .preferredFont(forTextStyle: .subheadline) + label.numberOfLines = 1 + label.text = "• Longitude" + label.textAlignment = .natural + label.textColor = .secondaryLabel + label.translatesAutoresizingMaskIntoConstraints = false + + return label + }() + + private lazy var longitudeValue = { + let label = UILabel() + + label.font = .preferredFont(forTextStyle: .subheadline) + label.numberOfLines = 1 + label.textAlignment = .natural + label.textColor = .secondaryLabel + label.translatesAutoresizingMaskIntoConstraints = false + + return label + }() + + private lazy var name = { + let label = UILabel() + + label.font = .preferredFont(forTextStyle: .headline) + label.numberOfLines = 0 + label.lineBreakMode = .byWordWrapping + label.text = "Untitled" + label.textAlignment = .natural + label.translatesAutoresizingMaskIntoConstraints = false + + return label + }() + + private lazy var stack = { + let stack = UIStackView() + + stack.alignment = .center + stack.axis = .horizontal + stack.distribution = .fill + stack.spacing = 12 + stack.translatesAutoresizingMaskIntoConstraints = false + + return stack + }() + + private lazy var stackData = { + let stack = UIStackView() + + stack.alignment = .leading + stack.axis = .vertical + stack.distribution = .fill + stack.spacing = 8 + stack.translatesAutoresizingMaskIntoConstraints = false + + return stack + }() + + private lazy var stackCoordinates = { + let stack = UIStackView() + + stack.alignment = .leading + stack.axis = .vertical + stack.distribution = .fill + stack.spacing = 2 + stack.translatesAutoresizingMaskIntoConstraints = false + + return stack + }() + + private lazy var stackLatitude = { + let stack = UIStackView() + + stack.alignment = .leading + stack.axis = .horizontal + stack.distribution = .fill + stack.spacing = 4 + stack.translatesAutoresizingMaskIntoConstraints = false + + return stack + }() + + private lazy var stackLongitude = { + let stack = UIStackView() + + stack.alignment = .leading + stack.axis = .horizontal + stack.distribution = .fill + stack.spacing = 4 + stack.translatesAutoresizingMaskIntoConstraints = false + + return stack + }() + + // MARK: Initialisers + + override init( + style: UITableViewCell.CellStyle, + reuseIdentifier: String? + ) { + super.init( + style: style, + reuseIdentifier: reuseIdentifier + ) + + setupCell() + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + // MARK: Functions + + func update( + iconName: String, + name: String?, + latitude: Float, + longitude: Float + ) { + self.icon.image = .init(systemName: iconName) + self.name.text = name ?? "Untitled" + self.latitudeValue.text = "\(latitude)" + self.longitudeValue.text = "\(longitude)" + } + +} + +// MARK: - Helpers + +private extension LocationViewCell { + + // MARK: Functions + + func setupCell() { + accessoryType = .disclosureIndicator + backgroundColor = .clear + + addSubview(stack) + + stack.addArrangedSubview(icon) + stack.addArrangedSubview(stackData) + + stackData.addArrangedSubview(name) + stackData.addArrangedSubview(stackCoordinates) + + stackCoordinates.addArrangedSubview(stackLatitude) + stackCoordinates.addArrangedSubview(stackLongitude) + + stackLatitude.addArrangedSubview(latitudeTitle) + stackLatitude.addArrangedSubview(latitudeValue) + + stackLongitude.addArrangedSubview(longitudeTitle) + stackLongitude.addArrangedSubview(longitudeValue) + + NSLayoutConstraint.activate([ + bottomAnchor.constraint(equalTo: stack.bottomAnchor, constant: 8), + leadingAnchor.constraint(equalTo: stack.leadingAnchor, constant: -20), + topAnchor.constraint(equalTo: stack.topAnchor, constant: -8), + trailingAnchor.constraint(equalTo: stack.trailingAnchor), + icon.bottomAnchor.constraint(equalTo: stack.bottomAnchor), + icon.topAnchor.constraint(equalTo: stack.topAnchor), + icon.widthAnchor.constraint(equalToConstant: 24), + ]) + } + +} diff --git a/DeepLinking.xcodeproj/project.pbxproj b/DeepLinking.xcodeproj/project.pbxproj index f6a330a..6d192f6 100644 --- a/DeepLinking.xcodeproj/project.pbxproj +++ b/DeepLinking.xcodeproj/project.pbxproj @@ -13,6 +13,7 @@ 02031EE829E68D9B003C108C /* LoadingSpinnerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02031EE729E68D9B003C108C /* LoadingSpinnerView.swift */; }; 02031EEA29E6B495003C108C /* ErrorMessageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02031EE929E6B495003C108C /* ErrorMessageView.swift */; }; 4656CBC229E6D33C00600EE6 /* LoadRemoteLocationsUseCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4656CBC129E6D33C00600EE6 /* LoadRemoteLocationsUseCase.swift */; }; + 4656CBC829E6F2E400600EE6 /* LocationViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4656CBC729E6F2E400600EE6 /* LocationViewCell.swift */; }; 46C3B7C629E5BF1500F8F57C /* LocationsListCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46C3B7C529E5BF1500F8F57C /* LocationsListCoordinator.swift */; }; 46C3B7CB29E5CD3200F8F57C /* LocationsListViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46C3B7CA29E5CD3200F8F57C /* LocationsListViewModel.swift */; }; 46C3B7CF29E5D00E00F8F57C /* LocationsAddViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46C3B7CE29E5D00E00F8F57C /* LocationsAddViewModel.swift */; }; @@ -130,6 +131,7 @@ 02031EE729E68D9B003C108C /* LoadingSpinnerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoadingSpinnerView.swift; sourceTree = ""; }; 02031EE929E6B495003C108C /* ErrorMessageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ErrorMessageView.swift; sourceTree = ""; }; 4656CBC129E6D33C00600EE6 /* LoadRemoteLocationsUseCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoadRemoteLocationsUseCase.swift; sourceTree = ""; }; + 4656CBC729E6F2E400600EE6 /* LocationViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationViewCell.swift; sourceTree = ""; }; 46C3B7C529E5BF1500F8F57C /* LocationsListCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationsListCoordinator.swift; sourceTree = ""; }; 46C3B7CA29E5CD3200F8F57C /* LocationsListViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationsListViewModel.swift; sourceTree = ""; }; 46C3B7CE29E5D00E00F8F57C /* LocationsAddViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationsAddViewModel.swift; sourceTree = ""; }; @@ -185,6 +187,7 @@ children = ( 02031EE729E68D9B003C108C /* LoadingSpinnerView.swift */, 02031EE929E6B495003C108C /* ErrorMessageView.swift */, + 4656CBC729E6F2E400600EE6 /* LocationViewCell.swift */, ); path = "View Components"; sourceTree = ""; @@ -539,6 +542,7 @@ 02031EC629E5FEE4003C108C /* BaseViewController.swift in Sources */, 46EB331B29E1CE04001D5EAF /* AppDelegate.swift in Sources */, 02031EBF29E5F949003C108C /* LocationsAddViewModeling.swift in Sources */, + 4656CBC829E6F2E400600EE6 /* LocationViewCell.swift in Sources */, 46C3B7DE29E5ED2E00F8F57C /* LocationsAddCoordinator.swift in Sources */, 02031EEA29E6B495003C108C /* ErrorMessageView.swift in Sources */, 46C3B7DC29E5ED2300F8F57C /* LocationsAddCoordination.swift in Sources */, -- 2.47.1 From 2dc81fcc5937ab5c6baecc993692917f5f220c65 Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Wed, 12 Apr 2023 18:42:07 +0200 Subject: [PATCH 13/13] Registered the LocationViewCell with the "table" outlet in the LocationsListViewController view controller and updated its "tableView(_: cellForRowAt: )" function to use and update the mentioned cell. --- .../LocationsListViewController.swift | 23 ++++++++++++++----- .../View Components/LocationViewCell.swift | 2 ++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewController.swift b/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewController.swift index 6410a46..5daf098 100644 --- a/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewController.swift +++ b/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewController.swift @@ -29,6 +29,8 @@ class LocationsListViewController: BaseViewController { table.delegate = self table.translatesAutoresizingMaskIntoConstraints = false + table.register(LocationViewCell.self, forCellReuseIdentifier: LocationViewCell.identifier) + return table }() @@ -79,11 +81,22 @@ extension LocationsListViewController: UITableViewDataSource { _ tableView: UITableView, cellForRowAt indexPath: IndexPath ) -> UITableViewCell { - let cell = tableView.dequeueReusableCell(withIdentifier: "cellID", for: indexPath) + guard let cell = tableView.dequeueReusableCell( + withIdentifier: LocationViewCell.identifier, + for: indexPath + ) as? LocationViewCell else { + return .init() + } + let entity = viewModel.dataItem(at: indexPath) - cell.textLabel?.text = entity.name - + cell.update( + iconName: entity.source == .remote ? "network" : "house", + name: entity.name, + latitude: entity.latitude, + longitude: entity.longitude + ) + return cell } @@ -119,9 +132,7 @@ private extension LocationsListViewController { error.onRetry = { self.viewModel.loadLocations() } - - table.register(UITableViewCell.self, forCellReuseIdentifier: "cellID") - + NSLayoutConstraint.activate([ error.widthAnchor.constraint(equalToConstant: 300), view.centerXAnchor.constraint(equalTo: error.centerXAnchor), diff --git a/Apps/Locations/Sources/View Components/LocationViewCell.swift b/Apps/Locations/Sources/View Components/LocationViewCell.swift index 39a700c..e60875e 100644 --- a/Apps/Locations/Sources/View Components/LocationViewCell.swift +++ b/Apps/Locations/Sources/View Components/LocationViewCell.swift @@ -10,6 +10,8 @@ import UIKit class LocationViewCell: UITableViewCell { + // MARK: Properties + static let identifier = "LocationViewCell" // MARK: Outlets -- 2.47.1