Skip to content

Commit

Permalink
Added ability to hide buttons/alert views (#1251)
Browse files Browse the repository at this point in the history
  • Loading branch information
Devota Aabel authored Sep 24, 2018
1 parent d56e07d commit 2d92c9b
Show file tree
Hide file tree
Showing 13 changed files with 639 additions and 176 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.mapbox.services.android.navigation.ui.v5;

import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.support.design.widget.FloatingActionButton;
import android.util.AttributeSet;

public class FeedbackButton extends ConstraintLayout implements NavigationButton {
private FloatingActionButton feedbackFab;
private MultiOnClickListener multiOnClickListener;

public FeedbackButton(Context context) {
this(context, null);
}

public FeedbackButton(Context context, AttributeSet attrs) {
this(context, attrs, -1);
}

public FeedbackButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initialize(context);
}

/**
* Adds an onClickListener to the button
*
* @param onClickListener to add
*/
@Override
public void addOnClickListener(OnClickListener onClickListener) {
multiOnClickListener.addListener(onClickListener);
}

/**
* Removes an onClickListener from the button
*
* @param onClickListener to remove
*/
@Override
public void removeOnClickListener(OnClickListener onClickListener) {
multiOnClickListener.removeListener(onClickListener);
}

/**
* Hides the button
*/
@Override
public void hide() {
setVisibility(GONE);
}

/**
* Shows the button
*/
@Override
public void show() {
setVisibility(VISIBLE);
}

@Override
protected void onFinishInflate() {
super.onFinishInflate();
bind();
}

@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
setupOnClickListeners();
}

@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
clearListeners();
}


private void setupOnClickListeners() {
multiOnClickListener = new MultiOnClickListener();
feedbackFab.setOnClickListener(multiOnClickListener);
}

private void clearListeners() {
multiOnClickListener.clearListeners();
multiOnClickListener = null;
setOnClickListener(null);
}

private void initialize(Context context) {
inflate(context, R.layout.feedback_button_layout, this);
}

private void bind() {
feedbackFab = findViewById(R.id.feedbackFab);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.mapbox.services.android.navigation.ui.v5;

import android.view.View;

import java.util.HashSet;
import java.util.Set;

class MultiOnClickListener implements View.OnClickListener {
private Set<View.OnClickListener> onClickListeners;

MultiOnClickListener() {
this.onClickListeners = new HashSet<>();
}

void addListener(View.OnClickListener onClickListener) {
onClickListeners.add(onClickListener);
}

void removeListener(View.OnClickListener onClickListener) {
onClickListeners.remove(onClickListener);
}

void clearListeners() {
onClickListeners.clear();
}

@Override
public void onClick(View view) {
for (View.OnClickListener onClickListener : onClickListeners) {
onClickListener.onClick(view);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.mapbox.services.android.navigation.ui.v5;

import android.view.View;

public interface NavigationButton {

/**
* Adds an onClickListener to the button
*
* @param onClickListener to add
*/
void addOnClickListener(View.OnClickListener onClickListener);

/**
* Removes an onClickListener from the button
*
* @param onClickListener to remove
*/
void removeOnClickListener(View.OnClickListener onClickListener);

/**
* Hides the button
*/
void hide();

/**
* Shows the button
*/
void show();
}
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,33 @@ public MapboxNavigation retrieveMapboxNavigation() {
return navigationViewModel.retrieveNavigation();
}

/**
* Returns the sound button used for muting instructions
*
* @return sound button
*/
public NavigationButton retrieveSoundButton() {
return instructionView.retrieveSoundButton();
}

/**
* Returns the feedback button for sending feedback about navigation
*
* @return feedback button
*/
public NavigationButton retrieveFeedbackButton() {
return instructionView.retrieveFeedbackButton();
}

/**
* Returns the re-center button for recentering on current location
*
* @return recenter button
*/
public NavigationButton retrieveRecenterButton() {
return recenterBtn;
}

private void initializeView() {
inflate(getContext(), R.layout.navigation_view_layout, this);
bind();
Expand Down Expand Up @@ -532,7 +559,7 @@ private void initializeNavigation(NavigationViewOptions options) {

private void initializeClickListeners() {
cancelBtn.setOnClickListener(new CancelBtnClickListener(navigationViewEventDispatcher));
recenterBtn.setOnClickListener(new RecenterBtnClickListener(navigationPresenter));
recenterBtn.addOnClickListener(new RecenterBtnClickListener(navigationPresenter));
routeOverviewBtn.setOnClickListener(new RouteOverviewBtnClickListener(navigationPresenter));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public class NavigationViewModel extends AndroidViewModel {
public final MutableLiveData<BannerInstructionModel> bannerInstructionModel = new MutableLiveData<>();
public final MutableLiveData<SummaryModel> summaryModel = new MutableLiveData<>();
public final MutableLiveData<Boolean> isOffRoute = new MutableLiveData<>();
public final MutableLiveData<Boolean> isFeedbackShowing = new MutableLiveData<>();
final MutableLiveData<Location> navigationLocation = new MutableLiveData<>();
final MutableLiveData<DirectionsRoute> route = new MutableLiveData<>();
final MutableLiveData<Point> destination = new MutableLiveData<>();
Expand Down Expand Up @@ -151,7 +150,6 @@ public void updateFeedback(FeedbackItem feedbackItem) {
*/
public void cancelFeedback() {
if (!TextUtils.isEmpty(feedbackId)) {
isFeedbackShowing.setValue(false);
navigation.cancelFeedback(feedbackId);
feedbackId = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
*
* @since 0.6.0
*/
public class RecenterButton extends CardView {

public class RecenterButton extends CardView implements NavigationButton {
private MultiOnClickListener multiOnClickListener;
private Animation slideUpBottom;

public RecenterButton(Context context) {
Expand All @@ -42,18 +42,40 @@ public RecenterButton(Context context, @Nullable AttributeSet attrs, int defStyl
*
* @since 0.6.0
*/
@Override
public void show() {
if (getVisibility() == INVISIBLE) {
setVisibility(VISIBLE);
startAnimation(slideUpBottom);
}
}

/**
* Adds an onClickListener to the button
*
* @param onClickListener to add
*/
@Override
public void addOnClickListener(OnClickListener onClickListener) {
multiOnClickListener.addListener(onClickListener);
}

/**
* Removes an onClickListener from the button
*
* @param onClickListener to remove
*/
@Override
public void removeOnClickListener(OnClickListener onClickListener) {
multiOnClickListener.removeListener(onClickListener);
}

/**
* Sets visibility to INVISIBLE.
*
* @since 0.6.0
*/
@Override
public void hide() {
if (getVisibility() == VISIBLE) {
setVisibility(INVISIBLE);
Expand All @@ -70,6 +92,29 @@ protected void onFinishInflate() {
initAnimation();
}

@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
setupOnClickListeners();
}

@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
clearListeners();
}

private void setupOnClickListeners() {
multiOnClickListener = new MultiOnClickListener();
setOnClickListener(multiOnClickListener);
}

private void clearListeners() {
multiOnClickListener.clearListeners();
multiOnClickListener = null;
setOnClickListener(null);
}

/**
* Inflates the layout.
*/
Expand Down
Loading

0 comments on commit 2d92c9b

Please sign in to comment.