|
| 1 | +package com.testerhome.nativeandroid.fragments; |
| 2 | + |
| 3 | +import android.os.Bundle; |
| 4 | +import android.support.v4.widget.SwipeRefreshLayout; |
| 5 | +import android.support.v7.widget.LinearLayoutManager; |
| 6 | +import android.support.v7.widget.RecyclerView; |
| 7 | +import android.util.Log; |
| 8 | + |
| 9 | +import com.testerhome.nativeandroid.BuildConfig; |
| 10 | +import com.testerhome.nativeandroid.R; |
| 11 | +import com.testerhome.nativeandroid.models.SearchResponse; |
| 12 | +import com.testerhome.nativeandroid.networks.RestAdapterUtils; |
| 13 | +import com.testerhome.nativeandroid.views.adapters.TopicsListAdapter; |
| 14 | +import com.testerhome.nativeandroid.views.widgets.DividerItemDecoration; |
| 15 | + |
| 16 | +import butterknife.Bind; |
| 17 | +import retrofit.Callback; |
| 18 | +import retrofit.Response; |
| 19 | +import retrofit.Retrofit; |
| 20 | + |
| 21 | +/** |
| 22 | + * Created by Bin Li on 2015/9/16. |
| 23 | + */ |
| 24 | +public class SearchResultFragment extends BaseFragment implements Callback<SearchResponse> { |
| 25 | + |
| 26 | + @Bind(R.id.rv_topic_list) |
| 27 | + RecyclerView recyclerViewTopicList; |
| 28 | + |
| 29 | + @Bind(R.id.srl_refresh) |
| 30 | + SwipeRefreshLayout swipeRefreshLayout; |
| 31 | + |
| 32 | + private int mNextCursor = 1; |
| 33 | + |
| 34 | + private TopicsListAdapter mAdatper; |
| 35 | + |
| 36 | + private String keyword; |
| 37 | + |
| 38 | + public static SearchResultFragment newInstance(String keyword) { |
| 39 | + Bundle args = new Bundle(); |
| 40 | + args.putString("keyword", keyword); |
| 41 | + SearchResultFragment fragment = new SearchResultFragment(); |
| 42 | + fragment.setArguments(args); |
| 43 | + return fragment; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + protected int getLayoutRes() { |
| 48 | + return R.layout.fragment_topics; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void onActivityCreated(Bundle savedInstanceState) { |
| 53 | + super.onActivityCreated(savedInstanceState); |
| 54 | + keyword = getArguments().getString("keyword"); |
| 55 | + |
| 56 | + loadTopics(true); |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + @Override |
| 61 | + protected void setupView() { |
| 62 | + mAdatper = new TopicsListAdapter(getActivity()); |
| 63 | + mAdatper.setListener(new TopicsListAdapter.EndlessListener() { |
| 64 | + @Override |
| 65 | + public void onListEnded() { |
| 66 | + if (mNextCursor > 1) { |
| 67 | + loadTopics(false); |
| 68 | + } |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + recyclerViewTopicList.setLayoutManager(new LinearLayoutManager(getActivity())); |
| 73 | + recyclerViewTopicList.addItemDecoration(new DividerItemDecoration(getActivity(), |
| 74 | + DividerItemDecoration.VERTICAL_LIST)); |
| 75 | + recyclerViewTopicList.setAdapter(mAdatper); |
| 76 | + swipeRefreshLayout.setColorSchemeResources( |
| 77 | + android.R.color.holo_blue_light, |
| 78 | + android.R.color.holo_red_light, |
| 79 | + android.R.color.holo_orange_light, |
| 80 | + android.R.color.holo_green_light); |
| 81 | + |
| 82 | + swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { |
| 83 | + @Override |
| 84 | + public void onRefresh() { |
| 85 | + mNextCursor = 1; |
| 86 | + loadTopics(false); |
| 87 | + } |
| 88 | + }); |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + private void loadTopics(boolean showloading) { |
| 93 | + |
| 94 | + if (swipeRefreshLayout != null) { |
| 95 | + swipeRefreshLayout.setRefreshing(true); |
| 96 | + } |
| 97 | + |
| 98 | + if (showloading) |
| 99 | + showLoadingView(); |
| 100 | + |
| 101 | + |
| 102 | + if (keyword != null) { |
| 103 | + RestAdapterUtils.getRestAPI(getActivity()).searchTopicsByKeyword(keyword, |
| 104 | + mNextCursor * 20).enqueue(this); |
| 105 | + } |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public void onResponse(Response<SearchResponse> response, Retrofit retrofit) { |
| 111 | + |
| 112 | + if (BuildConfig.DEBUG) |
| 113 | + Log.e("cache", "cache header = [" + response.headers().toString() + "]"); |
| 114 | + |
| 115 | + hideLoadingView(); |
| 116 | + if (swipeRefreshLayout != null && swipeRefreshLayout.isRefreshing()) { |
| 117 | + swipeRefreshLayout.setRefreshing(false); |
| 118 | + } |
| 119 | + |
| 120 | + Log.e("retrofit", response.raw().request().urlString()); |
| 121 | + if (response.body() != null && response.body().getTopics().size() > 0) { |
| 122 | + if (mNextCursor == 1) { |
| 123 | + mAdatper.setItems(response.body().getTopics()); |
| 124 | + } else { |
| 125 | + mAdatper.addItems(response.body().getTopics()); |
| 126 | + } |
| 127 | + if (response.body().getTopics().size() >= 10) { |
| 128 | + mNextCursor += 1; |
| 129 | + } else { |
| 130 | + mNextCursor = 1; |
| 131 | + } |
| 132 | + |
| 133 | + } else { |
| 134 | + mNextCursor = 1; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public void onFailure(Throwable t) { |
| 140 | + hideLoadingView(); |
| 141 | + if (swipeRefreshLayout != null && swipeRefreshLayout.isRefreshing()) { |
| 142 | + swipeRefreshLayout.setRefreshing(false); |
| 143 | + } |
| 144 | + if (BuildConfig.DEBUG) |
| 145 | + Log.e("cache", "failure() called with: " + "error = [" + t.getMessage() + "]", t); |
| 146 | + } |
| 147 | +} |
0 commit comments