deep-linking-sample/Apps/Locations/Sources/Use Cases/LoadRemoteLocationsUseCase.swift
Javier Cicchelli c8d2c288af [Feature] Locations list (#10)
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
2023-04-12 16:58:27 +00:00

76 lines
1.9 KiB
Swift

//
// 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<Persistence.Location>.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
)
}
}