Skip to content

Commit

Permalink
nav warning injection
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbarela committed Sep 6, 2024
1 parent 5578f93 commit c3c50df
Show file tree
Hide file tree
Showing 25 changed files with 155 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,12 @@ class NavigationalWarningsAreaViewModel: ObservableObject {
@Published var warnings: [NavigationalWarningModel] = []
var navArea: String?

var repository: NavigationalWarningRepository? {
didSet {
if let navArea = navArea {
getNavigationalWarnings(navArea: navArea)
}
}
}
@Injected(\.navWarningRepository)
var repository: NavigationalWarningRepository

func getNavigationalWarnings(navArea: String) {
Task {
let loaded = await self.repository?.getNavAreaNavigationalWarnings(navArea: navArea) ?? []
let loaded = await self.repository.getNavAreaNavigationalWarnings(navArea: navArea)
print("loaded \(loaded)")
await MainActor.run {
warnings = loaded
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 @@ -14,7 +14,6 @@ struct GeoPackageExportView: View {
@EnvironmentObject var portRepository: PortRepository
@EnvironmentObject var radioBeaconRepository: RadioBeaconRepository
@EnvironmentObject var routeRepository: RouteRepository
@EnvironmentObject var navigationalWarningRepository: NavigationalWarningRepository

@StateObject var viewModel: GeoPackageExportViewModel = GeoPackageExportViewModel()

Expand Down Expand Up @@ -99,7 +98,6 @@ struct GeoPackageExportView: View {
viewModel.portRepository = portRepository
viewModel.radioBeaconRepository = radioBeaconRepository
viewModel.routeRepository = routeRepository
viewModel.navigationalWarningRepository = navigationalWarningRepository
viewModel.setExportParameters(dataSources: dataSources, filters: filters, useMapRegion: useMapRegion)
Metrics.shared.geoPackageExportView()
}
Expand Down
11 changes: 4 additions & 7 deletions Marlin/Marlin/GeoPackage/GeoPackageExportViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class GeoPackageExportViewModel: ObservableObject {
var dgpsRepository: DGPSStationRepository
var radioBeaconRepository: RadioBeaconRepository?
var routeRepository: RouteRepository?
var navigationalWarningRepository: NavigationalWarningRepository?
@Injected(\.navWarningRepository)
var navigationalWarningRepository: NavigationalWarningRepository

var cancellable = Set<AnyCancellable>()
var countChangeCancellable: AnyCancellable?
Expand Down Expand Up @@ -139,7 +140,7 @@ class GeoPackageExportViewModel: ObservableObject {
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)
case .navWarning: $0[definition] = self.navigationalWarningRepository?.getCount(filters: filters)
case .navWarning: $0[definition] = self.navigationalWarningRepository.getCount(filters: filters)
case .light: $0[definition] = self.lightRepository?.getCount(filters: filters)
case .radioBeacon: $0[definition] = self.radioBeaconRepository?.getCount(filters: filters)
default: break
Expand Down Expand Up @@ -346,11 +347,7 @@ class GeoPackageExportViewModel: ObservableObject {
exportable = RadioBeaconGeoPackageExportable(radioBeaconRepository: radioBeaconRepository)
}
case DataSources.navWarning.key:
if let navigationalWarningRepository = navigationalWarningRepository {
exportable = NavigationalWarningGeoPackageExportable(
navigationalWarningRepository: navigationalWarningRepository
)
}
exportable = NavigationalWarningGeoPackageExportable()
case DataSources.route.key:
if let routeRepository = routeRepository {
exportable = RouteGeoPackageExportable(routeRepository: routeRepository)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ import sf_ios
class NavigationalWarningGeoPackageExportable: GeoPackageExportable {
static var definition: any DataSourceDefinition = DataSources.navWarning

let navigationalWarningRepository: NavigationalWarningRepository
init(navigationalWarningRepository: NavigationalWarningRepository) {
self.navigationalWarningRepository = navigationalWarningRepository
}
@Injected(\.navWarningRepository)
var navigationalWarningRepository: NavigationalWarningRepository

func createFeatures(
geoPackage: GPKGGeoPackage,
Expand Down
11 changes: 1 addition & 10 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 navigationalWarningRepository: NavigationalWarningRepository
var noticeToMarinersRepository: NoticeToMarinersRepository
var userPlaceRepository: UserPlaceRepository
var searchRepository: SearchRepository
Expand Down Expand Up @@ -179,10 +178,6 @@ struct MarlinApp: App {
localDataSource: RadioBeaconCoreDataDataSource(),
remoteDataSource: RadioBeaconRemoteDataSource()
)
navigationalWarningRepository = NavigationalWarningRepository(
localDataSource: NavigationalWarningCoreDataDataSource(),
remoteDataSource: NavigationalWarningRemoteDataSource()
)
noticeToMarinersRepository = NoticeToMarinersRepository(
localDataSource: NoticeToMarinersCoreDataDataSource(),
remoteDataSource: NoticeToMarinersRemoteDataSource()
Expand Down Expand Up @@ -211,15 +206,12 @@ struct MarlinApp: App {
lightsTileRepository = LightsTileRepository(localDataSource: lightRepository.localDataSource)
radioBeaconsTileRepository = RadioBeaconsTileRepository(localDataSource: radioBeaconRepository.localDataSource)
differentialGPSStationsTileRepository = DifferentialGPSStationsTileRepository()
navigationalWarningsMapFeatureRepository = NavigationalWarningsMapFeatureRepository(
localDataSource: navigationalWarningRepository.localDataSource
)
navigationalWarningsMapFeatureRepository = NavigationalWarningsMapFeatureRepository()

MSI.shared.addRepositories(
portRepository: portRepository,
lightRepository: lightRepository,
radioBeaconRepository: radioBeaconRepository,
navigationalWarningRepository: navigationalWarningRepository,
noticeToMarinersRepository: noticeToMarinersRepository,
routeRepository: routeRepository
)
Expand All @@ -245,7 +237,6 @@ struct MarlinApp: App {
.environmentObject(radioBeaconRepository)
.environmentObject(routeRepository)
.environmentObject(routeWaypointRepository)
.environmentObject(navigationalWarningRepository)
.environmentObject(noticeToMarinersRepository)
.environmentObject(userPlaceRepository)
.environmentObject(asamsTileRepository)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import Kingfisher
class NavigationalWarningDataLoadOperation: CountingDataLoadOperation {

var navigationalWarnings: [NavigationalWarningModel] = []
@Injected(\.navWarningLocalDataSource)
var localDataSource: NavigationalWarningLocalDataSource

init(navigationalWarnings: [NavigationalWarningModel], localDataSource: NavigationalWarningLocalDataSource) {
init(navigationalWarnings: [NavigationalWarningModel]) {
self.navigationalWarnings = navigationalWarnings
self.localDataSource = localDataSource
}

@MainActor override func finishLoad() {
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,
navigationalWarningRepository: NavigationalWarningRepository,
noticeToMarinersRepository: NoticeToMarinersRepository,
routeRepository: RouteRepository
) {
Expand All @@ -50,9 +49,7 @@ public class MSI {
radioBeaconInitializer = RadioBeaconInitializer(repository: radioBeaconRepository)
differentialGPSStationInitializer = DGPSStationInitializer()
publicationInitializer = PublicationInitializer()
navigationalWarningInitializer = NavigationalWarningInitializer(
repository: navigationalWarningRepository
)
navigationalWarningInitializer = NavigationalWarningInitializer()
noticeToMarinersInitializer = NoticeToMarinersInitializer(
repository: noticeToMarinersRepository
)
Expand Down
9 changes: 4 additions & 5 deletions Marlin/Marlin/Repository/Bookmark/BookmarkRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,21 @@ class BookmarkRepository: ObservableObject {
let noticeToMarinersRepository: NoticeToMarinersRepository?
@Injected(\.publicationRepository)
var publicationRepository: PublicationRepository
let navigationalWarningRepository: NavigationalWarningRepository?
@Injected(\.navWarningRepository)
var navigationalWarningRepository: NavigationalWarningRepository

init(
localDataSource: BookmarkLocalDataSource,
lightRepository: LightRepository? = nil,
portRepository: PortRepository? = nil,
radioBeaconRepository: RadioBeaconRepository? = nil,
noticeToMarinersRepository: NoticeToMarinersRepository? = nil,
navigationalWarningRepository: NavigationalWarningRepository? = nil
noticeToMarinersRepository: NoticeToMarinersRepository? = nil
) {
self.localDataSource = localDataSource
self.lightRepository = lightRepository
self.portRepository = portRepository
self.radioBeaconRepository = radioBeaconRepository
self.noticeToMarinersRepository = noticeToMarinersRepository
self.navigationalWarningRepository = navigationalWarningRepository
}

func getBookmark(itemKey: String, dataSource: String) -> BookmarkModel? {
Expand Down Expand Up @@ -85,7 +84,7 @@ class BookmarkRepository: ObservableObject {
return portRepository?.getPort(portNumber: Int(itemKey))
case DataSources.navWarning.key:
if split.count == 3 {
return navigationalWarningRepository?.getNavigationalWarning(
return navigationalWarningRepository.getNavigationalWarning(
msgYear: Int(split[0]) ?? 0,
msgNumber: Int(split[1]) ?? 0,
navArea: "\(split[2])"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ import UIKit
import BackgroundTasks
import Kingfisher

private struct NavigationalWarningLocalDataSourceProviderKey: InjectionKey {
static var currentValue: NavigationalWarningLocalDataSource = NavigationalWarningCoreDataDataSource()
}

extension InjectedValues {
var navWarningLocalDataSource: NavigationalWarningLocalDataSource {
get { Self[NavigationalWarningLocalDataSourceProviderKey.self] }
set { Self[NavigationalWarningLocalDataSourceProviderKey.self] = newValue }
}
}

protocol NavigationalWarningLocalDataSource {

func getNavAreasInformation() async -> [NavigationalAreaInformation]
Expand Down Expand Up @@ -404,8 +415,7 @@ extension NavigationalWarningCoreDataDataSource {

// Create an operation that performs the main part of the background task.
operation = NavigationalWarningDataLoadOperation(
navigationalWarnings: navigationalWarnings,
localDataSource: self
navigationalWarnings: navigationalWarnings
)

return await executeOperationInBackground(task: task)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,17 @@ class NavigationalWarningMapFeatureRepository: MapFeatureRepository, TileReposit
let msgYear: Int
let msgNumber: Int
let navArea: String
let localDataSource: NavigationalWarningLocalDataSource
@Injected(\.navWarningLocalDataSource)
var localDataSource: NavigationalWarningLocalDataSource

init(
msgYear: Int,
msgNumber: Int,
navArea: String,
localDataSource: NavigationalWarningLocalDataSource
navArea: String
) {
self.msgYear = msgYear
self.msgNumber = msgNumber
self.navArea = navArea
self.localDataSource = localDataSource
}

func getTileableItems(
Expand Down Expand Up @@ -127,13 +126,8 @@ class NavigationalWarningsMapFeatureRepository: MapFeatureRepository, TileReposi
[]
}

let localDataSource: NavigationalWarningLocalDataSource

init(
localDataSource: NavigationalWarningLocalDataSource
) {
self.localDataSource = localDataSource
}
@Injected(\.navWarningLocalDataSource)
var localDataSource: NavigationalWarningLocalDataSource

func getAnnotationsAndOverlays() async -> AnnotationsAndOverlays {
return AnnotationsAndOverlays(annotations: [], overlays: [])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@
import Foundation
import BackgroundTasks

private struct NavigationalWarningRemoteDataSourceProviderKey: InjectionKey {
static var currentValue: NavigationalWarningRemoteDataSource = NavigationalWarningRemoteDataSource()
}

extension InjectedValues {
var navWarningRemoteDataSource: NavigationalWarningRemoteDataSource {
get { Self[NavigationalWarningRemoteDataSourceProviderKey.self] }
set { Self[NavigationalWarningRemoteDataSourceProviderKey.self] = newValue }
}
}

class NavigationalWarningRemoteDataSource: RemoteDataSource<NavigationalWarningModel> {
init(cleanup: (() -> Void)? = nil) {
super.init(dataSource: DataSources.navWarning, cleanup: cleanup)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,22 @@ struct NavigationalAreaInformation: Identifiable {
let total: Int
}

private struct NavigationalWarningRepositoryProviderKey: InjectionKey {
static var currentValue: NavigationalWarningRepository = NavigationalWarningRepository()
}

extension InjectedValues {
var navWarningRepository: NavigationalWarningRepository {
get { Self[NavigationalWarningRepositoryProviderKey.self] }
set { Self[NavigationalWarningRepositoryProviderKey.self] = newValue }
}
}

class NavigationalWarningRepository: ObservableObject {
@Injected(\.navWarningLocalDataSource)
var localDataSource: NavigationalWarningLocalDataSource
@Injected(\.navWarningRemoteDataSource)
private var remoteDataSource: NavigationalWarningRemoteDataSource
init(localDataSource: NavigationalWarningLocalDataSource, remoteDataSource: NavigationalWarningRemoteDataSource) {
self.localDataSource = localDataSource
self.remoteDataSource = remoteDataSource
}

func createOperation() -> NavigationalWarningDataFetchOperation {
return NavigationalWarningDataFetchOperation()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import Foundation

class NavigationalWarningInitializer: Initializer {
let repository: NavigationalWarningRepository
@Injected(\.navWarningRepository)
var repository: NavigationalWarningRepository

init(repository: NavigationalWarningRepository) {
self.repository = repository
init() {
super.init(dataSource: DataSources.navWarning)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ struct CurrentNavigationalWarningSection: View {
}

struct NavigationalWarningAreasView: View {
@EnvironmentObject var repository: NavigationalWarningRepository
@ObservedObject var generalLocation = GeneralLocation.shared
@State var navArea: String?
var mapName: String?
Expand Down Expand Up @@ -78,7 +77,6 @@ struct NavigationalWarningAreasView: View {
})
.onAppear {
viewModel.currentNavAreaName = generalLocation.currentNavAreaName
viewModel.repository = repository
}
.accessibilityElement(children: .contain)
NavigationLink(value: MarlinRoute.exportGeoPackageDataSource( dataSource: .navWarning)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,26 @@ class NavigationalWarningAreasViewModel: ObservableObject {
}
}

var repository: NavigationalWarningRepository? {
didSet {
Task {
dataSourceUpdatedPub.store(in: &disposables)
@Injected(\.navWarningRepository)
var repository: NavigationalWarningRepository

init() {
Task {
dataSourceUpdatedPub.store(in: &disposables)

await populateWarningAreaInformation()
}
await populateWarningAreaInformation()
}
}

func populateWarningAreaInformation() async {
let info = await repository?.getNavAreasInformation()
let info = await repository.getNavAreasInformation()
await MainActor.run {
currentArea = info?.first { area in
currentArea = info.first { area in
area.navArea.name == currentNavAreaName
}
warningAreas = info?.filter({ area in
warningAreas = info.filter({ area in
area.navArea.name != currentNavAreaName
}) ?? []
})
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import MapKit
struct NavigationalWarningDetailView: View {
@EnvironmentObject var bookmarkRepository: BookmarkRepository
@StateObject var bookmarkViewModel: BookmarkViewModel = BookmarkViewModel()
@EnvironmentObject var navigationalWarningRepository: NavigationalWarningRepository
@EnvironmentObject var routeWaypointRepository: RouteWaypointRepository
@StateObject var viewModel: NavigationalWarningViewModel = NavigationalWarningViewModel()
@State var msgYear: Int
Expand All @@ -23,7 +22,6 @@ struct NavigationalWarningDetailView: View {
switch viewModel.navWarning {
case nil:
Color.clear.onAppear {
viewModel.repository = navigationalWarningRepository
viewModel.routeWaypointRepository = routeWaypointRepository
viewModel.getNavigationalWarning(
msgYear: msgYear,
Expand All @@ -47,8 +45,7 @@ struct NavigationalWarningDetailView: View {
let mapRepository = NavigationalWarningMapFeatureRepository(
msgYear: navigationalWarning.msgYear ?? -1,
msgNumber: navigationalWarning.msgNumber ?? -1,
navArea: navigationalWarning.navArea,
localDataSource: navigationalWarningRepository.localDataSource
navArea: navigationalWarning.navArea
)
DataSourceLocationMapView(
dataSourceLocation: navigationalWarning,
Expand Down
Loading

0 comments on commit c3c50df

Please sign in to comment.