Skip to content

Commit

Permalink
plusonelabs#325 Generified / unified Text Shadings - prepare for thei…
Browse files Browse the repository at this point in the history
…r extension
  • Loading branch information
yvolk committed Sep 15, 2019
1 parent ee57281 commit a114129
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 113 deletions.
20 changes: 11 additions & 9 deletions app/src/main/java/org/andstatus/todoagenda/AppWidgetProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.widget.RemoteViews;

import org.andstatus.todoagenda.prefs.AllSettings;
import org.andstatus.todoagenda.prefs.InstanceSettings;
import org.andstatus.todoagenda.prefs.TextShadingPref;
import org.andstatus.todoagenda.util.CalendarIntentUtil;
import org.andstatus.todoagenda.util.DateUtil;
import org.andstatus.todoagenda.util.PermissionsUtil;
Expand All @@ -26,7 +28,6 @@

import androidx.annotation.IdRes;

import static org.andstatus.todoagenda.Theme.themeNameToResId;
import static org.andstatus.todoagenda.util.CalendarIntentUtil.createOpenCalendarAtDayIntent;
import static org.andstatus.todoagenda.util.CalendarIntentUtil.createOpenCalendarEventPendingIntent;
import static org.andstatus.todoagenda.util.CalendarIntentUtil.createOpenCalendarPendingIntent;
Expand Down Expand Up @@ -147,17 +148,18 @@ private static void configureCurrentDate(InstanceSettings settings, RemoteViews
: " ";
rv.setTextViewText(viewId, formattedDate);
setTextSize(settings, rv, viewId, R.dimen.widget_header_title);
setTextColorFromAttr(settings.getWidgetHeaderThemeContext(), rv, viewId, R.attr.header);
setTextColorFromAttr(settings.getShadingContext(TextShadingPref.WIDGET_HEADER), rv, viewId, R.attr.header);
}

private static void setActionIcons(InstanceSettings settings, RemoteViews rv) {
setImageFromAttr(settings.getWidgetHeaderThemeContext(), rv, R.id.go_to_today, R.attr.header_action_go_to_today);
setImageFromAttr(settings.getWidgetHeaderThemeContext(), rv, R.id.add_event, R.attr.header_action_add_event);
setImageFromAttr(settings.getWidgetHeaderThemeContext(), rv, R.id.refresh, R.attr.header_action_refresh);
setImageFromAttr(settings.getWidgetHeaderThemeContext(), rv, R.id.overflow_menu, R.attr.header_action_overflow);
int themeId = themeNameToResId(settings.getWidgetHeaderTheme());
ContextThemeWrapper themeContext = settings.getShadingContext(TextShadingPref.WIDGET_HEADER);
setImageFromAttr(themeContext, rv, R.id.go_to_today, R.attr.header_action_go_to_today);
setImageFromAttr(themeContext, rv, R.id.add_event, R.attr.header_action_add_event);
setImageFromAttr(themeContext, rv, R.id.refresh, R.attr.header_action_refresh);
setImageFromAttr(themeContext, rv, R.id.overflow_menu, R.attr.header_action_overflow);
TextShading textShading = settings.getShading(TextShadingPref.WIDGET_HEADER);
int alpha = 255;
if (themeId == R.style.Theme_Calendar_Dark || themeId == R.style.Theme_Calendar_Light) {
if (textShading == TextShading.DARK || textShading == TextShading.LIGHT) {
alpha = 154;
}
setAlpha(rv, R.id.go_to_today, alpha);
Expand Down Expand Up @@ -234,7 +236,7 @@ private static void configureNoEvents(InstanceSettings settings, RemoteViews rv)
}
setTextSize(settings, rv, viewId, R.dimen.event_entry_details);
setBackgroundColor(rv, viewId, settings.getEventsBackgroundColor());
setTextColorFromAttr(settings.getEntryThemeContext(), rv, viewId, R.attr.eventEntryTitle);
setTextColorFromAttr(settings.getShadingContext(TextShadingPref.ENTRY), rv, viewId, R.attr.eventEntryTitle);
}

