Skip to content

Commit

Permalink
Consolidated theme methods and created attribute for route style
Browse files Browse the repository at this point in the history
  • Loading branch information
danesfeder committed Nov 21, 2017
1 parent f614397 commit 3481d67
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,9 @@ private void resetBottomSheetState(int bottomSheetState) {
* route.
*/
private void initRoute() {
mapRoute = new NavigationMapRoute(mapView, map, NavigationConstants.ROUTE_BELOW_LAYER);
int routeStyleRes = ThemeSwitcher.retrieveNavigationViewRouteStyle(getContext());
mapRoute = new NavigationMapRoute(null, mapView, map,
routeStyleRes, NavigationConstants.ROUTE_BELOW_LAYER);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.mapbox.services.android.navigation.ui.v5;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
Expand Down Expand Up @@ -31,10 +30,7 @@ static void setTheme(Context context, AttributeSet attrs) {
int uiMode = context.getResources().getConfiguration().uiMode
& Configuration.UI_MODE_NIGHT_MASK;
boolean darkThemeEnabled = uiMode == Configuration.UI_MODE_NIGHT_YES;
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(context.getString(R.string.dark_theme_enabled), darkThemeEnabled);
editor.apply();
updatePreferencesDarkEnabled(context, darkThemeEnabled);

TypedArray styledAttributes = context.obtainStyledAttributes(attrs, R.styleable.NavigationView);
int lightTheme = styledAttributes.getResourceId(R.styleable.NavigationView_navigationLightTheme,
Expand All @@ -46,20 +42,6 @@ static void setTheme(Context context, AttributeSet attrs) {
context.setTheme(darkThemeEnabled ? darkTheme : lightTheme);
}

/**
* Can be called to toggle the theme based on the current theme setting.
*
* @param activity {@link NavigationView} where the theme will be set
*/
static void toggleTheme(Activity activity) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
boolean darkThemeEnabled = preferences.getBoolean(activity.getString(R.string.dark_theme_enabled), false);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(activity.getString(R.string.dark_theme_enabled), !darkThemeEnabled);
editor.apply();
activity.recreate();
}

/**
* Sets the {@link MapView} style based on the current theme setting.
*
Expand Down Expand Up @@ -87,93 +69,35 @@ static Icon retrieveMapMarker(Context context) {
}

/**
* Looks are current theme and retrieves the primary color
* Looks are current theme and retrieves the color attribute
* for the given set theme.
*
* @param context to retrieve {@link SharedPreferences} and color with {@link ContextCompat}
* @param context to retrieve the set theme and resolved attribute and then color res Id with {@link ContextCompat}
* @return color resource identifier for primary theme color
*/
public static int retrieveNavigationViewPrimaryColor(Context context) {
TypedValue outValue = new TypedValue();
context.getTheme().resolveAttribute(R.attr.navigationViewPrimary, outValue, true);
return ContextCompat.getColor(context, outValue.resourceId);
}

/**
* Looks are current theme and retrieves the secondary color
* for the given set theme.
*
* @param context to retrieve {@link SharedPreferences} and color with {@link ContextCompat}
* @return color resource identifier for secondary theme color
*/
public static int retrieveNavigationViewSecondaryColor(Context context) {
TypedValue outValue = new TypedValue();
context.getTheme().resolveAttribute(R.attr.navigationViewSecondary, outValue, true);
return ContextCompat.getColor(context, outValue.resourceId);
}

/**
* Looks are current theme and retrieves the banner background color
* for the given set theme.
*
* @param context to retrieve {@link SharedPreferences} and color with {@link ContextCompat}
* @return color resource identifier for banner background color
*/
public static int retrieveNavigationViewBannerBackgroundColor(Context context) {
public static int retrieveNavigationViewThemeColor(Context context, int resId) {
TypedValue outValue = new TypedValue();
context.getTheme().resolveAttribute(R.attr.navigationViewBannerBackground, outValue, true);
context.getTheme().resolveAttribute(resId, outValue, true);
return ContextCompat.getColor(context, outValue.resourceId);
}

/**
* Looks are current theme and retrieves the banner maneuver primary color
* Looks are current theme and retrieves the route style
* for the given set theme.
*
* @param context to retrieve {@link SharedPreferences} and color with {@link ContextCompat}
* @return color resource identifier for banner maneuver primary theme color
* @param context to retrieve the resolved attribute
* @return style resource Id for the route
*/
public static int retrieveNavigationViewBannerManeuverPrimaryColor(Context context) {
public static int retrieveNavigationViewRouteStyle(Context context) {
TypedValue outValue = new TypedValue();
context.getTheme().resolveAttribute(R.attr.navigationViewBannerManeuverPrimary, outValue, true);
return ContextCompat.getColor(context, outValue.resourceId);
}

/**
* Looks are current theme and retrieves the banner maneuver secondary color
* for the given set theme.
*
* @param context to retrieve {@link SharedPreferences} and color with {@link ContextCompat}
* @return color resource identifier for banner maneuver secondary theme color
*/
public static int retrieveNavigationViewBannerManeuverSecondaryColor(Context context) {
TypedValue outValue = new TypedValue();
context.getTheme().resolveAttribute(R.attr.navigationViewBannerManeuverSecondary, outValue, true);
return ContextCompat.getColor(context, outValue.resourceId);
context.getTheme().resolveAttribute(R.attr.navigationViewRouteStyle, outValue, true);
return outValue.resourceId;
}

/**
* Looks are current theme and retrieves the progress color
* for the given set theme.
*
* @param context to retrieve {@link SharedPreferences} and color with {@link ContextCompat}
* @return color resource identifier for progress color
*/
public static int retrieveNavigationViewProgressColor(Context context) {
TypedValue outValue = new TypedValue();
context.getTheme().resolveAttribute(R.attr.navigationViewProgress, outValue, true);
return ContextCompat.getColor(context, outValue.resourceId);
}

/**
* Looks are current theme and retrieves the progress background color
* for the given set theme.
*
* @param context to retrieve {@link SharedPreferences} and color with {@link ContextCompat}
* @return color resource identifier for progress background color
*/
public static int retrieveNavigationViewProgressBackgroundColor(Context context) {
TypedValue outValue = new TypedValue();
context.getTheme().resolveAttribute(R.attr.navigationViewProgressBackground, outValue, true);
return ContextCompat.getColor(context, outValue.resourceId);
private static void updatePreferencesDarkEnabled(Context context, boolean darkThemeEnabled) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(context.getString(R.string.dark_theme_enabled), darkThemeEnabled);
editor.apply();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,10 @@ private void initAnimations() {

private void initBackground() {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) {
int progressColor = ThemeSwitcher.retrieveNavigationViewProgressColor(getContext());
int progressBackgroundColor = ThemeSwitcher.retrieveNavigationViewProgressBackgroundColor(getContext());
int progressColor = ThemeSwitcher.retrieveNavigationViewThemeColor(getContext(),
R.attr.navigationViewProgress);
int progressBackgroundColor = ThemeSwitcher.retrieveNavigationViewThemeColor(getContext(),
R.attr.navigationViewProgressBackground);

LayerDrawable progressBarDrawable = (LayerDrawable) alertProgressBar.getProgressDrawable();
// ProgressBar progress color
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,10 @@ public void onAnimationRepeat(Animator animation) {

private void initBackground(View view) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) {
int navigationViewPrimaryColor = ThemeSwitcher.retrieveNavigationViewPrimaryColor(getContext());
int navigationViewSecondaryColor = ThemeSwitcher.retrieveNavigationViewSecondaryColor(getContext());
int navigationViewPrimaryColor = ThemeSwitcher.retrieveNavigationViewThemeColor(getContext(),
R.attr.navigationViewPrimary);
int navigationViewSecondaryColor = ThemeSwitcher.retrieveNavigationViewThemeColor(getContext(),
R.attr.navigationViewSecondary);
// BottomSheet background
Drawable bottomSheetBackground = DrawableCompat.wrap(view.getBackground()).mutate();
DrawableCompat.setTint(bottomSheetBackground, navigationViewPrimaryColor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ void setFeedbackText(String feedbackText) {

private void initTextColor(TextView feedbackText) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) {
int navigationViewSecondaryColor = ThemeSwitcher.retrieveNavigationViewSecondaryColor(feedbackText.getContext());
int navigationViewSecondaryColor = ThemeSwitcher.retrieveNavigationViewThemeColor(feedbackText.getContext(),
R.attr.navigationViewSecondary);
// Text color
feedbackText.setTextColor(navigationViewSecondaryColor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,10 @@ private void bind() {
*/
private void initBackground() {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) {
int navigationViewPrimaryColor = ThemeSwitcher.retrieveNavigationViewPrimaryColor(getContext());
int navigationViewBannerBackgroundColor = ThemeSwitcher.retrieveNavigationViewBannerBackgroundColor(getContext());
int navigationViewPrimaryColor = ThemeSwitcher.retrieveNavigationViewThemeColor(getContext(),
R.attr.navigationViewPrimary);
int navigationViewBannerBackgroundColor = ThemeSwitcher.retrieveNavigationViewThemeColor(getContext(),
R.attr.navigationViewBannerBackground);
// Instruction Layout landscape - banner background
if (getContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
View instructionLayoutManeuver = findViewById(R.id.instructionManeuverLayout);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.util.AttributeSet;
import android.view.View;

import com.mapbox.services.android.navigation.ui.v5.R;
import com.mapbox.services.android.navigation.ui.v5.ThemeSwitcher;
import com.mapbox.services.android.navigation.v5.navigation.NavigationConstants;

Expand Down Expand Up @@ -65,8 +66,10 @@ protected void onFinishInflate() {
}

private void initManeuverColor() {
this.primaryColor = ThemeSwitcher.retrieveNavigationViewBannerManeuverPrimaryColor(getContext());
this.secondaryColor = ThemeSwitcher.retrieveNavigationViewBannerManeuverSecondaryColor(getContext());
this.primaryColor = ThemeSwitcher.retrieveNavigationViewThemeColor(getContext(),
R.attr.navigationViewBannerManeuverPrimary);
this.secondaryColor = ThemeSwitcher.retrieveNavigationViewThemeColor(getContext(),
R.attr.navigationViewBannerManeuverSecondary);
}

public void setManeuverType(String maneuverType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.view.View;

import com.mapbox.directions.v5.models.IntersectionLanes;
import com.mapbox.services.android.navigation.ui.v5.R;
import com.mapbox.services.android.navigation.ui.v5.ThemeSwitcher;
import com.mapbox.services.commons.utils.TextUtils;

Expand Down Expand Up @@ -114,7 +115,9 @@ public void updateLaneView(@NonNull IntersectionLanes lane, @NonNull String mane
}

private void initManeuverColor() {
this.primaryColor = ThemeSwitcher.retrieveNavigationViewBannerManeuverPrimaryColor(getContext());
this.secondaryColor = ThemeSwitcher.retrieveNavigationViewBannerManeuverSecondaryColor(getContext());
this.primaryColor = ThemeSwitcher.retrieveNavigationViewThemeColor(getContext(),
R.attr.navigationViewBannerManeuverPrimary);
this.secondaryColor = ThemeSwitcher.retrieveNavigationViewThemeColor(getContext(),
R.attr.navigationViewBannerManeuverSecondary);
}
}
3 changes: 3 additions & 0 deletions libandroid-navigation-ui/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
<!-- Map style -->
<attr name="navigationViewMapStyle" format="string"/>

<!-- Route style -->
<attr name="navigationViewRouteStyle" format="reference"/>

<!-- For setting the styles in XML -->
<attr name="navigationLightTheme" format="reference"/>
<attr name="navigationDarkTheme" format="reference"/>
Expand Down
4 changes: 4 additions & 0 deletions libandroid-navigation-ui/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
<item name="navigationViewProgress">@color/mapbox_navigation_view_color_progress</item>
<item name="navigationViewProgressBackground">@color/mapbox_navigation_view_color_progress_background</item>

<item name="navigationViewRouteStyle">@style/NavigationMapRoute</item>

<item name="navigationViewMapStyle">@string/navigation_guidance_day_v2</item>
</style>

Expand All @@ -51,6 +53,8 @@
<item name="navigationViewProgress">@color/mapbox_navigation_view_color_progress_dark</item>
<item name="navigationViewProgressBackground">@color/mapbox_navigation_view_color_progress_background_dark</item>

<item name="navigationViewRouteStyle">@style/NavigationMapRoute</item>

<item name="navigationViewMapStyle">@string/navigation_guidance_night_v2</item>
</style>

Expand Down

0 comments on commit 3481d67

Please sign in to comment.