Newer
Older
CitrusClient / app / src / main / java / com / example / citrusclient / views / MyBookshelfFragment.java
  1. package com.example.citrusclient.views;
  2.  
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.graphics.Color;
  6. import android.media.Image;
  7. import android.os.Bundle;
  8.  
  9. import androidx.annotation.NonNull;
  10. import androidx.annotation.Nullable;
  11. import androidx.fragment.app.Fragment;
  12. import androidx.lifecycle.Observer;
  13. import androidx.lifecycle.ViewModelProvider;
  14. import androidx.recyclerview.widget.GridLayoutManager;
  15. import androidx.recyclerview.widget.LinearLayoutManager;
  16. import androidx.recyclerview.widget.RecyclerView;
  17.  
  18. import android.util.Log;
  19. import android.view.LayoutInflater;
  20. import android.view.View;
  21. import android.view.ViewGroup;
  22. import android.widget.Button;
  23. import android.widget.GridLayout;
  24.  
  25. import com.example.citrusclient.Citrus;
  26. import com.example.citrusclient.R;
  27. import com.example.citrusclient.models.Book;
  28. import com.example.citrusclient.viewmodels.BooksViewModel;
  29. import com.google.android.material.floatingactionbutton.FloatingActionButton;
  30. import com.google.android.material.snackbar.Snackbar;
  31.  
  32. import java.util.ArrayList;
  33. import java.util.Calendar;
  34. import java.util.HashMap;
  35. import java.util.List;
  36.  
  37. /**
  38. * A simple {@link Fragment} subclass.
  39. * Use the {@link MyBookshelfFragment#newInstance} factory method to
  40. * create an instance of this fragment.
  41. */
  42. public class MyBookshelfFragment extends Fragment {
  43.  
  44. // TODO: Rename parameter arguments, choose names that match
  45. // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
  46. private static final String ARG_PARAM1 = "param1";
  47. private static final String ARG_PARAM2 = "param2";
  48.  
  49. // TODO: Rename and change types of parameters
  50. private String mParam1;
  51. private String mParam2;
  52.  
  53. public MyBookshelfFragment() {
  54. // Required empty public constructor
  55. }
  56.  
  57. /**
  58. * Use this factory method to create a new instance of
  59. * this fragment using the provided parameters.
  60. *
  61. * @param param1 Parameter 1.
  62. * @param param2 Parameter 2.
  63. * @return A new instance of fragment MyBookshelfFragment.
  64. */
  65. // TODO: Rename and change types and number of parameters
  66. public static MyBookshelfFragment newInstance(String param1, String param2) {
  67. MyBookshelfFragment fragment = new MyBookshelfFragment();
  68. Bundle args = new Bundle();
  69. args.putString(ARG_PARAM1, param1);
  70. args.putString(ARG_PARAM2, param2);
  71. fragment.setArguments(args);
  72. return fragment;
  73. }
  74.  
  75.  
  76. private List<Book> bookList;
  77. BooksViewModel booksViewModel;
  78.  
  79.  
  80. @Override
  81. public void onCreate(Bundle savedInstanceState) {
  82. super.onCreate(savedInstanceState);
  83. if (getArguments() != null) {
  84. mParam1 = getArguments().getString(ARG_PARAM1);
  85. mParam2 = getArguments().getString(ARG_PARAM2);
  86. }
  87.  
  88. booksViewModel = new ViewModelProvider(this).get(BooksViewModel.class);
  89.  
  90. }
  91.  
  92. @Override
  93. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  94. Bundle savedInstanceState) {
  95. // Inflate the layout for this fragment
  96.  
  97. return inflater.inflate(R.layout.fragment_my_bookshelf_fragment, container, false);
  98.  
  99. }
  100.  
  101.  
  102.  
  103. @Override
  104. public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
  105. super.onViewCreated(view, savedInstanceState);
  106.  
  107.  
  108. Citrus citrus = (Citrus)(getActivity().getApplication());
  109. String token = citrus.getToken();
  110. String accountId = citrus.getAccountId();
  111.  
  112. bookList = new ArrayList<>();
  113. // bookList.add(new Book("a", 1, "a", true, "#ff0000"));
  114.  
  115. RecyclerView recyclerView = view.findViewById(R.id.my_books_list);
  116. recyclerView.setHasFixedSize(true);
  117. RecyclerView.LayoutManager layoutManager = new GridLayoutManager(view.getContext(), 2);
  118. //new LinearLayoutManager(view.getContext());
  119. recyclerView.setLayoutManager(layoutManager);
  120. MyBookshelfAdapter bookAdapter = new MyBookshelfAdapter(bookList, getActivity());
  121. recyclerView.setAdapter(bookAdapter);
  122.  
  123. FloatingActionButton addButton = view.findViewById(R.id.book_add_button);
  124. addButton.setOnClickListener(v -> {
  125. ((MainActivity) getActivity()).showFragment(new CreateBookFragment());
  126. });
  127.  
  128. booksViewModel.getBookLiveData().observe(getViewLifecycleOwner(), new Observer<HashMap<Integer, Book>>() {
  129. @Override
  130. public void onChanged(HashMap<Integer, Book> integerBookHashMap) {
  131. if(integerBookHashMap != null){
  132. bookList = new ArrayList<>(integerBookHashMap.values());
  133. }else{
  134. bookList = new ArrayList<>();
  135. }
  136. bookAdapter.setBooks(bookList);
  137. }
  138. });
  139.  
  140. booksViewModel.loadBooks(accountId, token);
  141.  
  142.  
  143. }
  144.  
  145. }
  146.  
  147. class MyBookshelfAdapter extends RecyclerView.Adapter<MyBookshelfAdapter.MyBookViewHolder>{
  148.  
  149. private List<Book> bookList;
  150. private Context context;
  151.  
  152. MyBookshelfAdapter(List<Book> book, Context context){
  153. this.bookList = book;
  154. this.context = context;
  155. }
  156.  
  157. public void setBooks(List<Book> books){
  158. bookList = books;
  159. notifyDataSetChanged();
  160. }
  161.  
  162. @NonNull
  163. @Override
  164. public MyBookViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  165. View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.a_book, parent, false);
  166. return new MyBookViewHolder(view);
  167. }
  168.  
  169. @Override
  170. public void onBindViewHolder(@NonNull MyBookViewHolder holder, int position) {
  171. Book bookData = this.bookList.get(position);
  172. holder.bookButton.setText(bookData.getTitle());
  173. int red = Integer.parseInt(bookData.getColor().substring(1, 3), 16);
  174. int green = Integer.parseInt(bookData.getColor().substring(3, 5), 16);
  175. int blue = Integer.parseInt(bookData.getColor().substring(5, 7), 16);
  176. holder.bookButton.setBackgroundColor(Color.rgb(red, green, blue));
  177. holder.bookButton.setTextColor(Color.rgb(255 - red, 255 - green, 255 - blue));
  178. holder.bookButton.setOnClickListener(v -> {
  179. Calendar c = Calendar.getInstance();
  180. Activity activity = (Activity) context;
  181. Citrus citrus = (Citrus) activity.getApplication();
  182. citrus.setCurYear(c.get(Calendar.YEAR));
  183. citrus.setCurMonth(c.get(Calendar.MONTH) + 1);
  184. citrus.setCurDay(c.get(Calendar.DATE));
  185. citrus.setCurBookId(bookData.getBookId());
  186. ((MainActivity) activity).showFragment(new HomeFragment());
  187.  
  188. });
  189. }
  190.  
  191. @Override
  192. public int getItemCount() {
  193. return bookList.size();
  194. }
  195.  
  196. static class MyBookViewHolder extends RecyclerView.ViewHolder{
  197. Button bookButton;
  198. public MyBookViewHolder(@NonNull View itemView) {
  199. super(itemView);
  200. bookButton = itemView.findViewById(R.id.book_button);
  201. }
  202. }
  203.  
  204. }