From 3192956b65e676ebdfa827e78a243f44dc865737 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20V=C3=A4limaa?= Date: Thu, 14 Nov 2024 12:20:18 +0800 Subject: [PATCH] refactor: carplay view init refactoring --- .../navigation/GoogleMapsAutoMapView.kt | 1 - ios/Classes/BaseCarSceneDelegate.swift | 6 ++- ios/Classes/GoogleMapsNavigationView.swift | 49 +++++++------------ .../GoogleMapsNavigationViewFactory.swift | 3 +- 4 files changed, 23 insertions(+), 36 deletions(-) diff --git a/android/src/main/kotlin/com/google/maps/flutter/navigation/GoogleMapsAutoMapView.kt b/android/src/main/kotlin/com/google/maps/flutter/navigation/GoogleMapsAutoMapView.kt index 5d4ee1d..7ca8e9c 100644 --- a/android/src/main/kotlin/com/google/maps/flutter/navigation/GoogleMapsAutoMapView.kt +++ b/android/src/main/kotlin/com/google/maps/flutter/navigation/GoogleMapsAutoMapView.kt @@ -20,7 +20,6 @@ import android.view.View import com.google.android.gms.maps.GoogleMap import com.google.android.gms.maps.GoogleMapOptions import com.google.android.libraries.navigation.NavigationViewForAuto -import io.flutter.plugin.platform.PlatformView class GoogleMapsAutoMapView internal constructor( diff --git a/ios/Classes/BaseCarSceneDelegate.swift b/ios/Classes/BaseCarSceneDelegate.swift index bbd953a..8c37654 100644 --- a/ios/Classes/BaseCarSceneDelegate.swift +++ b/ios/Classes/BaseCarSceneDelegate.swift @@ -72,8 +72,10 @@ open class BaseCarSceneDelegate: UIResponder, CPTemplateApplicationSceneDelegate self.autoViewEventApi = autoViewEventApi self.navView = GoogleMapsNavigationView( frame: templateApplicationScene.carWindow.screen.bounds, + viewIdentifier: nil, isNavigationView: true, viewRegistry: viewRegistry, + viewEventApi: nil, navigationUIEnabledPreference: NavigationUIEnabledPreference.automatic, mapConfiguration: MapConfiguration( cameraPosition: nil, @@ -85,7 +87,8 @@ open class BaseCarSceneDelegate: UIResponder, CPTemplateApplicationSceneDelegate zoomGesturesEnabled: true, scrollGesturesEnabledDuringRotateOrZoom: false ), - imageRegistry: imageRegistry + imageRegistry: imageRegistry, + isCarPlayView: true ) self.navView?.setNavigationHeaderEnabled(false) self.navView?.setRecenterButtonEnabled(false) @@ -140,7 +143,6 @@ open class BaseCarSceneDelegate: UIResponder, CPTemplateApplicationSceneDelegate } func sendAutoScreenAvailabilityChangedEvent(isAvailable: Bool) { - print("lol: \(isAvailable)") autoViewEventApi?.onAutoScreenAvailabilityChanged(isAvailable: isAvailable) { _ in } } } diff --git a/ios/Classes/GoogleMapsNavigationView.swift b/ios/Classes/GoogleMapsNavigationView.swift index 4a1dc8e..d207b0c 100644 --- a/ios/Classes/GoogleMapsNavigationView.swift +++ b/ios/Classes/GoogleMapsNavigationView.swift @@ -62,50 +62,27 @@ class GoogleMapsNavigationView: NSObject, FlutterPlatformView, ViewSettledDelega return nil } - // CarPlay initializer. This will ignore viewId and viewEventApi. init(frame: CGRect, + viewIdentifier viewId: Int64?, isNavigationView: Bool, viewRegistry registry: GoogleMapsNavigationViewRegistry, + viewEventApi: ViewEventApi?, navigationUIEnabledPreference: NavigationUIEnabledPreference, mapConfiguration: MapConfiguration, - imageRegistry: ImageRegistry) { - _isNavigationView = true - _viewRegistry = registry - _mapConfiguration = mapConfiguration - _imageRegistry = imageRegistry - _isCarPlayView = true - - let mapViewOptions = GMSMapViewOptions() - _mapConfiguration.apply(to: mapViewOptions, withFrame: frame) - _mapView = ViewStateAwareGMSMapView(options: mapViewOptions) - _mapConfiguration.apply(to: _mapView) - - super.init() + imageRegistry: ImageRegistry, + isCarPlayView: Bool) { - _navigationUIEnabledPreference = navigationUIEnabledPreference - applyNavigationUIEnabledPreference() - - registry.registerCarPlayView(view: self) - _mapView.delegate = self - _mapView.viewSettledDelegate = self - } + if (!isCarPlayView && (viewId == nil || viewEventApi == nil)) { + fatalError("For non-carplay map view viewId and viewEventApi is required") + } - // Regular NavigationView initializer. - init(frame: CGRect, - viewIdentifier viewId: Int64, - isNavigationView: Bool, - viewRegistry registry: GoogleMapsNavigationViewRegistry, - viewEventApi: ViewEventApi, - navigationUIEnabledPreference: NavigationUIEnabledPreference, - mapConfiguration: MapConfiguration, - imageRegistry: ImageRegistry) { _viewId = viewId _isNavigationView = isNavigationView _viewRegistry = registry _viewEventApi = viewEventApi _mapConfiguration = mapConfiguration _imageRegistry = imageRegistry - _isCarPlayView = false + _isCarPlayView = isCarPlayView let mapViewOptions = GMSMapViewOptions() _mapConfiguration.apply(to: mapViewOptions, withFrame: frame) @@ -117,7 +94,7 @@ class GoogleMapsNavigationView: NSObject, FlutterPlatformView, ViewSettledDelega _navigationUIEnabledPreference = navigationUIEnabledPreference applyNavigationUIEnabledPreference() - registry.registerView(viewId: viewId, view: self) + registerView() _mapView.delegate = self _mapView.viewSettledDelegate = self } @@ -127,6 +104,14 @@ class GoogleMapsNavigationView: NSObject, FlutterPlatformView, ViewSettledDelega _mapView.delegate = nil } + func registerView() { + if _isCarPlayView { + _viewRegistry.registerCarPlayView(view: self) + } else { + _viewRegistry.registerView(viewId: _viewId!, view: self) + } + } + func unregisterView() { if _isCarPlayView { _viewRegistry.unregisterCarPlayView() diff --git a/ios/Classes/GoogleMapsNavigationViewFactory.swift b/ios/Classes/GoogleMapsNavigationViewFactory.swift index 52cbc57..708fe56 100644 --- a/ios/Classes/GoogleMapsNavigationViewFactory.swift +++ b/ios/Classes/GoogleMapsNavigationViewFactory.swift @@ -54,7 +54,8 @@ class GoogleMapsNavigationViewFactory: NSObject, FlutterPlatformViewFactory { .convertNavigationUIEnabledPreference(preference: params.navigationViewOptions? .navigationUIEnabledPreference), mapConfiguration: mapConfiguration, - imageRegistry: imageRegistry + imageRegistry: imageRegistry, + isCarPlayView: false ) } }