Skip to content
This repository was archived by the owner on Jul 10, 2025. It is now read-only.

Commit 73af901

Browse files
committed
map tests
1 parent a854328 commit 73af901

File tree

5 files changed

+786
-43
lines changed

5 files changed

+786
-43
lines changed

Marlin/Marlin.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@
147147
F75324B829807F04005C509F /* SubmitReportViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F75324B729807F04005C509F /* SubmitReportViewTests.swift */; };
148148
F75324BB2980906E005C509F /* CLLocationCoordinate2DExtensionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F75324BA2980906E005C509F /* CLLocationCoordinate2DExtensionsTests.swift */; };
149149
F75324BE298197E4005C509F /* OnboardingViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F75324BD298197E4005C509F /* OnboardingViewTests.swift */; };
150+
F75324C02983272A005C509F /* MarlinCompactWidthViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F75324BF2983272A005C509F /* MarlinCompactWidthViewTests.swift */; };
151+
F75324C229833BBB005C509F /* MalrinMapTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F75324C129833BBB005C509F /* MalrinMapTests.swift */; };
150152
F75F81D529526E9C0062A708 /* AsamSummaryViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F75F81D429526E9C0062A708 /* AsamSummaryViewTests.swift */; };
151153
F75F81D8295DE0EF0062A708 /* AboutCellTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F75F81D7295DE0EF0062A708 /* AboutCellTests.swift */; };
152154
F75F81DA295DE4640062A708 /* AboutViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F75F81D9295DE4640062A708 /* AboutViewTests.swift */; };
@@ -489,6 +491,8 @@
489491
F75324B729807F04005C509F /* SubmitReportViewTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SubmitReportViewTests.swift; sourceTree = "<group>"; };
490492
F75324BA2980906E005C509F /* CLLocationCoordinate2DExtensionsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CLLocationCoordinate2DExtensionsTests.swift; sourceTree = "<group>"; };
491493
F75324BD298197E4005C509F /* OnboardingViewTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingViewTests.swift; sourceTree = "<group>"; };
494+
F75324BF2983272A005C509F /* MarlinCompactWidthViewTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MarlinCompactWidthViewTests.swift; sourceTree = "<group>"; };
495+
F75324C129833BBB005C509F /* MalrinMapTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MalrinMapTests.swift; sourceTree = "<group>"; };
492496
F75F81D229526D5C0062A708 /* libKIF.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; path = libKIF.a; sourceTree = BUILT_PRODUCTS_DIR; };
493497
F75F81D429526E9C0062A708 /* AsamSummaryViewTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AsamSummaryViewTests.swift; sourceTree = "<group>"; };
494498
F75F81D7295DE0EF0062A708 /* AboutCellTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AboutCellTests.swift; sourceTree = "<group>"; };
@@ -1085,6 +1089,7 @@
10851089
F72281DE296F644600218FC0 /* DataSourceRailTests.swift */,
10861090
F72D88F72971FF8000D022FB /* HamburgerTests.swift */,
10871091
F75324B729807F04005C509F /* SubmitReportViewTests.swift */,
1092+
F75324BF2983272A005C509F /* MarlinCompactWidthViewTests.swift */,
10881093
);
10891094
path = Views;
10901095
sourceTree = "<group>";
@@ -1334,6 +1339,7 @@
13341339
children = (
13351340
F7D8FA322936C07100E7D0A1 /* Annotation */,
13361341
F73D9E242970E54C008AF40D /* MapSettingsTests.swift */,
1342+
F75324C129833BBB005C509F /* MalrinMapTests.swift */,
13371343
);
13381344
path = Map;
13391345
sourceTree = "<group>";
@@ -1951,6 +1957,7 @@
19511957
F75324B629806329005C509F /* DFRSDetailViewTests.swift in Sources */,
19521958
F72EAABC291C138000AC6026 /* PortDataTests.swift in Sources */,
19531959
F72D890E2979F48E00D022FB /* NavigationalWarningDetailTests.swift in Sources */,
1960+
F75324C229833BBB005C509F /* MalrinMapTests.swift in Sources */,
19541961
F72EAAC1291C274100AC6026 /* DifferentialGPSStationDataTests.swift in Sources */,
19551962
F72D88FC29785CE100D022FB /* LightSummaryViewTests.swift in Sources */,
19561963
F72D89042979C30500D022FB /* PortSummaryViewTests.swift in Sources */,
@@ -1959,6 +1966,7 @@
19591966
F72EAACB291C635100AC6026 /* NavigationalWarningDataTests.swift in Sources */,
19601967
F75F81E729623AAA0062A708 /* MarlinBottomSheetTests.swift in Sources */,
19611968
F7D8FA7F294240B400E7D0A1 /* NoticeToMarinersFullNoticeViewModelTests.swift in Sources */,
1969+
F75324C02983272A005C509F /* MarlinCompactWidthViewTests.swift in Sources */,
19621970
F72D88FA297211BC00D022FB /* FilterButtonTests.swift in Sources */,
19631971
F71DF482284E783600686951 /* TestHelper.swift in Sources */,
19641972
F72EAAC6291C334200AC6026 /* ElectronicPublicationDataTests.swift in Sources */,

