Skip to content

Commit

Permalink
Add cancel button to android
Browse files Browse the repository at this point in the history
  • Loading branch information
YoussefHenna committed Jul 30, 2024
1 parent 29e8d24 commit 72f67fd
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ExpoMapboxNavigationModule : Module() {
}

View(ExpoMapboxNavigationView::class) {
Events("onRouteProgressChanged")
Events("onRouteProgressChanged", "onCancelNavigation")

Prop("coordinates") { view: ExpoMapboxNavigationView, coordinates: List<Map<String, Any>> ->
val points = mutableListOf<Point>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class ExpoMapboxNavigationView(context: Context, appContext: AppContext) : ExpoV
private var isUsingRouteMatchingApi = false

private val onRouteProgressChanged by EventDispatcher()
private val onCancelNavigation by EventDispatcher()

private val mapboxNavigation = MapboxNavigationApp.current()
private var mapboxStyle: Style? = null
Expand Down Expand Up @@ -153,13 +154,19 @@ class ExpoMapboxNavigationView(context: Context, appContext: AppContext) : ExpoV
navigationCamera.requestNavigationCameraToFollowing()
}

private val cancelButtonId = 7
private val cancelButton = createCancelButton(cancelButtonId, parentConstraintLayout){
onCancelNavigation(mapOf())
}

private val parentConstraintSet = createAndApplyConstraintSet(
mapViewId=mapViewId,
maneuverViewId=maneuverViewId,
tripProgressViewId=tripProgressViewId,
soundButtonId=soundButtonId,
overviewButtonId=overviewButtonId,
recenterButtonId=recenterButtonId,
cancelButtonId=cancelButtonId,
constraintLayout=parentConstraintLayout
)

Expand Down Expand Up @@ -428,13 +435,25 @@ class ExpoMapboxNavigationView(context: Context, appContext: AppContext) : ExpoV
}
}

private fun createCancelButton(id: Int, parent: ViewGroup, onClick: (MapboxSoundButton) -> Unit): MapboxSoundButton {
return MapboxSoundButton(context).apply {
setId(id)
parent.addView(this)
findViewById<ImageView>(com.mapbox.navigation.ui.components.R.id.buttonIcon).setImageResource(R.drawable.icon_x)
setOnClickListener {
onClick(this)
}
}
}

private fun createAndApplyConstraintSet(
mapViewId: Int,
maneuverViewId: Int,
tripProgressViewId: Int,
soundButtonId: Int,
overviewButtonId: Int,
recenterButtonId: Int,
cancelButtonId: Int,
constraintLayout: ConstraintLayout
): ConstraintSet {
return ConstraintSet().apply{
Expand All @@ -450,7 +469,7 @@ class ExpoMapboxNavigationView(context: Context, appContext: AppContext) : ExpoV
connect(maneuverViewId, ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END, (4 * PIXEL_DENSITY).toInt())
constrainHeight(maneuverViewId, ConstraintSet.WRAP_CONTENT)

// Add TropProgressView constraints
// Add TripProgressView constraints
connect(tripProgressViewId, ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM)
connect(tripProgressViewId, ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START)
connect(tripProgressViewId, ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END)
Expand All @@ -476,6 +495,13 @@ class ExpoMapboxNavigationView(context: Context, appContext: AppContext) : ExpoV
constrainWidth(recenterButtonId, ConstraintSet.WRAP_CONTENT)
constrainHeight(recenterButtonId, ConstraintSet.WRAP_CONTENT)

// Add CancelButton constraints
connect(cancelButtonId, ConstraintSet.BOTTOM, tripProgressViewId, ConstraintSet.BOTTOM)
connect(cancelButtonId, ConstraintSet.TOP, tripProgressViewId, ConstraintSet.TOP)
connect(cancelButtonId, ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END, (16 * PIXEL_DENSITY).toInt())
constrainWidth(cancelButtonId, ConstraintSet.WRAP_CONTENT)
constrainHeight(cancelButtonId, ConstraintSet.WRAP_CONTENT)

applyTo(constraintLayout)
}
}
Expand Down
Binary file added android/src/main/res/drawable-hdpi/icon_x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added android/src/main/res/drawable-ldpi/icon_x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added android/src/main/res/drawable-mdpi/icon_x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added android/src/main/res/drawable-xhdpi/icon_x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added android/src/main/res/drawable-xxhdpi/icon_x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added android/src/main/res/drawable-xxxhdpi/icon_x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/ExpoMapboxNavigation.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ export type ExpoMapboxNavigationViewProps = {
useRouteMatchingApi?: boolean;
locale?: string;
onRouteProgressChanged?: (event: { nativeEvent: ProgressEvent }) => void;
onCancelNavigation?: () => void;
style?: StyleProp<ViewStyle>;
};

0 comments on commit 72f67fd

Please sign in to comment.