Skip to content

Commit

Permalink
Publication injection
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbarela committed Sep 5, 2024
1 parent 11c891f commit 5578f93
Show file tree
Hide file tree
Showing 44 changed files with 205 additions and 172 deletions.
10 changes: 1 addition & 9 deletions Marlin/Marlin/MarlinApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -180,10 +179,6 @@ struct MarlinApp: App {
localDataSource: RadioBeaconCoreDataDataSource(),
remoteDataSource: RadioBeaconRemoteDataSource()
)
publicationRepository = PublicationRepository(
localDataSource: PublicationCoreDataDataSource(),
remoteDataSource: PublicationRemoteDataSource()
)
navigationalWarningRepository = NavigationalWarningRepository(
localDataSource: NavigationalWarningCoreDataDataSource(),
remoteDataSource: NavigationalWarningRemoteDataSource()
Expand All @@ -207,8 +202,7 @@ struct MarlinApp: App {
lightRepository: lightRepository,
portRepository: portRepository,
radioBeaconRepository: radioBeaconRepository,
noticeToMarinersRepository: noticeToMarinersRepository,
publicationRepository: publicationRepository
noticeToMarinersRepository: noticeToMarinersRepository
)

asamsTileRepository = AsamsTileRepository()
Expand All @@ -225,7 +219,6 @@ struct MarlinApp: App {
portRepository: portRepository,
lightRepository: lightRepository,
radioBeaconRepository: radioBeaconRepository,
publicationRepository: publicationRepository,
navigationalWarningRepository: navigationalWarningRepository,
noticeToMarinersRepository: noticeToMarinersRepository,
routeRepository: routeRepository
Expand Down Expand Up @@ -253,7 +246,6 @@ struct MarlinApp: App {
.environmentObject(routeRepository)
.environmentObject(routeWaypointRepository)
.environmentObject(navigationalWarningRepository)
.environmentObject(publicationRepository)
.environmentObject(noticeToMarinersRepository)
.environmentObject(userPlaceRepository)
.environmentObject(asamsTileRepository)
Expand Down
4 changes: 2 additions & 2 deletions Marlin/Marlin/Network/ASAM/AsamDataLoadOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions Marlin/Marlin/Network/MODU/ModuDataLoadOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 1 addition & 4 deletions Marlin/Marlin/Networking/MSI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public class MSI {
portRepository: PortRepository,
lightRepository: LightRepository,
radioBeaconRepository: RadioBeaconRepository,
publicationRepository: PublicationRepository,
navigationalWarningRepository: NavigationalWarningRepository,
noticeToMarinersRepository: NoticeToMarinersRepository,
routeRepository: RouteRepository
Expand All @@ -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
)
Expand Down
2 changes: 1 addition & 1 deletion Marlin/Marlin/Repository/ASAM/AsamLocalDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
7 changes: 3 additions & 4 deletions Marlin/Marlin/Repository/Bookmark/BookmarkRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -44,15 +45,13 @@ class BookmarkRepository: ObservableObject {
portRepository: PortRepository? = nil,
radioBeaconRepository: RadioBeaconRepository? = nil,
noticeToMarinersRepository: NoticeToMarinersRepository? = nil,
publicationRepository: PublicationRepository? = nil,
navigationalWarningRepository: NavigationalWarningRepository? = nil
) {
self.localDataSource = localDataSource
self.lightRepository = lightRepository
self.portRepository = portRepository
self.radioBeaconRepository = radioBeaconRepository
self.noticeToMarinersRepository = noticeToMarinersRepository
self.publicationRepository = publicationRepository
self.navigationalWarningRepository = navigationalWarningRepository
}

Expand Down Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion Marlin/Marlin/Repository/MODU/ModuLocalDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]?
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<PublicationModel> {
var downloads: [String: DownloadManager] = [:]
init(cleanup: (() -> Void)? = nil) {
Expand Down
20 changes: 13 additions & 7 deletions Marlin/Marlin/Repository/Publication/PublicationRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<AnyCancellable>()

@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()
Expand Down
4 changes: 2 additions & 2 deletions Marlin/Marlin/Startup/ASAM/AsamInitialDataLoadOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
4 changes: 1 addition & 3 deletions Marlin/Marlin/Startup/ASAM/AsamInitializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions Marlin/Marlin/Startup/MODU/ModuInitialDataLoadOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
4 changes: 1 addition & 3 deletions Marlin/Marlin/Startup/MODU/ModuInitializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
10 changes: 4 additions & 6 deletions Marlin/Marlin/Startup/Publication/PublicationInitializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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()
Expand Down
1 change: 0 additions & 1 deletion Marlin/Marlin/UI/Publication/PublicationActionBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import SwiftUI

struct PublicationActionBar: View {
@EnvironmentObject var repository: PublicationRepository
@EnvironmentObject var bookmarkRepository: BookmarkRepository
@StateObject var bookmarkViewModel: BookmarkViewModel = BookmarkViewModel()

Expand Down
3 changes: 1 addition & 2 deletions Marlin/Marlin/UI/Publication/PublicationSummaryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import SwiftUI

struct PublicationSummaryView: DataSourceSummaryView {
@EnvironmentObject var repository: PublicationRepository
@EnvironmentObject var bookmarkRepository: BookmarkRepository
@StateObject var bookmarkViewModel: BookmarkViewModel = BookmarkViewModel()

Expand All @@ -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) {
Expand Down
Loading

0 comments on commit 5578f93

Please sign in to comment.