Permalink
Browse files

Refactor other fragments to use that nifty new method

Signed-off-by: Eddie Ringle <eddie@eringle.net>
  • Loading branch information...
1 parent 80b59f2 commit 6607eaf785b6c7acada054fa4c0da800f7f52547 @EddieRingle committed Apr 26, 2012
View
234 src/net/idlesoft/android/apps/github/ui/fragments/EventsFragment.java
@@ -28,6 +28,9 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
+import com.actionbarsherlock.view.Menu;
+import com.actionbarsherlock.view.MenuInflater;
+import com.actionbarsherlock.view.MenuItem;
import com.viewpagerindicator.TitlePageIndicator;
import net.idlesoft.android.apps.github.R;
import net.idlesoft.android.apps.github.ui.adapters.EventListAdapter;
@@ -68,10 +71,9 @@
class EventsDataFragment extends DataFragment
{
ArrayList<ListHolder> eventLists;
+ ListViewPager.MultiListPagerAdapter pagerAdapter;
User targetUser;
int currentItem;
- int currentItemScroll;
- int currentItemScrollTop;
public
int findListIndexByType(int listType)
@@ -182,7 +184,6 @@ void onItemClick(AdapterView<?> parent, View view, int position, long id)
ListViewPager mViewPager;
TitlePageIndicator mTitlePageIndicator;
- int mCurrentPage;
public
EventsFragment()
@@ -207,45 +208,26 @@ View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInst
return v;
}
- @Override
public
- void onActivityCreated(Bundle savedInstanceState)
+ void fetchData(final boolean freshen)
{
- super.onActivityCreated(savedInstanceState);
-
- final Bundle args = getArguments();
- final String userJson;
- if (args != null) {
- userJson = args.getString(ARG_TARGET_USER, null);
- if (userJson != null) {
- mDataFragment.targetUser = GsonUtils.fromJson(userJson, User.class);
- }
- }
- if (mDataFragment.targetUser == null) {
- mDataFragment.targetUser = new User();
- mDataFragment.targetUser.setLogin(getBaseActivity().getCurrentUserLogin());
- }
-
- if (mDataFragment.eventLists == null)
- mDataFragment.eventLists = new ArrayList<ListHolder>();
-
- ListViewPager.MultiListPagerAdapter adapter =
- new ListViewPager.MultiListPagerAdapter(getContext());
-
if (mDataFragment.targetUser.getLogin().equals(getBaseActivity().getCurrentUserLogin())
&& !mDataFragment.targetUser.getLogin().equals("")) {
/* Display received events */
- final IdleList<Event> list = new IdleList<Event>(getContext());
+ final IdleList<Event> list;
final ListHolder holder;
+ final int index = mDataFragment.findListIndexByType(LIST_RECEIVED);
+
+ if (freshen && index >= 0)
+ list = mViewPager.getAdapter().getList(index);
+ else
+ list = new IdleList<Event>(getContext());
list.setOnItemClickListener(new OnEventListItemClickListener());
list.setAdapter(new EventListAdapter(getBaseActivity()));
- final int index = mDataFragment.findListIndexByType(LIST_RECEIVED);
-
- if (index >= 0) {
+ if (index >= 0 && !freshen) {
holder = mDataFragment.eventLists.get(index);
-
list.setTitle(holder.title);
list.getListAdapter().fillWithItems(holder.events);
list.getListAdapter().notifyDataSetChanged();
@@ -256,79 +238,81 @@ void onActivityCreated(Bundle savedInstanceState)
list.setTitle(holder.title);
holder.gravatars = new ArrayList<Bitmap>();
holder.events = new ArrayList<Event>();
-
mDataFragment.eventLists.add(holder);
final DataFragment.DataTask.DataTaskRunnable receivedRunnable =
new DataFragment.DataTask.DataTaskRunnable()
- {
- @Override
- public
- void runTask() throws InterruptedException
- {
- try {
- final EventService es =
- new EventService(getBaseActivity().getGHClient());
- PageIterator<Event> itr =
- es.pageUserReceivedEvents(mDataFragment.targetUser.getLogin());
- holder.events.addAll(itr.next());
- } catch (IOException e) {
- e.printStackTrace();
- } catch (AccountsException e) {
- e.printStackTrace();
- }
- }
- };
+ {
+ @Override
+ public
+ void runTask() throws InterruptedException
+ {
+ try {
+ final EventService es =
+ new EventService(getBaseActivity().getGHClient());
+ PageIterator<Event> itr =
+ es.pageUserReceivedEvents(mDataFragment.targetUser.getLogin());
+ holder.events.addAll(itr.next());
+ } catch (IOException e) {
+ e.printStackTrace();
+ } catch (AccountsException e) {
+ e.printStackTrace();
+ }
+ }
+ };
final DataFragment.DataTask.DataTaskCallbacks receivedCallbacks =
new DataFragment.DataTask.DataTaskCallbacks()
- {
- @Override
- public
- void onTaskStart()
- {
- list.getProgressBar().setVisibility(View.VISIBLE);
- list.setFooterShown(true);
- list.setListShown(true);
- }
-
- @Override
- public
- void onTaskCancelled()
- {
- }
-
- @Override
- public
- void onTaskComplete()
- {
- list.setListShown(false);
- list.getListAdapter().fillWithItems(holder.events);
- list.getListAdapter().notifyDataSetChanged();
- list.getProgressBar().setVisibility(View.GONE);
- list.setListShown(true);
- }
- };
+ {
+ @Override
+ public
+ void onTaskStart()
+ {
+ list.getProgressBar().setVisibility(View.VISIBLE);
+ list.setFooterShown(true);
+ list.setListShown(true);
+ }
+
+ @Override
+ public
+ void onTaskCancelled()
+ {
+ }
+
+ @Override
+ public
+ void onTaskComplete()
+ {
+ list.setListShown(false);
+ list.getListAdapter().fillWithItems(holder.events);
+ list.getListAdapter().notifyDataSetChanged();
+ list.getProgressBar().setVisibility(View.GONE);
+ list.setListShown(true);
+ }
+ };
mDataFragment.executeNewTask(receivedRunnable, receivedCallbacks);
+ if (index < 0)
+ mViewPager.getAdapter().addList(list);
}
-
- adapter.addList(list);
}
if (!mDataFragment.targetUser.getLogin().equals("")) {
/* Display a user's public events */
- final IdleList<Event> list = new IdleList<Event>(getContext());
+ final IdleList<Event> list;
final ListHolder holder;
+ final int index = mDataFragment.findListIndexByType(LIST_PUBLIC);
+
+ if (freshen && index >= 0)
+ list = mViewPager.getAdapter().getList(index);
+ else
+ list = new IdleList<Event>(getContext());
list.setOnItemClickListener(new OnEventListItemClickListener());
list.setAdapter(new EventListAdapter(getBaseActivity()));
- final int index = mDataFragment.findListIndexByType(LIST_PUBLIC);
-
- if (index >= 0) {
+ if (index >= 0 && !freshen) {
holder = mDataFragment.eventLists.get(index);
-
list.setTitle(holder.title);
list.getListAdapter().fillWithItems(holder.events);
list.getListAdapter().notifyDataSetChanged();
@@ -338,7 +322,6 @@ void onTaskComplete()
holder.title = getString(R.string.events_public);
list.setTitle(holder.title);
holder.events = new ArrayList<Event>();
-
mDataFragment.eventLists.add(holder);
final DataFragment.DataTask.DataTaskRunnable publicRunnable =
@@ -393,24 +376,27 @@ void onTaskComplete()
};
mDataFragment.executeNewTask(publicRunnable, receivedCallbacks);
+ if (index < 0)
+ mViewPager.getAdapter().addList(list);
}
-
- adapter.addList(list);
}
{
/* Display timeline events */
- final IdleList<Event> list = new IdleList<Event>(getContext());
+ final IdleList<Event> list;
final ListHolder holder;
+ final int index = mDataFragment.findListIndexByType(LIST_TIMELINE);
+
+ if (freshen && index >= 0)
+ list = mViewPager.getAdapter().getList(index);
+ else
+ list = new IdleList<Event>(getContext());
list.setOnItemClickListener(new OnEventListItemClickListener());
list.setAdapter(new EventListAdapter(getBaseActivity()));
- final int index = mDataFragment.findListIndexByType(LIST_TIMELINE);
-
- if (index >= 0) {
+ if (index >= 0 && !freshen) {
holder = mDataFragment.eventLists.get(index);
-
list.setTitle(holder.title);
list.getListAdapter().fillWithItems(holder.events);
list.getListAdapter().notifyDataSetChanged();
@@ -420,7 +406,6 @@ void onTaskComplete()
holder.title = getString(R.string.events_timeline);
list.setTitle(holder.title);
holder.events = new ArrayList<Event>();
-
mDataFragment.eventLists.add(holder);
final DataFragment.DataTask.DataTaskRunnable publicRunnable =
@@ -475,13 +460,41 @@ void onTaskComplete()
};
mDataFragment.executeNewTask(publicRunnable, receivedCallbacks);
+ if (index < 0)
+ mViewPager.getAdapter().addList(list);
}
+ }
+ }
+
+ @Override
+ public
+ void onActivityCreated(Bundle savedInstanceState)
+ {
+ super.onActivityCreated(savedInstanceState);
- adapter.addList(list);
+ final Bundle args = getArguments();
+ final String userJson;
+ if (args != null) {
+ userJson = args.getString(ARG_TARGET_USER, null);
+ if (userJson != null) {
+ mDataFragment.targetUser = GsonUtils.fromJson(userJson, User.class);
+ }
}
+ if (mDataFragment.targetUser == null) {
+ mDataFragment.targetUser = new User();
+ mDataFragment.targetUser.setLogin(getBaseActivity().getCurrentUserLogin());
+ }
+
+ if (mDataFragment.eventLists == null)
+ mDataFragment.eventLists = new ArrayList<ListHolder>();
+
+ if (mDataFragment.pagerAdapter == null)
+ mDataFragment.pagerAdapter = new ListViewPager.MultiListPagerAdapter(getContext());
- mViewPager.setAdapter(adapter);
+ mViewPager.setAdapter(mDataFragment.pagerAdapter);
mTitlePageIndicator.setViewPager(mViewPager);
+
+ fetchData(false);
}
@Override
@@ -491,12 +504,6 @@ void onPause()
super.onPause();
mDataFragment.currentItem = mViewPager.getCurrentItem();
-
- mDataFragment.currentItemScroll = mViewPager.getAdapter().getList(mDataFragment.currentItem)
- .getFirstVisiblePosition();
- mDataFragment.currentItemScrollTop = mViewPager.getAdapter()
- .getList(mDataFragment.currentItem)
- .getChildAt(0).getTop();
}
@Override
@@ -506,8 +513,27 @@ void onResume()
super.onResume();
mViewPager.setCurrentItem(mDataFragment.currentItem);
- mViewPager.getAdapter().getList(mDataFragment.currentItem)
- .setSelectionFromTop(mDataFragment.currentItemScroll,
- mDataFragment.currentItemScrollTop);
+ }
+
+ @Override
+ public
+ void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
+ {
+ super.onCreateOptionsMenu(menu, inflater);
+
+ menu.findItem(R.id.actionbar_action_refresh).setVisible(true);
+ }
+
+ @Override
+ public
+ boolean onOptionsItemSelected(MenuItem item)
+ {
+ switch (item.getItemId()) {
+ case R.id.actionbar_action_refresh:
+ fetchData(true);
+ return true;
+ }
+
+ return super.onOptionsItemSelected(item);
}
}
View
122 src/net/idlesoft/android/apps/github/ui/fragments/FollowersFollowingFragment.java
@@ -27,6 +27,9 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
+import com.actionbarsherlock.view.Menu;
+import com.actionbarsherlock.view.MenuInflater;
+import com.actionbarsherlock.view.MenuItem;
import com.viewpagerindicator.TitlePageIndicator;
import net.idlesoft.android.apps.github.HubroidConstants;
import net.idlesoft.android.apps.github.R;
@@ -64,10 +67,9 @@
class FollowersFollowingDataFragment extends DataFragment
{
ArrayList<ListHolder> userLists;
+ ListViewPager.MultiListPagerAdapter pagerAdapter;
User targetUser;
int currentItem;
- int currentItemScroll;
- int currentItemScrollTop;
public
int findListIndexByType(int listType)
@@ -85,7 +87,6 @@ int findListIndexByType(int listType)
ListViewPager mViewPager;
TitlePageIndicator mTitlePageIndicator;
- int mCurrentPage;
public
FollowersFollowingFragment()
@@ -110,43 +111,24 @@ View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInst
return v;
}
- @Override
public
- void onActivityCreated(Bundle savedInstanceState)
+ void fetchData(final boolean freshen)
{
- super.onActivityCreated(savedInstanceState);
-
- final Bundle args = getArguments();
- final String userJson;
- if (args != null) {
- userJson = args.getString(HubroidConstants.ARG_TARGET_USER);
- if (userJson != null) {
- mDataFragment.targetUser = GsonUtils.fromJson(userJson, User.class);
- }
- }
- if (mDataFragment.targetUser == null) {
- mDataFragment.targetUser = new User();
- mDataFragment.targetUser.setLogin(getBaseActivity().getCurrentUserLogin());
- }
- getBaseActivity().getSupportActionBar().setTitle(mDataFragment.targetUser.getLogin());
- if (mDataFragment.userLists == null)
- mDataFragment.userLists = new ArrayList<ListHolder>();
-
- ListViewPager.MultiListPagerAdapter adapter =
- new ListViewPager.MultiListPagerAdapter(getContext());
-
if (!mDataFragment.targetUser.getLogin().equals("")) {
/* Display a user's followers */
- final IdleList<User> list = new IdleList<User>(getContext());
+ final IdleList<User> list;
final ListHolder holder;
+ final int index = mDataFragment.findListIndexByType(LIST_FOLLOWERS);
- list.setAdapter(new UserListAdapter(getBaseActivity()));
+ if (freshen && index >= 0)
+ list = mViewPager.getAdapter().getList(index);
+ else
+ list = new IdleList<User>(getContext());
- final int index = mDataFragment.findListIndexByType(LIST_FOLLOWERS);
+ list.setAdapter(new UserListAdapter(getBaseActivity()));
if (index >= 0) {
holder = mDataFragment.userLists.get(index);
-
list.setTitle(holder.title);
list.getListAdapter().fillWithItems(holder.users);
list.getListAdapter().notifyDataSetChanged();
@@ -156,7 +138,6 @@ void onActivityCreated(Bundle savedInstanceState)
holder.title = getString(R.string.followers);
list.setTitle(holder.title);
holder.users = new ArrayList<User>();
-
mDataFragment.userLists.add(holder);
final DataFragment.DataTask.DataTaskRunnable followersRunnable =
@@ -211,6 +192,8 @@ void onTaskComplete()
};
mDataFragment.executeNewTask(followersRunnable, followersCallbacks);
+ if (index < 0)
+ mViewPager.getAdapter().addList(list);
}
list.setOnItemClickListener(new AdapterView.OnItemClickListener()
@@ -229,22 +212,23 @@ void onItemClick(AdapterView<?> parent, View view, int position, long id)
getBaseActivity().finishFragmentTransaction();
}
});
-
- adapter.addList(list);
}
if (!mDataFragment.targetUser.getLogin().equals("")) {
/* Display a user's following */
- final IdleList<User> list = new IdleList<User>(getContext());
+ final IdleList<User> list;
final ListHolder holder;
+ final int index = mDataFragment.findListIndexByType(LIST_FOLLOWING);
- list.setAdapter(new UserListAdapter(getBaseActivity()));
+ if (freshen && index >= 0)
+ list = mViewPager.getAdapter().getList(index);
+ else
+ list = new IdleList<User>(getContext());
- final int index = mDataFragment.findListIndexByType(LIST_FOLLOWING);
+ list.setAdapter(new UserListAdapter(getBaseActivity()));
if (index >= 0) {
holder = mDataFragment.userLists.get(index);
-
list.setTitle(holder.title);
list.getListAdapter().fillWithItems(holder.users);
list.getListAdapter().notifyDataSetChanged();
@@ -254,7 +238,6 @@ void onItemClick(AdapterView<?> parent, View view, int position, long id)
holder.title = getString(R.string.following);
list.setTitle(holder.title);
holder.users = new ArrayList<User>();
-
mDataFragment.userLists.add(holder);
final DataFragment.DataTask.DataTaskRunnable followingRunnable =
@@ -309,6 +292,8 @@ void onTaskComplete()
};
mDataFragment.executeNewTask(followingRunnable, followingCallbacks);
+ if (index < 0)
+ mViewPager.getAdapter().addList(list);
}
list.setOnItemClickListener(new AdapterView.OnItemClickListener()
@@ -327,12 +312,38 @@ void onItemClick(AdapterView<?> parent, View view, int position, long id)
getBaseActivity().finishFragmentTransaction();
}
});
+ }
+ }
- adapter.addList(list);
+ @Override
+ public
+ void onActivityCreated(Bundle savedInstanceState)
+ {
+ super.onActivityCreated(savedInstanceState);
+
+ final Bundle args = getArguments();
+ final String userJson;
+ if (args != null) {
+ userJson = args.getString(HubroidConstants.ARG_TARGET_USER);
+ if (userJson != null) {
+ mDataFragment.targetUser = GsonUtils.fromJson(userJson, User.class);
+ }
}
+ if (mDataFragment.targetUser == null) {
+ mDataFragment.targetUser = new User();
+ mDataFragment.targetUser.setLogin(getBaseActivity().getCurrentUserLogin());
+ }
+ getBaseActivity().getSupportActionBar().setTitle(mDataFragment.targetUser.getLogin());
+ if (mDataFragment.userLists == null)
+ mDataFragment.userLists = new ArrayList<ListHolder>();
+
+ if (mDataFragment.pagerAdapter == null)
+ mDataFragment.pagerAdapter = new ListViewPager.MultiListPagerAdapter(getContext());
- mViewPager.setAdapter(adapter);
+ mViewPager.setAdapter(mDataFragment.pagerAdapter);
mTitlePageIndicator.setViewPager(mViewPager);
+
+ fetchData(false);
}
@Override
@@ -342,12 +353,6 @@ void onPause()
super.onPause();
mDataFragment.currentItem = mViewPager.getCurrentItem();
-
- mDataFragment.currentItemScroll = mViewPager.getAdapter().getList(mDataFragment.currentItem)
- .getFirstVisiblePosition();
- mDataFragment.currentItemScrollTop = mViewPager.getAdapter()
- .getList(mDataFragment.currentItem)
- .getChildAt(0).getTop();
}
@Override
@@ -357,8 +362,27 @@ void onResume()
super.onResume();
mViewPager.setCurrentItem(mDataFragment.currentItem);
- mViewPager.getAdapter().getList(mDataFragment.currentItem)
- .setSelectionFromTop(mDataFragment.currentItemScroll,
- mDataFragment.currentItemScrollTop);
+ }
+
+ @Override
+ public
+ void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
+ {
+ super.onCreateOptionsMenu(menu, inflater);
+
+ menu.findItem(R.id.actionbar_action_refresh).setVisible(true);
+ }
+
+ @Override
+ public
+ boolean onOptionsItemSelected(MenuItem item)
+ {
+ switch (item.getItemId()) {
+ case R.id.actionbar_action_refresh:
+ fetchData(true);
+ return true;
+ }
+
+ return super.onOptionsItemSelected(item);
}
}
View
110 src/net/idlesoft/android/apps/github/ui/fragments/ForksFragment.java
@@ -27,6 +27,9 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
+import com.actionbarsherlock.view.Menu;
+import com.actionbarsherlock.view.MenuInflater;
+import com.actionbarsherlock.view.MenuItem;
import com.viewpagerindicator.TitlePageIndicator;
import net.idlesoft.android.apps.github.R;
import net.idlesoft.android.apps.github.ui.adapters.RepositoryListAdapter;
@@ -62,10 +65,9 @@
class ForksDataFragment extends DataFragment
{
ArrayList<ListHolder> repositoryLists;
+ ListViewPager.MultiListPagerAdapter pagerAdapter;
Repository targetRepository;
int currentItem;
- int currentItemScroll;
- int currentItemScrollTop;
public
int findListIndexByType(int listType)
@@ -83,7 +85,6 @@ int findListIndexByType(int listType)
ListViewPager mViewPager;
TitlePageIndicator mTitlePageIndicator;
- int mCurrentPage;
public
ForksFragment()
@@ -108,47 +109,23 @@ View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInst
return v;
}
- @Override
- public
- void onActivityCreated(Bundle savedInstanceState)
- {
- super.onActivityCreated(savedInstanceState);
-
- final Bundle args = getArguments();
- final String repositoryJson;
- if (args != null) {
- repositoryJson = args.getString(ARG_TARGET_REPO);
- if (repositoryJson != null) {
- mDataFragment.targetRepository = GsonUtils.fromJson(repositoryJson,
- Repository.class);
- }
- }
- getBaseActivity().getSupportActionBar().setTitle(mDataFragment.targetRepository.getName());
- if (mDataFragment.repositoryLists == null)
- mDataFragment.repositoryLists = new ArrayList<ListHolder>();
- ListViewPager.MultiListPagerAdapter adapter =
- new ListViewPager.MultiListPagerAdapter(getContext());
-
- mViewPager.setAdapter(adapter);
- mTitlePageIndicator.setViewPager(mViewPager);
-
- fetchData(false);
- }
-
public
void fetchData(boolean freshen)
{
/* Display a user's repositories */
- final IdleList<Repository> list = new IdleList<Repository>(getContext());
+ final IdleList<Repository> list;
final ListHolder holder;
+ final int index = mDataFragment.findListIndexByType(LIST_FORKS);
- list.setAdapter(new RepositoryListAdapter(getBaseActivity()));
+ if (freshen && index >= 0)
+ list = mViewPager.getAdapter().getList(index);
+ else
+ list = new IdleList<Repository>(getContext());
- final int index = mDataFragment.findListIndexByType(LIST_FORKS);
+ list.setAdapter(new RepositoryListAdapter(getBaseActivity()));
if (index >= 0) {
holder = mDataFragment.repositoryLists.get(index);
-
list.setTitle(holder.title);
list.getListAdapter().fillWithItems(holder.repositories);
list.getListAdapter().notifyDataSetChanged();
@@ -158,7 +135,6 @@ void fetchData(boolean freshen)
holder.title = getString(R.string.forks);
list.setTitle(holder.title);
holder.repositories = new ArrayList<Repository>();
-
mDataFragment.repositoryLists.add(holder);
final DataFragment.DataTask.DataTaskRunnable forksRunnable =
@@ -214,6 +190,8 @@ void onTaskComplete()
};
mDataFragment.executeNewTask(forksRunnable, forksCallbacks);
+ if (index < 0)
+ mViewPager.getAdapter().addList(list);
}
list.setOnItemClickListener(new AdapterView.OnItemClickListener()
@@ -227,13 +205,38 @@ void onItemClick(AdapterView<?> parent, View view, int position, long id)
args.putString(ARG_TARGET_REPO, GsonUtils.toJson(target));
getBaseActivity().startFragmentTransaction();
getBaseActivity().addFragmentToTransaction(RepositoryFragment.class,
- R.id.fragment_container_more,
- args);
+ R.id.fragment_container_more, args);
getBaseActivity().finishFragmentTransaction();
}
});
+ }
+
+ @Override
+ public
+ void onActivityCreated(Bundle savedInstanceState)
+ {
+ super.onActivityCreated(savedInstanceState);
+
+ final Bundle args = getArguments();
+ final String repositoryJson;
+ if (args != null) {
+ repositoryJson = args.getString(ARG_TARGET_REPO);
+ if (repositoryJson != null) {
+ mDataFragment.targetRepository = GsonUtils.fromJson(repositoryJson,
+ Repository.class);
+ }
+ }
+ getBaseActivity().getSupportActionBar().setTitle(mDataFragment.targetRepository.getName());
+ if (mDataFragment.repositoryLists == null)
+ mDataFragment.repositoryLists = new ArrayList<ListHolder>();
+
+ if (mDataFragment.pagerAdapter == null)
+ mDataFragment.pagerAdapter = new ListViewPager.MultiListPagerAdapter(getContext());
- mViewPager.getAdapter().addList(list);
+ mViewPager.setAdapter(mDataFragment.pagerAdapter);
+ mTitlePageIndicator.setViewPager(mViewPager);
+
+ fetchData(false);
}
@Override
@@ -243,12 +246,6 @@ void onPause()
super.onPause();
mDataFragment.currentItem = mViewPager.getCurrentItem();
-
- mDataFragment.currentItemScroll = mViewPager.getAdapter().getList(mDataFragment.currentItem)
- .getFirstVisiblePosition();
- mDataFragment.currentItemScrollTop = mViewPager.getAdapter()
- .getList(mDataFragment.currentItem)
- .getChildAt(0).getTop();
}
@Override
@@ -258,8 +255,27 @@ void onResume()
super.onResume();
mViewPager.setCurrentItem(mDataFragment.currentItem);
- mViewPager.getAdapter().getList(mDataFragment.currentItem)
- .setSelectionFromTop(mDataFragment.currentItemScroll,
- mDataFragment.currentItemScrollTop);
+ }
+
+ @Override
+ public
+ void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
+ {
+ super.onCreateOptionsMenu(menu, inflater);
+
+ menu.findItem(R.id.actionbar_action_refresh).setVisible(true);
+ }
+
+ @Override
+ public
+ boolean onOptionsItemSelected(MenuItem item)
+ {
+ switch (item.getItemId()) {
+ case R.id.actionbar_action_refresh:
+ fetchData(true);
+ return true;
+ }
+
+ return super.onOptionsItemSelected(item);
}
}
View
5 src/net/idlesoft/android/apps/github/ui/fragments/IssuesFragment.java
@@ -75,8 +75,6 @@
ListViewPager.MultiListPagerAdapter pagerAdapter;
Repository targetRepository;
int currentItem;
- int currentItemScroll;
- int currentItemScrollTop;
public
int findListIndexByType(int listType)
@@ -94,7 +92,6 @@ int findListIndexByType(int listType)
ListViewPager mViewPager;
TitlePageIndicator mTitlePageIndicator;
- int mCurrentPage;
public
IssuesFragment()
@@ -233,7 +230,6 @@ void onTaskComplete()
list.setTitle(holder.title);
holder.gravatars = new ArrayList<Bitmap>();
holder.issues = new ArrayList<Issue>();
-
mDataFragment.issuesLists.add(holder);
final DataFragment.DataTask.DataTaskRunnable closedRunnable =
@@ -323,7 +319,6 @@ void onTaskComplete()
list.setTitle(holder.title);
holder.gravatars = new ArrayList<Bitmap>();
holder.issues = new ArrayList<Issue>();
-
mDataFragment.issuesLists.add(holder);
final DataFragment.DataTask.DataTaskRunnable assignedRunnable =
View
125 src/net/idlesoft/android/apps/github/ui/fragments/RepositoriesFragment.java
@@ -27,6 +27,9 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
+import com.actionbarsherlock.view.Menu;
+import com.actionbarsherlock.view.MenuInflater;
+import com.actionbarsherlock.view.MenuItem;
import com.viewpagerindicator.TitlePageIndicator;
import net.idlesoft.android.apps.github.HubroidConstants;
import net.idlesoft.android.apps.github.R;
@@ -67,10 +70,9 @@
class RepositoriesDataFragment extends DataFragment
{
ArrayList<ListHolder> repositoryLists;
+ ListViewPager.MultiListPagerAdapter pagerAdapter;
User targetUser;
int currentItem;
- int currentItemScroll;
- int currentItemScrollTop;
public
int findListIndexByType(int listType)
@@ -88,7 +90,6 @@ int findListIndexByType(int listType)
ListViewPager mViewPager;
TitlePageIndicator mTitlePageIndicator;
- int mCurrentPage;
public
RepositoriesFragment()
@@ -113,44 +114,24 @@ View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInst
return v;
}
- @Override
public
- void onActivityCreated(Bundle savedInstanceState)
+ void fetchData(final boolean freshen)
{
- super.onActivityCreated(savedInstanceState);
-
- final Bundle args = getArguments();
- final String userJson;
- if (args != null) {
- userJson = args.getString(HubroidConstants.ARG_TARGET_USER);
- if (userJson != null) {
- mDataFragment.targetUser = GsonUtils.fromJson(userJson, User.class);
- }
- }
- if (mDataFragment.targetUser == null) {
- mDataFragment.targetUser = new User();
- mDataFragment.targetUser.setLogin(getBaseActivity().getCurrentUserLogin());
- }
-
- if (mDataFragment.repositoryLists == null)
- mDataFragment.repositoryLists = new ArrayList<ListHolder>();
-
- getBaseActivity().getSupportActionBar().setTitle(R.string.repositories);
- ListViewPager.MultiListPagerAdapter adapter =
- new ListViewPager.MultiListPagerAdapter(getContext());
-
if (!mDataFragment.targetUser.getLogin().equals("")) {
/* Display a user's repositories */
- final IdleList<Repository> list = new IdleList<Repository>(getContext());
+ final IdleList<Repository> list;
final ListHolder holder;
+ final int index = mDataFragment.findListIndexByType(LIST_YOURS);
- list.setAdapter(new RepositoryListAdapter(getBaseActivity()));
+ if (freshen && index >= 0)
+ list = mViewPager.getAdapter().getList(index);
+ else
+ list = new IdleList<Repository>(getContext());
- final int index = mDataFragment.findListIndexByType(LIST_YOURS);
+ list.setAdapter(new RepositoryListAdapter(getBaseActivity()));
if (index >= 0) {
holder = mDataFragment.repositoryLists.get(index);
-
list.setTitle(holder.title);
list.getListAdapter().fillWithItems(holder.repositories);
list.getListAdapter().notifyDataSetChanged();
@@ -160,7 +141,6 @@ void onActivityCreated(Bundle savedInstanceState)
holder.title = mDataFragment.targetUser.getLogin();
list.setTitle(holder.title);
holder.repositories = new ArrayList<Repository>();
-
mDataFragment.repositoryLists.add(holder);
final DataFragment.DataTask.DataTaskRunnable yoursRunnable =
@@ -222,6 +202,8 @@ void onTaskComplete()
};
mDataFragment.executeNewTask(yoursRunnable, yoursCallbacks);
+ if (index < 0)
+ mViewPager.getAdapter().addList(list);
}
list.setOnItemClickListener(new AdapterView.OnItemClickListener()
@@ -240,22 +222,23 @@ void onItemClick(AdapterView<?> parent, View view, int position, long id)
getBaseActivity().finishFragmentTransaction();
}
});
-
- adapter.addList(list);
}
if (!mDataFragment.targetUser.getLogin().equals("")) {
/* Display a user's watched repositories */
- final IdleList<Repository> list = new IdleList<Repository>(getContext());
+ final IdleList<Repository> list;
final ListHolder holder;
+ final int index = mDataFragment.findListIndexByType(LIST_WATCHED);
- list.setAdapter(new RepositoryListAdapter(getBaseActivity()));
+ if (freshen && index >= 0)
+ list = mViewPager.getAdapter().getList(index);
+ else
+ list = new IdleList<Repository>(getContext());
- final int index = mDataFragment.findListIndexByType(LIST_WATCHED);
+ list.setAdapter(new RepositoryListAdapter(getBaseActivity()));
if (index >= 0) {
holder = mDataFragment.repositoryLists.get(index);
-
list.setTitle(holder.title);
list.getListAdapter().fillWithItems(holder.repositories);
list.getListAdapter().notifyDataSetChanged();
@@ -265,7 +248,6 @@ void onItemClick(AdapterView<?> parent, View view, int position, long id)
holder.title = getString(R.string.repositories_watched);
list.setTitle(holder.title);
holder.repositories = new ArrayList<Repository>();
-
mDataFragment.repositoryLists.add(holder);
final DataFragment.DataTask.DataTaskRunnable watchedRunnable =
@@ -320,6 +302,8 @@ void onTaskComplete()
};
mDataFragment.executeNewTask(watchedRunnable, watchedCallbacks);
+ if (index < 0)
+ mViewPager.getAdapter().addList(list);
}
list.setOnItemClickListener(new AdapterView.OnItemClickListener()
@@ -338,12 +322,40 @@ void onItemClick(AdapterView<?> parent, View view, int position, long id)
getBaseActivity().finishFragmentTransaction();
}
});
+ }
+ }
- adapter.addList(list);
+ @Override
+ public
+ void onActivityCreated(Bundle savedInstanceState)
+ {
+ super.onActivityCreated(savedInstanceState);
+
+ final Bundle args = getArguments();
+ final String userJson;
+ if (args != null) {
+ userJson = args.getString(HubroidConstants.ARG_TARGET_USER);
+ if (userJson != null) {
+ mDataFragment.targetUser = GsonUtils.fromJson(userJson, User.class);
+ }
}
+ if (mDataFragment.targetUser == null) {
+ mDataFragment.targetUser = new User();
+ mDataFragment.targetUser.setLogin(getBaseActivity().getCurrentUserLogin());
+ }
+
+ if (mDataFragment.repositoryLists == null)
+ mDataFragment.repositoryLists = new ArrayList<ListHolder>();
- mViewPager.setAdapter(adapter);
+ getBaseActivity().getSupportActionBar().setTitle(R.string.repositories);
+
+ if (mDataFragment.pagerAdapter == null)
+ mDataFragment.pagerAdapter = new ListViewPager.MultiListPagerAdapter(getContext());
+
+ mViewPager.setAdapter(mDataFragment.pagerAdapter);
mTitlePageIndicator.setViewPager(mViewPager);
+
+ fetchData(false);
}
@Override
@@ -353,12 +365,6 @@ void onPause()
super.onPause();
mDataFragment.currentItem = mViewPager.getCurrentItem();
-
- mDataFragment.currentItemScroll = mViewPager.getAdapter().getList(mDataFragment.currentItem)
- .getFirstVisiblePosition();
- mDataFragment.currentItemScrollTop = mViewPager.getAdapter()
- .getList(mDataFragment.currentItem)
- .getChildAt(0).getTop();
}
@Override
@@ -368,8 +374,27 @@ void onResume()
super.onResume();
mViewPager.setCurrentItem(mDataFragment.currentItem);
- mViewPager.getAdapter().getList(mDataFragment.currentItem)
- .setSelectionFromTop(mDataFragment.currentItemScroll,
- mDataFragment.currentItemScrollTop);
+ }
+
+ @Override
+ public
+ void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
+ {
+ super.onCreateOptionsMenu(menu, inflater);
+
+ menu.findItem(R.id.actionbar_action_refresh).setVisible(true);
+ }
+
+ @Override
+ public
+ boolean onOptionsItemSelected(MenuItem item)
+ {
+ switch (item.getItemId()) {
+ case R.id.actionbar_action_refresh:
+ fetchData(true);
+ return true;
+ }
+
+ return super.onOptionsItemSelected(item);
}
}
View
108 src/net/idlesoft/android/apps/github/ui/fragments/WatchersFragment.java
@@ -27,6 +27,9 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
+import com.actionbarsherlock.view.Menu;
+import com.actionbarsherlock.view.MenuInflater;
+import com.actionbarsherlock.view.MenuItem;
import com.viewpagerindicator.TitlePageIndicator;
import net.idlesoft.android.apps.github.R;
import net.idlesoft.android.apps.github.ui.adapters.UserListAdapter;
@@ -66,10 +69,9 @@
class WatchersDataFragment extends DataFragment
{
ArrayList<ListHolder> userLists;
+ ListViewPager.MultiListPagerAdapter pagerAdapter;
Repository targetRepository;
int currentItem;
- int currentItemScroll;
- int currentItemScrollTop;
public
int findListIndexByType(int listType)
@@ -87,7 +89,6 @@ int findListIndexByType(int listType)
ListViewPager mViewPager;
TitlePageIndicator mTitlePageIndicator;
- int mCurrentPage;
public
WatchersFragment()
@@ -112,48 +113,23 @@ View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInst
return v;
}
- @Override
- public
- void onActivityCreated(Bundle savedInstanceState)
- {
- super.onActivityCreated(savedInstanceState);
-
- final Bundle args = getArguments();
- final String repositoryJson;
- if (args != null) {
- repositoryJson = args.getString(ARG_TARGET_REPO);
- if (repositoryJson != null) {
- mDataFragment.targetRepository = GsonUtils.fromJson(repositoryJson,
- Repository.class);
- }
- }
- getBaseActivity().getSupportActionBar().setTitle(mDataFragment.targetRepository.getName());
- if (mDataFragment.userLists == null)
- mDataFragment.userLists = new ArrayList<ListHolder>();
-
- ListViewPager.MultiListPagerAdapter adapter =
- new ListViewPager.MultiListPagerAdapter(getContext());
-
- mViewPager.setAdapter(adapter);
- mTitlePageIndicator.setViewPager(mViewPager);
-
- fetchData(false);
- }
-
public
void fetchData(final boolean freshen)
{
/* Display a user's followers */
- final IdleList<User> list = new IdleList<User>(getContext());
+ final IdleList<User> list;
final ListHolder holder;
+ final int index = mDataFragment.findListIndexByType(LIST_WATCHERS);
- list.setAdapter(new UserListAdapter(getBaseActivity()));
+ if (freshen && index >= 0)
+ list = mViewPager.getAdapter().getList(index);
+ else
+ list = new IdleList<User>(getContext());
- final int index = mDataFragment.findListIndexByType(LIST_WATCHERS);
+ list.setAdapter(new UserListAdapter(getBaseActivity()));
if (index >= 0) {
holder = mDataFragment.userLists.get(index);
-
list.setTitle(holder.title);
list.getListAdapter().fillWithItems(holder.users);
list.getListAdapter().notifyDataSetChanged();
@@ -163,7 +139,6 @@ void fetchData(final boolean freshen)
holder.title = getString(R.string.watchers);
list.setTitle(holder.title);
holder.users = new ArrayList<User>();
-
mDataFragment.userLists.add(holder);
final DataFragment.DataTask.DataTaskRunnable watchersRunnable =
@@ -221,6 +196,8 @@ void onTaskComplete()
};
mDataFragment.executeNewTask(watchersRunnable, followersCallbacks);
+ if (index < 0)
+ mViewPager.getAdapter().addList(list);
}
list.setOnItemClickListener(new AdapterView.OnItemClickListener()
@@ -239,8 +216,34 @@ void onItemClick(AdapterView<?> parent, View view, int position, long id)
getBaseActivity().finishFragmentTransaction();
}
});
+ }
- mViewPager.getAdapter().addList(list);
+ @Override
+ public
+ void onActivityCreated(Bundle savedInstanceState)
+ {
+ super.onActivityCreated(savedInstanceState);
+
+ final Bundle args = getArguments();
+ final String repositoryJson;
+ if (args != null) {
+ repositoryJson = args.getString(ARG_TARGET_REPO);
+ if (repositoryJson != null) {
+ mDataFragment.targetRepository = GsonUtils.fromJson(repositoryJson,
+ Repository.class);
+ }
+ }
+ getBaseActivity().getSupportActionBar().setTitle(mDataFragment.targetRepository.getName());
+ if (mDataFragment.userLists == null)
+ mDataFragment.userLists = new ArrayList<ListHolder>();
+
+ if (mDataFragment.pagerAdapter == null)
+ mDataFragment.pagerAdapter = new ListViewPager.MultiListPagerAdapter(getContext());
+
+ mViewPager.setAdapter(mDataFragment.pagerAdapter);
+ mTitlePageIndicator.setViewPager(mViewPager);
+
+ fetchData(false);
}
@Override
@@ -250,12 +253,6 @@ void onPause()
super.onPause();
mDataFragment.currentItem = mViewPager.getCurrentItem();
-
- mDataFragment.currentItemScroll = mViewPager.getAdapter().getList(mDataFragment.currentItem)
- .getFirstVisiblePosition();
- mDataFragment.currentItemScrollTop = mViewPager.getAdapter()
- .getList(mDataFragment.currentItem)
- .getChildAt(0).getTop();
}
@Override
@@ -265,8 +262,27 @@ void onResume()
super.onResume();
mViewPager.setCurrentItem(mDataFragment.currentItem);
- mViewPager.getAdapter().getList(mDataFragment.currentItem)
- .setSelectionFromTop(mDataFragment.currentItemScroll,
- mDataFragment.currentItemScrollTop);
+ }
+
+ @Override
+ public
+ void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
+ {
+ super.onCreateOptionsMenu(menu, inflater);
+
+ menu.findItem(R.id.actionbar_action_refresh).setVisible(true);
+ }
+
+ @Override
+ public
+ boolean onOptionsItemSelected(MenuItem item)
+ {
+ switch (item.getItemId()) {
+ case R.id.actionbar_action_refresh:
+ fetchData(true);
+ return true;
+ }
+
+ return super.onOptionsItemSelected(item);
}
}

0 comments on commit 6607eaf

Please sign in to comment.