minor refactoring

This commit is contained in:
tibbi 2016-01-03 17:31:17 +01:00
parent 4448b988cb
commit feaa8f2de7
2 changed files with 9 additions and 7 deletions

View File

@ -26,6 +26,6 @@ public class Constants {
public static final String NINE = "nine"; public static final String NINE = "nine";
public static final String PREFS = "prefs"; public static final String PREFS = "prefs";
public static final String VALUE = "value"; public static final String CALC_VALUE = "calc_value";
public static final String WIDGET_BG_COLOR = "widget_bg_color"; public static final String WIDGET_BG_COLOR = "widget_bg_color";
} }

View File

@ -58,18 +58,20 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calculator {
} }
private void initVariables(Context context) { private void initVariables(Context context) {
prefs = initPrefs(context); final int defaultColor = context.getResources().getColor(R.color.dark_grey);
final int newBgColor = prefs.getInt(Constants.WIDGET_BG_COLOR, defaultColor);
final ComponentName component = new ComponentName(context, MyWidgetProvider.class); final ComponentName component = new ComponentName(context, MyWidgetProvider.class);
prefs = initPrefs(context);
remoteViews = new RemoteViews(context.getPackageName(), R.layout.activity_main); remoteViews = new RemoteViews(context.getPackageName(), R.layout.activity_main);
remoteViews.setViewVisibility(R.id.btn_reset, View.VISIBLE); remoteViews.setViewVisibility(R.id.btn_reset, View.VISIBLE);
final int newBgColor = prefs.getInt(Constants.WIDGET_BG_COLOR, context.getResources().getColor(R.color.dark_grey));
remoteViews.setInt(R.id.calculator_holder, "setBackgroundColor", newBgColor); remoteViews.setInt(R.id.calculator_holder, "setBackgroundColor", newBgColor);
widgetManager = AppWidgetManager.getInstance(context); widgetManager = AppWidgetManager.getInstance(context);
widgetIds = widgetManager.getAppWidgetIds(component); widgetIds = widgetManager.getAppWidgetIds(component);
final String value = prefs.getString(Constants.VALUE, "0"); final String displayValue = prefs.getString(Constants.CALC_VALUE, "0");
calc = new CalculatorImpl(this, value); calc = new CalculatorImpl(this, displayValue);
} }
private SharedPreferences initPrefs(Context context) { private SharedPreferences initPrefs(Context context) {
@ -173,7 +175,7 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calculator {
public void setValue(String value) { public void setValue(String value) {
remoteViews.setTextViewText(R.id.result, value); remoteViews.setTextViewText(R.id.result, value);
widgetManager.updateAppWidget(widgetIds, remoteViews); widgetManager.updateAppWidget(widgetIds, remoteViews);
prefs.edit().putString(Constants.VALUE, value).apply(); prefs.edit().putString(Constants.CALC_VALUE, value).apply();
} }
@Override @Override
@ -187,6 +189,6 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calculator {
if (prefs == null) if (prefs == null)
prefs = initPrefs(context); prefs = initPrefs(context);
prefs.edit().remove(Constants.VALUE).apply(); prefs.edit().remove(Constants.CALC_VALUE).apply();
} }
} }