|
| 1 | +package com.codepath.bestsellerlistapp; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.os.Bundle; |
| 5 | + |
| 6 | +import androidx.core.widget.ContentLoadingProgressBar; |
| 7 | +import androidx.fragment.app.Fragment; |
| 8 | +import androidx.recyclerview.widget.GridLayoutManager; |
| 9 | +import androidx.recyclerview.widget.LinearLayoutManager; |
| 10 | +import androidx.recyclerview.widget.RecyclerView; |
| 11 | + |
| 12 | +import android.util.Log; |
| 13 | +import android.view.LayoutInflater; |
| 14 | +import android.view.View; |
| 15 | +import android.view.ViewGroup; |
| 16 | +import android.widget.Toast; |
| 17 | + |
| 18 | +import com.codepath.bestsellerlistapp.models.BestSellerBook; |
| 19 | +import com.codepath.bestsellerlistapp.networking.CallbackResponse; |
| 20 | +import com.codepath.bestsellerlistapp.networking.NYTimesApiClient; |
| 21 | + |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +/** |
| 25 | + * A fragment representing a list of Items. |
| 26 | + */ |
| 27 | +public class BestSellerBooksFragment extends Fragment implements OnListFragmentInteractionListener{ |
| 28 | + |
| 29 | + /** |
| 30 | + * Mandatory empty constructor for the fragment manager to instantiate the |
| 31 | + * fragment (e.g. upon screen orientation changes). |
| 32 | + */ |
| 33 | + public BestSellerBooksFragment() { |
| 34 | + } |
| 35 | + |
| 36 | + @SuppressWarnings("unused") |
| 37 | + public static BestSellerBooksFragment newInstance(int columnCount) { |
| 38 | + BestSellerBooksFragment fragment = new BestSellerBooksFragment(); |
| 39 | + Bundle args = new Bundle(); |
| 40 | + fragment.setArguments(args); |
| 41 | + return fragment; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 46 | + Bundle savedInstanceState) { |
| 47 | + View view = inflater.inflate(R.layout.fragment_best_seller_books_list, container, false); |
| 48 | + ContentLoadingProgressBar progressBar = (ContentLoadingProgressBar) view.findViewById(R.id.progress); |
| 49 | + RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.list); |
| 50 | + |
| 51 | + // Set the adapter |
| 52 | + Context context = view.getContext(); |
| 53 | + recyclerView.setLayoutManager(new GridLayoutManager(context, 2)); |
| 54 | + updateAdapter(progressBar, recyclerView); |
| 55 | + return view; |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | + private void updateAdapter(final ContentLoadingProgressBar progressBar, final RecyclerView recyclerView) { |
| 60 | + progressBar.show(); |
| 61 | + NYTimesApiClient nyTimesApiClient = new NYTimesApiClient(); |
| 62 | + nyTimesApiClient.getBestSellersList(new CallbackResponse<List<BestSellerBook>>() { |
| 63 | + @Override |
| 64 | + public void onSuccess(List<BestSellerBook> models) { |
| 65 | + progressBar.hide(); |
| 66 | + recyclerView.setAdapter(new BestSellerBooksRecyclerViewAdapter(models, BestSellerBooksFragment.this)); |
| 67 | + Log.d("BestSellerBooksFragment", "response successful"); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void onFailure(Throwable error) { |
| 72 | + progressBar.hide(); |
| 73 | + Log.e("BestSellerBooksFragment", error.getMessage()); |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + @Override |
| 81 | + public void onAttach(Context context) { |
| 82 | + super.onAttach(context); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void onDetach() { |
| 87 | + super.onDetach(); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void onItemClick(BestSellerBook item) { |
| 92 | + Toast.makeText(getContext(), "test", Toast.LENGTH_LONG).show(); |
| 93 | + } |
| 94 | +} |
0 commit comments