Skip to content

Commit

Permalink
asam injection
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbarela committed Sep 5, 2024
1 parent c3b0d14 commit ea675d8
Show file tree
Hide file tree
Showing 22 changed files with 136 additions and 99 deletions.
8 changes: 3 additions & 5 deletions Marlin/Marlin/GeoPackage/AsamGeoPackageExportable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ import sf_ios
class AsamGeoPackageExportable: GeoPackageExportable {
static var definition: any DataSourceDefinition = DataSources.asam

let asamRepository: AsamRepository
init(asamRepository: AsamRepository) {
self.asamRepository = asamRepository
}

@Injected(\.asamRepository)
var asamRepository: AsamRepository

var sfGeometry: SFGeometry?

func createFeatures(
Expand Down
2 changes: 0 additions & 2 deletions Marlin/Marlin/GeoPackage/GeoPackageExportView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import SwiftUI
struct GeoPackageExportView: View {
@EnvironmentObject var dataSourceList: DataSourceList

@EnvironmentObject var asamRepository: AsamRepository
@EnvironmentObject var lightRepository: LightRepository
@EnvironmentObject var portRepository: PortRepository
@EnvironmentObject var dgpsRepository: DGPSStationRepository
Expand Down Expand Up @@ -97,7 +96,6 @@ struct GeoPackageExportView: View {
.navigationTitle("GeoPackage Export")
.background(Color.backgroundColor)
.onAppear {
viewModel.asamRepository = asamRepository
viewModel.lightRepository = lightRepository
viewModel.portRepository = portRepository
viewModel.dgpsRepository = dgpsRepository
Expand Down
9 changes: 4 additions & 5 deletions Marlin/Marlin/GeoPackage/GeoPackageExportViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class DataSourceExportProgress: Identifiable, Hashable, Equatable, ObservableObj
}

class GeoPackageExportViewModel: ObservableObject {
var asamRepository: AsamRepository?
@Injected(\.asamRepository)
var asamRepository: AsamRepository
@Injected(\.moduRepository)
var moduRepository: ModuRepository
var lightRepository: LightRepository?
Expand Down Expand Up @@ -133,7 +134,7 @@ class GeoPackageExportViewModel: ObservableObject {

switch definition {
case .route: $0[definition] = self.routeRepository?.getCount(filters: filters)
case .asam: $0[definition] = self.asamRepository?.getCount(filters: filters)
case .asam: $0[definition] = self.asamRepository.getCount(filters: filters)
case .modu: $0[definition] = self.moduRepository.getCount(filters: filters)
case .differentialGPSStation: $0[definition] = self.dgpsRepository?.getCount(filters: filters)
case .port: $0[definition] = self.portRepository?.getCount(filters: filters)
Expand Down Expand Up @@ -326,9 +327,7 @@ class GeoPackageExportViewModel: ObservableObject {
print("definition.key \(definition.key)")
switch definition.key {
case DataSources.asam.key:
if let asamRepository = asamRepository {
exportable = AsamGeoPackageExportable(asamRepository: asamRepository)
}
exportable = AsamGeoPackageExportable()
case DataSources.modu.key:
exportable = ModuGeoPackageExportable()
case DataSources.dgps.key:
Expand Down
10 changes: 1 addition & 9 deletions Marlin/Marlin/MarlinApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ struct MarlinApp: App {

@StateObject var dataSourceList: DataSourceList = DataSourceList()
var bookmarkRepository: BookmarkRepository
var asamRepository: AsamRepository
var portRepository: PortRepository
var differentialGPSStationRepository: DGPSStationRepository
var lightRepository: LightRepository
Expand Down Expand Up @@ -170,10 +169,6 @@ struct MarlinApp: App {
appState = MSI.shared.appState
persistentStore = PersistenceController.shared

asamRepository = AsamRepository(
localDataSource: AsamCoreDataDataSource(),
remoteDataSource: AsamRemoteDataSource()
)
portRepository = PortRepository(
localDataSource: PortCoreDataDataSource(),
remoteDataSource: PortRemoteDataSource()
Expand Down Expand Up @@ -214,7 +209,6 @@ struct MarlinApp: App {

bookmarkRepository = BookmarkRepository(
localDataSource: BookmarkCoreDataDataSource(),
asamRepository: asamRepository,
dgpsRepository: differentialGPSStationRepository,
lightRepository: lightRepository,
portRepository: portRepository,
Expand All @@ -223,7 +217,7 @@ struct MarlinApp: App {
publicationRepository: publicationRepository
)

asamsTileRepository = AsamsTileRepository(localDataSource: asamRepository.localDataSource)
asamsTileRepository = AsamsTileRepository()
modusTileRepository = ModusTileRepository()
portsTileRepository = PortsTileRepository(localDataSource: portRepository.localDataSource)
lightsTileRepository = LightsTileRepository(localDataSource: lightRepository.localDataSource)
Expand All @@ -236,7 +230,6 @@ struct MarlinApp: App {
)

MSI.shared.addRepositories(
asamRepository: asamRepository,
portRepository: portRepository,
lightRepository: lightRepository,
radioBeaconRepository: radioBeaconRepository,
Expand All @@ -263,7 +256,6 @@ struct MarlinApp: App {
.environmentObject(appState)
.environmentObject(dataSourceList)
.environmentObject(bookmarkRepository)
.environmentObject(asamRepository)
.environmentObject(lightRepository)
.environmentObject(portRepository)
.environmentObject(differentialGPSStationRepository)
Expand Down
3 changes: 1 addition & 2 deletions Marlin/Marlin/Networking/MSI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public class MSI {

// swiftlint:disable function_parameter_count
func addRepositories(
asamRepository: AsamRepository,
portRepository: PortRepository,
lightRepository: LightRepository,
radioBeaconRepository: RadioBeaconRepository,
Expand All @@ -46,7 +45,7 @@ public class MSI {
// self.noticeToMarinersRepository = noticeToMarinersRepository
// self.routeRepository = routeRepository

asamInitializer = AsamInitializer(repository: asamRepository)
asamInitializer = AsamInitializer()
moduInitializer = ModuInitializer()
portInitializer = PortInitializer(repository: portRepository)
lightInitializer = LightInitializer(repository: lightRepository)
Expand Down
11 changes: 11 additions & 0 deletions Marlin/Marlin/Repository/ASAM/AsamLocalDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ import Combine
import UIKit
import BackgroundTasks

private struct AsamLocalDataSourceProviderKey: InjectionKey {
static var currentValue: AsamLocalDataSource = AsamCoreDataDataSource()
}

extension InjectedValues {
var asamLocalDataSource: AsamLocalDataSource {
get { Self[AsamLocalDataSourceProviderKey.self] }
set { Self[AsamLocalDataSourceProviderKey.self] = newValue }
}
}

protocol AsamLocalDataSource {
func getNewestAsam() -> AsamModel?
@discardableResult
Expand Down
11 changes: 11 additions & 0 deletions Marlin/Marlin/Repository/ASAM/AsamRemoteDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ import Foundation
import UIKit
import BackgroundTasks

private struct AsamRemoteDataSourceProviderKey: InjectionKey {
static var currentValue: AsamRemoteDataSource = AsamRemoteDataSource()
}

extension InjectedValues {
var asamRemoteDataSource: AsamRemoteDataSource {
get { Self[AsamRemoteDataSourceProviderKey.self] }
set { Self[AsamRemoteDataSourceProviderKey.self] = newValue }
}
}

class AsamRemoteDataSource: RemoteDataSource<AsamModel> {

init(cleanup: (() -> Void)? = nil) {
Expand Down
19 changes: 14 additions & 5 deletions Marlin/Marlin/Repository/ASAM/AsamRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,23 @@ enum AsamItem: Hashable, Identifiable {
case sectionHeader(header: String)
}

private struct AsamRepositoryProviderKey: InjectionKey {
static var currentValue: AsamRepository = AsamRepository()
}

extension InjectedValues {
var asamRepository: AsamRepository {
get { Self[AsamRepositoryProviderKey.self] }
set { Self[AsamRepositoryProviderKey.self] = newValue }
}
}

class AsamRepository: ObservableObject {
@Injected(\.asamLocalDataSource)
var localDataSource: AsamLocalDataSource
@Injected(\.asamRemoteDataSource)
private var remoteDataSource: AsamRemoteDataSource
init(localDataSource: AsamLocalDataSource, remoteDataSource: AsamRemoteDataSource) {
self.localDataSource = localDataSource
self.remoteDataSource = remoteDataSource
}


func createOperation() -> AsamDataFetchOperation {
let newestAsam = localDataSource.getNewestAsam()
return AsamDataFetchOperation(dateString: newestAsam?.dateString)
Expand Down
15 changes: 7 additions & 8 deletions Marlin/Marlin/Repository/ASAM/AsamTileRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ class AsamTileRepository: TileRepository, ObservableObject {
""
}
let reference: String
let localDataSource: AsamLocalDataSource

@Injected(\.asamLocalDataSource)
var localDataSource: AsamLocalDataSource

init(reference: String, localDataSource: AsamLocalDataSource) {
init(reference: String) {
self.reference = reference
self.localDataSource = localDataSource
}

func getTileableItems(
Expand Down Expand Up @@ -73,11 +74,9 @@ class AsamsTileRepository: TileRepository, ObservableObject {
var filterCacheKey: String {
UserDefaults.standard.filter(DataSources.asam).getCacheKey()
}
let localDataSource: AsamLocalDataSource

init(localDataSource: AsamLocalDataSource) {
self.localDataSource = localDataSource
}

@Injected(\.asamLocalDataSource)
var localDataSource: AsamLocalDataSource

func getTileableItems(
minLatitude: Double,
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 @@ -25,7 +25,8 @@ enum BookmarkItem: Hashable, Identifiable {
class BookmarkRepository: ObservableObject {
let localDataSource: BookmarkLocalDataSource

let asamRepository: AsamRepository?
@Injected(\.asamRepository)
var asamRepository: AsamRepository
let dgpsRepository: DGPSStationRepository?
let lightRepository: LightRepository?
@Injected(\.moduRepository)
Expand All @@ -38,7 +39,6 @@ class BookmarkRepository: ObservableObject {

init(
localDataSource: BookmarkLocalDataSource,
asamRepository: AsamRepository? = nil,
dgpsRepository: DGPSStationRepository? = nil,
lightRepository: LightRepository? = nil,
portRepository: PortRepository? = nil,
Expand All @@ -48,7 +48,6 @@ class BookmarkRepository: ObservableObject {
navigationalWarningRepository: NavigationalWarningRepository? = nil
) {
self.localDataSource = localDataSource
self.asamRepository = asamRepository
self.dgpsRepository = dgpsRepository
self.lightRepository = lightRepository
self.portRepository = portRepository
Expand Down Expand Up @@ -81,7 +80,7 @@ class BookmarkRepository: ObservableObject {
let split = itemKey.split(separator: "--")
switch dataSource {
case DataSources.asam.key:
return asamRepository?.getAsam(reference: itemKey)
return asamRepository.getAsam(reference: itemKey)
case DataSources.modu.key:
return moduRepository.getModu(name: itemKey)
case DataSources.port.key:
Expand Down
6 changes: 3 additions & 3 deletions Marlin/Marlin/Startup/ASAM/AsamInitializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import BackgroundTasks

class AsamInitializer: Initializer {

let repository: AsamRepository
@Injected(\.asamRepository)
var repository: AsamRepository

init(repository: AsamRepository) {
self.repository = repository
init() {
super.init(dataSource: DataSources.asam)
}

Expand Down
5 changes: 1 addition & 4 deletions Marlin/Marlin/UI/ASAM/AsamDetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import SwiftUI
import MapKit

struct AsamDetailView: View {
@EnvironmentObject var asamRepository: AsamRepository
@EnvironmentObject var routeWaypointRepository: RouteWaypointRepository
@StateObject var viewModel: AsamViewModel = AsamViewModel()
@State var reference: String
Expand All @@ -36,8 +35,7 @@ struct AsamDetailView: View {
mixins: [
AsamMap(
repository: AsamTileRepository(
reference: asam.reference ?? "",
localDataSource: asamRepository.localDataSource
reference: asam.reference ?? ""
)
)
]
Expand Down Expand Up @@ -84,7 +82,6 @@ struct AsamDetailView: View {
viewModel.getAsam(reference: reference, waypointURI: waypointURI)
}
.onAppear {
viewModel.repository = asamRepository
viewModel.routeWaypointRepository = routeWaypointRepository
viewModel.getAsam(reference: reference, waypointURI: waypointURI)
Metrics.shared.dataSourceDetail(dataSource: DataSources.asam)
Expand Down
2 changes: 0 additions & 2 deletions Marlin/Marlin/UI/ASAM/AsamList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import Foundation
import SwiftUI

struct AsamList: View {
@EnvironmentObject var asamRepository: AsamRepository
@StateObject var viewModel: AsamsViewModel = AsamsViewModel()

@EnvironmentObject var router: MarlinRouter
Expand Down Expand Up @@ -125,7 +124,6 @@ struct AsamList: View {
}
}
.onAppear {
viewModel.repository = asamRepository
Metrics.shared.dataSourceList(dataSource: DataSources.asam)
}
.modifier(
Expand Down
11 changes: 3 additions & 8 deletions Marlin/Marlin/UI/ASAM/AsamViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@ class AsamViewModel: ObservableObject, Identifiable {
self.reference = reference
}

var repository: AsamRepository? {
didSet {
if let reference = reference {
getAsam(reference: reference)
}
}
}
@Injected(\.asamRepository)
var repository: AsamRepository
var routeWaypointRepository: RouteWaypointRepository?

@discardableResult
Expand All @@ -31,7 +26,7 @@ class AsamViewModel: ObservableObject, Identifiable {
asam = routeWaypointRepository?.getAsam(waypointURI: waypointURI)
return asam
} else {
asam = repository?.getAsam(reference: reference)
asam = repository.getAsam(reference: reference)
return asam
}
}
Expand Down
18 changes: 5 additions & 13 deletions Marlin/Marlin/UI/ASAM/AsamsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class AsamsViewModel: ObservableObject {
@Published var loaded: Bool = false
private var disposables = Set<AnyCancellable>()

private var _repository: AsamRepository?
@Injected(\.asamRepository)
private var repository: AsamRepository

var dataSourceUpdatedPub: AnyCancellable {
return NotificationCenter.default.publisher(for: .DataSourceUpdated)
Expand All @@ -30,17 +31,9 @@ class AsamsViewModel: ObservableObject {
}
}

var repository: AsamRepository? {
get {
return _repository
}
set {
if _repository == nil {
dataSourceUpdatedPub.store(in: &disposables)
_repository = newValue
fetchAsams()
}
}
init() {
dataSourceUpdatedPub.store(in: &disposables)
fetchAsams()
}

var publisher: AnyPublisher<CollectionDifference<AsamModel>, Never>?
Expand Down Expand Up @@ -78,7 +71,6 @@ class AsamsViewModel: ObservableObject {
if publisher != nil {
return
}
guard let repository = _repository else { return }
Publishers.PublishAndRepeat(
onOutputFrom: trigger.signal(activatedBy: TriggerId.reload)
) { [trigger, repository] in
Expand Down
2 changes: 0 additions & 2 deletions Marlin/Marlin/UI/ASAM/Sheet/AsamRouteSheetView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import Foundation
import SwiftUI

struct AsamRouteSheetView: View {
@EnvironmentObject var asamRepository: AsamRepository
var itemKey: String
var focusNotification: NSNotification.Name
@ObservedObject var routeViewModel: RouteViewModel
Expand Down Expand Up @@ -48,7 +47,6 @@ struct AsamRouteSheetView: View {
)
}
.onAppear {
viewModel.repository = asamRepository
viewModel.getAsam(reference: itemKey)
}
}
Expand Down
Loading

0 comments on commit ea675d8

Please sign in to comment.