mirror of https://github.com/tasks/tasks
Merge pull request #174 from sbosley/120302_sb_swipe_lists_subtasks
Swipe between lists! Woooopull/14/head
commit
0f13cf01f7
@ -0,0 +1,121 @@
|
||||
package com.todoroo.astrid.adapter;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.os.Parcelable;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.support.v4.app.FragmentStatePagerAdapter;
|
||||
|
||||
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;
|
||||
|
||||
public class TaskListFragmentPagerAdapter extends FragmentStatePagerAdapter implements FilterDataSourceChangedListener {
|
||||
|
||||
private final HashMap<Integer, Fragment> positionToFragment;
|
||||
private final HashMap<Filter, Class<?>> customTaskLists;
|
||||
|
||||
private final FilterAdapter filterAdapter; // Shares an adapter instance with the filter list fragment
|
||||
|
||||
public TaskListFragmentPagerAdapter(FragmentManager fm, FilterAdapter filterAdapter) {
|
||||
super(fm);
|
||||
this.filterAdapter = filterAdapter;
|
||||
filterAdapter.setDataSourceChangedListener(this);
|
||||
positionToFragment = new HashMap<Integer, Fragment>();
|
||||
customTaskLists = new HashMap<Filter, Class<?>>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void filterDataSourceChanged() {
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates and returns a fragment for the filter at the specified position.
|
||||
* Also maps the position to the fragment in a cache for later lookup
|
||||
*/
|
||||
@Override
|
||||
public Fragment getItem(int position) {
|
||||
Filter filter = filterAdapter.getItem(position);
|
||||
Fragment fragment = getFragmentForFilter(filter);
|
||||
positionToFragment.put(position, fragment);
|
||||
return fragment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lookup the fragment for the specified position
|
||||
* @param position
|
||||
* @return
|
||||
*/
|
||||
public Fragment lookupFragmentForPosition(int position) {
|
||||
return positionToFragment.get(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getPageTitle(int position) {
|
||||
return filterAdapter.getItem(position).title;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the specified filter to the data source if it doesn't exist,
|
||||
* returning the position of that filter regardless
|
||||
* @param filter
|
||||
* @return
|
||||
*/
|
||||
public int addOrLookup(Filter filter) {
|
||||
return filterAdapter.addOrLookup(filter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the filter at the specified position
|
||||
* @param position
|
||||
* @return
|
||||
*/
|
||||
public Filter getFilter(int position) {
|
||||
return filterAdapter.getItem(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
return filterAdapter.getCount();
|
||||
}
|
||||
|
||||
public void setCustomTaskListForFilter(Filter f, Class<?> customTaskList) {
|
||||
customTaskLists.put(f, customTaskList);
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
public void clearCustomTaskListForFilter(Filter f) {
|
||||
customTaskLists.remove(f);
|
||||
}
|
||||
|
||||
private Fragment getFragmentForFilter(Filter filter) {
|
||||
Bundle extras = getExtrasForFilter(filter);
|
||||
Class<?> customList = customTaskLists.get(filter);
|
||||
if (customList == null)
|
||||
customList = TaskListFragment.class;
|
||||
return TaskListFragment.instantiateWithFilterAndExtras(filter, extras, customList);
|
||||
}
|
||||
|
||||
// Constructs extras corresponding to the specified filter that can be used as arguments to the fragment
|
||||
private Bundle getExtrasForFilter(Filter filter) {
|
||||
Bundle extras;
|
||||
if (filter instanceof FilterWithCustomIntent) {
|
||||
extras = ((FilterWithCustomIntent) filter).customExtras;
|
||||
} else {
|
||||
extras = new Bundle();
|
||||
}
|
||||
if (filter != null)
|
||||
extras.putParcelable(TaskListFragment.TOKEN_FILTER, filter);
|
||||
return extras;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Parcelable saveState() {
|
||||
return null; // Don't save state
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
package com.todoroo.astrid.ui;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.v4.view.PagerAdapter;
|
||||
import android.support.v4.view.ViewPager;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.MotionEvent;
|
||||
|
||||
import com.todoroo.astrid.activity.TaskListFragment;
|
||||
import com.todoroo.astrid.adapter.TaskListFragmentPagerAdapter;
|
||||
import com.todoroo.astrid.api.Filter;
|
||||
import com.todoroo.astrid.utility.Flags;
|
||||
|
||||
public class TaskListFragmentPager extends ViewPager {
|
||||
|
||||
public TaskListFragmentPager(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAdapter(PagerAdapter adapter) {
|
||||
if (!(adapter instanceof TaskListFragmentPagerAdapter))
|
||||
throw new ClassCastException("TaskListFragmentPager requires an adapter of type TaskListFragmentPagerAdapter"); //$NON-NLS-1$
|
||||
super.setAdapter(adapter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the supplied filter, adding it to the data source if it doesn't exist
|
||||
* @param f
|
||||
*/
|
||||
public void showFilter(Filter f) {
|
||||
TaskListFragmentPagerAdapter adapter = (TaskListFragmentPagerAdapter) getAdapter();
|
||||
showFilter(adapter.addOrLookup(f));
|
||||
}
|
||||
|
||||
public void showFilterWithCustomTaskList(Filter f, Class<?> customTaskList) {
|
||||
TaskListFragmentPagerAdapter adapter = (TaskListFragmentPagerAdapter) getAdapter();
|
||||
adapter.setCustomTaskListForFilter(f, customTaskList);
|
||||
showFilter(adapter.addOrLookup(f));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the filter at the supplied index, with animation
|
||||
* @param index
|
||||
*/
|
||||
public void showFilter(int index) {
|
||||
setCurrentItem(index, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the currently showing fragment
|
||||
* @return
|
||||
*/
|
||||
public TaskListFragment getCurrentFragment() {
|
||||
return (TaskListFragment) ((TaskListFragmentPagerAdapter) getAdapter()).lookupFragmentForPosition(getCurrentItem());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onInterceptTouchEvent(MotionEvent ev) {
|
||||
if (!Flags.check(Flags.TLFP_NO_INTERCEPT_TOUCH))
|
||||
return super.onInterceptTouchEvent(ev);
|
||||
return false;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue