Refactored static subtasks methods into a subtasks helper class

pull/14/head
Sam Bosley 12 years ago
parent 6f2635c9d6
commit b62e1755f1

@ -0,0 +1,82 @@
package com.todoroo.astrid.subtasks;
import android.content.SharedPreferences;
import com.todoroo.andlib.service.ContextManager;
import com.todoroo.andlib.sql.Criterion;
import com.todoroo.astrid.actfm.TagViewFragment;
import com.todoroo.astrid.api.Filter;
import com.todoroo.astrid.api.FilterWithCustomIntent;
import com.todoroo.astrid.core.CoreFilterExposer;
import com.todoroo.astrid.core.PluginServices;
import com.todoroo.astrid.core.SortHelper;
import com.todoroo.astrid.dao.TaskDao.TaskCriteria;
import com.todoroo.astrid.data.Metadata;
import com.todoroo.astrid.data.TagData;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.utility.AstridPreferences;
public class SubtasksHelper {
public static boolean shouldUseSubtasksFragmentForFilter(Filter filter) {
if(filter == null || CoreFilterExposer.isInbox(filter) || SubtasksHelper.isTagFilter(filter)) {
SharedPreferences publicPrefs = AstridPreferences.getPublicPrefs(ContextManager.getContext());
int sortFlags = publicPrefs.getInt(SortHelper.PREF_SORT_FLAGS, 0);
if(SortHelper.isManualSort(sortFlags))
return true;
}
return false;
}
public static Class<?> subtasksClassForFilter(Filter filter) {
if (SubtasksHelper.isTagFilter(filter))
return SubtasksTagListFragment.class;
return SubtasksListFragment.class;
}
public static boolean isTagFilter(Filter filter) {
if (filter instanceof FilterWithCustomIntent) {
String className = ((FilterWithCustomIntent) filter).customTaskList.getClassName();
if (TagViewFragment.class.getName().equals(className)
|| SubtasksTagListFragment.class.getName().equals(className)) // Need to check this subclass because some shortcuts/widgets may have been saved with it
return true;
}
return false;
}
@SuppressWarnings("nls")
public static String applySubtasksToWidgetFilter(Filter filter, String query, String tagName, int limit) {
if (SubtasksHelper.shouldUseSubtasksFragmentForFilter(filter)) {
// care for manual ordering
if(tagName == null)
tagName = SubtasksMetadata.LIST_ACTIVE_TASKS;
else {
TagData tag = PluginServices.getTagDataService().getTag(tagName, TagData.PROPERTIES);
if (tag != null)
tagName = "td:"+tag.getId();
else
tagName = SubtasksMetadata.LIST_ACTIVE_TASKS;
}
String subtaskJoin = String.format("LEFT JOIN %s ON (%s = %s AND %s = '%s' AND %s = '%s') ",
Metadata.TABLE, Task.ID, Metadata.TASK,
Metadata.KEY, SubtasksMetadata.METADATA_KEY,
SubtasksMetadata.TAG, tagName);
if(!query.contains(subtaskJoin)) {
query = subtaskJoin + query;
query = query.replaceAll("ORDER BY .*", "");
query = query + String.format(" ORDER BY %s, %s, IFNULL(CAST(%s AS LONG), %s)",
Task.DELETION_DATE, Task.COMPLETION_DATE,
SubtasksMetadata.ORDER, Task.CREATION_DATE);
if (limit > 0)
query = query + " LIMIT " + limit;
query = query.replace(TaskCriteria.isVisible().toString(),
Criterion.all.toString());
filter.setFilterQueryOverride(query);
}
}
return query;
}
}

