This PR contains the work done to fetch a set of locations from a remote server to then persist them into the persistence stack and finally to display these data into the locations list screen. To give further details about the work done: - [x] implemented the `LoadingSpinnerView` and `ErrorMessageView` custom views; - [x] implemented the outlets of the `LocationsListViewController` view controller; - [x] add properties and functions to the `LocationsListViewModeling` protocol to support reactive updates, load data, and table data source conformances; - [x] deactivated the Location entity code generation from the Core Data model in the `Persistence` library; - [x] add fetch requests builder functions to the `NSFetchRequest+Location` extension in the `Persistence` library; - [x] implemented the `LoadRemoteLocationUseCase` use case; - [x] implemented the loading of locations in the LocationsListViewModel view model; - [x] implemented properties and functions in the LocationsListViewModel view model to support the table data source conformance of the `LocationsListViewController` view controller; - [x] implemented the `LocationViewCell` custom cell; - [x] registered the `LocationViewCell` with the table of the `LocationsListViewController` view controller and implemented its update with real data from Location entities. Co-authored-by: Javier Cicchelli <javier@rock-n-code.com> Reviewed-on: rock-n-code/deep-linking-assignment#10
224 lines
5.9 KiB
Swift
224 lines
5.9 KiB
Swift
//
|
|
// 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 {
|
|
|
|
// MARK: Properties
|
|
|
|
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),
|
|
])
|
|
}
|
|
|
|
}
|