private static List<Integer> asList(final int[] is) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.andstatus.todoagenda.prefs.AllSettings;
import org.andstatus.todoagenda.prefs.InstanceSettings;
import org.andstatus.todoagenda.prefs.TextShadingPref;
import org.andstatus.todoagenda.provider.EventProviderType;
import org.andstatus.todoagenda.util.DateUtil;
import org.andstatus.todoagenda.util.PermissionsUtil;
Expand Down Expand Up @@ -93,10 +94,13 @@ private void reload() {

private List<WidgetEntryVisualizer<? extends WidgetEntry>> getVisualizers() {
List<WidgetEntryVisualizer<? extends WidgetEntry>> visualizers = new ArrayList<>();
visualizers.add(new DayHeaderVisualizer(getSettings().getDayHeaderThemeContext(), widgetId));
DayHeaderVisualizer dayHeaderVisualizer = new DayHeaderVisualizer(
getSettings().getShadingContext(TextShadingPref.DAY_HEADER),
widgetId);
visualizers.add(dayHeaderVisualizer);
for (EventProviderType type : EventProviderType.values()) {
if (type.hasEventSources()) {
visualizers.add(type.getVisualizer(getSettings().getEntryThemeContext(), widgetId));
visualizers.add(type.getVisualizer(getSettings().getShadingContext(TextShadingPref.ENTRY), widgetId));
}
}
return visualizers;
Expand Down
31 changes: 31 additions & 0 deletions app/src/main/java/org/andstatus/todoagenda/TextShading.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.andstatus.todoagenda;

import androidx.annotation.StringRes;
import androidx.annotation.StyleRes;

public enum TextShading {

WHITE(R.style.Theme_Calendar_White, R.string.appearance_theme_white),
LIGHT(R.style.Theme_Calendar_Light, R.string.appearance_theme_light),
DARK(R.style.Theme_Calendar_Dark, R.string.appearance_theme_dark),
BLACK(R.style.Theme_Calendar_Black, R.string.appearance_theme_black);

@StyleRes
public final int themeResId;
@StringRes
public final int titleResId;

TextShading(int themeResId, int titleResId) {
this.themeResId = themeResId;
this.titleResId = titleResId;
}

public static TextShading fromName(String themeName, TextShading defaultShading) {
try {
return TextShading.valueOf(themeName);
} catch (Exception e) {
return defaultShading;
}
}

}
26 changes: 0 additions & 26 deletions app/src/main/java/org/andstatus/todoagenda/Theme.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import android.widget.RemoteViews;

import org.andstatus.todoagenda.R;
import org.andstatus.todoagenda.TextShading;
import org.andstatus.todoagenda.prefs.TextShadingPref;
import org.andstatus.todoagenda.provider.EventProvider;
import org.andstatus.todoagenda.util.DateUtil;
import org.andstatus.todoagenda.widget.CalendarEntry;
Expand All @@ -16,7 +18,6 @@
import java.util.Collections;
import java.util.List;

import static org.andstatus.todoagenda.Theme.themeNameToResId;
import static org.andstatus.todoagenda.util.RemoteViewsUtil.setAlpha;
import static org.andstatus.todoagenda.util.RemoteViewsUtil.setBackgroundColor;
import static org.andstatus.todoagenda.util.RemoteViewsUtil.setImageFromAttr;
Expand Down Expand Up @@ -57,9 +58,9 @@ private void setIndicator(RemoteViews rv, boolean showIndication, int viewId, in
if (showIndication) {
rv.setViewVisibility(viewId, View.VISIBLE);
setImageFromAttr(getContext(), rv, viewId, imageAttrId);
int themeId = themeNameToResId(getSettings().getEntryTheme());
TextShading textShading = getSettings().getShading(TextShadingPref.ENTRY);
int alpha = 255;
if (themeId == R.style.Theme_Calendar_Dark || themeId == R.style.Theme_Calendar_Light) {
if (textShading == TextShading.DARK || textShading == TextShading.LIGHT) {
alpha = 128;
}
setAlpha(rv, viewId, alpha);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
import android.text.TextUtils;

import org.andstatus.todoagenda.EndedSomeTimeAgo;
import org.andstatus.todoagenda.TextShading;
import org.andstatus.todoagenda.widget.EventEntryLayout;
import org.andstatus.todoagenda.widget.WidgetHeaderLayout;

import java.util.List;
import java.util.Map;

import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_ABBREVIATE_DATES;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_ABBREVIATE_DATES_DEFAULT;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_ACTIVE_SOURCES;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_DATE_FORMAT;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_DATE_FORMAT_DEFAULT;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_DAY_HEADER_ALIGNMENT;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_DAY_HEADER_THEME;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_ENTRY_THEME;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_EVENTS_BACKGROUND_COLOR;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_EVENTS_BACKGROUND_COLOR_DEFAULT;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_EVENTS_ENDED;
Expand Down Expand Up @@ -53,7 +53,6 @@
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_WIDGET_HEADER_BACKGROUND_COLOR;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_WIDGET_HEADER_BACKGROUND_COLOR_DEFAULT;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_WIDGET_HEADER_LAYOUT;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_WIDGET_HEADER_THEME;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_WIDGET_ID;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_WIDGET_INSTANCE_NAME;

Expand Down Expand Up @@ -95,9 +94,9 @@ public static void fromInstanceSettings(Context context, Integer widgetId) {
.getShowOnlyClosestInstanceOfRecurringEvent());
setBoolean(context, PREF_INDICATE_ALERTS, settings.getIndicateAlerts());
setBoolean(context, PREF_INDICATE_RECURRING, settings.getIndicateRecurring());
setString(context, PREF_WIDGET_HEADER_THEME, settings.getWidgetHeaderTheme());
setString(context, PREF_DAY_HEADER_THEME, settings.getDayHeaderTheme());
setString(context, PREF_ENTRY_THEME, settings.getEntryTheme());
for (Map.Entry<TextShadingPref, TextShading> entry: settings.shadings.entrySet()) {
setString(context, entry.getKey().preferenceName, entry.getValue().name());
}
setString(context, PREF_WIDGET_HEADER_LAYOUT, settings.getWidgetHeaderLayout().value);
setString(context, PREF_TEXT_SIZE_SCALE, settings.getTextSizeScale());
setString(context, PREF_DAY_HEADER_ALIGNMENT, settings.getDayHeaderAlignment());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,51 @@
package org.andstatus.todoagenda.prefs;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.PreferenceFragment;

import org.andstatus.todoagenda.R;
import org.andstatus.todoagenda.TextShading;

public class ColorsPreferencesFragment extends PreferenceFragment {
public class ColorsPreferencesFragment extends PreferenceFragment
implements SharedPreferences.OnSharedPreferenceChangeListener {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences_colors);
}

@Override
public void onResume() {
super.onResume();
getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
showShadings();
}

private void showShadings() {
for (TextShadingPref shadingPref : TextShadingPref.values()) {
showShading(shadingPref);
}
}

@Override
public void onPause() {
super.onPause();
getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
showShadings();
}

private void showShading(TextShadingPref prefs) {
ListPreference preference = (ListPreference) findPreference(prefs.preferenceName);
if (preference != null) {
TextShading shading = TextShading.fromName(preference.getValue(), prefs.defaultShading);
preference.setSummary(getActivity().getString(shading.titleResId));
}
}
}
Loading

0 comments on commit a114129

Please sign in to comment.