diff --git a/Marlin/Marlin/MarlinApp.swift b/Marlin/Marlin/MarlinApp.swift index f07ad7ff..52821a30 100644 --- a/Marlin/Marlin/MarlinApp.swift +++ b/Marlin/Marlin/MarlinApp.swift @@ -141,7 +141,6 @@ struct MarlinApp: App { var portRepository: PortRepository var lightRepository: LightRepository var radioBeaconRepository: RadioBeaconRepository - var publicationRepository: PublicationRepository var navigationalWarningRepository: NavigationalWarningRepository var noticeToMarinersRepository: NoticeToMarinersRepository var userPlaceRepository: UserPlaceRepository @@ -180,10 +179,6 @@ struct MarlinApp: App { localDataSource: RadioBeaconCoreDataDataSource(), remoteDataSource: RadioBeaconRemoteDataSource() ) - publicationRepository = PublicationRepository( - localDataSource: PublicationCoreDataDataSource(), - remoteDataSource: PublicationRemoteDataSource() - ) navigationalWarningRepository = NavigationalWarningRepository( localDataSource: NavigationalWarningCoreDataDataSource(), remoteDataSource: NavigationalWarningRemoteDataSource() @@ -207,8 +202,7 @@ struct MarlinApp: App { lightRepository: lightRepository, portRepository: portRepository, radioBeaconRepository: radioBeaconRepository, - noticeToMarinersRepository: noticeToMarinersRepository, - publicationRepository: publicationRepository + noticeToMarinersRepository: noticeToMarinersRepository ) asamsTileRepository = AsamsTileRepository() @@ -225,7 +219,6 @@ struct MarlinApp: App { portRepository: portRepository, lightRepository: lightRepository, radioBeaconRepository: radioBeaconRepository, - publicationRepository: publicationRepository, navigationalWarningRepository: navigationalWarningRepository, noticeToMarinersRepository: noticeToMarinersRepository, routeRepository: routeRepository @@ -253,7 +246,6 @@ struct MarlinApp: App { .environmentObject(routeRepository) .environmentObject(routeWaypointRepository) .environmentObject(navigationalWarningRepository) - .environmentObject(publicationRepository) .environmentObject(noticeToMarinersRepository) .environmentObject(userPlaceRepository) .environmentObject(asamsTileRepository) diff --git a/Marlin/Marlin/Network/ASAM/AsamDataLoadOperation.swift b/Marlin/Marlin/Network/ASAM/AsamDataLoadOperation.swift index 6fbe2513..3e611205 100644 --- a/Marlin/Marlin/Network/ASAM/AsamDataLoadOperation.swift +++ b/Marlin/Marlin/Network/ASAM/AsamDataLoadOperation.swift @@ -11,11 +11,11 @@ import Kingfisher class AsamDataLoadOperation: CountingDataLoadOperation { var asams: [AsamModel] = [] + @Injected(\.asamLocalDataSource) var localDataSource: AsamLocalDataSource - init(asams: [AsamModel], localDataSource: AsamLocalDataSource) { + init(asams: [AsamModel]) { self.asams = asams - self.localDataSource = localDataSource } @MainActor override func finishLoad() { diff --git a/Marlin/Marlin/Network/DifferentialGPSStation/DGPSStationDataLoadOperation.swift b/Marlin/Marlin/Network/DifferentialGPSStation/DGPSStationDataLoadOperation.swift index bb66af4d..ed8d281e 100644 --- a/Marlin/Marlin/Network/DifferentialGPSStation/DGPSStationDataLoadOperation.swift +++ b/Marlin/Marlin/Network/DifferentialGPSStation/DGPSStationDataLoadOperation.swift @@ -11,11 +11,11 @@ import Kingfisher class DGPSStationDataLoadOperation: CountingDataLoadOperation { var dgpss: [DGPSStationModel] = [] + @Injected(\.dgpsLocalDataSource) var localDataSource: DGPSStationLocalDataSource - init(dgpss: [DGPSStationModel], localDataSource: DGPSStationLocalDataSource) { + init(dgpss: [DGPSStationModel]) { self.dgpss = dgpss - self.localDataSource = localDataSource } @MainActor override func finishLoad() { diff --git a/Marlin/Marlin/Network/MODU/ModuDataLoadOperation.swift b/Marlin/Marlin/Network/MODU/ModuDataLoadOperation.swift index e60cbc51..e9d1bebe 100644 --- a/Marlin/Marlin/Network/MODU/ModuDataLoadOperation.swift +++ b/Marlin/Marlin/Network/MODU/ModuDataLoadOperation.swift @@ -10,11 +10,11 @@ import Kingfisher class ModuDataLoadOperation: CountingDataLoadOperation { var modus: [ModuModel] = [] + @Injected(\.moduLocalDataSource) var localDataSource: ModuLocalDataSource - init(modus: [ModuModel], localDataSource: ModuLocalDataSource) { + init(modus: [ModuModel]) { self.modus = modus - self.localDataSource = localDataSource } @MainActor override func finishLoad() { diff --git a/Marlin/Marlin/Network/Publication/PublicationDataLoadOperation.swift b/Marlin/Marlin/Network/Publication/PublicationDataLoadOperation.swift index 6699884a..4395ade5 100644 --- a/Marlin/Marlin/Network/Publication/PublicationDataLoadOperation.swift +++ b/Marlin/Marlin/Network/Publication/PublicationDataLoadOperation.swift @@ -11,11 +11,11 @@ import CoreData class PublicationDataLoadOperation: CountingDataLoadOperation { var epubs: [PublicationModel] = [] + @Injected(\.publicationLocalDataSource) var localDataSource: PublicationLocalDataSource - init(epubs: [PublicationModel], localDataSource: PublicationLocalDataSource) { + init(epubs: [PublicationModel]) { self.epubs = epubs - self.localDataSource = localDataSource } override func loadData() async { diff --git a/Marlin/Marlin/Networking/MSI.swift b/Marlin/Marlin/Networking/MSI.swift index d1c6a253..7fedd999 100644 --- a/Marlin/Marlin/Networking/MSI.swift +++ b/Marlin/Marlin/Networking/MSI.swift @@ -28,7 +28,6 @@ public class MSI { portRepository: PortRepository, lightRepository: LightRepository, radioBeaconRepository: RadioBeaconRepository, - publicationRepository: PublicationRepository, navigationalWarningRepository: NavigationalWarningRepository, noticeToMarinersRepository: NoticeToMarinersRepository, routeRepository: RouteRepository @@ -50,9 +49,7 @@ public class MSI { lightInitializer = LightInitializer(repository: lightRepository) radioBeaconInitializer = RadioBeaconInitializer(repository: radioBeaconRepository) differentialGPSStationInitializer = DGPSStationInitializer() - publicationInitializer = PublicationInitializer( - repository: publicationRepository - ) + publicationInitializer = PublicationInitializer() navigationalWarningInitializer = NavigationalWarningInitializer( repository: navigationalWarningRepository ) diff --git a/Marlin/Marlin/Repository/ASAM/AsamLocalDataSource.swift b/Marlin/Marlin/Repository/ASAM/AsamLocalDataSource.swift index e0010c8e..254af119 100644 --- a/Marlin/Marlin/Repository/ASAM/AsamLocalDataSource.swift +++ b/Marlin/Marlin/Repository/ASAM/AsamLocalDataSource.swift @@ -320,7 +320,7 @@ extension AsamCoreDataDataSource { NSLog("Received \(count) \(DataSources.asam.key) records.") // Create an operation that performs the main part of the background task. - operation = AsamDataLoadOperation(asams: asams, localDataSource: self) + operation = AsamDataLoadOperation(asams: asams) return await executeOperationInBackground(task: task) } diff --git a/Marlin/Marlin/Repository/Bookmark/BookmarkRepository.swift b/Marlin/Marlin/Repository/Bookmark/BookmarkRepository.swift index 3bd86c09..fb627c2b 100644 --- a/Marlin/Marlin/Repository/Bookmark/BookmarkRepository.swift +++ b/Marlin/Marlin/Repository/Bookmark/BookmarkRepository.swift @@ -35,7 +35,8 @@ class BookmarkRepository: ObservableObject { let portRepository: PortRepository? let radioBeaconRepository: RadioBeaconRepository? let noticeToMarinersRepository: NoticeToMarinersRepository? - let publicationRepository: PublicationRepository? + @Injected(\.publicationRepository) + var publicationRepository: PublicationRepository let navigationalWarningRepository: NavigationalWarningRepository? init( @@ -44,7 +45,6 @@ class BookmarkRepository: ObservableObject { portRepository: PortRepository? = nil, radioBeaconRepository: RadioBeaconRepository? = nil, noticeToMarinersRepository: NoticeToMarinersRepository? = nil, - publicationRepository: PublicationRepository? = nil, navigationalWarningRepository: NavigationalWarningRepository? = nil ) { self.localDataSource = localDataSource @@ -52,7 +52,6 @@ class BookmarkRepository: ObservableObject { self.portRepository = portRepository self.radioBeaconRepository = radioBeaconRepository self.noticeToMarinersRepository = noticeToMarinersRepository - self.publicationRepository = publicationRepository self.navigationalWarningRepository = navigationalWarningRepository } @@ -117,7 +116,7 @@ class BookmarkRepository: ObservableObject { ) } case DataSources.epub.key: - return publicationRepository?.getPublication(s3Key: itemKey) + return publicationRepository.getPublication(s3Key: itemKey) case GeoPackageFeatureItem.key: return GeoPackageFeatureItem.getItem( context: PersistenceController.current.newTaskContext(), diff --git a/Marlin/Marlin/Repository/DifferentialGPSStation/DGPSStationLocalDataSource.swift b/Marlin/Marlin/Repository/DifferentialGPSStation/DGPSStationLocalDataSource.swift index a096811b..08a91767 100644 --- a/Marlin/Marlin/Repository/DifferentialGPSStation/DGPSStationLocalDataSource.swift +++ b/Marlin/Marlin/Repository/DifferentialGPSStation/DGPSStationLocalDataSource.swift @@ -331,7 +331,7 @@ extension DGPSStationCoreDataDataSource { NSLog("Received \(count) \(DataSources.dgps.key) records.") // Create an operation that performs the main part of the background task. - operation = DGPSStationDataLoadOperation(dgpss: dgpss, localDataSource: self) + operation = DGPSStationDataLoadOperation(dgpss: dgpss) return await executeOperationInBackground(task: task) } diff --git a/Marlin/Marlin/Repository/MODU/ModuLocalDataSource.swift b/Marlin/Marlin/Repository/MODU/ModuLocalDataSource.swift index 83e59a8c..2dc4bc0c 100644 --- a/Marlin/Marlin/Repository/MODU/ModuLocalDataSource.swift +++ b/Marlin/Marlin/Repository/MODU/ModuLocalDataSource.swift @@ -307,7 +307,7 @@ class ModuCoreDataDataSource: CoreDataDataSource, ModuLocalDataSource, Observabl NSLog("Received \(count) \(DataSources.modu.key) records.") // Create an operation that performs the main part of the background task. - operation = ModuDataLoadOperation(modus: modus, localDataSource: self) + operation = ModuDataLoadOperation(modus: modus) return await executeOperationInBackground(task: task) } diff --git a/Marlin/Marlin/Repository/Publication/PublicationLocalDataSource.swift b/Marlin/Marlin/Repository/Publication/PublicationLocalDataSource.swift index a4918d36..a530c543 100644 --- a/Marlin/Marlin/Repository/Publication/PublicationLocalDataSource.swift +++ b/Marlin/Marlin/Repository/Publication/PublicationLocalDataSource.swift @@ -11,6 +11,17 @@ import Combine import UIKit import BackgroundTasks +private struct PublicationLocalDataSourceProviderKey: InjectionKey { + static var currentValue: PublicationLocalDataSource = PublicationCoreDataDataSource() +} + +extension InjectedValues { + var publicationLocalDataSource: PublicationLocalDataSource { + get { Self[PublicationLocalDataSourceProviderKey.self] } + set { Self[PublicationLocalDataSourceProviderKey.self] = newValue } + } +} + protocol PublicationLocalDataSource { func getPublication(s3Key: String?) -> PublicationModel? func getSections(filters: [DataSourceFilterParameter]?) async -> [PublicationItem]? @@ -490,7 +501,7 @@ extension PublicationCoreDataDataSource { NSLog("Received \(count) \(DataSources.epub.key) records.") // Create an operation that performs the main part of the background task. - operation = PublicationDataLoadOperation(epubs: epubs, localDataSource: self) + operation = PublicationDataLoadOperation(epubs: epubs) return await executeOperationInBackground(task: task) } diff --git a/Marlin/Marlin/Repository/Publication/PublicationRemoteDataSource.swift b/Marlin/Marlin/Repository/Publication/PublicationRemoteDataSource.swift index e94fab39..6b7ce167 100644 --- a/Marlin/Marlin/Repository/Publication/PublicationRemoteDataSource.swift +++ b/Marlin/Marlin/Repository/Publication/PublicationRemoteDataSource.swift @@ -9,6 +9,17 @@ import Foundation import BackgroundTasks import Combine +private struct PublicationRemoteDataSourceProviderKey: InjectionKey { + static var currentValue: PublicationRemoteDataSource = PublicationRemoteDataSource() +} + +extension InjectedValues { + var publicationRemoteDataSource: PublicationRemoteDataSource { + get { Self[PublicationRemoteDataSourceProviderKey.self] } + set { Self[PublicationRemoteDataSourceProviderKey.self] = newValue } + } +} + class PublicationRemoteDataSource: RemoteDataSource { var downloads: [String: DownloadManager] = [:] init(cleanup: (() -> Void)? = nil) { diff --git a/Marlin/Marlin/Repository/Publication/PublicationRepository.swift b/Marlin/Marlin/Repository/Publication/PublicationRepository.swift index c24b4b73..d2f4f76e 100644 --- a/Marlin/Marlin/Repository/Publication/PublicationRepository.swift +++ b/Marlin/Marlin/Repository/Publication/PublicationRepository.swift @@ -25,18 +25,24 @@ enum PublicationItem: Hashable, Identifiable { case pubType(type: PublicationTypeEnum, count: Int) } +private struct PublicationRepositoryProviderKey: InjectionKey { + static var currentValue: PublicationRepository = PublicationRepository() +} + +extension InjectedValues { + var publicationRepository: PublicationRepository { + get { Self[PublicationRepositoryProviderKey.self] } + set { Self[PublicationRepositoryProviderKey.self] = newValue } + } +} + class PublicationRepository: ObservableObject { private var cancellables = Set() + @Injected(\.publicationLocalDataSource) var localDataSource: PublicationLocalDataSource + @Injected(\.publicationRemoteDataSource) private var remoteDataSource: PublicationRemoteDataSource - init( - localDataSource: PublicationLocalDataSource, - remoteDataSource: PublicationRemoteDataSource - ) { - self.localDataSource = localDataSource - self.remoteDataSource = remoteDataSource - } func createOperation() -> PublicationDataFetchOperation { return PublicationDataFetchOperation() diff --git a/Marlin/Marlin/Startup/ASAM/AsamInitialDataLoadOperation.swift b/Marlin/Marlin/Startup/ASAM/AsamInitialDataLoadOperation.swift index 96cba946..bc09bce8 100644 --- a/Marlin/Marlin/Startup/ASAM/AsamInitialDataLoadOperation.swift +++ b/Marlin/Marlin/Startup/ASAM/AsamInitialDataLoadOperation.swift @@ -9,11 +9,11 @@ import Foundation import Kingfisher class AsamInitialDataLoadOperation: CountingDataLoadOperation { + @Injected(\.asamLocalDataSource) var localDataSource: AsamLocalDataSource var bundle: Bundle - init(localDataSource: AsamLocalDataSource, bundle: Bundle = .main) { - self.localDataSource = localDataSource + init(bundle: Bundle = .main) { self.bundle = bundle } diff --git a/Marlin/Marlin/Startup/ASAM/AsamInitializer.swift b/Marlin/Marlin/Startup/ASAM/AsamInitializer.swift index 72f3ecce..0847aeb6 100644 --- a/Marlin/Marlin/Startup/ASAM/AsamInitializer.swift +++ b/Marlin/Marlin/Startup/ASAM/AsamInitializer.swift @@ -23,9 +23,7 @@ class AsamInitializer: Initializer { override func fetch() { if repository.getCount(filters: nil) == 0 { - let initialDataLoadOperation = AsamInitialDataLoadOperation( - localDataSource: self.repository.localDataSource - ) + let initialDataLoadOperation = AsamInitialDataLoadOperation() initialDataLoadOperation.completionBlock = { Task { await self.repository.fetchAsams() diff --git a/Marlin/Marlin/Startup/DifferentialGPSStation/DGPSStationInitialDataLoadOperation.swift b/Marlin/Marlin/Startup/DifferentialGPSStation/DGPSStationInitialDataLoadOperation.swift index 4029825c..2d12428d 100644 --- a/Marlin/Marlin/Startup/DifferentialGPSStation/DGPSStationInitialDataLoadOperation.swift +++ b/Marlin/Marlin/Startup/DifferentialGPSStation/DGPSStationInitialDataLoadOperation.swift @@ -9,11 +9,11 @@ import Foundation import Kingfisher class DGPSStationInitialDataLoadOperation: CountingDataLoadOperation { + @Injected(\.dgpsLocalDataSource) var localDataSource: DGPSStationLocalDataSource var bundle: Bundle - init(localDataSource: DGPSStationLocalDataSource, bundle: Bundle = .main) { - self.localDataSource = localDataSource + init(bundle: Bundle = .main) { self.bundle = bundle } diff --git a/Marlin/Marlin/Startup/DifferentialGPSStation/DGPSStationInitializer.swift b/Marlin/Marlin/Startup/DifferentialGPSStation/DGPSStationInitializer.swift index 27420a92..958d3f30 100644 --- a/Marlin/Marlin/Startup/DifferentialGPSStation/DGPSStationInitializer.swift +++ b/Marlin/Marlin/Startup/DifferentialGPSStation/DGPSStationInitializer.swift @@ -21,9 +21,7 @@ import Foundation override func fetch() { if repository.getCount(filters: nil) == 0 { - let initialDataLoadOperation = DGPSStationInitialDataLoadOperation( - localDataSource: self.repository.localDataSource - ) + let initialDataLoadOperation = DGPSStationInitialDataLoadOperation() initialDataLoadOperation.completionBlock = { Task { await self.repository.fetch() diff --git a/Marlin/Marlin/Startup/MODU/ModuInitialDataLoadOperation.swift b/Marlin/Marlin/Startup/MODU/ModuInitialDataLoadOperation.swift index a95feaeb..1eb5228c 100644 --- a/Marlin/Marlin/Startup/MODU/ModuInitialDataLoadOperation.swift +++ b/Marlin/Marlin/Startup/MODU/ModuInitialDataLoadOperation.swift @@ -9,11 +9,11 @@ import Foundation import Kingfisher class ModuInitialDataLoadOperation: CountingDataLoadOperation { + @Injected(\.moduLocalDataSource) var localDataSource: ModuLocalDataSource var bundle: Bundle - init(localDataSource: ModuLocalDataSource, bundle: Bundle = .main) { - self.localDataSource = localDataSource + init(bundle: Bundle = .main) { self.bundle = bundle } diff --git a/Marlin/Marlin/Startup/MODU/ModuInitializer.swift b/Marlin/Marlin/Startup/MODU/ModuInitializer.swift index 625f1a8d..9544dee8 100644 --- a/Marlin/Marlin/Startup/MODU/ModuInitializer.swift +++ b/Marlin/Marlin/Startup/MODU/ModuInitializer.swift @@ -22,9 +22,7 @@ class ModuInitializer: Initializer { override func fetch() { if repository.getCount(filters: nil) == 0 { - let initialDataLoadOperation = ModuInitialDataLoadOperation( - localDataSource: self.repository.localDataSource - ) + let initialDataLoadOperation = ModuInitialDataLoadOperation() initialDataLoadOperation.completionBlock = { Task { await self.repository.fetchModus() diff --git a/Marlin/Marlin/Startup/Publication/PublicationInitialDataLoadOperation.swift b/Marlin/Marlin/Startup/Publication/PublicationInitialDataLoadOperation.swift index d975dd9d..ad82e560 100644 --- a/Marlin/Marlin/Startup/Publication/PublicationInitialDataLoadOperation.swift +++ b/Marlin/Marlin/Startup/Publication/PublicationInitialDataLoadOperation.swift @@ -8,11 +8,11 @@ import Foundation class PublicationInitialDataLoadOperation: CountingDataLoadOperation { + @Injected(\.publicationLocalDataSource) var localDataSource: PublicationLocalDataSource var bundle: Bundle - init(localDataSource: PublicationLocalDataSource, bundle: Bundle = .main) { - self.localDataSource = localDataSource + init(bundle: Bundle = .main) { self.bundle = bundle } diff --git a/Marlin/Marlin/Startup/Publication/PublicationInitializer.swift b/Marlin/Marlin/Startup/Publication/PublicationInitializer.swift index dce12987..c09fa17b 100644 --- a/Marlin/Marlin/Startup/Publication/PublicationInitializer.swift +++ b/Marlin/Marlin/Startup/Publication/PublicationInitializer.swift @@ -10,10 +10,10 @@ import BackgroundTasks class PublicationInitializer: Initializer { - let repository: PublicationRepository + @Injected(\.publicationRepository) + var repository: PublicationRepository - init(repository: PublicationRepository) { - self.repository = repository + init() { super.init(dataSource: DataSources.epub) } @@ -23,9 +23,7 @@ class PublicationInitializer: Initializer { override func fetch() { if repository.getCount(filters: nil) == 0 { - let initialDataLoadOperation = PublicationInitialDataLoadOperation( - localDataSource: self.repository.localDataSource - ) + let initialDataLoadOperation = PublicationInitialDataLoadOperation() initialDataLoadOperation.completionBlock = { Task { await self.repository.fetch() diff --git a/Marlin/Marlin/UI/Publication/PublicationActionBar.swift b/Marlin/Marlin/UI/Publication/PublicationActionBar.swift index 360b2066..8757af38 100644 --- a/Marlin/Marlin/UI/Publication/PublicationActionBar.swift +++ b/Marlin/Marlin/UI/Publication/PublicationActionBar.swift @@ -8,7 +8,6 @@ import SwiftUI struct PublicationActionBar: View { - @EnvironmentObject var repository: PublicationRepository @EnvironmentObject var bookmarkRepository: BookmarkRepository @StateObject var bookmarkViewModel: BookmarkViewModel = BookmarkViewModel() diff --git a/Marlin/Marlin/UI/Publication/PublicationSummaryView.swift b/Marlin/Marlin/UI/Publication/PublicationSummaryView.swift index befceeef..dc7ec087 100644 --- a/Marlin/Marlin/UI/Publication/PublicationSummaryView.swift +++ b/Marlin/Marlin/UI/Publication/PublicationSummaryView.swift @@ -8,7 +8,6 @@ import SwiftUI struct PublicationSummaryView: DataSourceSummaryView { - @EnvironmentObject var repository: PublicationRepository @EnvironmentObject var bookmarkRepository: BookmarkRepository @StateObject var bookmarkViewModel: BookmarkViewModel = BookmarkViewModel() @@ -35,7 +34,7 @@ struct PublicationSummaryView: DataSourceSummaryView { switch viewModel.publication { case nil: Color.clear.onAppear { - viewModel.setupModel(repository: repository, s3Key: s3Key) + viewModel.setupModel(s3Key: s3Key) } case .some(let publication): VStack(alignment: .leading, spacing: 8) { diff --git a/Marlin/Marlin/UI/Publication/PublicationViewModel.swift b/Marlin/Marlin/UI/Publication/PublicationViewModel.swift index aa33fc43..dc43a7f9 100644 --- a/Marlin/Marlin/UI/Publication/PublicationViewModel.swift +++ b/Marlin/Marlin/UI/Publication/PublicationViewModel.swift @@ -9,13 +9,13 @@ import Foundation import Combine class PublicationViewModel: ObservableObject { - var repository: PublicationRepository? + @Injected(\.publicationRepository) + var repository: PublicationRepository var s3Key: String? var disposables = Set() @Published var publication: PublicationModel? - func setupModel(repository: PublicationRepository, s3Key: String) { - self.repository = repository + func setupModel(s3Key: String) { self.s3Key = s3Key publication = repository.getPublication(s3Key: s3Key) repository.observePublication(s3Key: s3Key)? @@ -27,28 +27,28 @@ class PublicationViewModel: ObservableObject { } func checkFileExists() -> Bool { - guard let repository = repository, let s3Key = s3Key else { + guard let s3Key = s3Key else { return false } return repository.checkFileExists(id: s3Key) } func deleteFile() { - guard let repository = repository, let s3Key = s3Key else { + guard let s3Key = s3Key else { return } repository.deleteFile(id: s3Key) } func downloadFile() { - guard let repository = repository, let s3Key = s3Key else { + guard let s3Key = s3Key else { return } repository.downloadFile(id: s3Key) } func cancelDownload() { - guard let repository = repository, let s3Key = s3Key else { + guard let s3Key = s3Key else { return } repository.cancelDownload(s3Key: s3Key) diff --git a/Marlin/Marlin/UI/Publication/PublicationsChaptersList.swift b/Marlin/Marlin/UI/Publication/PublicationsChaptersList.swift index 06872274..999f7078 100644 --- a/Marlin/Marlin/UI/Publication/PublicationsChaptersList.swift +++ b/Marlin/Marlin/UI/Publication/PublicationsChaptersList.swift @@ -9,7 +9,6 @@ import Foundation import SwiftUI struct PublicationsChaptersList: View { - @EnvironmentObject var repository: PublicationRepository @StateObject var viewModel: PublicationsChaptersListViewModel = PublicationsChaptersListViewModel() var pubTypeId: Int @@ -44,7 +43,6 @@ struct PublicationsChaptersList: View { .listRowBackground(Color.surfaceColor) .listStyle(.grouped) .task { - viewModel.repository = repository viewModel.pubTypeId = pubTypeId Metrics.shared.appRoute(["epubs", PublicationTypeEnum(rawValue: pubTypeId)?.description ?? "pubs"]) } diff --git a/Marlin/Marlin/UI/Publication/PublicationsChaptersListViewModel.swift b/Marlin/Marlin/UI/Publication/PublicationsChaptersListViewModel.swift index e0f8d102..2803fab8 100644 --- a/Marlin/Marlin/UI/Publication/PublicationsChaptersListViewModel.swift +++ b/Marlin/Marlin/UI/Publication/PublicationsChaptersListViewModel.swift @@ -21,18 +21,16 @@ class PublicationsChaptersListViewModel: ObservableObject { init(pubTypeId: Int? = nil) { self.pubTypeId = pubTypeId - } - - var repository: PublicationRepository? { - didSet { - Task { - await fetchPublications() - } + Task { + await fetchPublications() } } + @Injected(\.publicationRepository) + var repository: PublicationRepository + func fetchPublications() async { - if let pubTypeId = pubTypeId, let repository = repository { + if let pubTypeId = pubTypeId { let fetched = await repository.getPublications(typeId: pubTypeId) let grouped = Dictionary(grouping: fetched, by: { $0.fullPubFlag }) let fetchedCompleteVolumes = grouped[true]?.sorted { diff --git a/Marlin/Marlin/UI/Publication/PublicationsCompleteVolumeListViewModel.swift b/Marlin/Marlin/UI/Publication/PublicationsCompleteVolumeListViewModel.swift index ef45e373..a88116f3 100644 --- a/Marlin/Marlin/UI/Publication/PublicationsCompleteVolumeListViewModel.swift +++ b/Marlin/Marlin/UI/Publication/PublicationsCompleteVolumeListViewModel.swift @@ -19,18 +19,16 @@ class PublicationsCompleteVolumeListViewModel: ObservableObject { init(pubTypeId: Int? = nil) { self.pubTypeId = pubTypeId - } - - var repository: PublicationRepository? { - didSet { - Task { - await fetchPublications() - } + Task { + await fetchPublications() } } + @Injected(\.publicationRepository) + var repository: PublicationRepository + func fetchPublications() async { - if let pubTypeId = pubTypeId, let repository = repository { + if let pubTypeId = pubTypeId { let fetched = await repository.getPublications(typeId: pubTypeId) .filter({ epub in epub.fullPubFlag == true diff --git a/Marlin/Marlin/UI/Publication/PublicationsCompleteVolumesList.swift b/Marlin/Marlin/UI/Publication/PublicationsCompleteVolumesList.swift index 1ba8c4b9..5b8af574 100644 --- a/Marlin/Marlin/UI/Publication/PublicationsCompleteVolumesList.swift +++ b/Marlin/Marlin/UI/Publication/PublicationsCompleteVolumesList.swift @@ -9,7 +9,6 @@ import Foundation import SwiftUI struct PublicationsCompleteVolumesList: View { - @EnvironmentObject var repository: PublicationRepository @StateObject var viewModel: PublicationsCompleteVolumeListViewModel = PublicationsCompleteVolumeListViewModel() var pubTypeId: Int @@ -31,7 +30,6 @@ struct PublicationsCompleteVolumesList: View { .listRowBackground(Color.surfaceColor) .listStyle(.grouped) .task { - viewModel.repository = repository viewModel.pubTypeId = pubTypeId Metrics.shared.appRoute(["epubs", PublicationTypeEnum(rawValue: pubTypeId)?.description ?? "pubs"]) } diff --git a/Marlin/Marlin/UI/Publication/PublicationsNestedFolder.swift b/Marlin/Marlin/UI/Publication/PublicationsNestedFolder.swift index 63dda76b..99b716aa 100644 --- a/Marlin/Marlin/UI/Publication/PublicationsNestedFolder.swift +++ b/Marlin/Marlin/UI/Publication/PublicationsNestedFolder.swift @@ -9,8 +9,7 @@ import Foundation import SwiftUI struct PublicationsNestedFolder: View { - @EnvironmentObject var repository: PublicationRepository - @StateObject var viewModel: PublicationsNestedFolderViewModel = + @StateObject var viewModel: PublicationsNestedFolderViewModel = PublicationsNestedFolderViewModel() var pubTypeId: Int @@ -44,7 +43,6 @@ struct PublicationsNestedFolder: View { .navigationTitle((PublicationTypeEnum(rawValue: pubTypeId) ?? .unknown).description) .navigationBarTitleDisplayMode(.inline) .task { - viewModel.repository = repository viewModel.pubTypeId = pubTypeId Metrics.shared.appRoute(["epubs", PublicationTypeEnum(rawValue: pubTypeId)?.description ?? "pubs"]) } diff --git a/Marlin/Marlin/UI/Publication/PublicationsNestedFolderViewModel.swift b/Marlin/Marlin/UI/Publication/PublicationsNestedFolderViewModel.swift index 599d4471..28071e13 100644 --- a/Marlin/Marlin/UI/Publication/PublicationsNestedFolderViewModel.swift +++ b/Marlin/Marlin/UI/Publication/PublicationsNestedFolderViewModel.swift @@ -20,18 +20,16 @@ class PublicationsNestedFolderViewModel: ObservableObject { init(pubTypeId: Int? = nil) { self.pubTypeId = pubTypeId - } - - var repository: PublicationRepository? { - didSet { - Task { - await fetchPublications() - } + Task { + await fetchPublications() } } + @Injected(\.publicationRepository) + var repository: PublicationRepository + func fetchPublications() async { - if let pubTypeId = pubTypeId, let repository = repository { + if let pubTypeId = pubTypeId { let fetched = await repository.getPublications(typeId: pubTypeId) .sorted { if ($0.pubDownloadOrder ?? -1) < ($1.pubDownloadOrder ?? -1) { diff --git a/Marlin/Marlin/UI/Publication/PublicationsSectionList.swift b/Marlin/Marlin/UI/Publication/PublicationsSectionList.swift index bc782664..b6286856 100644 --- a/Marlin/Marlin/UI/Publication/PublicationsSectionList.swift +++ b/Marlin/Marlin/UI/Publication/PublicationsSectionList.swift @@ -8,7 +8,6 @@ import SwiftUI struct PublicationsSectionList: View { - @EnvironmentObject var repository: PublicationRepository @StateObject var viewModel: PublicationsSectionListViewModel = PublicationsSectionListViewModel() @@ -69,7 +68,6 @@ struct PublicationsSectionList: View { .navigationTitle(DataSources.epub.fullName) .navigationBarTitleDisplayMode(.inline) .onAppear { - viewModel.repository = repository Metrics.shared.appRoute(["epubs"]) } } diff --git a/Marlin/Marlin/UI/Publication/PublicationsSectionListViewModel.swift b/Marlin/Marlin/UI/Publication/PublicationsSectionListViewModel.swift index 14912168..c45575cf 100644 --- a/Marlin/Marlin/UI/Publication/PublicationsSectionListViewModel.swift +++ b/Marlin/Marlin/UI/Publication/PublicationsSectionListViewModel.swift @@ -28,17 +28,18 @@ class PublicationsSectionListViewModel: ObservableObject { } } - var repository: PublicationRepository? { - didSet { - Task { - dataSourceUpdatedPub.store(in: &disposables) - await fetchSections() - } + @Injected(\.publicationRepository) + var repository: PublicationRepository + + init() { + Task { + dataSourceUpdatedPub.store(in: &disposables) + await fetchSections() } } func fetchSections() async { - let fetched = await repository?.getSections() ?? [] + let fetched = await repository.getSections() await MainActor.run { sections = fetched } diff --git a/Marlin/Marlin/UI/Publication/PublicationsTypeIdListView.swift b/Marlin/Marlin/UI/Publication/PublicationsTypeIdListView.swift index b3ab9518..6b893706 100644 --- a/Marlin/Marlin/UI/Publication/PublicationsTypeIdListView.swift +++ b/Marlin/Marlin/UI/Publication/PublicationsTypeIdListView.swift @@ -9,7 +9,6 @@ import Foundation import SwiftUI struct PublicationsTypeIdListView: View { - @EnvironmentObject var repository: PublicationRepository @StateObject var viewModel: PublicationsTypeIdListViewModel = PublicationsTypeIdListViewModel() var pubTypeId: Int @@ -29,7 +28,6 @@ struct PublicationsTypeIdListView: View { .listRowBackground(Color.surfaceColor) .listStyle(.grouped) .task { - viewModel.repository = repository viewModel.pubTypeId = pubTypeId Metrics.shared.appRoute(["epubs", PublicationTypeEnum(rawValue: pubTypeId)?.description ?? "pubs"]) } diff --git a/Marlin/Marlin/UI/Publication/PublicationsTypeIdListViewModel.swift b/Marlin/Marlin/UI/Publication/PublicationsTypeIdListViewModel.swift index e9bea29c..cda2283d 100644 --- a/Marlin/Marlin/UI/Publication/PublicationsTypeIdListViewModel.swift +++ b/Marlin/Marlin/UI/Publication/PublicationsTypeIdListViewModel.swift @@ -20,18 +20,16 @@ class PublicationsTypeIdListViewModel: ObservableObject { init(pubTypeId: Int? = nil) { self.pubTypeId = pubTypeId - } - - var repository: PublicationRepository? { - didSet { - Task { - await fetchPublications() - } + Task { + await fetchPublications() } } + @Injected(\.publicationRepository) + var repository: PublicationRepository + func fetchPublications() async { - if let pubTypeId = pubTypeId, let repository = repository { + if let pubTypeId = pubTypeId { let fetched = await repository.getPublications(typeId: pubTypeId) await MainActor.run { publications = fetched diff --git a/Marlin/MarlinTests/DataSources/ASAM/ASAMDataTests.swift b/Marlin/MarlinTests/DataSources/ASAM/ASAMDataTests.swift index 8b2f0de1..de3404e9 100644 --- a/Marlin/MarlinTests/DataSources/ASAM/ASAMDataTests.swift +++ b/Marlin/MarlinTests/DataSources/ASAM/ASAMDataTests.swift @@ -89,8 +89,9 @@ final class ASAMDataTests: XCTestCase { let bundle = MockBundle() bundle.mockPath = "asamMockData.json" - - let operation = AsamInitialDataLoadOperation(localDataSource: AsamCoreDataDataSource(), bundle: bundle) + InjectedValues[\.asamLocalDataSource] = AsamCoreDataDataSource() + + let operation = AsamInitialDataLoadOperation(bundle: bundle) operation.start() waitForExpectations(timeout: 10, handler: nil) @@ -147,11 +148,11 @@ final class ASAMDataTests: XCTestCase { let localDataSource = AsamCoreDataDataSource() InjectedValues[\.asamLocalDataSource] = localDataSource let remoteDataSource = AsamRemoteDataSource() - InjectedValues[\.asamRemoteDataSource] + InjectedValues[\.asamRemoteDataSource] = remoteDataSource let repository = AsamRepository() - let operation = AsamInitialDataLoadOperation(localDataSource: repository.localDataSource, bundle: bundle) + let operation = AsamInitialDataLoadOperation(bundle: bundle) operation.start() await fulfillment(of: [loadingNotification, loadedNotification, didSaveNotification, batchUpdateCompleteNotification], timeout: 10) @@ -314,8 +315,9 @@ final class ASAMDataTests: XCTestCase { let bundle = MockBundle() bundle.tempFileContents = jsonObject + InjectedValues[\.asamLocalDataSource] = AsamCoreDataDataSource() - let operation = AsamInitialDataLoadOperation(localDataSource: AsamCoreDataDataSource(), bundle: bundle) + let operation = AsamInitialDataLoadOperation(bundle: bundle) operation.start() waitForExpectations(timeout: 10, handler: nil) @@ -379,8 +381,9 @@ final class ASAMDataTests: XCTestCase { let bundle = MockBundle() bundle.tempFileContents = jsonObject - - let operation = AsamInitialDataLoadOperation(localDataSource: AsamCoreDataDataSource(), bundle: bundle) + InjectedValues[\.asamLocalDataSource] = AsamCoreDataDataSource() + + let operation = AsamInitialDataLoadOperation(bundle: bundle) operation.start() waitForExpectations(timeout: 10, handler: nil) @@ -444,8 +447,9 @@ final class ASAMDataTests: XCTestCase { let bundle = MockBundle() bundle.tempFileContents = jsonObject - - let operation = AsamInitialDataLoadOperation(localDataSource: AsamCoreDataDataSource(), bundle: bundle) + InjectedValues[\.asamLocalDataSource] = AsamCoreDataDataSource() + + let operation = AsamInitialDataLoadOperation(bundle: bundle) operation.start() waitForExpectations(timeout: 10, handler: nil) diff --git a/Marlin/MarlinTests/DataSources/DifferentialGPSStation/DifferentialGPSStationDataTests.swift b/Marlin/MarlinTests/DataSources/DifferentialGPSStation/DifferentialGPSStationDataTests.swift index 7a2ac8dc..70d35437 100644 --- a/Marlin/MarlinTests/DataSources/DifferentialGPSStation/DifferentialGPSStationDataTests.swift +++ b/Marlin/MarlinTests/DataSources/DifferentialGPSStation/DifferentialGPSStationDataTests.swift @@ -89,9 +89,8 @@ final class DifferentialGPSStationDataTests: XCTestCase { bundle.mockPath = "dgpsMockData.json" let localDataSource = DGPSStationCoreDataDataSource() - let operation = DGPSStationInitialDataLoadOperation( - localDataSource: localDataSource, bundle: bundle - ) + InjectedValues[\.dgpsLocalDataSource] = localDataSource + let operation = DGPSStationInitialDataLoadOperation(bundle: bundle) operation.start() waitForExpectations(timeout: 10, handler: nil) @@ -180,9 +179,11 @@ final class DifferentialGPSStationDataTests: XCTestCase { let bundle = MockBundle() bundle.tempFileContents = jsonObject + + InjectedValues[\.dgpsLocalDataSource] = DGPSStationCoreDataDataSource() let operation = DGPSStationInitialDataLoadOperation( - localDataSource: DGPSStationCoreDataDataSource(), bundle: bundle + bundle: bundle ) operation.start() @@ -265,9 +266,11 @@ final class DifferentialGPSStationDataTests: XCTestCase { let bundle = MockBundle() bundle.tempFileContents = jsonObject + + InjectedValues[\.dgpsLocalDataSource] = DGPSStationCoreDataDataSource() let operation = DGPSStationInitialDataLoadOperation( - localDataSource: DGPSStationCoreDataDataSource(), bundle: bundle + bundle: bundle ) operation.start() waitForExpectations(timeout: 10, handler: nil) @@ -350,8 +353,10 @@ final class DifferentialGPSStationDataTests: XCTestCase { let bundle = MockBundle() bundle.tempFileContents = jsonObject + InjectedValues[\.dgpsLocalDataSource] = DGPSStationCoreDataDataSource() + let operation = DGPSStationInitialDataLoadOperation( - localDataSource: DGPSStationCoreDataDataSource(), bundle: bundle + bundle: bundle ) operation.start() waitForExpectations(timeout: 10, handler: nil) diff --git a/Marlin/MarlinTests/DataSources/ElectronicPublication/PublicationDataTests.swift b/Marlin/MarlinTests/DataSources/ElectronicPublication/PublicationDataTests.swift index 98cc15f8..063f6097 100644 --- a/Marlin/MarlinTests/DataSources/ElectronicPublication/PublicationDataTests.swift +++ b/Marlin/MarlinTests/DataSources/ElectronicPublication/PublicationDataTests.swift @@ -90,8 +90,9 @@ final class PublicationDataTests: XCTestCase { let bundle = MockBundle() bundle.mockPath = "epubMockData.json" + InjectedValues[\.publicationLocalDataSource] = PublicationCoreDataDataSource() - let operation = PublicationInitialDataLoadOperation(localDataSource: PublicationCoreDataDataSource(), bundle: bundle) + let operation = PublicationInitialDataLoadOperation(bundle: bundle) operation.start() waitForExpectations(timeout: 10, handler: nil) @@ -222,7 +223,12 @@ final class PublicationDataTests: XCTestCase { return true } - let repository = PublicationRepository(localDataSource: PublicationCoreDataDataSource(), remoteDataSource: PublicationRemoteDataSource()) + let localDataSource = PublicationCoreDataDataSource() + let remoteDataSource = PublicationRemoteDataSource() + InjectedValues[\.publicationLocalDataSource] = localDataSource + InjectedValues[\.publicationRemoteDataSource] = remoteDataSource + + let repository = PublicationRepository() let fetched = await repository.fetch() @@ -361,7 +367,7 @@ final class PublicationDataTests: XCTestCase { let bundle = MockBundle() bundle.tempFileContentArray = jsonObject - let operation = PublicationInitialDataLoadOperation(localDataSource: PublicationCoreDataDataSource(), bundle: bundle) + let operation = PublicationInitialDataLoadOperation(bundle: bundle) operation.start() waitForExpectations(timeout: 10, handler: nil) diff --git a/Marlin/MarlinTests/DataSources/ElectronicPublication/PublicationListTests.swift b/Marlin/MarlinTests/DataSources/ElectronicPublication/PublicationListTests.swift index 719c4c57..678dc7da 100644 --- a/Marlin/MarlinTests/DataSources/ElectronicPublication/PublicationListTests.swift +++ b/Marlin/MarlinTests/DataSources/ElectronicPublication/PublicationListTests.swift @@ -50,7 +50,10 @@ final class PublicationListTests: XCTestCase { bundle.mockPath = "fullEpubList.json" let localDataSource = PublicationStaticLocalDataSource() - let operation = PublicationInitialDataLoadOperation(localDataSource: localDataSource, bundle: bundle) + let remoteDataSource = PublicationRemoteDataSource() + InjectedValues[\.publicationLocalDataSource] = localDataSource + InjectedValues[\.publicationRemoteDataSource] = remoteDataSource + let operation = PublicationInitialDataLoadOperation(bundle: bundle) operation.start() waitForExpectations(timeout: 10, handler: nil) @@ -76,12 +79,11 @@ final class PublicationListTests: XCTestCase { } } let passThrough = PassThrough() - let repository = PublicationRepository(localDataSource: localDataSource, remoteDataSource: PublicationRemoteDataSource()) + let repository = PublicationRepository() let bookmarkLocalDataSource = BookmarkStaticLocalDataSource() let bookmarkRepository = BookmarkRepository(localDataSource: bookmarkLocalDataSource) let container = Container(passThrough: passThrough) - .environmentObject(repository) .environmentObject(bookmarkRepository) let controller = UIHostingController(rootView: container) diff --git a/Marlin/MarlinTests/DataSources/ElectronicPublication/PublicationSummaryViewTests.swift b/Marlin/MarlinTests/DataSources/ElectronicPublication/PublicationSummaryViewTests.swift index e04ff454..7a36b21a 100644 --- a/Marlin/MarlinTests/DataSources/ElectronicPublication/PublicationSummaryViewTests.swift +++ b/Marlin/MarlinTests/DataSources/ElectronicPublication/PublicationSummaryViewTests.swift @@ -42,16 +42,17 @@ final class PublicationSummaryViewTests: XCTestCase { epub.pubsecLastModified = Date(timeIntervalSince1970: 0) let localDataSource = PublicationStaticLocalDataSource() - localDataSource.map[epub.s3Key ?? ""] = epub let remoteDataSource = PublicationRemoteDataSource() - let repository = PublicationRepository(localDataSource: localDataSource, remoteDataSource: remoteDataSource) + InjectedValues[\.publicationLocalDataSource] = localDataSource + InjectedValues[\.publicationRemoteDataSource] = remoteDataSource + localDataSource.map[epub.s3Key ?? ""] = epub + let bookmarkLocalDataSource = BookmarkStaticLocalDataSource() - let bookmarkRepository = BookmarkRepository(localDataSource: bookmarkLocalDataSource, publicationRepository: repository) + let bookmarkRepository = BookmarkRepository(localDataSource: bookmarkLocalDataSource) let summary = PublicationSummaryView(s3Key: epub.s3Key ?? "") .setShowMoreDetails(false) - .environmentObject(repository) .environmentObject(bookmarkRepository) .environmentObject(MarlinRouter()) @@ -92,17 +93,20 @@ final class PublicationSummaryViewTests: XCTestCase { epub.isDownloaded = false let localDataSource = PublicationStaticLocalDataSource() + let remoteDataSource = PublicationStaticRemoteDataSource() + InjectedValues[\.publicationLocalDataSource] = localDataSource + InjectedValues[\.publicationRemoteDataSource] = remoteDataSource localDataSource.map[epub.s3Key ?? ""] = epub localDataSource.deleteFile(s3Key: epub.s3Key ?? "") - let remoteDataSource = PublicationStaticRemoteDataSource() - let repository = PublicationRepository(localDataSource: localDataSource, remoteDataSource: remoteDataSource) + + @Injected(\.publicationRepository) + var repository: PublicationRepository let bookmarkLocalDataSource = BookmarkStaticLocalDataSource() - let bookmarkRepository = BookmarkRepository(localDataSource: bookmarkLocalDataSource, publicationRepository: repository) + let bookmarkRepository = BookmarkRepository(localDataSource: bookmarkLocalDataSource) let summary = PublicationSummaryView(s3Key: epub.s3Key ?? "") .setShowMoreDetails(false) - .environmentObject(repository) .environmentObject(bookmarkRepository) .environmentObject(MarlinRouter()) @@ -169,17 +173,17 @@ final class PublicationSummaryViewTests: XCTestCase { epub.isDownloaded = false let localDataSource = PublicationStaticLocalDataSource() + let remoteDataSource = PublicationStaticRemoteDataSource() + InjectedValues[\.publicationLocalDataSource] = localDataSource + InjectedValues[\.publicationRemoteDataSource] = remoteDataSource localDataSource.map[epub.s3Key ?? ""] = epub localDataSource.deleteFile(s3Key: epub.s3Key ?? "") - let remoteDataSource = PublicationStaticRemoteDataSource() - let repository = PublicationRepository(localDataSource: localDataSource, remoteDataSource: remoteDataSource) let bookmarkLocalDataSource = BookmarkStaticLocalDataSource() - let bookmarkRepository = BookmarkRepository(localDataSource: bookmarkLocalDataSource, publicationRepository: repository) + let bookmarkRepository = BookmarkRepository(localDataSource: bookmarkLocalDataSource) let summary = PublicationSummaryView(s3Key: epub.s3Key ?? "") .setShowMoreDetails(false) - .environmentObject(repository) .environmentObject(bookmarkRepository) .environmentObject(MarlinRouter()) @@ -230,17 +234,18 @@ final class PublicationSummaryViewTests: XCTestCase { epub.isDownloading = true let localDataSource = PublicationStaticLocalDataSource() + let remoteDataSource = PublicationStaticRemoteDataSource() + InjectedValues[\.publicationLocalDataSource] = localDataSource + InjectedValues[\.publicationRemoteDataSource] = remoteDataSource localDataSource.map[epub.s3Key ?? ""] = epub localDataSource.deleteFile(s3Key: epub.s3Key ?? "") - let remoteDataSource = PublicationStaticRemoteDataSource() - let repository = PublicationRepository(localDataSource: localDataSource, remoteDataSource: remoteDataSource) + let repository = PublicationRepository() let bookmarkLocalDataSource = BookmarkStaticLocalDataSource() - let bookmarkRepository = BookmarkRepository(localDataSource: bookmarkLocalDataSource, publicationRepository: repository) + let bookmarkRepository = BookmarkRepository(localDataSource: bookmarkLocalDataSource) let summary = PublicationSummaryView(s3Key: epub.s3Key ?? "") .setShowMoreDetails(false) - .environmentObject(repository) .environmentObject(bookmarkRepository) .environmentObject(MarlinRouter()) let controller = UIHostingController(rootView: summary) diff --git a/Marlin/MarlinTests/DataSources/MODU/ModuDataTests.swift b/Marlin/MarlinTests/DataSources/MODU/ModuDataTests.swift index 6a4132f2..bf96f4ba 100644 --- a/Marlin/MarlinTests/DataSources/MODU/ModuDataTests.swift +++ b/Marlin/MarlinTests/DataSources/MODU/ModuDataTests.swift @@ -68,8 +68,10 @@ final class ModuDataTests: XCTestCase { let bundle = MockBundle() bundle.mockPath = "moduMockData.json" + + InjectedValues[\.moduLocalDataSource] = ModuCoreDataDataSource() - let operation = ModuInitialDataLoadOperation(localDataSource: ModuCoreDataDataSource(), bundle: bundle) + let operation = ModuInitialDataLoadOperation(bundle: bundle) operation.start() waitForExpectations(timeout: 10, handler: nil) @@ -135,8 +137,10 @@ final class ModuDataTests: XCTestCase { let bundle = MockBundle() bundle.tempFileContents = jsonObject + + InjectedValues[\.moduLocalDataSource] = ModuCoreDataDataSource() - let operation = ModuInitialDataLoadOperation(localDataSource: ModuCoreDataDataSource(), bundle: bundle) + let operation = ModuInitialDataLoadOperation(bundle: bundle) operation.start() waitForExpectations(timeout: 10, handler: nil) @@ -202,8 +206,8 @@ final class ModuDataTests: XCTestCase { let bundle = MockBundle() bundle.tempFileContents = jsonObject - - let operation = ModuInitialDataLoadOperation(localDataSource: ModuCoreDataDataSource(), bundle: bundle) + InjectedValues[\.moduLocalDataSource] = ModuCoreDataDataSource() + let operation = ModuInitialDataLoadOperation(bundle: bundle) operation.start() waitForExpectations(timeout: 10, handler: nil) @@ -270,7 +274,9 @@ final class ModuDataTests: XCTestCase { let bundle = MockBundle() bundle.tempFileContents = jsonObject - let operation = ModuInitialDataLoadOperation(localDataSource: ModuCoreDataDataSource(), bundle: bundle) + InjectedValues[\.moduLocalDataSource] = ModuCoreDataDataSource() + + let operation = ModuInitialDataLoadOperation(bundle: bundle) operation.start() waitForExpectations(timeout: 10, handler: nil) diff --git a/Marlin/MarlinTests/Repository/DifferentialGPSStation/DifferentialGPSStationCoreDataDataSourceTests.swift b/Marlin/MarlinTests/Repository/DifferentialGPSStation/DifferentialGPSStationCoreDataDataSourceTests.swift index af8c6669..c0b2554c 100644 --- a/Marlin/MarlinTests/Repository/DifferentialGPSStation/DifferentialGPSStationCoreDataDataSourceTests.swift +++ b/Marlin/MarlinTests/Repository/DifferentialGPSStation/DifferentialGPSStationCoreDataDataSourceTests.swift @@ -77,7 +77,7 @@ final class DifferentialGPSStationCoreDataDataSourceTests: XCTestCase { return } let dataSource = DGPSStationCoreDataDataSource() - + InjectedValues[\.dgpsLocalDataSource] = dataSource XCTAssertEqual(dataSource.getCount(filters: nil), 1) } @@ -148,7 +148,7 @@ final class DifferentialGPSStationCoreDataDataSourceTests: XCTestCase { } let dataSource = DGPSStationCoreDataDataSource() - + InjectedValues[\.dgpsLocalDataSource] = dataSource let retrieved = dataSource.getDifferentialGPSStation(featureNumber: Int(newItem.featureNumber), volumeNumber: newItem.volumeNumber) XCTAssertEqual(retrieved?.featureNumber, Int(newItem.featureNumber)) XCTAssertEqual(retrieved?.volumeNumber, newItem.volumeNumber) @@ -228,7 +228,7 @@ final class DifferentialGPSStationCoreDataDataSourceTests: XCTestCase { } let dataSource = DGPSStationCoreDataDataSource() - + InjectedValues[\.dgpsLocalDataSource] = dataSource let retrieved = dataSource.getNewestDifferentialGPSStation() XCTAssertEqual(retrieved?.featureNumber, Int(newItem2.featureNumber)) XCTAssertEqual(retrieved?.volumeNumber, newItem2.volumeNumber) @@ -309,7 +309,7 @@ final class DifferentialGPSStationCoreDataDataSourceTests: XCTestCase { } let dataSource = DGPSStationCoreDataDataSource() - + InjectedValues[\.dgpsLocalDataSource] = dataSource let retrieved = await dataSource.getDifferentialGPSStationsInBounds(filters: nil, minLatitude: 19, maxLatitude: 21, minLongitude: 19, maxLongitude: 21) XCTAssertEqual(retrieved.count, 1) XCTAssertEqual(retrieved[0].featureNumber, Int(exactly: newItem2.featureNumber!)!) @@ -386,7 +386,7 @@ final class DifferentialGPSStationCoreDataDataSourceTests: XCTestCase { let trigger = Trigger() let dataSource = DGPSStationCoreDataDataSource() - + InjectedValues[\.dgpsLocalDataSource] = dataSource Publishers.PublishAndRepeat( onOutputFrom: trigger.signal(activatedBy: TriggerId.reload) ) { [trigger, dataSource] in @@ -514,7 +514,7 @@ final class DifferentialGPSStationCoreDataDataSourceTests: XCTestCase { let trigger = Trigger() let dataSource = DGPSStationCoreDataDataSource() - + InjectedValues[\.dgpsLocalDataSource] = dataSource Publishers.PublishAndRepeat( onOutputFrom: trigger.signal(activatedBy: TriggerId.reload) ) { [trigger, dataSource] in @@ -650,7 +650,8 @@ final class DifferentialGPSStationCoreDataDataSourceTests: XCTestCase { dgps.noticeYear = "2011" let dataSource = DGPSStationCoreDataDataSource() - + InjectedValues[\.dgpsLocalDataSource] = dataSource + let inserted = await dataSource.insert(dgpss: [dgps]) XCTAssertEqual(1, inserted) @@ -685,11 +686,13 @@ final class DifferentialGPSStationCoreDataDataSourceTests: XCTestCase { dgps.noticeYear = "2011" let dataSource = DGPSStationCoreDataDataSource() + InjectedValues[\.dgpsLocalDataSource] = dataSource let inserted = await dataSource.insert(dgpss: [dgps]) XCTAssertEqual(1, inserted) let retrieved = await dataSource.getDifferentialGPSStations(filters: [DataSourceFilterParameter(property: DataSourceProperty(name: "featureNumber", key: "featureNumber", type: .int), comparison: DataSourceFilterComparison.equals, valueInt: dgps.featureNumber)]) + print("retrieved \(retrieved)") XCTAssertEqual(1, retrieved.count) XCTAssertEqual(retrieved[0].featureNumber, dgps.featureNumber) XCTAssertEqual(retrieved[0].volumeNumber, dgps.volumeNumber) diff --git a/Marlin/MarlinTests/Repository/ElectronicPublication/PublicationRepositoryTests.swift b/Marlin/MarlinTests/Repository/ElectronicPublication/PublicationRepositoryTests.swift index 16647232..02a7e686 100644 --- a/Marlin/MarlinTests/Repository/ElectronicPublication/PublicationRepositoryTests.swift +++ b/Marlin/MarlinTests/Repository/ElectronicPublication/PublicationRepositoryTests.swift @@ -112,8 +112,10 @@ final class PublicationRepositoryTests: XCTestCase { } let localDataSource = PublicationStaticLocalDataSource() let remoteDataSource = PublicationStaticRemoteDataSource() + InjectedValues[\.publicationLocalDataSource] = localDataSource + InjectedValues[\.publicationRemoteDataSource] = remoteDataSource remoteDataSource.list = models - let repository = PublicationRepository(localDataSource: localDataSource, remoteDataSource: remoteDataSource) + let repository = PublicationRepository() let modus = await repository.fetch() XCTAssertEqual(2, modus.count) @@ -130,8 +132,10 @@ final class PublicationRepositoryTests: XCTestCase { func testCreateOperation() { let localDataSource = PublicationStaticLocalDataSource() let remoteDataSource = PublicationStaticRemoteDataSource() + InjectedValues[\.publicationLocalDataSource] = localDataSource + InjectedValues[\.publicationRemoteDataSource] = remoteDataSource - let repository = PublicationRepository(localDataSource: localDataSource, remoteDataSource: remoteDataSource) + let repository = PublicationRepository() let operation = repository.createOperation() XCTAssertNotNil(operation) } diff --git a/Marlin/MarlinTests/Repository/Modu/ModuCoreDataDataSourceTests.swift b/Marlin/MarlinTests/Repository/Modu/ModuCoreDataDataSourceTests.swift index 14d40ea7..3230ed4f 100644 --- a/Marlin/MarlinTests/Repository/Modu/ModuCoreDataDataSourceTests.swift +++ b/Marlin/MarlinTests/Repository/Modu/ModuCoreDataDataSourceTests.swift @@ -67,6 +67,7 @@ final class ModuCoreDataDataSourceTests: XCTestCase { return } let dataSource = ModuCoreDataDataSource() + InjectedValues[\.moduLocalDataSource] = dataSource XCTAssertEqual(dataSource.getCount(filters: nil), 1) } @@ -118,6 +119,7 @@ final class ModuCoreDataDataSourceTests: XCTestCase { } let dataSource = ModuCoreDataDataSource() + InjectedValues[\.moduLocalDataSource] = dataSource let retrieved = dataSource.getModu(name: newItem.name) XCTAssertEqual(retrieved?.name, newItem.name) @@ -176,13 +178,15 @@ final class ModuCoreDataDataSourceTests: XCTestCase { } let dataSource = ModuCoreDataDataSource() - + InjectedValues[\.moduLocalDataSource] = dataSource + let retrieved = dataSource.getNewestModu() XCTAssertEqual(retrieved?.name, newItem2.name) } func testGetNewestEmpty() { let dataSource = ModuCoreDataDataSource() + InjectedValues[\.moduLocalDataSource] = dataSource let retrieved = dataSource.getNewestModu() XCTAssertNil(retrieved) @@ -235,6 +239,7 @@ final class ModuCoreDataDataSourceTests: XCTestCase { } let dataSource = ModuCoreDataDataSource() + InjectedValues[\.moduLocalDataSource] = dataSource let retrieved = await dataSource.getModusInBounds(filters: nil, minLatitude: 19, maxLatitude: 21, minLongitude: 19, maxLongitude: 21) XCTAssertEqual(retrieved.count, 1) @@ -300,6 +305,7 @@ final class ModuCoreDataDataSourceTests: XCTestCase { let trigger = Trigger() let dataSource = ModuCoreDataDataSource() + InjectedValues[\.moduLocalDataSource] = dataSource Publishers.PublishAndRepeat( onOutputFrom: trigger.signal(activatedBy: TriggerId.reload) @@ -410,6 +416,7 @@ final class ModuCoreDataDataSourceTests: XCTestCase { let trigger = Trigger() let dataSource = ModuCoreDataDataSource() + InjectedValues[\.moduLocalDataSource] = dataSource Publishers.PublishAndRepeat( onOutputFrom: trigger.signal(activatedBy: TriggerId.reload) @@ -527,6 +534,7 @@ final class ModuCoreDataDataSourceTests: XCTestCase { modu2.subregion = 63 let dataSource = ModuCoreDataDataSource() + InjectedValues[\.moduLocalDataSource] = dataSource let inserted = await dataSource.insert(modus: [modu2]) XCTAssertEqual(1, inserted) @@ -551,6 +559,7 @@ final class ModuCoreDataDataSourceTests: XCTestCase { modu2.subregion = 63 let dataSource = ModuCoreDataDataSource() + InjectedValues[\.moduLocalDataSource] = dataSource let inserted = await dataSource.insert(modus: [modu2]) XCTAssertEqual(1, inserted) diff --git a/Marlin/MarlinTests/Repository/Port/PortCoreDataDataSourceTests.swift b/Marlin/MarlinTests/Repository/Port/PortCoreDataDataSourceTests.swift index 7c174d8c..2f5844e7 100644 --- a/Marlin/MarlinTests/Repository/Port/PortCoreDataDataSourceTests.swift +++ b/Marlin/MarlinTests/Repository/Port/PortCoreDataDataSourceTests.swift @@ -376,7 +376,7 @@ final class PortCoreDataDataSourceTests: XCTestCase { asam.victim = "Ship" let dataSource = AsamCoreDataDataSource() - + InjectedValues[\.asamLocalDataSource] = dataSource let inserted = await dataSource.insert(asams: [asam]) XCTAssertEqual(1, inserted)