NixFragment.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package club.thepenguins.android.fragments;
  2. import android.os.Bundle;
  3. import androidx.fragment.app.Fragment;
  4. import androidx.recyclerview.widget.LinearLayoutManager;
  5. import androidx.recyclerview.widget.RecyclerView;
  6. import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
  7. import android.util.Log;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import org.jsoup.parser.Parser;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. import club.thepenguins.android.R;
  15. import club.thepenguins.android.adapters.PostRecyclerAdapter;
  16. import club.thepenguins.android.api.APIService;
  17. import club.thepenguins.android.data.Model;
  18. import club.thepenguins.android.data.Posts;
  19. import club.thepenguins.android.utils.Constants;
  20. import retrofit2.Call;
  21. import retrofit2.Callback;
  22. import retrofit2.Response;
  23. import retrofit2.Retrofit;
  24. import retrofit2.converter.gson.GsonConverterFactory;
  25. /**
  26. * A simple {@link Fragment} subclass.
  27. * Use the {@link NixFragment#newInstance} factory method to
  28. * create an instance of this fragment.
  29. */
  30. public class NixFragment extends Fragment {
  31. // TODO: Rename parameter arguments, choose names that match
  32. // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
  33. private static final String ARG_PARAM1 = "param1";
  34. private static final String ARG_PARAM2 = "param2";
  35. // TODO: Rename and change types of parameters
  36. private String mParam1;
  37. private String mParam2;
  38. private RecyclerView recyclerView;
  39. private LinearLayoutManager LayoutManager;
  40. private ArrayList<Model> list;
  41. private PostRecyclerAdapter adapter;
  42. public static List<Posts> mListPost;
  43. private SwipeRefreshLayout swipeContainer;
  44. public NixFragment() {
  45. // Required empty public constructor
  46. }
  47. /**
  48. * Use this factory method to create a new instance of
  49. * this fragment using the provided parameters.
  50. *
  51. * @param param1 Parameter 1.
  52. * @param param2 Parameter 2.
  53. * @return A new instance of fragment NixFragment.
  54. */
  55. // TODO: Rename and change types and number of parameters
  56. public static NixFragment newInstance(String param1, String param2) {
  57. NixFragment fragment = new NixFragment();
  58. Bundle args = new Bundle();
  59. args.putString(ARG_PARAM1, param1);
  60. args.putString(ARG_PARAM2, param2);
  61. fragment.setArguments(args);
  62. return fragment;
  63. }
  64. @Override
  65. public void onCreate(Bundle savedInstanceState) {
  66. super.onCreate(savedInstanceState);
  67. if (getArguments() != null) {
  68. mParam1 = getArguments().getString(ARG_PARAM1);
  69. mParam2 = getArguments().getString(ARG_PARAM2);
  70. }
  71. }
  72. @Override
  73. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  74. Bundle savedInstanceState) {
  75. View rootView = inflater.inflate(R.layout.fragment_linux, container, false);
  76. getActivity().setTitle("Choose Fragment");
  77. recyclerView = rootView.findViewById(R.id.recycler_view);
  78. LayoutManager = new LinearLayoutManager(rootView.getContext(), LinearLayoutManager.VERTICAL, false);
  79. recyclerView.setLayoutManager(LayoutManager);
  80. swipeContainer = rootView.findViewById(R.id.swiperefresh);
  81. list = new ArrayList<>();
  82. getRetrofit();
  83. adapter = new PostRecyclerAdapter(list, rootView.getContext());
  84. recyclerView.setAdapter(adapter);
  85. swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
  86. @Override
  87. public void onRefresh() {
  88. adapter.clear();
  89. getRetrofit();
  90. }
  91. });
  92. swipeContainer.setColorSchemeResources(android.R.color.holo_blue_bright,
  93. android.R.color.holo_green_light,
  94. android.R.color.holo_orange_light,
  95. android.R.color.holo_red_light);
  96. return rootView;
  97. }
  98. private void getRetrofit() {
  99. swipeContainer.setRefreshing(true);
  100. Retrofit retrofit = new Retrofit.Builder()
  101. .baseUrl(Constants.BaseUrl)
  102. .addConverterFactory(GsonConverterFactory.create())
  103. .build();
  104. APIService service = retrofit.create(APIService.class);
  105. Call<List<Posts>> call = service.getCategoryPosts("12");
  106. call.enqueue(new Callback<List<Posts>>() {
  107. @Override
  108. public void onResponse(Call<List<Posts>> call, Response<List<Posts>> response) {
  109. mListPost = response.body();
  110. for (int i = 0; i < response.body().size(); i++) {
  111. list.add(new Model(Parser.unescapeEntities(response.body().get(i).getTitle().getRendered(), false), response.body().get(i).getContent().getRendered(), response.body().get(i).getEmbedded().getWpFeaturedmedia().get(0).getSourceUrl(), response.body().get(i).getLinks().getSelf().get(0).getHref(), response.body().get(i).getEmbedded().getAuthor().get(0).getName(), response.body().get(i).getLink()));
  112. //Log.d("Linux", "onResponse: " + response.body().get(i).getEmbedded().getWpFeaturedmedia().get(0).getSourceUrl());
  113. }
  114. adapter.notifyDataSetChanged();
  115. swipeContainer.setRefreshing(false);
  116. }
  117. @Override
  118. public void onFailure(Call<List<Posts>> call, Throwable t) {
  119. Log.d("Linux", "onFailure: ", t);
  120. }
  121. });
  122. }
  123. }