|
1 | | - |
2 | 1 | package com.mapbox.navigation.examples.androidauto.app |
3 | 2 |
|
4 | 3 | import android.os.Bundle |
5 | | -import androidx.appcompat.app.AppCompatActivity |
| 4 | +import android.view.View |
| 5 | +import androidx.appcompat.widget.AppCompatImageView |
| 6 | +import androidx.lifecycle.lifecycleScope |
| 7 | +import com.mapbox.api.directions.v5.models.BannerInstructions |
| 8 | +import com.mapbox.common.LogConfiguration |
| 9 | +import com.mapbox.common.LoggingLevel |
| 10 | +import com.mapbox.navigation.base.ExperimentalPreviewMapboxNavigationAPI |
| 11 | +import com.mapbox.navigation.core.MapboxNavigation |
| 12 | +import com.mapbox.navigation.core.internal.extensions.flowRoutesUpdated |
| 13 | +import com.mapbox.navigation.core.lifecycle.MapboxNavigationApp |
| 14 | +import com.mapbox.navigation.core.trip.session.BannerInstructionsObserver |
6 | 15 | import com.mapbox.navigation.examples.androidauto.CarAppSyncComponent |
7 | | -import com.mapbox.navigation.examples.androidauto.databinding.MapboxActivityNavigationViewBinding |
| 16 | +import com.mapbox.navigation.examples.androidauto.databinding.ActivityMainBinding |
| 17 | +import com.mapbox.navigation.examples.androidauto.databinding.LayoutDrawerMenuNavViewBinding |
| 18 | +import com.mapbox.navigation.examples.androidauto.utils.NavigationViewController |
| 19 | +import com.mapbox.navigation.examples.androidauto.utils.TestRoutes |
| 20 | +import com.mapbox.navigation.ui.base.installer.installComponents |
| 21 | +import com.mapbox.navigation.ui.base.lifecycle.UIComponent |
| 22 | +import com.mapbox.navigation.ui.maps.guidance.junction.api.MapboxJunctionApi |
| 23 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 24 | +import kotlinx.coroutines.channels.awaitClose |
| 25 | +import kotlinx.coroutines.flow.Flow |
| 26 | +import kotlinx.coroutines.flow.callbackFlow |
| 27 | +import kotlinx.coroutines.launch |
| 28 | + |
| 29 | +class MainActivity : DrawerActivity() { |
| 30 | + |
| 31 | + private lateinit var binding: ActivityMainBinding |
| 32 | + private lateinit var menuBinding: LayoutDrawerMenuNavViewBinding |
8 | 33 |
|
9 | | -class MainActivity : AppCompatActivity() { |
10 | | - private lateinit var binding: MapboxActivityNavigationViewBinding |
| 34 | + override fun onCreateContentView(): View { |
| 35 | + binding = ActivityMainBinding.inflate(layoutInflater) |
| 36 | + CarAppSyncComponent.getInstance().attachNavigationView(binding.navigationView) |
| 37 | + return binding.root |
| 38 | + } |
11 | 39 |
|
| 40 | + override fun onCreateMenuView(): View { |
| 41 | + menuBinding = LayoutDrawerMenuNavViewBinding.inflate(layoutInflater) |
| 42 | + return menuBinding.root |
| 43 | + } |
| 44 | + |
| 45 | + private lateinit var controller: NavigationViewController |
| 46 | + |
| 47 | + @OptIn(ExperimentalPreviewMapboxNavigationAPI::class) |
12 | 48 | override fun onCreate(savedInstanceState: Bundle?) { |
13 | 49 | super.onCreate(savedInstanceState) |
14 | | - binding = MapboxActivityNavigationViewBinding.inflate(layoutInflater) |
15 | | - setContentView(binding.root) |
16 | 50 |
|
17 | | - // TODO going to expose a public api to share a replay controller |
18 | | - // This allows to simulate your location |
19 | | -// binding.navigationView.api.routeReplayEnabled(true) |
| 51 | + LogConfiguration.setLoggingLevel("nav-sdk", LoggingLevel.DEBUG) |
20 | 52 |
|
21 | | - CarAppSyncComponent.getInstance().attachNavigationView(binding.navigationView) |
| 53 | + controller = NavigationViewController(this, binding.navigationView) |
| 54 | + |
| 55 | + menuBinding.toggleReplay.isChecked = binding.navigationView.api.isReplayEnabled() |
| 56 | + menuBinding.toggleReplay.setOnCheckedChangeListener { _, isChecked -> |
| 57 | + binding.navigationView.api.routeReplayEnabled(isChecked) |
| 58 | + } |
| 59 | + |
| 60 | + menuBinding.junctionViewTestButton.setOnClickListener { |
| 61 | + lifecycleScope.launch { |
| 62 | + val (origin, destination) = TestRoutes.valueOf( |
| 63 | + menuBinding.spinnerTestRoute.selectedItem as String |
| 64 | + ) |
| 65 | + controller.startActiveGuidance(origin, destination) |
| 66 | + closeDrawers() |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + MapboxNavigationApp.installComponents(this) { |
| 71 | + component(Junctions(binding.junctionImageView)) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Simple component for detecting and rendering Junction Views. |
| 77 | + */ |
| 78 | + private class Junctions( |
| 79 | + private val imageView: AppCompatImageView |
| 80 | + ) : UIComponent() { |
| 81 | + private var junctionApi: MapboxJunctionApi? = null |
| 82 | + |
| 83 | + override fun onAttached(mapboxNavigation: MapboxNavigation) { |
| 84 | + super.onAttached(mapboxNavigation) |
| 85 | + val token = mapboxNavigation.navigationOptions.accessToken!! |
| 86 | + junctionApi = MapboxJunctionApi(token) |
| 87 | + |
| 88 | + mapboxNavigation.flowBannerInstructions().observe { instructions -> |
| 89 | + junctionApi?.generateJunction(instructions) { result -> |
| 90 | + result.fold( |
| 91 | + { imageView.setImageBitmap(null) }, |
| 92 | + { imageView.setImageBitmap(it.bitmap) } |
| 93 | + ) |
| 94 | + } |
| 95 | + } |
| 96 | + mapboxNavigation.flowRoutesUpdated().observe { |
| 97 | + if (it.navigationRoutes.isEmpty()) { |
| 98 | + imageView.setImageBitmap(null) |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + override fun onDetached(mapboxNavigation: MapboxNavigation) { |
| 104 | + super.onDetached(mapboxNavigation) |
| 105 | + junctionApi?.cancelAll() |
| 106 | + junctionApi = null |
| 107 | + } |
| 108 | + |
| 109 | + @OptIn(ExperimentalCoroutinesApi::class) |
| 110 | + private fun MapboxNavigation.flowBannerInstructions(): Flow<BannerInstructions> = |
| 111 | + callbackFlow { |
| 112 | + val observer = BannerInstructionsObserver { trySend(it) } |
| 113 | + registerBannerInstructionsObserver(observer) |
| 114 | + awaitClose { unregisterBannerInstructionsObserver(observer) } |
| 115 | + } |
22 | 116 | } |
23 | 117 | } |
0 commit comments