Skip to content

Commit

Permalink
fix(HomeMapView): Improve map layer backgrounding behavior (#464)
Browse files Browse the repository at this point in the history
* chore: bump mapbox version

* debug logs

* fix(HomeMapView): reload images when missing

* feat(HomeMapView): persist layer unless colorScheme changes

* chore: revert staging backend env change

* chore(Podfile.lock): undo sentry conflict

* cleanup: backout stray changes

* fix: add missing Package.resolved changes

* Revert "fix: add missing Package.resolved changes"

This reverts commit 79e8195.

* revert mapbox upgrade: issues with location permission popup
  • Loading branch information
KaylaBrady authored Oct 15, 2024
1 parent 14bce93 commit f3a677a
Show file tree
Hide file tree
Showing 23 changed files with 45 additions and 28 deletions.
2 changes: 1 addition & 1 deletion iosApp/iosApp/ContentView.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import CoreLocation
@_spi(Experimental) import MapboxMaps
import shared
import SwiftPhoenixClient
import SwiftUI
@_spi(Experimental) import MapboxMaps

struct ContentView: View {
@Environment(\.scenePhase) private var scenePhase
Expand Down
2 changes: 1 addition & 1 deletion iosApp/iosApp/ContentViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
//

import Foundation
import shared
@_spi(Experimental) import MapboxMaps
import shared

class ContentViewModel: ObservableObject {
@Published var configResponse: ApiResult<ConfigResponse>?
Expand Down
3 changes: 1 addition & 2 deletions iosApp/iosApp/Fetchers/ViewportProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
//

import Combine
import shared
import SwiftUI
@_spi(Experimental) import MapboxMaps
import shared
import SwiftUI

class ViewportProvider: ObservableObject {
enum Defaults {
Expand Down
15 changes: 13 additions & 2 deletions iosApp/iosApp/Pages/Map/AnnotatedMap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
// Copyright © 2024 MBTA. All rights reserved.
//

@_spi(Experimental) import MapboxMaps
import shared
import SwiftUI
@_spi(Experimental) import MapboxMaps

struct AnnotatedMap: View {
static let annotationTextZoomThreshold = 19.0
Expand All @@ -28,6 +28,7 @@ struct AnnotatedMap: View {

@ObservedObject var viewportProvider: ViewportProvider
@Environment(\.colorScheme) var colorScheme
@Environment(\.scenePhase) var scenePhase

var handleCameraChange: (CameraChanged) -> Void
var handleStyleLoaded: () -> Void
Expand All @@ -53,13 +54,18 @@ struct AnnotatedMap: View {
// The initial run of this happens before any required data is loaded, so it does nothing and
// handleTryLayerInit always performs the first layer creation, but once the data is in place,
// this handles any time the map is reloaded again, like for a light/dark mode switch.
handleStyleLoaded()
if scenePhase == .active {
// onStyleLoaded was unexpectedly called when app moved to background because the colorScheme
// changes twice while backgrounding. Ensure it is only called when the app is active.
handleStyleLoaded()
}
}
.additionalSafeAreaInsets(.bottom, sheetHeight)
.accessibilityIdentifier("transitMap")
.onReceive(viewportProvider.cameraStatePublisher) { newCameraState in
zoomLevel = newCameraState.zoom
}
.withScenePhaseHandlers(onActive: onActive)
.task {
do {
mapDebug = try await getSettingUsecase.execute(setting: .map).boolValue
Expand All @@ -69,6 +75,11 @@ struct AnnotatedMap: View {
}
}

func onActive() {
// re-load styles in case the colorScheme changed while in the background
handleStyleLoaded()
}

private var allVehicles: [Vehicle]? {
switch (vehicles, selectedVehicle) {
case (.none, .none): nil
Expand Down
2 changes: 1 addition & 1 deletion iosApp/iosApp/Pages/Map/AnnotationLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
// Copyright © 2024 MBTA. All rights reserved.
//

import SwiftUI
@_spi(Experimental) import MapboxMaps
import SwiftUI

extension View {
func annotationLabel<Content: View>(_ content: Content) -> ModifiedContent<Self, FloatingLabel<Content>> {
Expand Down
2 changes: 1 addition & 1 deletion iosApp/iosApp/Pages/Map/HomeMapView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
// Copyright © 2024 MBTA. All rights reserved.
//

@_spi(Experimental) import MapboxMaps
import os
import shared
import SwiftUI
@_spi(Experimental) import MapboxMaps

struct HomeMapView: View {
var analytics: NearbyTransitAnalytics = AnalyticsProvider.shared
Expand Down
2 changes: 1 addition & 1 deletion iosApp/iosApp/Pages/Map/HomeMapViewHandlerExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
// Copyright © 2024 MBTA. All rights reserved.
//

@_spi(Experimental) import MapboxMaps
import shared
import SwiftUI
@_spi(Experimental) import MapboxMaps

/*
Functions for handling interactions with the map, like prop change, navigation, and tapping.
Expand Down
4 changes: 2 additions & 2 deletions iosApp/iosApp/Pages/Map/HomeMapViewLayerExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
// Copyright © 2024 MBTA. All rights reserved.
//

@_spi(Experimental) import MapboxMaps
import shared
import SwiftUI
@_spi(Experimental) import MapboxMaps

/*
Functions for manipulating the layers displayed on the map.
Expand Down Expand Up @@ -60,13 +60,13 @@ extension HomeMapView {

func refreshMap() {
if let layerManager = mapVM.layerManager {
updateGlobalMapDataSources()
if layerManager.currentScheme != colorScheme {
layerManager.addIcons(recreate: true)
addLayers(layerManager, recreate: true)
} else {
addLayers(layerManager)
}
updateGlobalMapDataSources()
}
}

Expand Down
13 changes: 10 additions & 3 deletions iosApp/iosApp/Pages/Map/MapLayerManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
// Copyright © 2024 MBTA. All rights reserved.
//

@_spi(Experimental) import MapboxMaps
import os
import shared
import SwiftUI
@_spi(Experimental) import MapboxMaps

protocol IMapLayerManager {
var currentScheme: ColorScheme? { get }
Expand Down Expand Up @@ -56,6 +56,13 @@ class MapLayerManager: IMapLayerManager {
}
}

/*
Adds persistent layers so that they are persisted even if the underlying map style changes. To intentionally
re-create the layers due to a change that corresponds with a style change (such as colorScheme changing),
set recreate to true.
https://docs.mapbox.com/ios/maps/api/11.5.0/documentation/mapboxmaps/stylemanager/addpersistentlayer(_:layerposition:)
*/
func addLayers(colorScheme: ColorScheme, recreate: Bool = false) {
let colorPalette = getColorPalette(colorScheme: colorScheme)
currentScheme = colorScheme
Expand All @@ -76,9 +83,9 @@ class MapLayerManager: IMapLayerManager {
}

if map.layerExists(withId: "puck") {
try map.addLayer(layer, layerPosition: .below("puck"))
try map.addPersistentLayer(layer, layerPosition: .below("puck"))
} else {
try map.addLayer(layer)
try map.addPersistentLayer(layer)
}
} catch {
Logger().error("Failed to add layer \(layer.id)\n\(error)")
Expand Down
2 changes: 1 addition & 1 deletion iosApp/iosApp/Pages/Map/MapViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

import Combine
import Foundation
import shared
@_spi(Experimental) import MapboxMaps
import shared

class MapViewModel: ObservableObject {
@Published var selectedVehicle: Vehicle?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

import Combine
import CoreLocation
@_spi(Experimental) import MapboxMaps
import os
import shared
import SwiftUI
@_spi(Experimental) import MapboxMaps

struct NearbyTransitPageView: View {
@ObservedObject var nearbyVM: NearbyViewModel
Expand Down
2 changes: 1 addition & 1 deletion iosApp/iosApp/Pages/NearbyTransit/NearbyTransitView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import Combine
import CoreLocation
import FirebaseAnalytics
@_spi(Experimental) import MapboxMaps
import os
import shared
import SwiftUI
@_spi(Experimental) import MapboxMaps

struct NearbyTransitView: View {
var analytics: NearbyTransitAnalytics = AnalyticsProvider.shared
Expand Down
2 changes: 1 addition & 1 deletion iosApp/iosApp/Pages/Search/SearchViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
// Copyright © 2024 MBTA. All rights reserved.
//

import shared
@_spi(Experimental) import MapboxMaps
import shared

class SearchViewModel: ObservableObject {
@Published var routeResultsEnabled: Bool
Expand Down
2 changes: 1 addition & 1 deletion iosApp/iosApp/Utils/AppVariantExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Copyright © 2024 MBTA. All rights reserved.
//

import MapboxMaps
@_spi(Experimental) import MapboxMaps
import shared
import SwiftUI

Expand Down
2 changes: 1 addition & 1 deletion iosApp/iosApp/Utils/MapboxBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Copyright © 2024 MBTA. All rights reserved.
//

import MapboxMaps
@_spi(Experimental) import MapboxMaps
import shared

private func bridgeStyleObject<T: Decodable>(_ object: MapboxStyleObject) -> T {
Expand Down
2 changes: 1 addition & 1 deletion iosApp/iosAppTests/Fetchers/ViewportProviderTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
//

@testable import iosApp
import shared
@_spi(Experimental) import MapboxMaps
import shared
import XCTest

final class ViewportProviderTest: XCTestCase {
Expand Down
2 changes: 1 addition & 1 deletion iosApp/iosAppTests/Pages/Map/HomeMapViewTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
//

@testable import iosApp
@_spi(Experimental) import MapboxMaps
import shared
import SwiftPhoenixClient
import SwiftUI
import ViewInspector
import XCTest
@_spi(Experimental) import MapboxMaps

final class HomeMapViewTest: XCTestCase {
override func setUp() {
Expand Down
2 changes: 1 addition & 1 deletion iosApp/iosAppTests/Pages/Map/MapHttpInterceptorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
//

@testable import iosApp
import XCTest
@_spi(Experimental) import MapboxMaps
import XCTest

final class MapHttpInterceptorTests: XCTestCase {
override func setUp() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
import Combine
import CoreLocation
@testable import iosApp
@_spi(Experimental) import MapboxMaps
import shared
import SwiftPhoenixClient
import SwiftUI
import ViewInspector
import XCTest
@_spi(Experimental) import MapboxMaps

// swiftlint:disable:next type_body_length
final class NearbyTransitPageViewTests: XCTestCase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

import Combine
@testable import iosApp
@_spi(Experimental) import MapboxMaps
import shared
import SwiftPhoenixClient
import SwiftUI
import ViewInspector
import XCTest
@_spi(Experimental) import MapboxMaps

final class StopDetailsPageTests: XCTestCase {
override func setUp() {
Expand Down
2 changes: 1 addition & 1 deletion iosApp/iosAppTests/Views/ContentViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
import Combine
import Foundation
@testable import iosApp
@_spi(Experimental) import MapboxMaps
import shared
import SwiftPhoenixClient
import SwiftUI
import ViewInspector
import XCTest
@_spi(Experimental) import MapboxMaps

final class ContentViewTests: XCTestCase {
override func setUp() {
Expand Down
2 changes: 1 addition & 1 deletion iosApp/iosAppTests/Views/HomeMapViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

import Combine
@testable import iosApp
import ViewInspector
@_spi(Experimental) import MapboxMaps
import shared
import SwiftUI
import ViewInspector
import XCTest

// swiftlint:disable:next type_body_length
Expand Down
2 changes: 1 addition & 1 deletion iosApp/iosAppTests/Views/NearbyTransitViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
import Combine
import CoreLocation
@testable import iosApp
@_spi(Experimental) import MapboxMaps
import shared
import SwiftPhoenixClient
import SwiftUI
import ViewInspector
import XCTest
@_spi(Experimental) import MapboxMaps

// swiftlint:disable:next type_body_length
final class NearbyTransitViewTests: XCTestCase {
Expand Down

0 comments on commit f3a677a

Please sign in to comment.