Implemented the LoadRemoteLocationsUseCase use case.
This commit is contained in:
parent
4f315d7bfb
commit
8c50ce3653
@ -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<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
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
@ -12,6 +12,7 @@
|
|||||||
02031EC929E60B29003C108C /* DependencyService+Keys.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02031EC829E60B29003C108C /* DependencyService+Keys.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 */; };
|
02031EE829E68D9B003C108C /* LoadingSpinnerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02031EE729E68D9B003C108C /* LoadingSpinnerView.swift */; };
|
||||||
02031EEA29E6B495003C108C /* ErrorMessageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02031EE929E6B495003C108C /* ErrorMessageView.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 */; };
|
46C3B7C629E5BF1500F8F57C /* LocationsListCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46C3B7C529E5BF1500F8F57C /* LocationsListCoordinator.swift */; };
|
||||||
46C3B7CB29E5CD3200F8F57C /* LocationsListViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46C3B7CA29E5CD3200F8F57C /* LocationsListViewModel.swift */; };
|
46C3B7CB29E5CD3200F8F57C /* LocationsListViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46C3B7CA29E5CD3200F8F57C /* LocationsListViewModel.swift */; };
|
||||||
46C3B7CF29E5D00E00F8F57C /* LocationsAddViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46C3B7CE29E5D00E00F8F57C /* LocationsAddViewModel.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 = "<group>"; };
|
02031EC829E60B29003C108C /* DependencyService+Keys.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "DependencyService+Keys.swift"; sourceTree = "<group>"; };
|
||||||
02031EE729E68D9B003C108C /* LoadingSpinnerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoadingSpinnerView.swift; sourceTree = "<group>"; };
|
02031EE729E68D9B003C108C /* LoadingSpinnerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoadingSpinnerView.swift; sourceTree = "<group>"; };
|
||||||
02031EE929E6B495003C108C /* ErrorMessageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ErrorMessageView.swift; sourceTree = "<group>"; };
|
02031EE929E6B495003C108C /* ErrorMessageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ErrorMessageView.swift; sourceTree = "<group>"; };
|
||||||
|
4656CBC129E6D33C00600EE6 /* LoadRemoteLocationsUseCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoadRemoteLocationsUseCase.swift; sourceTree = "<group>"; };
|
||||||
46C3B7C529E5BF1500F8F57C /* LocationsListCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationsListCoordinator.swift; sourceTree = "<group>"; };
|
46C3B7C529E5BF1500F8F57C /* LocationsListCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationsListCoordinator.swift; sourceTree = "<group>"; };
|
||||||
46C3B7CA29E5CD3200F8F57C /* LocationsListViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationsListViewModel.swift; sourceTree = "<group>"; };
|
46C3B7CA29E5CD3200F8F57C /* LocationsListViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationsListViewModel.swift; sourceTree = "<group>"; };
|
||||||
46C3B7CE29E5D00E00F8F57C /* LocationsAddViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationsAddViewModel.swift; sourceTree = "<group>"; };
|
46C3B7CE29E5D00E00F8F57C /* LocationsAddViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationsAddViewModel.swift; sourceTree = "<group>"; };
|
||||||
@ -214,6 +216,14 @@
|
|||||||
path = Coordination;
|
path = Coordination;
|
||||||
sourceTree = "<group>";
|
sourceTree = "<group>";
|
||||||
};
|
};
|
||||||
|
4656CBC029E6D31800600EE6 /* Use Cases */ = {
|
||||||
|
isa = PBXGroup;
|
||||||
|
children = (
|
||||||
|
4656CBC129E6D33C00600EE6 /* LoadRemoteLocationsUseCase.swift */,
|
||||||
|
);
|
||||||
|
path = "Use Cases";
|
||||||
|
sourceTree = "<group>";
|
||||||
|
};
|
||||||
46C3B7C429E5BEE900F8F57C /* Coordinators */ = {
|
46C3B7C429E5BEE900F8F57C /* Coordinators */ = {
|
||||||
isa = PBXGroup;
|
isa = PBXGroup;
|
||||||
children = (
|
children = (
|
||||||
@ -318,6 +328,7 @@
|
|||||||
46C3B7C929E5CB8F00F8F57C /* Screens */,
|
46C3B7C929E5CB8F00F8F57C /* Screens */,
|
||||||
02031EC429E5FEB1003C108C /* View Controllers */,
|
02031EC429E5FEB1003C108C /* View Controllers */,
|
||||||
02031EE629E68D7A003C108C /* View Components */,
|
02031EE629E68D7A003C108C /* View Components */,
|
||||||
|
4656CBC029E6D31800600EE6 /* Use Cases */,
|
||||||
);
|
);
|
||||||
path = Sources;
|
path = Sources;
|
||||||
sourceTree = "<group>";
|
sourceTree = "<group>";
|
||||||
@ -533,6 +544,7 @@
|
|||||||
46C3B7DC29E5ED2300F8F57C /* LocationsAddCoordination.swift in Sources */,
|
46C3B7DC29E5ED2300F8F57C /* LocationsAddCoordination.swift in Sources */,
|
||||||
46C3B7D829E5E55000F8F57C /* LocationsListCoordination.swift in Sources */,
|
46C3B7D829E5E55000F8F57C /* LocationsListCoordination.swift in Sources */,
|
||||||
46C3B7D629E5E50500F8F57C /* LocationsListViewModeling.swift in Sources */,
|
46C3B7D629E5E50500F8F57C /* LocationsListViewModeling.swift in Sources */,
|
||||||
|
4656CBC229E6D33C00600EE6 /* LoadRemoteLocationsUseCase.swift in Sources */,
|
||||||
46C3B7CF29E5D00E00F8F57C /* LocationsAddViewModel.swift in Sources */,
|
46C3B7CF29E5D00E00F8F57C /* LocationsAddViewModel.swift in Sources */,
|
||||||
02031EC929E60B29003C108C /* DependencyService+Keys.swift in Sources */,
|
02031EC929E60B29003C108C /* DependencyService+Keys.swift in Sources */,
|
||||||
46C3B7D129E5D06D00F8F57C /* LocationsAddViewController.swift in Sources */,
|
46C3B7D129E5D06D00F8F57C /* LocationsAddViewController.swift in Sources */,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user