Skip to content

Commit

Permalink
Extract typed value logic to own method
Browse files Browse the repository at this point in the history
  • Loading branch information
danesfeder committed Nov 21, 2017
1 parent 3481d67 commit 12e085e
Showing 1 changed file with 23 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.TypedValue;
Expand All @@ -19,6 +20,18 @@
*/
public class ThemeSwitcher {

/**
* Looks are current theme and retrieves the color attribute
* for the given set theme.
*
* @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 retrieveNavigationViewThemeColor(Context context, int resId) {
TypedValue outValue = obtainTypedValue(context, resId);
return ContextCompat.getColor(context, outValue.resourceId);
}

/**
* Called in onCreate() to check the UI Mode (day or night)
* and set the theme colors accordingly.
Expand Down Expand Up @@ -49,8 +62,7 @@ static void setTheme(Context context, AttributeSet attrs) {
* @param map the style will be set on
*/
static void setMapStyle(Context context, MapboxMap map, MapboxMap.OnStyleLoadedListener listener) {
TypedValue mapStyleAttr = new TypedValue();
context.getTheme().resolveAttribute(R.attr.navigationViewMapStyle, mapStyleAttr, true);
TypedValue mapStyleAttr = obtainTypedValue(context, R.attr.navigationViewMapStyle);
String styleUrl = mapStyleAttr.string.toString();
map.setStyleUrl(styleUrl, listener);
}
Expand All @@ -68,32 +80,25 @@ static Icon retrieveMapMarker(Context context) {
return iconFactory.fromResource(darkThemeEnabled ? R.drawable.map_marker_dark : R.drawable.map_marker_light);
}

/**
* Looks are current theme and retrieves the color attribute
* for the given set theme.
*
* @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 retrieveNavigationViewThemeColor(Context context, int resId) {
TypedValue outValue = new TypedValue();
context.getTheme().resolveAttribute(resId, outValue, true);
return ContextCompat.getColor(context, outValue.resourceId);
}

/**
* Looks are current theme and retrieves the route style
* for the given set theme.
*
* @param context to retrieve the resolved attribute
* @return style resource Id for the route
*/
public static int retrieveNavigationViewRouteStyle(Context context) {
TypedValue outValue = new TypedValue();
context.getTheme().resolveAttribute(R.attr.navigationViewRouteStyle, outValue, true);
static int retrieveNavigationViewRouteStyle(Context context) {
TypedValue outValue = obtainTypedValue(context, R.attr.navigationViewRouteStyle);
return outValue.resourceId;
}

@NonNull
private static TypedValue obtainTypedValue(Context context, int resId) {
TypedValue outValue = new TypedValue();
context.getTheme().resolveAttribute(resId, outValue, true);
return outValue;
}

private static void updatePreferencesDarkEnabled(Context context, boolean darkThemeEnabled) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
Expand Down

0 comments on commit 12e085e

Please sign in to comment.