diff --git a/astrid/src/com/todoroo/astrid/adapter/TaskListFragmentPagerAdapter.java b/astrid/src/com/todoroo/astrid/adapter/TaskListFragmentPagerAdapter.java new file mode 100644 index 000000000..9e51001a6 --- /dev/null +++ b/astrid/src/com/todoroo/astrid/adapter/TaskListFragmentPagerAdapter.java @@ -0,0 +1,55 @@ +package com.todoroo.astrid.adapter; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; + +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.api.Filter; +import com.todoroo.astrid.api.FilterWithCustomIntent; + +public class TaskListFragmentPagerAdapter extends FragmentStatePagerAdapter { + + private final Filter[] filters; + + public TaskListFragmentPagerAdapter(FragmentManager fm, Filter[] filters) { + super(fm); + this.filters = filters; + } + + @Override + public Fragment getItem(int position) { + return getFragmentForFilter(filters[position]); + } + + @Override + public int getCount() { + return filters.length; + } + + private Fragment getFragmentForFilter(Filter filter) { + if (filter instanceof FilterWithCustomIntent) { + try { + Class component = Class.forName(((FilterWithCustomIntent) filter).customTaskList.getClassName()); + Constructor constructor = component.getConstructor(Boolean.class, Filter.class); + return (Fragment) constructor.newInstance(true, filter); + } catch (NoSuchMethodException e) { + return new TaskListFragment(); + } catch (InvocationTargetException e) { + return new TaskListFragment(); + } catch (ClassNotFoundException e) { + return new TaskListFragment(); + } catch (IllegalAccessException e) { + return new TaskListFragment(); + } catch (InstantiationException e) { + return new TaskListFragment(); + } + } else { + return new TaskListFragment(); + } + } + +}