Remove date caching

pull/384/head
Alex Baker 9 years ago
parent 568f9637de
commit 6914fe6ebe

@ -8,6 +8,8 @@ package com.todoroo.andlib.utility;
import android.content.Context;
import android.text.format.DateFormat;
import com.todoroo.astrid.data.Task;
import org.tasks.R;
import org.tasks.time.DateTime;
@ -159,6 +161,15 @@ public class DateUtilities {
return getDateString(date) + ", " + getTimeString(context, date);
}
public static String getRelativeDateStringWithTime(Context context, long timestamp) {
String string = DateUtilities.getRelativeDay(context, timestamp, false);
if (Task.hasDueTime(timestamp)) {
string = String.format("%s %s", string, //$NON-NLS-1$
DateUtilities.getTimeString(context, timestamp));
}
return string;
}
/**
* @return yesterday, today, tomorrow, or null
*/

@ -50,7 +50,6 @@ import org.tasks.dialogs.DialogBuilder;
import org.tasks.preferences.ActivityPreferences;
import org.tasks.ui.CheckBoxes;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicReference;
import timber.log.Timber;
@ -399,28 +398,6 @@ public class TaskAdapter extends CursorAdapter implements Filterable {
filesControlSet.getView().performClick();
}
/* ======================================================================
* ============================================================== details
* ====================================================================== */
private final HashMap<Long, String> dateCache = new HashMap<>(8);
private String formatDate(long date) {
if(dateCache.containsKey(date)) {
return dateCache.get(date);
}
String formatString = "%s %s";
String string = DateUtilities.getRelativeDay(fragment.getActivity(), date, true);
if(Task.hasDueTime(date)) {
string = String.format(formatString, string, //$NON-NLS-1$
DateUtilities.getTimeString(fragment.getActivity(), date));
}
dateCache.put(date, string);
return string;
}
/* ======================================================================
* ======================================================= event handlers
* ====================================================================== */
@ -530,11 +507,11 @@ public class TaskAdapter extends CursorAdapter implements Filterable {
} else {
dueDateView.setTextAppearance(activity, R.style.TextAppearance_TAd_ItemDueDate);
}
String dateValue = formatDate(dueDate);
String dateValue = DateUtilities.getRelativeDateStringWithTime(context, dueDate);
dueDateView.setText(dateValue);
dueDateView.setVisibility(View.VISIBLE);
} else if(task.isCompleted()) {
String dateValue = formatDate(task.getCompletionDate());
String dateValue = DateUtilities.getRelativeDateStringWithTime(context, task.getCompletionDate());
dueDateView.setText(resources.getString(R.string.TAd_completed, dateValue));
dueDateView.setTextAppearance(activity, R.style.TextAppearance_TAd_ItemDueDate_Completed);
dueDateView.setVisibility(View.VISIBLE);

@ -5,12 +5,10 @@
*/
package com.todoroo.astrid.widget;
import android.annotation.TargetApi;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.astrid.activity.TaskEditFragment;
@ -85,7 +83,6 @@ public class TasksWidget extends InjectingAppWidgetProvider {
updateScrollableWidgets(context, null);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static void updateScrollableWidgets(Context context, int[] widgetIds) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
if (widgetIds == null) {

@ -1,58 +0,0 @@
package org.tasks.widget;
import android.content.Context;
import android.content.res.Resources;
import android.view.View;
import android.widget.RemoteViews;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.astrid.data.Task;
import org.tasks.R;
import org.tasks.injection.ForApplication;
import java.util.HashMap;
import java.util.Map;
import javax.inject.Inject;
public class DueDateFormatter {
private final Map<Long, String> dateCache = new HashMap<>();
private final Context context;
private final Resources resources;
@Inject
public DueDateFormatter(@ForApplication Context context) {
this.context = context;
resources = context.getResources();
}
public void formatDueDate(RemoteViews row, Task task, int textColor) {
if (task.hasDueDate() || task.hasDueTime()) {
row.setViewVisibility(R.id.dueDate, View.VISIBLE);
row.setTextViewText(R.id.dueDate, task.isCompleted()
? resources.getString(R.string.TAd_completed, formatDate(task.getCompletionDate()))
: formatDate(task.getDueDate()));
row.setTextColor(R.id.dueDate, task.isOverdue() ? resources.getColor(R.color.overdue) : textColor);
} else {
row.setViewVisibility(R.id.dueDate, View.GONE);
}
}
private String formatDate(long date) {
if (dateCache.containsKey(date)) {
return dateCache.get(date);
}
String formatString = "%s %s";
String string = DateUtilities.getRelativeDay(context, date, false);
if (Task.hasDueTime(date)) {
string = String.format(formatString, string, //$NON-NLS-1$
DateUtilities.getTimeString(context, date));
}
dateCache.put(date, string);
return string;
}
}

@ -13,6 +13,7 @@ import android.widget.RemoteViews;
import android.widget.RemoteViewsService;
import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.astrid.activity.TaskEditFragment;
import com.todoroo.astrid.activity.TaskListActivity;
import com.todoroo.astrid.activity.TaskListFragment;
@ -28,11 +29,8 @@ import com.todoroo.astrid.widget.WidgetConfigActivity;
import org.tasks.R;
import org.tasks.preferences.Preferences;
import org.tasks.ui.CheckBoxes;
import org.tasks.ui.WidgetCheckBoxes;
import java.util.List;
import timber.log.Timber;
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@ -49,7 +47,6 @@ public class ScrollableViewsFactory implements RemoteViewsService.RemoteViewsFac
private final boolean dark;
private final boolean showDueDates;
private final boolean hideCheckboxes;
private final DueDateFormatter dueDateFormatter;
private TodorooCursor<Task> cursor;
@ -70,7 +67,6 @@ public class ScrollableViewsFactory implements RemoteViewsService.RemoteViewsFac
this.taskService = taskService;
checkBoxes = new WidgetCheckBoxes(context);
dueDateFormatter = new DueDateFormatter(context);
dark = preferences.useDarkWidgetTheme(widgetId);
showDueDates = preferences.getBoolean(WidgetConfigActivity.PREF_SHOW_DUE_DATE + widgetId, false);
hideCheckboxes = preferences.getBoolean(WidgetConfigActivity.PREF_HIDE_CHECKBOXES + widgetId, false);
@ -156,7 +152,7 @@ public class ScrollableViewsFactory implements RemoteViewsService.RemoteViewsFac
}
if (showDueDates) {
dueDateFormatter.formatDueDate(row, task, textColor);
formatDueDate(row, task, textColor);
} else if (task.hasDueDate() && task.isOverdue()) {
textColor = r.getColor(R.color.overdue);
}
@ -216,4 +212,17 @@ public class ScrollableViewsFactory implements RemoteViewsService.RemoteViewsFac
return subtasksHelper.applySubtasksToWidgetFilter(filter, query, tagName, 0);
}
public void formatDueDate(RemoteViews row, Task task, int textColor) {
if (task.hasDueDate()) {
Resources resources = context.getResources();
row.setViewVisibility(R.id.dueDate, View.VISIBLE);
row.setTextViewText(R.id.dueDate, task.isCompleted()
? resources.getString(R.string.TAd_completed, DateUtilities.getRelativeDateStringWithTime(context, task.getCompletionDate()))
: DateUtilities.getRelativeDateStringWithTime(context, task.getDueDate()));
row.setTextColor(R.id.dueDate, task.isOverdue() ? resources.getColor(R.color.overdue) : textColor);
} else {
row.setViewVisibility(R.id.dueDate, View.GONE);
}
}
}

Loading…
Cancel
Save