Skip to content

Commit

Permalink
light injection
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbarela committed Sep 6, 2024
1 parent 192a018 commit 91c09be
Show file tree
Hide file tree
Showing 24 changed files with 109 additions and 105 deletions.
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 lightRepository: LightRepository
@EnvironmentObject var portRepository: PortRepository
@EnvironmentObject var radioBeaconRepository: RadioBeaconRepository
@EnvironmentObject var routeRepository: RouteRepository
Expand Down Expand Up @@ -94,7 +93,6 @@ struct GeoPackageExportView: View {
.navigationTitle("GeoPackage Export")
.background(Color.backgroundColor)
.onAppear {
viewModel.lightRepository = lightRepository
viewModel.portRepository = portRepository
viewModel.radioBeaconRepository = radioBeaconRepository
viewModel.routeRepository = routeRepository
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 @@ -37,7 +37,8 @@ class GeoPackageExportViewModel: ObservableObject {
var asamRepository: AsamRepository
@Injected(\.moduRepository)
var moduRepository: ModuRepository
var lightRepository: LightRepository?
@Injected(\.lightRepository)
var lightRepository: LightRepository
var portRepository: PortRepository?
@Injected(\.dgpsRepository)
var dgpsRepository: DGPSStationRepository
Expand Down Expand Up @@ -141,7 +142,7 @@ class GeoPackageExportViewModel: ObservableObject {
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 .light: $0[definition] = self.lightRepository?.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 @@ -335,9 +336,7 @@ class GeoPackageExportViewModel: ObservableObject {
case DataSources.dgps.key:
exportable = DGPSStationGeoPackageExportable()
case DataSources.light.key:
if let lightRepository = lightRepository {
exportable = LightGeoPackageExportable(lightRepository: lightRepository)
}
exportable = LightGeoPackageExportable()
case DataSources.port.key:
if let portRepository = portRepository {
exportable = PortGeoPackageExportable(portRepository: portRepository)
Expand Down
8 changes: 3 additions & 5 deletions Marlin/Marlin/GeoPackage/LightGeoPackageExportable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ import sf_ios
class LightGeoPackageExportable: GeoPackageExportable {
static var definition: any DataSourceDefinition = DataSources.light

let lightRepository: LightRepository
init(lightRepository: LightRepository) {
self.lightRepository = lightRepository
}

@Injected(\.lightRepository)
private var lightRepository: LightRepository

func createFeatures(
geoPackage: GPKGGeoPackage,
table: GPKGFeatureTable,
Expand Down
10 changes: 1 addition & 9 deletions Marlin/Marlin/MarlinApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ struct MarlinApp: App {
@StateObject var dataSourceList: DataSourceList = DataSourceList()
var bookmarkRepository: BookmarkRepository
var portRepository: PortRepository
var lightRepository: LightRepository
var radioBeaconRepository: RadioBeaconRepository
var noticeToMarinersRepository: NoticeToMarinersRepository
var userPlaceRepository: UserPlaceRepository
Expand Down Expand Up @@ -170,10 +169,6 @@ struct MarlinApp: App {
localDataSource: PortCoreDataDataSource(),
remoteDataSource: PortRemoteDataSource()
)
lightRepository = LightRepository(
localDataSource: LightCoreDataDataSource(),
remoteDataSource: LightRemoteDataSource()
)
radioBeaconRepository = RadioBeaconRepository(
localDataSource: RadioBeaconCoreDataDataSource(),
remoteDataSource: RadioBeaconRemoteDataSource()
Expand All @@ -194,7 +189,6 @@ struct MarlinApp: App {

bookmarkRepository = BookmarkRepository(
localDataSource: BookmarkCoreDataDataSource(),
lightRepository: lightRepository,
portRepository: portRepository,
radioBeaconRepository: radioBeaconRepository,
noticeToMarinersRepository: noticeToMarinersRepository
Expand All @@ -203,14 +197,13 @@ struct MarlinApp: App {
asamsTileRepository = AsamsTileRepository()
modusTileRepository = ModusTileRepository()
portsTileRepository = PortsTileRepository(localDataSource: portRepository.localDataSource)
lightsTileRepository = LightsTileRepository(localDataSource: lightRepository.localDataSource)
lightsTileRepository = LightsTileRepository()
radioBeaconsTileRepository = RadioBeaconsTileRepository(localDataSource: radioBeaconRepository.localDataSource)
differentialGPSStationsTileRepository = DifferentialGPSStationsTileRepository()
navigationalWarningsMapFeatureRepository = NavigationalWarningsMapFeatureRepository()

MSI.shared.addRepositories(
portRepository: portRepository,
lightRepository: lightRepository,
radioBeaconRepository: radioBeaconRepository,
noticeToMarinersRepository: noticeToMarinersRepository,
routeRepository: routeRepository
Expand All @@ -232,7 +225,6 @@ struct MarlinApp: App {
.environmentObject(appState)
.environmentObject(dataSourceList)
.environmentObject(bookmarkRepository)
.environmentObject(lightRepository)
.environmentObject(portRepository)
.environmentObject(radioBeaconRepository)
.environmentObject(routeRepository)
Expand Down
4 changes: 2 additions & 2 deletions Marlin/Marlin/Network/Light/LightDataLoadOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import Kingfisher
class LightDataLoadOperation: CountingDataLoadOperation {

var lights: [LightModel] = []
@Injected(\.lightLocalDataSource)
var localDataSource: LightLocalDataSource

init(lights: [LightModel], localDataSource: LightLocalDataSource) {
init(lights: [LightModel]) {
self.lights = lights
self.localDataSource = localDataSource
}

@MainActor override func finishLoad() {
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 @@ -26,7 +26,6 @@ public class MSI {
// swiftlint:disable function_parameter_count

Check warning on line 26 in Marlin/Marlin/Networking/MSI.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Superfluous Disable Command Violation: SwiftLint rule 'function_parameter_count' did not trigger a violation in the disabled region; remove the disable command (superfluous_disable_command)

Check warning on line 26 in Marlin/Marlin/Networking/MSI.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Superfluous Disable Command Violation: SwiftLint rule 'function_parameter_count' did not trigger a violation in the disabled region; remove the disable command (superfluous_disable_command)
func addRepositories(
portRepository: PortRepository,
lightRepository: LightRepository,
radioBeaconRepository: RadioBeaconRepository,
noticeToMarinersRepository: NoticeToMarinersRepository,
routeRepository: RouteRepository
Expand All @@ -45,7 +44,7 @@ public class MSI {
asamInitializer = AsamInitializer()
moduInitializer = ModuInitializer()
portInitializer = PortInitializer(repository: portRepository)
lightInitializer = LightInitializer(repository: lightRepository)
lightInitializer = LightInitializer()
radioBeaconInitializer = RadioBeaconInitializer(repository: radioBeaconRepository)
differentialGPSStationInitializer = DGPSStationInitializer()
publicationInitializer = PublicationInitializer()
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 @@ -29,7 +29,8 @@ class BookmarkRepository: ObservableObject {
var asamRepository: AsamRepository
@Injected(\.dgpsRepository)
var dgpsRepository: DGPSStationRepository
let lightRepository: LightRepository?
@Injected(\.lightRepository)
var lightRepository: LightRepository
@Injected(\.moduRepository)
var moduRepository: ModuRepository
let portRepository: PortRepository?
Expand All @@ -42,13 +43,11 @@ class BookmarkRepository: ObservableObject {

init(
localDataSource: BookmarkLocalDataSource,
lightRepository: LightRepository? = nil,
portRepository: PortRepository? = nil,
radioBeaconRepository: RadioBeaconRepository? = nil,
noticeToMarinersRepository: NoticeToMarinersRepository? = nil
) {
self.localDataSource = localDataSource
self.lightRepository = lightRepository
self.portRepository = portRepository
self.radioBeaconRepository = radioBeaconRepository
self.noticeToMarinersRepository = noticeToMarinersRepository
Expand Down Expand Up @@ -101,7 +100,7 @@ class BookmarkRepository: ObservableObject {
}
case DataSources.light.key:
if split.count == 3 {
return lightRepository?.getCharacteristic(
return lightRepository.getCharacteristic(
featureNumber: "\(split[0])",
volumeNumber: "\(split[1])",
characteristicNumber: 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extension LightCoreDataDataSource {
NSLog("Received \(count) \(DataSources.light.key) records.")

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

return await executeOperationInBackground(task: task)
}
Expand Down
11 changes: 11 additions & 0 deletions Marlin/Marlin/Repository/Light/LightLocalDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ import Foundation
import Combine
import BackgroundTasks

private struct LightLocalDataSourceProviderKey: InjectionKey {
static var currentValue: LightLocalDataSource = LightCoreDataDataSource()
}

extension InjectedValues {
var lightLocalDataSource: LightLocalDataSource {
get { Self[LightLocalDataSourceProviderKey.self] }
set { Self[LightLocalDataSourceProviderKey.self] = newValue }
}
}

protocol LightLocalDataSource {

func getCharacteristic(
Expand Down
11 changes: 11 additions & 0 deletions Marlin/Marlin/Repository/Light/LightRemoteDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@
import Foundation
import BackgroundTasks

private struct LightRemoteDataSourceProviderKey: InjectionKey {
static var currentValue: LightRemoteDataSource = LightRemoteDataSource()
}

extension InjectedValues {
var lightRemoteDataSource: LightRemoteDataSource {
get { Self[LightRemoteDataSourceProviderKey.self] }
set { Self[LightRemoteDataSourceProviderKey.self] = newValue }
}
}

class LightRemoteDataSource: RemoteDataSource<LightModel> {
init() {
super.init(dataSource: DataSources.light)
Expand Down
19 changes: 14 additions & 5 deletions Marlin/Marlin/Repository/Light/LightRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,23 @@ enum LightItem: Hashable, Identifiable {
case sectionHeader(header: String)
}

private struct LightRepositoryProviderKey: InjectionKey {
static var currentValue: LightRepository = LightRepository()
}

extension InjectedValues {
var lightRepository: LightRepository {
get { Self[LightRepositoryProviderKey.self] }
set { Self[LightRepositoryProviderKey.self] = newValue }
}
}

class LightRepository: ObservableObject {
@Injected(\.lightLocalDataSource)
var localDataSource: LightLocalDataSource
@Injected(\.lightRemoteDataSource)
private var remoteDataSource: LightRemoteDataSource
init(localDataSource: LightLocalDataSource, remoteDataSource: LightRemoteDataSource) {
self.localDataSource = localDataSource
self.remoteDataSource = remoteDataSource
}


func createOperations() -> [LightDataFetchOperation] {
return Light.lightVolumes.map { lightVolume in
let newestLight = localDataSource.getNewestLight(volumeNumber: lightVolume.volumeNumber)
Expand Down
13 changes: 5 additions & 8 deletions Marlin/Marlin/Repository/Light/LightTileRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ class LightTileRepository: TileRepository, ObservableObject {

let featureNumber: String
let volumeNumber: String
let localDataSource: LightLocalDataSource
@Injected(\.lightLocalDataSource)
private var localDataSource: LightLocalDataSource

init(featureNumber: String, volumeNumber: String, localDataSource: LightLocalDataSource) {
init(featureNumber: String, volumeNumber: String) {
self.featureNumber = featureNumber
self.volumeNumber = volumeNumber
self.localDataSource = localDataSource
}

func getTileableItems(
Expand Down Expand Up @@ -87,11 +87,8 @@ class LightsTileRepository: TileRepository, ObservableObject {
var filterCacheKey: String {
UserDefaults.standard.filter(DataSources.light).getCacheKey()
}
let localDataSource: LightLocalDataSource

init(localDataSource: LightLocalDataSource) {
self.localDataSource = localDataSource
}
@Injected(\.lightLocalDataSource)
var localDataSource: LightLocalDataSource

func getTileableItems(
minLatitude: Double,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import Foundation

class LightInitialDataLoadOperation: CountingDataLoadOperation {
@Injected(\.lightLocalDataSource)
var localDataSource: LightLocalDataSource
var bundle: Bundle

init(localDataSource: LightLocalDataSource, bundle: Bundle = .main) {
self.localDataSource = localDataSource
init(bundle: Bundle = .main) {
self.bundle = bundle
}

Expand Down
10 changes: 4 additions & 6 deletions Marlin/Marlin/Startup/Light/LightInitializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import BackgroundTasks

class LightInitializer: Initializer {

let repository: LightRepository
@Injected(\.lightRepository)
var repository: LightRepository

init(repository: LightRepository) {
self.repository = repository
init() {
super.init(dataSource: DataSources.light)
}

Expand Down Expand Up @@ -44,9 +44,7 @@ class LightInitializer: Initializer {

override func fetch() {
if repository.getCount(filters: nil) == 0 {
let initialDataLoadOperation = LightInitialDataLoadOperation(
localDataSource: self.repository.localDataSource
)
let initialDataLoadOperation = LightInitialDataLoadOperation()
initialDataLoadOperation.completionBlock = {
Task {
await self.repository.fetchLights()
Expand Down
5 changes: 1 addition & 4 deletions Marlin/Marlin/UI/Light/LightDetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import CoreData
struct LightDetailView: View {
@EnvironmentObject var bookmarkRepository: BookmarkRepository
@EnvironmentObject var router: MarlinRouter
@EnvironmentObject var lightRepository: LightRepository
@StateObject var viewModel: LightViewModel = LightViewModel()
@State var featureNumber: String
@State var volumeNumber: String
Expand Down Expand Up @@ -42,8 +41,7 @@ struct LightDetailView: View {
LightMap(
repository: LightTileRepository(
featureNumber: featureNumber,
volumeNumber: volumeNumber,
localDataSource: lightRepository.localDataSource
volumeNumber: volumeNumber
)
)
]
Expand Down Expand Up @@ -117,7 +115,6 @@ struct LightDetailView: View {
viewModel.getLights(featureNumber: featureNumber, volumeNumber: volumeNumber, waypointURI: waypointURI)
}
.onAppear {
viewModel.repository = lightRepository
viewModel.getLights(featureNumber: featureNumber, volumeNumber: volumeNumber, waypointURI: waypointURI)
Metrics.shared.dataSourceDetail(dataSource: DataSources.light)
}
Expand Down
2 changes: 0 additions & 2 deletions Marlin/Marlin/UI/Light/LightList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import Foundation
import SwiftUI

struct LightList: View {
@EnvironmentObject var lightRepository: LightRepository
@StateObject var viewModel: LightsViewModel = LightsViewModel()

@EnvironmentObject var router: MarlinRouter
Expand Down Expand Up @@ -125,7 +124,6 @@ struct LightList: View {
}
}
.onAppear {
viewModel.repository = lightRepository
Metrics.shared.dataSourceList(dataSource: DataSources.light)
}
.modifier(
Expand Down
12 changes: 4 additions & 8 deletions Marlin/Marlin/UI/Light/LightViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,17 @@ class LightViewModel: ObservableObject, Identifiable {
var featureNumber: String?
var volumeNumber: String?

var repository: LightRepository? {
didSet {
if let featureNumber = featureNumber, let volumeNumber = volumeNumber {
getLights(featureNumber: featureNumber, volumeNumber: volumeNumber)
}
}
}
@Injected(\.lightRepository)
private var repository: LightRepository

var routeWaypointRepository: RouteWaypointRepository?

@discardableResult
func getLights(featureNumber: String?, volumeNumber: String?, waypointURI: URL? = nil) -> [LightModel] {
if let waypointURI = waypointURI {
lights = routeWaypointRepository?.getLight(waypointURI: waypointURI) ?? []
} else {
lights = repository?.getLight(featureNumber: featureNumber, volumeNumber: volumeNumber) ?? []
lights = repository.getLight(featureNumber: featureNumber, volumeNumber: volumeNumber) ?? []
return lights
}
return []
Expand Down
Loading

0 comments on commit 91c09be

Please sign in to comment.