Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mapbox.services.android.navigation.testapp.example.ui

import android.Manifest
import android.app.Activity
import android.arch.lifecycle.ViewModelProviders
import android.content.Intent
import android.content.pm.PackageManager
Expand Down Expand Up @@ -37,9 +38,9 @@ import kotlinx.android.synthetic.main.activity_example.*

private const val ZERO_PADDING = 0
private const val BOTTOMSHEET_MULTIPLIER = 4
private const val CHANGE_SETTING_REQUEST_CODE = 1

class ExampleActivity : HistoryActivity(), ExampleView {

private var map: NavigationMapboxMap? = null
private val viewModel by lazy(mode = LazyThreadSafetyMode.NONE) {
ViewModelProviders.of(this).get(ExampleViewModel::class.java)
Expand Down Expand Up @@ -193,10 +194,6 @@ class ExampleActivity : HistoryActivity(), ExampleView {
locationFab.visibility = visibility
}

override fun updateDirectionsFabVisibility(visibility: Int) {
directionsFab.visibility = visibility
}

override fun updateNavigationFabVisibility(visibility: Int) {
navigationFab.visibility = visibility
}
Expand Down Expand Up @@ -242,7 +239,14 @@ class ExampleActivity : HistoryActivity(), ExampleView {
}

override fun showSettings() {
startActivity(Intent(this, NavigationSettingsActivity::class.java))
startActivityForResult(Intent(this, NavigationSettingsActivity::class.java), CHANGE_SETTING_REQUEST_CODE)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == CHANGE_SETTING_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
viewModel.updateProfile()
}
}

override fun adjustMapPaddingForNavigation() {
Expand Down Expand Up @@ -293,7 +297,6 @@ class ExampleActivity : HistoryActivity(), ExampleView {

settingsFab.setOnClickListener { presenter.onSettingsFabClick() }
locationFab.setOnClickListener { presenter.onLocationFabClick() }
directionsFab.setOnClickListener { presenter.onDirectionsFabClick() }
navigationFab.setOnClickListener { presenter.onNavigationFabClick() }
cancelFab.setOnClickListener { presenter.onCancelFabClick() }
attribution.setOnClickListener { presenter.onAttributionsClick(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import android.view.View.INVISIBLE
import android.view.View.VISIBLE
import com.mapbox.api.directions.v5.models.DirectionsRoute
import com.mapbox.api.geocoding.v5.models.CarmenFeature
import com.mapbox.api.geocoding.v5.models.GeocodingResponse
import com.mapbox.geojson.Point
import com.mapbox.mapboxsdk.camera.CameraPosition
import com.mapbox.mapboxsdk.camera.CameraUpdate
Expand All @@ -35,7 +34,7 @@ private const val ONE_SECOND = 1000

class ExamplePresenter(private val view: ExampleView, private val viewModel: ExampleViewModel) {

private var state: PresenterState = PresenterState.SHOW_LOCATION
private var presenterState: PresenterState = PresenterState.SHOW_LOCATION

fun onPermissionsGranted(granted: Boolean) {
if (granted) {
Expand All @@ -56,8 +55,6 @@ class ExamplePresenter(private val view: ExampleView, private val viewModel: Exa

fun onAutocompleteClick() {
view.selectAllAutocompleteText()
view.updateLocationFabVisibility(INVISIBLE)
view.updateSettingsFabVisibility(INVISIBLE)
view.updateAutocompleteBottomSheetState(BottomSheetBehavior.STATE_EXPANDED)
}

Expand All @@ -75,14 +72,9 @@ class ExamplePresenter(private val view: ExampleView, private val viewModel: Exa
}
}

fun onDirectionsFabClick() {
state = PresenterState.FIND_ROUTE
viewModel.findRouteToDestination()
}

fun onNavigationFabClick() {
if (viewModel.canNavigate()) {
state = PresenterState.NAVIGATE
presenterState = PresenterState.NAVIGATE
view.showAlternativeRoutes(false)
view.addMapProgressChangeListener(viewModel.retrieveNavigation())
view.updateNavigationFabVisibility(INVISIBLE)
Expand All @@ -97,59 +89,49 @@ class ExamplePresenter(private val view: ExampleView, private val viewModel: Exa
}

fun onCancelFabClick() {
state = PresenterState.SHOW_LOCATION
clearView()
}

private fun clearView() {
presenterState = PresenterState.SHOW_LOCATION
viewModel.stopNavigation()
view.removeRoute()
view.clearMarkers()
view.resetMapPadding()
view.updateLocationRenderMode(RenderMode.NORMAL)
view.updateCameraTrackingMode(NavigationCamera.NAVIGATION_TRACKING_MODE_NONE)
view.updateLocationFabVisibility(VISIBLE)
view.updateNavigationFabVisibility(INVISIBLE)
view.updateSettingsFabVisibility(VISIBLE)
view.updateCancelFabVisibility(INVISIBLE)
view.updateInstructionViewVisibility(INVISIBLE)
view.updateAutocompleteBottomSheetState(BottomSheetBehavior.STATE_COLLAPSED)
}

fun onAutocompleteBottomSheetStateChange(state: Int) {
when (state) {
fun onAutocompleteBottomSheetStateChange(newState: Int) {
when (newState) {
BottomSheetBehavior.STATE_COLLAPSED -> {
viewModel.collapsedBottomSheet = true
view.hideSoftKeyboard()
if (this.state == PresenterState.SHOW_LOCATION) {
view.updateLocationFabVisibility(VISIBLE)
view.updateSettingsFabVisibility(VISIBLE)
}
presenterState = PresenterState.SHOW_LOCATION
}
BottomSheetBehavior.STATE_EXPANDED -> {
viewModel.collapsedBottomSheet = false
presenterState = PresenterState.SEARCH
}
}
}

fun onDestinationFound(feature: CarmenFeature) {
feature.center()?.let {
if (state == PresenterState.ROUTE_FOUND) {
view.removeRoute()
viewModel.primaryRoute = null
view.updateNavigationFabVisibility(INVISIBLE)
}
state = PresenterState.SELECTED_DESTINATION
viewModel.destination.value = it
view.clearMarkers()
view.hideSoftKeyboard()
view.updateAutocompleteBottomSheetState(BottomSheetBehavior.STATE_COLLAPSED)
view.updateDestinationMarker(it)
view.updateMapCamera(buildCameraUpdateFrom(it), TWO_SECONDS)
view.updateLocationFabVisibility(INVISIBLE)
view.updateSettingsFabVisibility(INVISIBLE)
view.updateDirectionsFabVisibility(VISIBLE)
viewModel.findRouteToDestination()
}
}

fun onLocationUpdate(location: Location?) {
location?.let {
if (state == PresenterState.SHOW_LOCATION) {
if (presenterState == PresenterState.SHOW_LOCATION) {
view.updateMapCamera(buildCameraUpdateFrom(location), TWO_SECONDS)
}
view.updateAutocompleteProximity(location)
Expand All @@ -159,25 +141,20 @@ class ExamplePresenter(private val view: ExampleView, private val viewModel: Exa

fun onRouteFound(routes: List<DirectionsRoute>?) {
routes?.let { directionsRoutes ->
when (state) {
PresenterState.FIND_ROUTE -> {
state = PresenterState.ROUTE_FOUND
view.transition()
view.showAlternativeRoutes(true)
view.updateRoutes(directionsRoutes)
view.updateDirectionsFabVisibility(INVISIBLE)
view.updateNavigationFabVisibility(VISIBLE)
viewModel.destination.value?.let { destination ->
moveCameraToInclude(destination)
}
}
PresenterState.NAVIGATE -> {
view.updateRoutes(directionsRoutes)
}
else -> {
// TODO no impl
if (presenterState != PresenterState.NAVIGATE) {
view.clearMarkers()
view.transition()
view.showAlternativeRoutes(true)
view.updateLocationFabVisibility(INVISIBLE)
view.updateSettingsFabVisibility(INVISIBLE)
view.updateNavigationFabVisibility(VISIBLE)
viewModel.destination.value?.let { destination ->
view.updateDestinationMarker(destination)
moveCameraToInclude(destination)
}
presenterState = PresenterState.SHOW_ROUTE
}
view.updateRoutes(directionsRoutes)
}
}

Expand All @@ -198,24 +175,36 @@ class ExamplePresenter(private val view: ExampleView, private val viewModel: Exa
}

fun onMapLongClick(point: LatLng): Boolean {
viewModel.reverseGeocode(point)
viewModel.destination.value = Point.fromLngLat(point.longitude, point.latitude)
viewModel.findRouteToDestination()
return true
}

fun onBackPressed(): Boolean {
if (!viewModel.collapsedBottomSheet) {
view.updateAutocompleteBottomSheetState(BottomSheetBehavior.STATE_COLLAPSED)
return false
when (presenterState) {
PresenterState.SEARCH -> {
view.updateAutocompleteBottomSheetState(BottomSheetBehavior.STATE_COLLAPSED)
return false
}
PresenterState.SHOW_ROUTE -> {
clearView()
return false
}

PresenterState.NAVIGATE -> {
clearView()
return false
}

else -> return true
}
return true
}

fun subscribe(owner: LifecycleOwner) {
viewModel.location.observe(owner, Observer { onLocationUpdate(it) })
viewModel.routes.observe(owner, Observer { onRouteFound(it) })
viewModel.progress.observe(owner, Observer { onProgressUpdate(it) })
viewModel.milestone.observe(owner, Observer { onMilestoneUpdate(it) })
viewModel.geocode.observe(owner, Observer { onGeocodingResponse(it) })
viewModel.activateLocationEngine()
}

Expand Down Expand Up @@ -258,14 +247,4 @@ class ExamplePresenter(private val view: ExampleView, private val viewModel: Exa
view.updateMapCameraFor(bounds, padding, TWO_SECONDS)
}
}

private fun onGeocodingResponse(it: GeocodingResponse?) {
val features = it?.features()
val isValidFeatureList = features?.isNotEmpty() ?: false
if (isValidFeatureList) {
features?.first()?.let { firstFeature ->
onDestinationFound(firstFeature)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ interface ExampleView: PermissionsListener, OnMapReadyCallback,

fun updateLocationFabVisibility(visibility: Int)

fun updateDirectionsFabVisibility(visibility: Int)

fun updateNavigationFabVisibility(visibility: Int)

fun updateCancelFabVisibility(visibility: Int)
Expand Down
Loading