Skip to content

Commit

Permalink
#42 Single line layout for Event Entry created
Browse files Browse the repository at this point in the history
  • Loading branch information
yvolk committed Mar 26, 2017
1 parent c8d5cbf commit 6e5f55d
Show file tree
Hide file tree
Showing 13 changed files with 269 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ private static float getDimension(Context context, int dimenId) {
}
}

public static void setSingleLine(RemoteViews rv, int viewId, boolean singleLine) {
rv.setBoolean(viewId, METHOD_SET_SINGLE_LINE, singleLine);
public static void setMultiline(RemoteViews rv, int viewId, boolean multiLine) {
rv.setBoolean(viewId, METHOD_SET_SINGLE_LINE, !multiLine);
}

public static void setImageFromAttr(Context context, RemoteViews rv, int viewId, int attrResId) {
public static void setImageFromAttr(Context context, RemoteViews rv, int viewId, int attrResId) {
TypedValue outValue = new TypedValue();
context.getTheme().resolveAttribute(attrResId, outValue, true);
setImage(rv, viewId, outValue.resourceId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.RemoteViews;

Expand All @@ -14,6 +13,7 @@
import com.plusonelabs.calendar.R;
import com.plusonelabs.calendar.prefs.CalendarPreferences;
import com.plusonelabs.calendar.widget.CalendarEntry;
import com.plusonelabs.calendar.widget.EventEntryLayout;
import com.plusonelabs.calendar.widget.WidgetEntry;

import org.joda.time.DateTime;
Expand All @@ -25,7 +25,7 @@
import static com.plusonelabs.calendar.RemoteViewsUtil.setAlpha;
import static com.plusonelabs.calendar.RemoteViewsUtil.setBackgroundColor;
import static com.plusonelabs.calendar.RemoteViewsUtil.setImageFromAttr;
import static com.plusonelabs.calendar.RemoteViewsUtil.setSingleLine;
import static com.plusonelabs.calendar.RemoteViewsUtil.setMultiline;
import static com.plusonelabs.calendar.RemoteViewsUtil.setTextColorFromAttr;
import static com.plusonelabs.calendar.RemoteViewsUtil.setTextSize;
import static com.plusonelabs.calendar.Theme.getCurrentThemeId;
Expand All @@ -49,34 +49,23 @@ public CalendarEventVisualizer(Context context) {

public RemoteViews getRemoteView(WidgetEntry eventEntry) {
CalendarEntry event = (CalendarEntry) eventEntry;
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.event_entry);
EventEntryLayout eventEntryLayout = CalendarPreferences.getEventEntryLayout(context);
RemoteViews rv = new RemoteViews(context.getPackageName(), eventEntryLayout.layoutId);
rv.setOnClickFillInIntent(R.id.event_entry, createOnItemClickIntent(event.getEvent()));
setTitle(event, rv);
setEventDetails(event, rv);
eventEntryLayout.visualizeEvent(context, event, rv);
setAlarmActive(event, rv);
setRecurring(event, rv);
setColor(event, rv);
return rv;
}

private void setTitle(CalendarEntry event, RemoteViews rv) {
private void setTitle(CalendarEntry event, RemoteViews rv) {
rv.setTextViewText(R.id.event_entry_title, event.getTitle(context));
setTextSize(context, rv, R.id.event_entry_title, R.dimen.event_entry_title);
setTextColorFromAttr(context, rv, R.id.event_entry_title, R.attr.eventEntryTitle);
setSingleLine(rv, R.id.event_entry_title,
!prefs.getBoolean(PREF_MULTILINE_TITLE, PREF_MULTILINE_TITLE_DEFAULT));
}

private void setEventDetails(CalendarEntry entry, RemoteViews rv) {
String eventDetails = entry.getEventDetails(context);
if (TextUtils.isEmpty(eventDetails)) {
rv.setViewVisibility(R.id.event_entry_details, View.GONE);
} else {
rv.setViewVisibility(R.id.event_entry_details, View.VISIBLE);
rv.setTextViewText(R.id.event_entry_details, eventDetails);
setTextSize(context, rv, R.id.event_entry_details, R.dimen.event_entry_details);
setTextColorFromAttr(context, rv, R.id.event_entry_details, R.attr.eventEntryDetails);
}
setMultiline(rv, R.id.event_entry_title,
prefs.getBoolean(PREF_MULTILINE_TITLE, PREF_MULTILINE_TITLE_DEFAULT));
}

private void setAlarmActive(CalendarEntry entry, RemoteViews rv) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.plusonelabs.calendar.prefs;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
Expand All @@ -13,7 +14,8 @@

import java.util.TimeZone;

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

@Override
public void onCreate(Bundle savedInstanceState) {
Expand All @@ -25,8 +27,17 @@ public void onCreate(Bundle savedInstanceState) {
public void onResume() {
super.onResume();
showLockTimeZone(true);
showEventEntryLayout();
getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}

private void showEventEntryLayout() {
Preference preference = findPreference(CalendarPreferences.PREF_EVENT_ENTRY_LAYOUT);
if (preference != null) {
preference.setSummary(CalendarPreferences.getEventEntryLayout(getActivity()).summaryResId);
}
}

private void showLockTimeZone(boolean setAlso) {
CheckBoxPreference preference = (CheckBoxPreference) findPreference(CalendarPreferences.PREF_LOCK_TIME_ZONE);
if (preference != null) {
Expand Down Expand Up @@ -78,7 +89,19 @@ public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preferen
@Override
public void onPause() {
super.onPause();
getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
EventAppWidgetProvider.updateEventList(getActivity());
EventAppWidgetProvider.updateAllWidgets(getActivity());
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
switch (key) {
case CalendarPreferences.PREF_EVENT_ENTRY_LAYOUT:
showEventEntryLayout();
break;
default:
break;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.plusonelabs.calendar.DateUtil;
import com.plusonelabs.calendar.EndedSomeTimeAgo;
import com.plusonelabs.calendar.Theme;
import com.plusonelabs.calendar.widget.EventEntryLayout;

import org.joda.time.DateTimeZone;
import org.json.JSONException;
Expand Down Expand Up @@ -57,6 +58,7 @@ public class CalendarPreferences {
private static final boolean PREF_ABBREVIATE_DATES_DEFAULT = false;
static final String PREF_LOCK_TIME_ZONE = "lockTimeZone";
private static final String PREF_LOCKED_TIME_ZONE_ID = "lockedTimeZoneId";
static final String PREF_EVENT_ENTRY_LAYOUT = "eventEntryLayout";

private static volatile String lockedTimeZoneId = null;

Expand Down Expand Up @@ -227,4 +229,13 @@ public static void setLockedTimeZoneId(Context context, String value) {
public static boolean isTimeZoneLocked(Context context) {
return !TextUtils.isEmpty(getLockedTimeZoneId(context));
}

public static void setEventEntryLayout(Context context, EventEntryLayout value) {
setStringPreference(context, PREF_EVENT_ENTRY_LAYOUT, value.value);
}

public static EventEntryLayout getEventEntryLayout(Context context) {
return EventEntryLayout.fromValue(PreferenceManager.getDefaultSharedPreferences(context).getString(
PREF_EVENT_ENTRY_LAYOUT, ""));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class CalendarEntry extends WidgetEntry {
private static final String SPACE_ARROW = " →";
private static final String ARROW_SPACE = "→ ";
private static final String EMPTY_STRING = "";
private static final String SPACE_DASH_SPACE = " - ";
static final String SPACE_DASH_SPACE = " - ";
private static final String SPACE_PIPE_SPACE = " | ";

private DateTime endDate;
Expand Down Expand Up @@ -90,20 +90,24 @@ public CalendarEvent getEvent() {
return event;
}

public String getEventDetails(Context context) {
if (spansOneFullDay() && !(isStartOfMultiDayEvent()
|| isEndOfMultiDayEvent())
|| isAllDay() && CalendarPreferences.getFillAllDayEvents(context)) {
return "";
} else {
String eventDetails = createTimeSpanString(context);
if (CalendarPreferences.getShowLocation(context) && getLocation() != null && !getLocation().isEmpty()) {
eventDetails += SPACE_PIPE_SPACE + getLocation();
}
return eventDetails;
}
public String getEventDetails(Context context) {
if (hideEventDetails(context)) return "";
String eventDetails = createTimeSpanString(context);
if (CalendarPreferences.getShowLocation(context) && getLocation() != null && !getLocation().isEmpty()) {
eventDetails += SPACE_PIPE_SPACE + getLocation();
}
return eventDetails;
}

String getEventTimeString(Context context) {
return hideEventDetails(context) ? "" : createTimeSpanString(context);
}

private boolean hideEventDetails(Context context) {
return spansOneFullDay() && !(isStartOfMultiDayEvent() || isEndOfMultiDayEvent()) ||
isAllDay() && CalendarPreferences.getFillAllDayEvents(context);
}

private String createTimeSpanString(Context context) {
if (isAllDay() && !CalendarPreferences.getFillAllDayEvents(context)) {
DateTime dateTime = getEvent().getEndDate().minusDays(1);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.plusonelabs.calendar.widget;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.StringRes;
import android.text.TextUtils;
import android.view.View;
import android.widget.RemoteViews;

import com.plusonelabs.calendar.R;
import com.plusonelabs.calendar.RemoteViewsUtil;
import com.plusonelabs.calendar.prefs.CalendarPreferences;

import static com.plusonelabs.calendar.RemoteViewsUtil.setTextColorFromAttr;
import static com.plusonelabs.calendar.RemoteViewsUtil.setTextSize;

/**
* @author [email protected]
*/
public enum EventEntryLayout {
DEFAULT(R.layout.event_entry, "DEFAULT", R.string.default_multiline_layout),
ONE_LINE(R.layout.event_entry_one_line, "ONE_LINE", R.string.single_line_layout) {
@Override
public void visualizeEvent(Context context, CalendarEntry entry, RemoteViews rv) {
setEventDate(context, entry, rv);
setEventTime(context, entry, rv);
}
};

@LayoutRes
public final int layoutId;
public final String value;
@StringRes
public final int summaryResId;

EventEntryLayout(@LayoutRes int layoutId, String value, int summaryResId) {
this.layoutId = layoutId;
this.value = value;
this.summaryResId = summaryResId;
}

public static EventEntryLayout fromValue(String value) {
EventEntryLayout layout = DEFAULT;
for (EventEntryLayout item : EventEntryLayout.values()) {
if (item.value.equals(value)) {
layout = item;
break;
}
}
return layout;
}

public void visualizeEvent(Context context, CalendarEntry entry, RemoteViews rv) {
setEventDetails(context, entry, rv);
}

protected void setEventDate(Context context, CalendarEntry entry, RemoteViews rv) {
if (CalendarPreferences.getShowDayHeaders(context)) {
rv.setViewVisibility(R.id.event_entry_date, View.GONE);
} else {
rv.setViewVisibility(R.id.event_entry_date, View.VISIBLE);
rv.setTextViewText(R.id.event_entry_date, entry.getDateString(context));
}
}

protected void setEventTime(Context context, CalendarEntry entry, RemoteViews rv) {
RemoteViewsUtil.setMultiline(rv, R.id.event_entry_time, CalendarPreferences.getShowEndTime(context));
rv.setTextViewText(R.id.event_entry_time, entry.getEventTimeString(context).replace(CalendarEntry
.SPACE_DASH_SPACE, " "));
}

private void setEventDetails(Context context, CalendarEntry entry, RemoteViews rv) {
String eventDetails = entry.getEventDetails(context);
if (TextUtils.isEmpty(eventDetails)) {
rv.setViewVisibility(R.id.event_entry_details, View.GONE);
} else {
rv.setViewVisibility(R.id.event_entry_details, View.VISIBLE);
rv.setTextViewText(R.id.event_entry_details, eventDetails);
setTextSize(context, rv, R.id.event_entry_details, R.dimen.event_entry_details);
setTextColorFromAttr(context, rv, R.id.event_entry_details, R.attr.eventEntryDetails);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package com.plusonelabs.calendar.widget;

import android.content.Context;

import com.plusonelabs.calendar.DateUtil;
import com.plusonelabs.calendar.R;

import org.joda.time.DateTime;
import org.joda.time.Days;

public class WidgetEntry implements Comparable<WidgetEntry> {

Expand All @@ -23,6 +29,20 @@ public String toString() {
return this.getClass().getSimpleName() + " [startDate=" + startDate + "]";
}

public CharSequence getDateString(Context context) {
Days days = Days.daysBetween(DateUtil.now().withTimeAtStartOfDay(), startDate.withTimeAtStartOfDay());
switch (days.getDays()) {
case -1:
return context.getText(R.string.yesterday);
case 0:
return context.getText(R.string.today);
case 1:
return context.getText(R.string.tomorrow);
default:
return Integer.toString(days.getDays());
}
}

@Override
public int compareTo(WidgetEntry otherEvent) {
if (getStartDate().isAfter(otherEvent.getStartDate())) {
Expand Down
10 changes: 6 additions & 4 deletions app/calendar-widget/src/main/res/layout/event_entry.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
android:layout_alignWithParentIfMissing="true"
android:layout_toLeftOf="@+id/event_entry_indicator_layout_ref"
android:gravity="center_vertical"
tools:ignore="SelectableText" />
tools:text="Event Entry Title. It may be long and not fit in one line"
tools:ignore="SelectableText"/>

<TextView
android:id="@+id/event_entry_details"
Expand All @@ -41,16 +42,17 @@
android:layout_alignParentLeft="true"
android:layout_below="@id/event_entry_title"
android:layout_toLeftOf="@+id/event_entry_indicator_layout_ref"
tools:ignore="SelectableText" />
tools:text="Event Entry Details, which could be quite long and span more than one line"
tools:ignore="SelectableText"/>

<include
android:id="@+id/event_entry_indicator_layout_ref"
layout="@layout/event_entry_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/event_entry_title"
android:layout_alignParentRight="true"
android:layout_alignTop="@id/event_entry_title"
layout="@layout/event_entry_indicator" />
android:layout_alignTop="@id/event_entry_title"/>
</RelativeLayout>

</LinearLayout>
Loading

0 comments on commit 6e5f55d

Please sign in to comment.