Skip to content

Commit

Permalink
pre-select most recently used list from list of writable lists
Browse files Browse the repository at this point in the history
  • Loading branch information
korelstar committed Oct 22, 2016
1 parent 50ffd2c commit 68ae655
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
6 changes: 6 additions & 0 deletions opentasks/src/main/java/org/dmfs/tasks/EditTaskFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.dmfs.tasks.model.TaskFieldAdapters;
import org.dmfs.tasks.utils.ContentValueMapper;
import org.dmfs.tasks.utils.OnModelLoadedListener;
import org.dmfs.tasks.utils.RecentlyUsedLists;
import org.dmfs.tasks.utils.TasksListCursorSpinnerAdapter;
import org.dmfs.tasks.widget.ListenableScrollView;
import org.dmfs.tasks.widget.ListenableScrollView.OnScrollListener;
Expand Down Expand Up @@ -772,6 +773,11 @@ public void saveAndExit()
mValues.ensureUpdates(RECURRENCE_VALUES);
}

if(mValues.isInsert()) {
// update recently used lists
RecentlyUsedLists.use(getContext(), mValues.getAsLong(Tasks.LIST_ID));
}

mTaskUri = mValues.persist(activity);

// return proper result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.dmfs.provider.tasks.TaskContract.Tasks;
import org.dmfs.tasks.model.ContentSet;
import org.dmfs.tasks.model.TaskFieldAdapters;
import org.dmfs.tasks.utils.RecentlyUsedLists;
import org.dmfs.tasks.utils.TasksListCursorSpinnerAdapter;

import android.annotation.TargetApi;
Expand Down Expand Up @@ -358,6 +359,7 @@ private void editTask()
private void createTask()
{
ContentSet content = buildContentSet();
RecentlyUsedLists.use(getContext(), content.getAsLong(Tasks.LIST_ID)); // update recently used lists
content.persist(getActivity());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.dmfs.tasks.R;
import org.dmfs.tasks.TaskListActivity;
import org.dmfs.tasks.model.ContentSet;
import org.dmfs.tasks.utils.RecentlyUsedLists;
import org.dmfs.tasks.utils.WidgetConfigurationDatabaseHelper;

import java.util.ArrayList;
Expand Down Expand Up @@ -103,7 +104,14 @@ else if(action.equals(ACTION_CREATE_TASK))
editTaskIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if(!writableLists.isEmpty())
{
Long preselectList = writableLists.get(0); // always first of list, TODO implement better selection strategy for 2+ lists
Long preselectList;
if(writableLists.size()==1) {
// if there is only one list, then select this one
preselectList = writableLists.get(0);
} else {
// if there are multiple lists, then select the most recently used
preselectList = RecentlyUsedLists.getRecentFromList(context, writableLists);
}
ContentSet contentSet = new ContentSet(Tasks.getContentUri(authority));
contentSet.put(Tasks.LIST_ID, preselectList);
editTaskIntent.putExtra(EditTaskActivity.EXTRA_DATA_CONTENT_SET, contentSet);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.dmfs.tasks.utils;

import android.content.Context;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
* Helper class to record recently used lists in order to provide a pre-selection to the user.
*/
public class RecentlyUsedLists {
public static final String PREFERENCE_KEY = "RecentlyUsedLists";

/**
* Gets the lists of TaskLists ordered by recently use.
* @param context Context
* @return List of TaskLists where the most recently used list is on position 0.
*/
public static List<Long> getList(Context context) {
String strLists = PreferenceManager.getDefaultSharedPreferences(context).getString(PREFERENCE_KEY, "");
Log.d(RecentlyUsedLists.class.getSimpleName(), "getList: "+strLists);
List<Long> lists = new ArrayList<>();
if(strLists.length()>0) {
for (String lid : strLists.split(",")) {
lists.add(Long.parseLong(lid));
}
}
return lists;
}

/**
* Saves the ordered lists of TaskLists.
* @param context Context
* @param lists List of TaskLists where the most recently used list is on position 0.
*/
private static void setList(Context context, List<Long> lists) {
String strLists = TextUtils.join(",", lists);
Log.d(RecentlyUsedLists.class.getSimpleName(), "setList: "+strLists);
PreferenceManager.getDefaultSharedPreferences(context).edit().putString(PREFERENCE_KEY, strLists).commit();
}

/**
* Searches for the best suitable TaskList in dependence of which is most recently used.
* @param context Context
* @param allowedLists List of TaskLists
* @return The most recently used TaskLists from <code>allowedLists</code>
*/
public static Long getRecentFromList(Context context, List<Long> allowedLists) {
List<Long> recentlyLists = getList(context);
for (Long listId : recentlyLists) {
if(allowedLists.contains(listId)) {
return listId;
}
}
return allowedLists.get(0);
}

/**
* Mark a TaskLists as "used", which means that as new task was just created.
* @param context Context
* @param listId Id of the TaskList, where a task was just created.
*/
public static void use(Context context, Long listId) {
List<Long> lists = getList(context);
lists.remove(listId); // does nothing, if "listId" is not in "lists"
lists.add(0, listId);
setList(context, lists);
}
}

0 comments on commit 68ae655

Please sign in to comment.