Back to project page demo-flickr-feed-android.
The source code is released under:
This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a co...
If you think the Android project demo-flickr-feed-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package uk.org.tomek.flickrfeed.fagment; /*from w w w.j av a 2 s. c o m*/ import android.app.Fragment; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.LinearLayout; import java.util.List; import butterknife.ButterKnife; import butterknife.InjectView; import retrofit.Callback; import retrofit.RetrofitError; import retrofit.client.Response; import uk.org.tomek.flickrfeed.App; import uk.org.tomek.flickrfeed.R; import uk.org.tomek.flickrfeed.adapter.FeedAdapter; import uk.org.tomek.flickrfeed.model.Feed; import uk.org.tomek.flickrfeed.model.Item; import uk.org.tomek.flickrfeed.utils.Logger; /** * Old placeholder fragment, displayed when app is started. * <p/> * Created by tomek on 02/11/14. */ public class StartingFragment extends Fragment{ public static final String TAG = StartingFragment.class.getSimpleName(); private final Logger mLogger = Logger.getLogger(StartingFragment.class); @InjectView(R.id.btn_start) Button mStartButton; @InjectView(R.id.my_recycler_view) RecyclerView mRecyclerView; @InjectView(R.id.progressContainer) LinearLayout mProgressContainer; private FeedAdapter mAdapter; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mLogger.v("fragment view created"); View rootView = inflater.inflate(R.layout.fragment_main, container, false); ButterKnife.inject(this, rootView); // get preferences final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity()); mRecyclerView.setHasFixedSize(true); // set layout manager mRecyclerView.setLayoutManager(layoutManager); mAdapter = new FeedAdapter(getActivity()); mRecyclerView.setAdapter(mAdapter); mStartButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String searchTerm = sharedPreferences.getString(SettingsFragment.FLICKR_SEARCH_TERM_KEY, "rome"); getActivity().setTitle(searchTerm); sendFlickrRequest(searchTerm); } }); return rootView; } /** * Sends request to Flickr website to retrieve the feed. * * @param searchTerm search term used in the request */ public void sendFlickrRequest(final String searchTerm) { mLogger.d("Sending request searchTerm:%s", searchTerm); setListShown(false); App.getRestClient().getFlickrService().listPosts(searchTerm, new Callback<Feed>() { @Override public void success(final Feed feed, Response response) { if (feed != null) { List<Item> items = feed.getItems(); mLogger.i("Fetch Success %d messages, thread:%s", items.size(), Thread.currentThread().getName()); // set adapter mAdapter.setFeeds(items); savePostsForLater(items); setListShown(true); } } @Override public void failure(RetrofitError error) { mLogger.e(error.getMessage(), error.getCause()); } }); } /** * Shows the list/hides the progress bar. * Method is similar to the one used by {@link android.app.ListFragment}. * * @param shouldShow */ private void setListShown(boolean shouldShow) { if (shouldShow) { mRecyclerView.setVisibility(View.VISIBLE); mProgressContainer.setVisibility(View.GONE); } else { mRecyclerView.setVisibility(View.GONE); mProgressContainer.setVisibility(View.VISIBLE); } } /** * Saves Posts in the DB. * @param posts */ private void savePostsForLater(final List<Item> posts) { App.getSingleThreadPool().execute(new Runnable() { @Override public void run() { mLogger.d("Saving posts to db, thread:%s", Thread.currentThread().getName()); List<Item> items = Item.listAll(Item.class); if (items != null && !items.isEmpty()) { mLogger.v("Cleaning Item db items"); Item.deleteAll(Item.class); } Item.saveInTx(posts); } }); } @Override public void onDestroy() { super.onDestroy(); ButterKnife.reset(this); } }