Skip to content

Commit

Permalink
refactor: carplay view init refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
illuminati1911 committed Nov 14, 2024
1 parent f018a1b commit 8add516
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 4 additions & 2 deletions ios/Classes/BaseCarSceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -140,7 +143,6 @@ open class BaseCarSceneDelegate: UIResponder, CPTemplateApplicationSceneDelegate
}

func sendAutoScreenAvailabilityChangedEvent(isAvailable: Bool) {
print("lol: \(isAvailable)")
autoViewEventApi?.onAutoScreenAvailabilityChanged(isAvailable: isAvailable) { _ in }
}
}
51 changes: 19 additions & 32 deletions ios/Classes/GoogleMapsNavigationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
imageRegistry: ImageRegistry,
isCarPlayView: Bool) {

super.init()

_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)
Expand All @@ -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
}
Expand All @@ -127,6 +104,16 @@ class GoogleMapsNavigationView: NSObject, FlutterPlatformView, ViewSettledDelega
_mapView.delegate = nil
}

func registerView() {
if _isCarPlayView {
_viewRegistry.registerCarPlayView(view: self)
} else {
if let _viewId {
_viewRegistry.registerView(viewId: _viewId, view: self)
}
}
}

func unregisterView() {
if _isCarPlayView {
_viewRegistry.unregisterCarPlayView()
Expand Down
3 changes: 2 additions & 1 deletion ios/Classes/GoogleMapsNavigationViewFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ class GoogleMapsNavigationViewFactory: NSObject, FlutterPlatformViewFactory {
.convertNavigationUIEnabledPreference(preference: params.navigationViewOptions?
.navigationUIEnabledPreference),
mapConfiguration: mapConfiguration,
imageRegistry: imageRegistry
imageRegistry: imageRegistry,
isCarPlayView: false
)
}
}

0 comments on commit 8add516

Please sign in to comment.