Marlin/Marlin/Map/MarlinMap.swift

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,20 @@ class MapState: ObservableObject {
3838
@Published var showDFRS: Bool?
3939

4040
@Published var searchResults: [MKMapItem]?
41-
}
42-
43-
struct MarlinMap: UIViewRepresentable {
44-
@Environment(\.colorScheme) var colorScheme
4541

4642
@AppStorage("mapType") var mapType: Int = Int(MKMapType.standard.rawValue)
4743
@AppStorage("showGARS") var showGARS: Bool = false
4844
@AppStorage("showMGRS") var showMGRS: Bool = false
4945
@AppStorage("showMapScale") var showMapScale = false
50-
46+
}
47+
48+
struct MarlinMap: UIViewRepresentable {
5149
@ObservedObject var mapState: MapState
5250

5351
var mixins: [MapMixin]?
5452
var name: String
5553

56-
init(name: String, mixins: [MapMixin]? = [], mapState: MapState? = nil, annotationToShrink: EnlargableAnnotation? = nil, focusedAnnotation: AnnotationWithView? = nil) {
54+
init(name: String, mixins: [MapMixin]? = [], mapState: MapState? = nil) {
5755
self.name = name
5856
if let mapState = mapState {
5957
self.mapState = mapState
@@ -86,12 +84,9 @@ struct MarlinMap: UIViewRepresentable {
8684
mapView.isPitchEnabled = false
8785
mapView.showsCompass = false
8886
mapView.tintColor = UIColor(Color.primaryColorVariant)
87+
mapView.isAccessibilityElement = true
88+
mapView.accessibilityLabel = name
8989

90-
if showMapScale {
91-
let scale = MKScaleView(mapView: mapView)
92-
scale.scaleVisibility = .visible // always visible
93-
mapView.addSubview(scale)
94-
}
9590
context.coordinator.mapView = mapView
9691

9792
mapView.register(EnlargedAnnotationView.self, forAnnotationViewWithReuseIdentifier: EnlargedAnnotationView.ReuseID)
@@ -108,6 +103,27 @@ struct MarlinMap: UIViewRepresentable {
108103
func updateUIView(_ mapView: MKMapView, context: Context) {
109104
print("Update ui view")
110105
context.coordinator.mapView = mapView
106+
107+
let scale = context.coordinator.mapScale ?? mapView.subviews.first { view in
108+
return (view as? MKScaleView) != nil
109+
}
110+
111+
if mapState.showMapScale {
112+
if scale == nil {
113+
let scale = MKScaleView(mapView: mapView)
114+
scale.scaleVisibility = .visible // always visible
115+
scale.isAccessibilityElement = true
116+
scale.accessibilityLabel = "Map Scale"
117+
mapView.addSubview(scale)
118+
context.coordinator.mapScale = scale
119+
} else if let scale = scale {
120+
mapView.addSubview(scale)
121+
}
122+
} else if let scale = scale {
123+
scale.removeFromSuperview()
124+
}
125+
126+
// TODO: I think this is not used anymore
111127
context.coordinator.updateDataSources(fetchRequests: mapState.fetchRequests)
112128

113129
if let center = mapState.center, center.center.latitude != context.coordinator.setCenter?.latitude, center.center.longitude != context.coordinator.setCenter?.longitude {
@@ -147,22 +163,22 @@ struct MarlinMap: UIViewRepresentable {
147163
mapView.removeOverlays(overlaysToRemove)
148164
mapView.addOverlays(overlaysToAdd)
149165

150-
if mapType == ExtraMapTypes.osm.rawValue {
166+
if mapState.mapType == ExtraMapTypes.osm.rawValue {
151167
if context.coordinator.osmOverlay == nil {
152168
context.coordinator.osmOverlay = MKTileOverlay(urlTemplate: "https://osm.gs.mil/tiles/default/{z}/{x}/{y}.png")
153169
context.coordinator.osmOverlay?.tileSize = CGSize(width: 512, height: 512)
154170
context.coordinator.osmOverlay?.canReplaceMapContent = true
155171
}
156172
mapView.removeOverlay(context.coordinator.osmOverlay!)
157173
mapView.insertOverlay(context.coordinator.osmOverlay!, at: 0, level: .aboveRoads)
158-
} else if let mkmapType = MKMapType(rawValue: UInt(mapType)) {
174+
} else if let mkmapType = MKMapType(rawValue: UInt(mapState.mapType)) {
159175
mapView.mapType = mkmapType
160176
if let osmOverlay = context.coordinator.osmOverlay {
161177
mapView.removeOverlay(osmOverlay)
162178
}
163179
}
164180

165-
if showGARS {
181+
if mapState.showGARS {
166182
if context.coordinator.garsOverlay == nil {
167183
context.coordinator.garsOverlay = GARSTileOverlay(512, 512)
168184
}
@@ -173,7 +189,7 @@ struct MarlinMap: UIViewRepresentable {
173189
}
174190
}
175191

176-
if showMGRS {
192+
if mapState.showMGRS {
177193
if context.coordinator.mgrsOverlay == nil {
178194
context.coordinator.mgrsOverlay = MGRSTileOverlay(512, 512)
179195
}
@@ -185,16 +201,6 @@ struct MarlinMap: UIViewRepresentable {
185201
}
186202
}
187203

188-
func MKMapRectForCoordinateRegion(region:MKCoordinateRegion) -> MKMapRect {
189-
let topLeft = CLLocationCoordinate2D(latitude: region.center.latitude + (region.span.latitudeDelta/2), longitude: region.center.longitude - (region.span.longitudeDelta/2))
190-
let bottomRight = CLLocationCoordinate2D(latitude: region.center.latitude - (region.span.latitudeDelta/2), longitude: region.center.longitude + (region.span.longitudeDelta/2))
191-
192-
let a = MKMapPoint(topLeft)
193-
let b = MKMapPoint(bottomRight)
194-
195-
return MKMapRect(origin: MKMapPoint(x:min(a.x,b.x), y:min(a.y,b.y)), size: MKMapSize(width: abs(a.x-b.x), height: abs(a.y-b.y)))
196-
}
197-
198204
func makeCoordinator() -> MarlinMapCoordinator {
199205
return MarlinMapCoordinator(self)
200206
}
@@ -208,13 +214,14 @@ class MarlinMapCoordinator: NSObject, MKMapViewDelegate, UIGestureRecognizerDele
208214
var mgrsOverlay: MGRSTileOverlay?
209215

210216
var mapView: MKMapView?
217+
var mapScale: MKScaleView?
211218
var marlinMap: MarlinMap
212219
var focusedAnnotation: EnlargableAnnotation?
213220
var focusMapOnItemSink: AnyCancellable?
214221

215222
var setCenter: CLLocationCoordinate2D?
216223
var trackingModeSet: MKUserTrackingMode?
217-
224+
// TODO: I think this is not used anymore
218225
var fetchedResultsControllers: [String : NSFetchedResultsController<NSFetchRequestResult>] = [:]
219226

220227
init(_ marlinMap: MarlinMap) {
@@ -228,7 +235,7 @@ class MarlinMapCoordinator: NSObject, MKMapViewDelegate, UIGestureRecognizerDele
228235
self?.focusItem(notification:$0)
229236
})
230237
}
231-
238+
// TODO: I think this is not used anymore
232239
func updateDataSources(fetchRequests: [String : NSFetchRequest<NSFetchRequestResult>]) {
233240
for (key, fetchRequest) in fetchRequests {
234241
if let controller = fetchedResultsControllers[key] {
@@ -246,7 +253,7 @@ class MarlinMapCoordinator: NSObject, MKMapViewDelegate, UIGestureRecognizerDele
246253
}
247254
}
248255
}
249-
256+
// TODO: I think this is not used anymore
250257
func initiateFetchResultsController(fetchedResultsController: NSFetchedResultsController<NSFetchRequestResult>?) {
251258
fetchedResultsController?.delegate = self
252259
do {
@@ -264,12 +271,12 @@ class MarlinMapCoordinator: NSObject, MKMapViewDelegate, UIGestureRecognizerDele
264271
func addAnnotation(annotation: MKAnnotation) {
265272
mapView?.addAnnotation(annotation)
266273
}
267-
274+
// TODO: I think this is not used anymore
268275
func updateAnnotation(annotation: MKAnnotation) {
269276
mapView?.removeAnnotation(annotation)
270277
mapView?.addAnnotation(annotation)
271278
}
272-
279+
// TODO: I think this is not used anymore
273280
func deleteAnnotation(annotation: MKAnnotation) {
274281
var mapAnnotation: MKAnnotation?
275282
if let asam = annotation as? Asam {
@@ -308,6 +315,7 @@ class MarlinMapCoordinator: NSObject, MKMapViewDelegate, UIGestureRecognizerDele
308315
UIView.animate(withDuration: 0.5, delay: 0.0, options: .curveEaseInOut, animations: {
309316
focusedAnnotation.shrinkAnnotation()
310317
}) { complete in
318+
print("xxx remove annotation")
311319
self.mapView?.removeAnnotation(focusedAnnotation)
312320
}
313321
self.focusedAnnotation = nil
@@ -328,16 +336,6 @@ class MarlinMapCoordinator: NSObject, MKMapViewDelegate, UIGestureRecognizerDele
328336
mapView?.addAnnotation(ea)
329337
}
330338

331-
func MKMapRectForCoordinateRegion(region:MKCoordinateRegion) -> MKMapRect {
332-
let topLeft = CLLocationCoordinate2D(latitude: region.center.latitude + (region.span.latitudeDelta/2), longitude: region.center.longitude - (region.span.longitudeDelta/2))
333-
let bottomRight = CLLocationCoordinate2D(latitude: region.center.latitude - (region.span.latitudeDelta/2), longitude: region.center.longitude + (region.span.longitudeDelta/2))
334-
335-
let a = MKMapPoint(topLeft)
336-
let b = MKMapPoint(bottomRight)
337-
338-
return MKMapRect(origin: MKMapPoint(x:min(a.x,b.x), y:min(a.y,b.y)), size: MKMapSize(width: abs(a.x-b.x), height: abs(a.y-b.y)))
339-
}
340-
341339
@objc func singleTapGensture(tapGestureRecognizer: UITapGestureRecognizer) {
342340
guard let mapGesture = tapGestureRecognizer as? MapSingleTap, let mapView = mapGesture.mapView else {
343341
return
@@ -487,14 +485,14 @@ class MarlinMapCoordinator: NSObject, MKMapViewDelegate, UIGestureRecognizerDele
487485
}
488486

489487
}
490-
488+
// TODO: I think this is not used anymore
491489
extension MarlinMapCoordinator: NSFetchedResultsControllerDelegate {
492490
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
493491
guard let annotation = anObject as? MKAnnotation else {
494492
return
495493
}
496494
switch(type) {
497-
495+
498496
case .insert:
499497
self.addAnnotation(annotation: annotation)
500498
case .delete:

Marlin/Marlin/Settings/AboutView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ struct AboutView: View {
5959
Toggle(isOn: $showMapScale, label: {
6060
HStack {
6161
Image(systemName: "ruler.fill")
62-
Text("Show Map Scale (requires restart)")
62+
Text("Show Map Scale")
6363
.primary()
6464
}
6565
})

0 commit comments

Comments
 (0)