@ -9,7 +9,6 @@ import android.app.PendingIntent.CanceledException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.speech.SpeechRecognizer;
import android.support.v4.app.Fragment;
@ -27,26 +26,21 @@ import com.todoroo.andlib.service.ContextManager;
import com.todoroo.andlib.utility.AndroidUtilities;
import com.todoroo.andlib.utility.DialogUtilities;
import com.todoroo.astrid.actfm.TagUpdatesFragment;
import com.todoroo.astrid.actfm.TagViewFragment;
import com.todoroo.astrid.api.AstridApiConstants;
import com.todoroo.astrid.api.Filter;
import com.todoroo.astrid.api.FilterListItem;
import com.todoroo.astrid.api.FilterWithCustomIntent;
import com.todoroo.astrid.api.IntentFilter;
import com.todoroo.astrid.core.CoreFilterExposer;
import com.todoroo.astrid.core.PluginServices;
import com.todoroo.astrid.core.SearchFilter;
import com.todoroo.astrid.core.SortHelper;
import com.todoroo.astrid.data.TagData;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.service.StartupService;
import com.todoroo.astrid.service.StatisticsConstants;
import com.todoroo.astrid.service.StatisticsService;
import com.todoroo.astrid.subtasks.SubtasksListFragment;
import com.todoroo.astrid.subtasks.SubtasksTagListFragment;
import com.todoroo.astrid.subtasks.SubtasksHelper;
import com.todoroo.astrid.ui.DateChangedAlerts;
import com.todoroo.astrid.ui.QuickAddBar;
import com.todoroo.astrid.utility.AstridPreferences;
import com.todoroo.astrid.voice.VoiceRecognizer;
/**
@ -210,8 +204,8 @@ public class AstridActivity extends FragmentActivity
public void setupTasklistFragmentWithFilter(Filter filter, Bundle extras) {
Class<?> customTaskList = null;
if (shouldUseSubtasksFragmentForFilter(filter))
customTaskList = subtasksClassForFilter(filter);
if (SubtasksHelper.shouldUseSubtasksFragmentForFilter(filter))
customTaskList = SubtasksHelper.subtasksClassForFilter(filter);
setupTasklistFragmentWithFilterAndCustomTaskList(filter, extras, customTaskList);
}
@ -237,32 +231,6 @@ public class AstridActivity extends FragmentActivity
}
}
public static boolean shouldUseSubtasksFragmentForFilter(Filter filter) {
if(filter == null || CoreFilterExposer.isInbox(filter) || isTagFilter(filter)) {
SharedPreferences publicPrefs = AstridPreferences.getPublicPrefs(ContextManager.getContext());
int sortFlags = publicPrefs.getInt(SortHelper.PREF_SORT_FLAGS, 0);
if(SortHelper.isManualSort(sortFlags))
return true;
}
return false;
}
public static Class<?> subtasksClassForFilter(Filter filter) {
if (isTagFilter(filter))
return SubtasksTagListFragment.class;
return SubtasksListFragment.class;
}
public static boolean isTagFilter(Filter filter) {
if (filter instanceof FilterWithCustomIntent) {
String className = ((FilterWithCustomIntent) filter).customTaskList.getClassName();
if (TagViewFragment.class.getName().equals(className)
|| SubtasksTagListFragment.class.getName().equals(className)) // Need to check this subclass because some shortcuts/widgets may have been saved with it
return true;
}
return false;
}
@Override
public void onTaskListItemClicked(long taskId) {
Intent intent = new Intent(this, TaskEditActivity.class);

@ -13,11 +13,11 @@ import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import com.todoroo.astrid.activity.AstridActivity;
import com.todoroo.astrid.activity.TaskListFragment;
import com.todoroo.astrid.adapter.FilterAdapter.FilterDataSourceChangedListener;
import com.todoroo.astrid.api.Filter;
import com.todoroo.astrid.api.FilterWithCustomIntent;
import com.todoroo.astrid.subtasks.SubtasksHelper;
public class TaskListFragmentPagerAdapter extends FragmentStatePagerAdapter implements FilterDataSourceChangedListener {
@ -98,8 +98,8 @@ public class TaskListFragmentPagerAdapter extends FragmentStatePagerAdapter impl
private Fragment getFragmentForFilter(Filter filter) {
Bundle extras = getExtrasForFilter(filter);
Class<?> customList = null;
if (AstridActivity.shouldUseSubtasksFragmentForFilter(filter))
customList = AstridActivity.subtasksClassForFilter(filter);
if (SubtasksHelper.shouldUseSubtasksFragmentForFilter(filter))
customList = SubtasksHelper.subtasksClassForFilter(filter);
return TaskListFragment.instantiateWithFilterAndExtras(filter, extras, customList);
}

@ -28,12 +28,10 @@ import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.service.ContextManager;
import com.todoroo.andlib.service.DependencyInjectionService;
import com.todoroo.andlib.sql.Criterion;
import com.todoroo.andlib.utility.AndroidUtilities;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.actfm.TagViewFragment;
import com.todoroo.astrid.activity.AstridActivity;
import com.todoroo.astrid.activity.TaskEditActivity;
import com.todoroo.astrid.activity.TaskEditFragment;
import com.todoroo.astrid.activity.TaskListActivity;
@ -44,15 +42,12 @@ import com.todoroo.astrid.api.PermaSql;
import com.todoroo.astrid.core.CoreFilterExposer;
import com.todoroo.astrid.core.SortHelper;
import com.todoroo.astrid.dao.Database;
import com.todoroo.astrid.dao.TaskDao.TaskCriteria;
import com.todoroo.astrid.data.Metadata;
import com.todoroo.astrid.data.TagData;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.service.AstridDependencyInjector;
import com.todoroo.astrid.service.TagDataService;
import com.todoroo.astrid.service.TaskService;
import com.todoroo.astrid.service.ThemeService;
import com.todoroo.astrid.subtasks.SubtasksMetadata;
import com.todoroo.astrid.subtasks.SubtasksHelper;
import com.todoroo.astrid.utility.AstridPreferences;
import com.todoroo.astrid.utility.Constants;
@ -175,7 +170,7 @@ public class TasksWidget extends AppWidgetProvider {
Filter filter = null;
try {
filter = getFilter(widgetId);
if (AstridActivity.isTagFilter(filter))
if (SubtasksHelper.isTagFilter(filter))
((FilterWithCustomIntent) filter).customTaskList = new ComponentName(context, TagViewFragment.class); // In case legacy widget was created with subtasks fragment
views.setTextViewText(R.id.widget_title, filter.title);
views.removeAllViews(R.id.taskbody);
@ -186,36 +181,8 @@ public class TasksWidget extends AppWidgetProvider {
String query = SortHelper.adjustQueryForFlagsAndSort(
filter.getSqlQuery(), flags, sort).replaceAll("LIMIT \\d+", "") + " LIMIT " + numberOfTasks;
if (AstridActivity.shouldUseSubtasksFragmentForFilter(filter)) {
// care for manual ordering
String tagName = Preferences.getStringValue(WidgetConfigActivity.PREF_TITLE + widgetId);
if(tagName == null)
tagName = SubtasksMetadata.LIST_ACTIVE_TASKS;
else {
TagData tag = tagDataService.getTag(tagName, TagData.PROPERTIES);
if (tag != null)
tagName = "td:"+tag.getId();
else
tagName = SubtasksMetadata.LIST_ACTIVE_TASKS;
}
String subtaskJoin = String.format("LEFT JOIN %s ON (%s = %s AND %s = '%s' AND %s = '%s') ",
Metadata.TABLE, Task.ID, Metadata.TASK,
Metadata.KEY, SubtasksMetadata.METADATA_KEY,
SubtasksMetadata.TAG, tagName);
if(!query.contains(subtaskJoin)) {
query = subtaskJoin + query;
query = query.replaceAll("ORDER BY .*", "");
query = query + String.format(" ORDER BY %s, %s, IFNULL(CAST(%s AS LONG), %s) LIMIT %d",
Task.DELETION_DATE, Task.COMPLETION_DATE,
SubtasksMetadata.ORDER, Task.CREATION_DATE,
numberOfTasks);
query = query.replace(TaskCriteria.isVisible().toString(),
Criterion.all.toString());
filter.setFilterQueryOverride(query);
}
}
String tagName = Preferences.getStringValue(WidgetConfigActivity.PREF_TITLE + widgetId);
query = SubtasksHelper.applySubtasksToWidgetFilter(filter, query, tagName, numberOfTasks);
database.openForReading();
cursor = taskService.fetchFiltered(query, null, Task.ID, Task.TITLE, Task.DUE_DATE, Task.COMPLETION_DATE);

Loading…
Cancel
Save