From 3a44f859e481a0c5093ce69ca6ec3d6f284342df Mon Sep 17 00:00:00 2001 From: chandu-android Date: Sun, 5 Jan 2014 03:13:54 -0500 Subject: [PATCH] Commit for the ViewPager Spike --- AndroidManifest.xml | 6 +- pom.xml | 5 + .../{main.xml => allbook_marks_main.xml} | 0 ...st_item.xml => allbookmarks_list_item.xml} | 0 res/layout/book_mark_search_layout.xml | 6 + res/layout/bookie_main_container_layout.xml | 6 + res/layout/mybook_marks_main.xml | 6 + res/layout/mybookmarks_list_item.xml | 9 + res/menu/main_activity_actions.xml | 16 +- ...ity.java => AllBookMarksListFragment.java} | 134 ++------- .../bmark/android/BookMarkMainActivity.java | 272 ++++++++++++++++++ .../bmark/android/BookMarkSearchFragment.java | 70 +++++ .../us/bmark/android/MyBookMarksFragment.java | 197 +++++++++++++ 13 files changed, 605 insertions(+), 122 deletions(-) rename res/layout/{main.xml => allbook_marks_main.xml} (100%) rename res/layout/{list_item.xml => allbookmarks_list_item.xml} (100%) create mode 100644 res/layout/book_mark_search_layout.xml create mode 100644 res/layout/bookie_main_container_layout.xml create mode 100644 res/layout/mybook_marks_main.xml create mode 100644 res/layout/mybookmarks_list_item.xml rename src/main/java/us/bmark/android/{BookmarkListActivity.java => AllBookMarksListFragment.java} (63%) create mode 100644 src/main/java/us/bmark/android/BookMarkMainActivity.java create mode 100644 src/main/java/us/bmark/android/BookMarkSearchFragment.java create mode 100644 src/main/java/us/bmark/android/MyBookMarksFragment.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 5e81441..83a0050 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -14,7 +14,7 @@ android:theme="@android:style/Theme.Holo" android:allowBackup="true"> @@ -24,7 +24,7 @@ + android:parentActivityName=".BookMarkMainActivity" > @@ -35,7 +35,7 @@ + android:parentActivityName=".BookMarkMainActivity"/> \ No newline at end of file diff --git a/pom.xml b/pom.xml index dc3e25b..423eb43 100755 --- a/pom.xml +++ b/pom.xml @@ -35,6 +35,11 @@ okhttp 1.2.1 + + com.google.android + support-v4 + r6 + diff --git a/res/layout/main.xml b/res/layout/allbook_marks_main.xml similarity index 100% rename from res/layout/main.xml rename to res/layout/allbook_marks_main.xml diff --git a/res/layout/list_item.xml b/res/layout/allbookmarks_list_item.xml similarity index 100% rename from res/layout/list_item.xml rename to res/layout/allbookmarks_list_item.xml diff --git a/res/layout/book_mark_search_layout.xml b/res/layout/book_mark_search_layout.xml new file mode 100644 index 0000000..2b6b418 --- /dev/null +++ b/res/layout/book_mark_search_layout.xml @@ -0,0 +1,6 @@ + + + diff --git a/res/layout/bookie_main_container_layout.xml b/res/layout/bookie_main_container_layout.xml new file mode 100644 index 0000000..80a99db --- /dev/null +++ b/res/layout/bookie_main_container_layout.xml @@ -0,0 +1,6 @@ + + \ No newline at end of file diff --git a/res/layout/mybook_marks_main.xml b/res/layout/mybook_marks_main.xml new file mode 100644 index 0000000..2b6b418 --- /dev/null +++ b/res/layout/mybook_marks_main.xml @@ -0,0 +1,6 @@ + + + diff --git a/res/layout/mybookmarks_list_item.xml b/res/layout/mybookmarks_list_item.xml new file mode 100644 index 0000000..0a88675 --- /dev/null +++ b/res/layout/mybookmarks_list_item.xml @@ -0,0 +1,9 @@ + + + + \ No newline at end of file diff --git a/res/menu/main_activity_actions.xml b/res/menu/main_activity_actions.xml index e1bec92..c8371ee 100644 --- a/res/menu/main_activity_actions.xml +++ b/res/menu/main_activity_actions.xml @@ -1,14 +1,14 @@ - - + + + + + + + + { - private static final int ROW_VIEW_ID = R.layout.list_item; + private static final int ROW_VIEW_ID = R.layout.allbookmarks_list_item; BookmarkArrayAdapter(Context context, List objects) { super(context, ROW_VIEW_ID, objects); @@ -125,7 +118,7 @@ private class ServiceCallback implements Callback { @Override public void success(BookmarkList bookmarkList, Response response) { - setProgressBarIndeterminateVisibility(false); + getActivity().setProgressBarIndeterminateVisibility(false); bmarks.addAll(bookmarkList.bmarks); Log.w(TAG, "on success for bookmark list, fetched " + bmarks.size()); adapter.notifyDataSetChanged(); @@ -134,34 +127,23 @@ public void success(BookmarkList bookmarkList, Response response) { @Override public void failure(RetrofitError error) { - setProgressBarIndeterminateVisibility(false); + getActivity().setProgressBarIndeterminateVisibility(false); errorHandler.handleError(error); } } @Override - public void onCreate(Bundle savedInstanceState) { + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreate(savedInstanceState); - settings = new SharedPrefsBackedUserSettings(this); - errorHandler = new JustDisplayToastErrorHandler(this,settings); + view = inflater.inflate(R.layout.allbook_marks_main, container, false); + settings = new SharedPrefsBackedUserSettings(getActivity()); + errorHandler = new JustDisplayToastErrorHandler(getActivity(),settings); countPP = getResources().getInteger(R.integer.default_number_of_bookmarks_to_get); setUpService(); - requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); - setContentView(R.layout.main); - getActionBar().setDisplayHomeAsUpEnabled(false); // Hides the '<' button in the ActionBar - getActionBar().setHomeButtonEnabled(true); // Enables the 'B' icon to be tappable on the list Activity - adapter = new BookmarkArrayAdapter(this,bmarks); + adapter = new BookmarkArrayAdapter(getActivity(),bmarks); setListAdapter(adapter); - setUpListView(); loadMoreData(); - } - - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - MenuInflater inflater = getMenuInflater(); - inflater.inflate(R.menu.main_activity_actions, menu); - return super.onCreateOptionsMenu(menu); + return view; } private void setUpService() { @@ -174,18 +156,14 @@ private void setUpService() { private void refreshWithNewestGlobal() { int nextPage = pagesLoaded; - setProgressBarIndeterminateVisibility(true); + getActivity().setProgressBarIndeterminateVisibility(true); service.everyonesRecent(countPP, nextPage, new ServiceCallback()); } - private void refreshWithNewestUser() { - int nextPage = pagesLoaded; - setProgressBarIndeterminateVisibility(true); - service.recent(settings.getUsername(), - settings.getApiKey(), - countPP, - nextPage, - new ServiceCallback()); + @Override + public void onStart() { + super.onStart(); + setUpListView(); } private void setUpListView() { @@ -214,7 +192,7 @@ public boolean onItemLongClick(AdapterView parent, View view, final Bundle bundle = new Bundle(); String bmarkJson = (new Gson()).toJson(bmark); bundle.putString(IntentConstants.EXTRAS_KEY_BMARK, bmarkJson); - final Intent intent = new Intent(BookmarkListActivity.this, + final Intent intent = new Intent(getActivity(), BookMarkDetailActivity.class); intent.putExtras(bundle); startActivity(intent); @@ -225,70 +203,6 @@ public boolean onItemLongClick(AdapterView parent, View view, lv.setOnScrollListener(new EndlessScrollListener()); } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - - switch( item.getItemId() ) { - case android.R.id.home: - case R.id.action_everyones_recent: - Log.v(TAG, "global button clicked"); - flipState(State.ALL); - return true; - case R.id.action_recent: - Log.v(TAG, "user button clicked"); - flipState(State.MINE); - return true; - case R.id.action_settings: - Intent settingsIntent = - new Intent(BookmarkListActivity.this, SettingsActivity.class); - BookmarkListActivity.this.startActivity(settingsIntent); - return true; - case R.id.action_search: - displaySearchDialog(); - default: - return super.onOptionsItemSelected(item); - } - } - - private void flipState(State desiredState) { - this.state = desiredState; - bmarks = new ArrayList(countPP); - adapter = new BookmarkArrayAdapter(this,bmarks); - setListAdapter(adapter); - pagesLoaded = 0; - getListView().setOnScrollListener(new EndlessScrollListener()); - - loadMoreData(); - } - - private void displaySearchDialog() { - AlertDialog.Builder alert = new AlertDialog.Builder(this); - - alert.setTitle(R.string.search_dialog_title); - alert.setMessage(R.string.search_dialog_message); - - final EditText input = new EditText(this); - alert.setView(input); - - alert.setPositiveButton(R.string.search_dialog_positive_button, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int whichButton) { - searchTerms = input.getText().toString(); - flipState(State.SEARCH); - } - }); - - alert.setNegativeButton(R.string.search_dialog_cancel_button, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int whichButton) { - // Do nothing - } - }); - - alert.show(); - } - private void refreshWithSearch() { String terms; try { @@ -297,7 +211,7 @@ private void refreshWithSearch() { Log.e(TAG,"UTF-8 not supported?",e); return; } - setProgressBarIndeterminateVisibility(true); + getActivity().setProgressBarIndeterminateVisibility(true); final int nextPage = pagesLoaded; service.search(settings.getUsername(),settings.getApiKey(), terms,countPP,nextPage, @@ -311,12 +225,12 @@ public void success(SearchResult searchResult, Response response) { adapter.notifyDataSetChanged(); pagesLoaded++; } - setProgressBarIndeterminateVisibility(false); + getActivity().setProgressBarIndeterminateVisibility(false); } @Override public void failure(RetrofitError error) { - setProgressBarIndeterminateVisibility(false); + getActivity().setProgressBarIndeterminateVisibility(false); errorHandler.handleError(error); } }); @@ -325,8 +239,6 @@ public void failure(RetrofitError error) { private void loadMoreData() { switch(state) { case ALL : refreshWithNewestGlobal(); break; - case MINE : refreshWithNewestUser(); break; - case SEARCH : refreshWithSearch(); break; } } diff --git a/src/main/java/us/bmark/android/BookMarkMainActivity.java b/src/main/java/us/bmark/android/BookMarkMainActivity.java new file mode 100644 index 0000000..b7cef2e --- /dev/null +++ b/src/main/java/us/bmark/android/BookMarkMainActivity.java @@ -0,0 +1,272 @@ +package us.bmark.android; + +import android.app.ActionBar; +import android.app.AlertDialog; +import android.app.FragmentTransaction; +import android.content.DialogInterface; +import android.content.Intent; +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentActivity; +import android.support.v4.app.FragmentManager; +import android.support.v4.app.FragmentPagerAdapter; +import android.support.v4.view.ViewPager; +import android.util.Log; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; +import android.view.Window; +import android.widget.EditText; + +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.List; + +import retrofit.Callback; +import retrofit.RestAdapter; +import retrofit.RetrofitError; +import retrofit.client.Response; +import us.bmark.android.prefs.SettingsActivity; +import us.bmark.android.prefs.SharedPrefsBackedUserSettings; +import us.bmark.bookieclient.BookieService; +import us.bmark.bookieclient.Bookmark; +import us.bmark.bookieclient.SearchResult; + +import static java.net.URLEncoder.encode; + +public class BookMarkMainActivity extends FragmentActivity implements ActionBar.TabListener { + + static final int NUM_ITEMS = 3; + static final String TAG = AllBookMarksListFragment.class.getName(); + private String searchTerms; + private int pagesLoaded; + private int countPP; + private UserSettings settings; + private BookieService service; + private ActionBar actionBar; + private List bmarks = + new ArrayList(); + private String[] tabs = { "All", "Mine", "Search" }; + + + MainActivityPagerAdapter pagerAdapter; + ViewPager viewPager; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); + getActionBar().setDisplayHomeAsUpEnabled(false); // Hides the '<' button in the ActionBar + getActionBar().setHomeButtonEnabled(true); // Enables the 'B' icon to be tappable on the list Activity + setContentView(R.layout.bookie_main_container_layout); + settings = new SharedPrefsBackedUserSettings(this); + countPP = getResources().getInteger(R.integer.default_number_of_bookmarks_to_get); + setUpService(); + pagerAdapter = new MainActivityPagerAdapter(getSupportFragmentManager()); + viewPager = (ViewPager)findViewById(R.id.pager); + viewPager.setAdapter(pagerAdapter); + viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { + + @Override + public void onPageSelected(int position) { + actionBar.setSelectedNavigationItem(position); + } + + @Override + public void onPageScrolled(int arg0, float arg1, int arg2) { + } + + @Override + public void onPageScrollStateChanged(int arg0) { + } + }); + + actionBar = getActionBar(); + actionBar.setHomeButtonEnabled(false); + actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); + + // Adding Tabs + for (String tab_name : tabs) { + actionBar.addTab(actionBar.newTab().setText(tab_name) + .setTabListener(this)); + } + + } + + @Override + public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { + viewPager.setCurrentItem(tab.getPosition()); + } + + @Override + public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { + + } + + @Override + public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { + + } + +// private class BookmarkArrayAdapter extends ArrayAdapter { +// +// private static final int ROW_VIEW_ID = R.layout.allbookmarks_list_item; +// +// BookmarkArrayAdapter(Context context, List objects) { +// super(context, ROW_VIEW_ID, objects); +// } +// +// @Override +// public View getView(int position, View convertView, ViewGroup parent) { +// +// View row = convertView; +// Bookmark bmark = this.getItem(position); +// +// if (row == null) { +// LayoutInflater inflater = ((Activity) this.getContext()).getLayoutInflater(); +// row = inflater.inflate(ROW_VIEW_ID, parent, false); +// } +// +// TextView textView = (TextView) row.findViewById(R.id.bookmarkListRowTextView); +// final String description = isBlank(bmark.description) ? bmark.url : bmark.description; +// textView.setText(description); +// +// return row; +// } +// } + + private static class MainActivityPagerAdapter extends FragmentPagerAdapter{ + + public MainActivityPagerAdapter(FragmentManager fm) { + super(fm); + } + + @Override + public Fragment getItem(int index) { + switch (index) { + case 0: + return new AllBookMarksListFragment(); + case 1: + return new MyBookMarksFragment(); + case 2: + return new BookMarkSearchFragment(); + } + return null; + } + + @Override + public int getCount() { + return NUM_ITEMS; + } + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + MenuInflater inflater = getMenuInflater(); + inflater.inflate(R.menu.main_activity_actions, menu); + return super.onCreateOptionsMenu(menu); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + + switch( item.getItemId() ) { +// case android.R.id.home: +// case R.id.action_everyones_recent: +// Log.v(TAG, "global button clicked"); +// //flipState(State.ALL); +// return true; +// case R.id.action_recent: +// Log.v(TAG, "user button clicked"); +// //flipState(State.MINE); +// return true; + case R.id.action_settings: + Intent settingsIntent = + new Intent(this, SettingsActivity.class); + startActivity(settingsIntent); + return true; + case R.id.action_search: + displaySearchDialog(); + default: + return super.onOptionsItemSelected(item); + } + } + + private void refreshWithSearch() { + String terms; + try { + terms = encode(searchTerms, "UTF-8"); + } catch (UnsupportedEncodingException e) { + Log.e(TAG, "UTF-8 not supported?", e); + return; + } + setProgressBarIndeterminateVisibility(true); + final int nextPage = pagesLoaded; + service.search(settings.getUsername(),settings.getApiKey(), + terms,countPP,nextPage, + new Callback() { + + @Override + public void success(SearchResult searchResult, Response response) { + Log.w(TAG, "on success search :" + searchResult.result_count); + if(searchResult.result_count>0) { + bmarks.addAll(searchResult.search_results); + // adapter = new BookmarkArrayAdapter(BookMarkMainActivity.this,bmarks); + //adapter.notifyDataSetChanged(); + viewPager.setCurrentItem(2); + BookMarkSearchFragment searchFragment = (BookMarkSearchFragment) getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.pager + ":" + viewPager.getCurrentItem()); + if (searchFragment != null) { + searchFragment.reDrawList(bmarks); + } + pagesLoaded++; + } + setProgressBarIndeterminateVisibility(false); + } + + @Override + public void failure(RetrofitError error) { + setProgressBarIndeterminateVisibility(false); + //errorHandler.handleError(error); + } + }); + } + + public List getAdapter() { + return bmarks; + } + + private void setUpService() { + String serverUrl = settings.getBaseUrl(); + RestAdapter restAdapter = new RestAdapter.Builder() + .setServer(serverUrl).build(); + restAdapter.setLogLevel(RestAdapter.LogLevel.FULL); + service = restAdapter.create(BookieService.class); + } + + private void displaySearchDialog() { + AlertDialog.Builder alert = new AlertDialog.Builder(this); + + alert.setTitle(R.string.search_dialog_title); + alert.setMessage(R.string.search_dialog_message); + + final EditText input = new EditText(this); + alert.setView(input); + + alert.setPositiveButton(R.string.search_dialog_positive_button, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int whichButton) { + searchTerms = input.getText().toString(); + refreshWithSearch(); + } + }); + + alert.setNegativeButton(R.string.search_dialog_cancel_button, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int whichButton) { + // Do nothing + } + }); + + alert.show(); + } +} diff --git a/src/main/java/us/bmark/android/BookMarkSearchFragment.java b/src/main/java/us/bmark/android/BookMarkSearchFragment.java new file mode 100644 index 0000000..b94bdc6 --- /dev/null +++ b/src/main/java/us/bmark/android/BookMarkSearchFragment.java @@ -0,0 +1,70 @@ +package us.bmark.android; + +import android.app.Activity; +import android.content.Context; +import android.os.Bundle; +import android.support.v4.app.ListFragment; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.TextView; + +import java.util.ArrayList; +import java.util.List; + +import us.bmark.bookieclient.Bookmark; + +import static us.bmark.android.utils.Utils.isBlank; + +public class BookMarkSearchFragment extends ListFragment { + + View view; + private List bmarks = + new ArrayList(); + private BookmarkArrayAdapter adapter; + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + view = (View) getActivity().findViewById(R.layout.book_mark_search_layout); + bmarks = ((BookMarkMainActivity) getActivity()).getAdapter(); + if (bmarks != null) { + adapter = new BookmarkArrayAdapter(getActivity(),bmarks); + setListAdapter(adapter); + } + return view; + } + + private class BookmarkArrayAdapter extends ArrayAdapter { + + private static final int ROW_VIEW_ID = R.layout.allbookmarks_list_item; + + BookmarkArrayAdapter(Context context, List objects) { + super(context, ROW_VIEW_ID, objects); + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + + View row = convertView; + Bookmark bmark = this.getItem(position); + + if (row == null) { + LayoutInflater inflater = ((Activity) this.getContext()).getLayoutInflater(); + row = inflater.inflate(ROW_VIEW_ID, parent, false); + } + + TextView textView = (TextView) row.findViewById(R.id.bookmarkListRowTextView); + final String description = isBlank(bmark.description) ? bmark.url : bmark.description; + textView.setText(description); + + return row; + } + } + + public void reDrawList(List bookMarks) { + bmarks = bookMarks; + adapter.notifyDataSetChanged(); + } +} diff --git a/src/main/java/us/bmark/android/MyBookMarksFragment.java b/src/main/java/us/bmark/android/MyBookMarksFragment.java new file mode 100644 index 0000000..dd269fd --- /dev/null +++ b/src/main/java/us/bmark/android/MyBookMarksFragment.java @@ -0,0 +1,197 @@ +package us.bmark.android; + +import android.app.Activity; +import android.content.Context; +import android.content.Intent; +import android.net.Uri; +import android.os.Bundle; +import android.support.v4.app.ListFragment; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.AbsListView; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; +import android.widget.ListView; +import android.widget.TextView; + +import com.google.gson.Gson; + +import java.util.ArrayList; +import java.util.List; + +import retrofit.Callback; +import retrofit.RestAdapter; +import retrofit.RetrofitError; +import retrofit.client.Response; +import us.bmark.android.prefs.SharedPrefsBackedUserSettings; +import us.bmark.android.utils.IntentConstants; +import us.bmark.bookieclient.BookieService; +import us.bmark.bookieclient.BookieServiceUtils; +import us.bmark.bookieclient.Bookmark; +import us.bmark.bookieclient.BookmarkList; + +import static us.bmark.android.utils.Utils.isBlank; + +public class MyBookMarksFragment extends ListFragment{ + + private static final String TAG = MyBookMarksFragment.class.getName(); + + private int countPP; + View view; + private UserSettings settings; + private BookmarkArrayAdapter adapter; + private int pagesLoaded; + private BookieService service; + private List bmarks = + new ArrayList(); + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + view = inflater.inflate(R.layout.mybook_marks_main, container, false); + settings = new SharedPrefsBackedUserSettings(getActivity()); + countPP = getResources().getInteger(R.integer.default_number_of_bookmarks_to_get); + adapter = new BookmarkArrayAdapter(getActivity(),bmarks); + setUpService(); + setListAdapter(adapter); + loadMoreData(); + return view; + } + + private class BookmarkArrayAdapter extends ArrayAdapter { + + private static final int ROW_VIEW_ID = R.layout.allbookmarks_list_item; + + BookmarkArrayAdapter(Context context, List objects) { + super(context, ROW_VIEW_ID, objects); + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + + View row = convertView; + Bookmark bmark = this.getItem(position); + + if (row == null) { + LayoutInflater inflater = ((Activity) this.getContext()).getLayoutInflater(); + row = inflater.inflate(ROW_VIEW_ID, parent, false); + } + + TextView textView = (TextView) row.findViewById(R.id.bookmarkListRowTextView); + final String description = isBlank(bmark.description) ? bmark.url : bmark.description; + textView.setText(description); + + return row; + } + } + + @Override + public void onStart() { + super.onStart(); + setUpListView(); + } + + private void setUpListView() { + ListView lv = getListView(); + + lv.setTextFilterEnabled(true); + + lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView parent, View view, + int position, long id) { + final Bookmark bmark = ((Bookmark) parent.getAdapter().getItem(position)); + + final Uri uri = Uri.parse(BookieServiceUtils.urlForRedirect(bmark, + settings.getBaseUrl(), + settings.getUsername())); + startActivity(new Intent(Intent.ACTION_VIEW, uri)); + } + }); + + lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { + @Override + public boolean onItemLongClick(AdapterView parent, View view, + int position, long id) { + final Bookmark bmark = ((Bookmark) parent.getAdapter().getItem(position)); + final Bundle bundle = new Bundle(); + String bmarkJson = (new Gson()).toJson(bmark); + bundle.putString(IntentConstants.EXTRAS_KEY_BMARK, bmarkJson); + final Intent intent = new Intent(getActivity(), + BookMarkDetailActivity.class); + intent.putExtras(bundle); + startActivity(intent); + return true; + } + }); + + lv.setOnScrollListener(new EndlessScrollListener()); + } + + private class EndlessScrollListener implements AbsListView.OnScrollListener { + private final int threshold = countPP / 5; + private int previousTotal = 0; + private boolean loading = true; + + @Override + public void onScroll(AbsListView view, int firstVisibleItem, + int visibleItemCount, int totalItemCount) { + if (loading) { + if (totalItemCount > previousTotal) { + loading = false; + previousTotal = totalItemCount; + } + } + if (!loading && ((totalItemCount - visibleItemCount) <= (firstVisibleItem + threshold))) { + loadMoreData(); + loading = true; + } + } + + @Override + public void onScrollStateChanged(AbsListView view, int scrollState) { + } + } + + private void setUpService() { + String serverUrl = settings.getBaseUrl(); + RestAdapter restAdapter = new RestAdapter.Builder() + .setServer(serverUrl).build(); + restAdapter.setLogLevel(RestAdapter.LogLevel.FULL); + service = restAdapter.create(BookieService.class); + } + + private void refreshWithNewestUser() { + int nextPage = pagesLoaded; + getActivity().setProgressBarIndeterminateVisibility(true); + service.recent(settings.getUsername(), + settings.getApiKey(), + countPP, + nextPage, + new ServiceCallback()); + } + + private class ServiceCallback implements Callback { + + @Override + public void success(BookmarkList bookmarkList, Response response) { + getActivity().setProgressBarIndeterminateVisibility(false); + bmarks.addAll(bookmarkList.bmarks); + Log.w(TAG, "on success for bookmark list, fetched " + bmarks.size()); + adapter.notifyDataSetChanged(); + pagesLoaded++; + } + + @Override + public void failure(RetrofitError error) { + getActivity().setProgressBarIndeterminateVisibility(false); + //errorHandler.handleError(error); + } + } + + private void loadMoreData() { + refreshWithNewestUser(); + } +}