Add option to set default list

Closes #111
Closes #284
Closes #256
pull/384/head
Alex Baker 9 years ago
parent d91da502d0
commit 16e117719f

@ -23,7 +23,6 @@ import com.todoroo.astrid.api.Filter;
import com.todoroo.astrid.api.FilterListItem;
import com.todoroo.astrid.api.FilterWithCustomIntent;
import com.todoroo.astrid.api.PermaSql;
import com.todoroo.astrid.core.BuiltInFilterExposer;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.repeats.RepeatControlSet;
import com.todoroo.astrid.service.StartupService;
@ -38,6 +37,7 @@ import org.tasks.fragments.TaskEditControlSetFragmentManager;
import org.tasks.injection.InjectingAppCompatActivity;
import org.tasks.intents.TaskIntents;
import org.tasks.preferences.ActivityPreferences;
import org.tasks.preferences.DefaultFilterProvider;
import org.tasks.receivers.RepeatConfirmationReceiver;
import org.tasks.ui.EmptyTaskEditFragment;
import org.tasks.ui.NavigationDrawerFragment;
@ -68,6 +68,7 @@ public class TaskListActivity extends InjectingAppCompatActivity implements
@Inject TaskService taskService;
@Inject TaskEditControlSetFragmentManager taskEditControlSetFragmentManager;
@Inject RepeatConfirmationReceiver repeatConfirmationReceiver;
@Inject DefaultFilterProvider defaultFilterProvider;
public static final int REQUEST_UPGRADE = 505;
@ -129,7 +130,7 @@ public class TaskListActivity extends InjectingAppCompatActivity implements
} else {
taskListFragment = getTaskListFragment();
if (taskListFragment == null) {
taskListFragment = newTaskListFragment(getDefaultFilter());
taskListFragment = newTaskListFragment(defaultFilterProvider.getDefaultFilter());
}
}
loadTaskListFragment(taskListFragment);
@ -196,10 +197,6 @@ public class TaskListActivity extends InjectingAppCompatActivity implements
AndroidUtilities.tryUnregisterReceiver(this, repeatConfirmationReceiver);
}
protected Filter getDefaultFilter() {
return BuiltInFilterExposer.getMyTasksFilter(getResources());
}
@Override
public void onFilterItemClicked(FilterListItem item) {
TaskEditFragment tef = getTaskEditFragment();

@ -18,6 +18,7 @@ import javax.inject.Inject;
public class FilterSelectionActivity extends InjectingAppCompatActivity {
public static final String EXTRA_FILTER = "extra_filter";
public static final String EXTRA_FILTER_NAME = "extra_filter_name";
public static final String EXTRA_FILTER_SQL = "extra_filter_query";
public static final String EXTRA_FILTER_VALUES = "extra_filter_values";
@ -42,6 +43,7 @@ public class FilterSelectionActivity extends InjectingAppCompatActivity {
public void onClick(DialogInterface dialog, int which) {
final Filter selectedFilter = (Filter) filterAdapter.getItem(which);
setResult(RESULT_OK, new Intent() {{
putExtra(EXTRA_FILTER, selectedFilter);
putExtra(EXTRA_FILTER_NAME, selectedFilter.listingTitle);
putExtra(EXTRA_FILTER_SQL, selectedFilter.getSqlQuery());
if (selectedFilter.valuesForNewTasks != null) {

@ -5,18 +5,26 @@ import android.os.Bundle;
import android.preference.Preference;
import com.todoroo.astrid.activity.BeastModePreferences;
import com.todoroo.astrid.api.Filter;
import org.tasks.R;
import org.tasks.activities.FilterSelectionActivity;
import org.tasks.injection.InjectingPreferenceActivity;
import javax.inject.Inject;
public class AppearancePreferences extends InjectingPreferenceActivity {
private static final int REQUEST_CUSTOMIZE = 1004;
private static final int REQUEST_DEFAULT_LIST = 1005;
private static final String EXTRA_BUNDLE = "extra_bundle";
public static String EXTRA_RESTART = "extra_restart";
public static String EXTRA_FILTERS_CHANGED = "extra_filters_changed";
@Inject Preferences preferences;
@Inject DefaultFilterProvider defaultFilterProvider;
private Bundle result;
@Override
@ -42,6 +50,16 @@ public class AppearancePreferences extends InjectingPreferenceActivity {
return true;
}
});
Preference defaultList = findPreference(getString(R.string.p_default_list_name));
Filter filter = defaultFilterProvider.getDefaultFilter();
defaultList.setSummary(filter.listingTitle);
defaultList.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
startActivityForResult(new Intent(AppearancePreferences.this, FilterSelectionActivity.class), REQUEST_DEFAULT_LIST);
return true;
}
});
}
@Override
@ -65,6 +83,12 @@ public class AppearancePreferences extends InjectingPreferenceActivity {
if (resultCode == RESULT_OK) {
result.putBoolean(EXTRA_RESTART, true);
}
} else if (requestCode == REQUEST_DEFAULT_LIST) {
if (resultCode == RESULT_OK) {
Filter filter = data.getParcelableExtra(FilterSelectionActivity.EXTRA_FILTER);
defaultFilterProvider.setDefaultFilter(filter);
findPreference(getString(R.string.p_default_list_name)).setSummary(filter.listingTitle);
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}

@ -0,0 +1,83 @@
package org.tasks.preferences;
import android.content.ComponentName;
import android.content.ContentValues;
import android.content.Context;
import com.todoroo.andlib.utility.AndroidUtilities;
import com.todoroo.astrid.api.CustomFilter;
import com.todoroo.astrid.api.Filter;
import com.todoroo.astrid.api.FilterWithCustomIntent;
import com.todoroo.astrid.core.BuiltInFilterExposer;
import org.tasks.R;
import org.tasks.injection.ForApplication;
import javax.inject.Inject;
import timber.log.Timber;
import static com.google.common.base.Strings.isNullOrEmpty;
public class DefaultFilterProvider {
private static final int TYPE_FILTER = 0;
private static final int TYPE_CUSTOM_FILTER = 1;
private static final int TYPE_FILTER_WITH_CUSTOM_INTENT = 2;
private final Context context;
private final Preferences preferences;
@Inject
public DefaultFilterProvider(@ForApplication Context context, Preferences preferences) {
this.context = context;
this.preferences = preferences;
}
public Filter getDefaultFilter() {
String listName = preferences.getStringValue(R.string.p_default_list_name);
if (!isNullOrEmpty(listName)) {
try {
ContentValues valuesForNewTasks = AndroidUtilities.contentValuesFromSerializedString(preferences.getStringValue(R.string.p_default_list_values));
String sqlQuery = preferences.getStringValue(R.string.p_default_list_sql);
switch (preferences.getInt(R.string.p_default_list_type)) {
case TYPE_FILTER_WITH_CUSTOM_INTENT:
FilterWithCustomIntent filterWithCustomIntent = new FilterWithCustomIntent(listName, sqlQuery, valuesForNewTasks);
filterWithCustomIntent.customExtras = AndroidUtilities.bundleFromSerializedString(preferences.getStringValue(R.string.p_default_list_extras));
filterWithCustomIntent.customTaskList = ComponentName.unflattenFromString(preferences.getStringValue(R.string.p_default_list_class));
return filterWithCustomIntent;
case TYPE_CUSTOM_FILTER:
return new CustomFilter(listName, sqlQuery, valuesForNewTasks, preferences.getLong(R.string.p_default_list_id, 0L));
}
return new Filter(listName, sqlQuery, valuesForNewTasks);
} catch (Exception e) {
Timber.e(e, e.getMessage());
}
}
return BuiltInFilterExposer.getMyTasksFilter(context.getResources());
}
public void setDefaultFilter(Filter filter) {
preferences.setString(R.string.p_default_list_name, filter.listingTitle);
preferences.setString(R.string.p_default_list_sql, filter.getSqlQuery());
preferences.setInt(R.string.p_default_list_type, getFilterType(filter));
preferences.setString(R.string.p_default_list_values, filter.valuesForNewTasks != null
? AndroidUtilities.contentValuesToSerializedString(filter.valuesForNewTasks)
: null);
if (filter instanceof FilterWithCustomIntent) {
preferences.setString(R.string.p_default_list_class, ((FilterWithCustomIntent) filter).customTaskList.flattenToString());
preferences.setString(R.string.p_default_list_extras, AndroidUtilities.bundleToSerializedString(((FilterWithCustomIntent) filter).customExtras));
} else if (filter instanceof CustomFilter) {
preferences.setLong(R.string.p_default_list_id, ((CustomFilter) filter).getId());
}
}
private int getFilterType(Filter filter) {
if (filter instanceof FilterWithCustomIntent) {
return TYPE_FILTER_WITH_CUSTOM_INTENT;
} else if (filter instanceof CustomFilter) {
return TYPE_CUSTOM_FILTER;
}
return TYPE_FILTER;
}
}

@ -237,10 +237,18 @@ public class Preferences {
editor.commit();
}
public long getLong(int resourceId, long defValue) {
return getLong(context.getString(resourceId), defValue);
}
public long getLong(String key, long defValue) {
return prefs.getLong(key, defValue);
}
public void setLong(int resourceId, long value) {
setLong(context.getString(resourceId), value);
}
public void setLong(String key, long value) {
Editor editor = prefs.edit();
editor.putLong(key, value);

@ -280,4 +280,12 @@
<string name="p_collect_statistics">collect_statistics</string>
<string name="p_back_button_saves_task">back_button_saves_task</string>
<string name="p_default_list_name">default_list_name</string>
<string name="p_default_list_sql">default_list_sql</string>
<string name="p_default_list_values">default_list_values</string>
<string name="p_default_list_type">default_list_type</string>
<string name="p_default_list_id">default_list_id</string>
<string name="p_default_list_class">default_list_class</string>
<string name="p_default_list_extras">default_list_extras</string>
</resources>

@ -898,6 +898,7 @@ File %1$s contained %2$s.\n\n
<string name="doze_notifications_on">Android will allow limited interruptions while device is in Doze mode</string>
<string name="no_title">(No title)</string>
<string name="back_button_saves_task">Back button saves task</string>
<string name="default_list">Default list</string>
<string-array name="sync_SPr_interval_entries">
<!-- sync_SPr_interval_entries: Synchronization Intervals -->

@ -8,6 +8,11 @@
android:title="@string/EPr_use_dark_theme" />
<PreferenceCategory android:title="@string/task_list_options">
<com.todoroo.astrid.ui.MultilinePreference
android:key="@string/p_default_list_name"
android:title="@string/default_list" />
<com.todoroo.astrid.ui.MultilineListPreference
android:defaultValue="16"
android:entries="@array/EPr_font_size"

Loading…
Cancel
Save