Newer
Older
CitrusClient / app / src / main / java / com / example / citrusclient / views / HomeFragment.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.view.LayoutInflater;
  19. import android.view.View;
  20. import android.view.ViewGroup;
  21. import android.widget.Button;
  22. import android.widget.CheckBox;
  23. import android.widget.TextView;
  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.models.Schedule;
  29. import com.example.citrusclient.models.Todo;
  30. import com.example.citrusclient.viewmodels.BooksViewModel;
  31. import com.example.citrusclient.viewmodels.ScheduleViewModel;
  32. import com.example.citrusclient.viewmodels.TodosViewModel;
  33. import com.google.android.material.floatingactionbutton.FloatingActionButton;
  34.  
  35. import java.security.PrivateKey;
  36. import java.util.ArrayList;
  37. import java.util.Calendar;
  38. import java.util.HashMap;
  39. import java.util.List;
  40.  
  41. import java.time.LocalDate;
  42.  
  43. /**
  44. * A simple {@link Fragment} subclass.
  45. * Use the {@link HomeFragment#newInstance} factory method to
  46. * create an instance of this fragment.
  47. */
  48. public class HomeFragment extends Fragment {
  49.  
  50. // TODO: Rename parameter arguments, choose names that match
  51. // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
  52. private static final String ARG_PARAM1 = "param1";
  53. private static final String ARG_PARAM2 = "param2";
  54.  
  55. // TODO: Rename and change types of parameters
  56. private String mParam1;
  57. private String mParam2;
  58.  
  59. public HomeFragment() {
  60. // Required empty public constructor
  61. }
  62.  
  63. /**
  64. * Use this factory method to create a new instance of
  65. * this fragment using the provided parameters.
  66. *
  67. * @param param1 Parameter 1.
  68. * @param param2 Parameter 2.
  69. * @return A new instance of fragment HomeFragment.
  70. */
  71. // TODO: Rename and change types and number of parameters
  72. public static HomeFragment newInstance(String param1, String param2) {
  73. HomeFragment fragment = new HomeFragment();
  74. Bundle args = new Bundle();
  75. args.putString(ARG_PARAM1, param1);
  76. args.putString(ARG_PARAM2, param2);
  77. fragment.setArguments(args);
  78. return fragment;
  79. }
  80.  
  81. @Override
  82. public void onCreate(Bundle savedInstanceState) {
  83. super.onCreate(savedInstanceState);
  84. if (getArguments() != null) {
  85. mParam1 = getArguments().getString(ARG_PARAM1);
  86. mParam2 = getArguments().getString(ARG_PARAM2);
  87. }
  88.  
  89. todosViewModel = new ViewModelProvider(this).get(TodosViewModel.class);
  90. scheduleViewModel = new ViewModelProvider(this).get(ScheduleViewModel.class);
  91. booksViewModel = new ViewModelProvider(this).get(BooksViewModel.class);
  92.  
  93. }
  94.  
  95.  
  96.  
  97. @Override
  98. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  99. Bundle savedInstanceState) {
  100. // Inflate the layout for this fragment
  101.  
  102. return inflater.inflate(R.layout.fragment_home, container, false);
  103. }
  104.  
  105. private List<Todo> todoList;
  106. TodosViewModel todosViewModel;
  107. private List<Schedule> scheduleList;
  108. ScheduleViewModel scheduleViewModel;
  109.  
  110. private HashMap<Integer, Book> integerBookHashMap;
  111. BooksViewModel booksViewModel;
  112.  
  113. private LocalDate openDate;
  114. int year;
  115. int month;
  116. int day;
  117.  
  118. public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
  119. super.onViewCreated(view, savedInstanceState);
  120.  
  121.  
  122. Citrus citrus = (Citrus)(getActivity().getApplication());
  123. String token = citrus.getToken();
  124. String accountId = citrus.getAccountId();
  125. year = citrus.getCurYear();
  126. month = citrus.getCurMonth();
  127. day = citrus.getCurDay();
  128.  
  129. int curBookId = citrus.getCurBookId();
  130.  
  131. openDate = LocalDate.of(year, month, day);
  132.  
  133. todoList = new ArrayList<>();
  134. scheduleList = new ArrayList<>();
  135.  
  136. TextView curDate = view.findViewById(R.id.year_month_day);
  137. curDate.setText(year + "年" + month + "月" + day + "日");
  138.  
  139.  
  140. RecyclerView todoRecyclerView = view.findViewById(R.id.my_todos_list);
  141. todoRecyclerView.setHasFixedSize(true);
  142. RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(view.getContext());
  143. todoRecyclerView.setLayoutManager(layoutManager);
  144. MyTodoshelfAdapter todoAdapter = new MyTodoshelfAdapter(todoList, integerBookHashMap, todosViewModel, citrus);
  145. todoRecyclerView.setAdapter(todoAdapter);
  146.  
  147. RecyclerView scheduleRecyclerView = view.findViewById(R.id.my_schedule_list);
  148. scheduleRecyclerView.setHasFixedSize(true);
  149. RecyclerView.LayoutManager scheduleLayoutManager = new LinearLayoutManager(view.getContext());
  150. scheduleRecyclerView.setLayoutManager(scheduleLayoutManager);
  151. MyScheduleshelfAdapter scheduleAdapter = new MyScheduleshelfAdapter(scheduleList, integerBookHashMap);
  152. scheduleRecyclerView.setAdapter(scheduleAdapter);
  153.  
  154.  
  155. booksViewModel.loadBooks(accountId, token);
  156. System.out.println(token);
  157.  
  158. booksViewModel.getBookLiveData().observe(getViewLifecycleOwner(), new Observer<HashMap<Integer, Book>>() {
  159. @Override
  160. public void onChanged(HashMap<Integer, Book> idBookHashMap) {
  161. if (idBookHashMap != null) {
  162. if(curBookId != -1) {
  163. integerBookHashMap = new HashMap<>();
  164. Book book = idBookHashMap.get(curBookId);
  165. integerBookHashMap.put(curBookId, book);
  166. todosViewModel.loadTodosByDay(accountId, curBookId, year, month, day, token);
  167. citrus.setCurBookId(-1);
  168. } else {
  169. todoList = new ArrayList<>();
  170. integerBookHashMap = new HashMap<>(idBookHashMap);
  171. for (Book book : integerBookHashMap.values()) {
  172. todosViewModel.loadTodosByDay(accountId, book.getBookId(), year, month, day, token);
  173. }
  174. }
  175. scheduleViewModel.updateSchedulesByDay(accountId, year, month, day, token);
  176. }
  177. }
  178. });
  179.  
  180. todosViewModel.getTodosByDayLiveData().observe(getViewLifecycleOwner(), new Observer<HashMap<Integer, Todo>>() {
  181. @Override
  182. public void onChanged(HashMap<Integer, Todo> idTodoHashMap) {
  183. if(idTodoHashMap != null) {
  184. todoList.addAll(idTodoHashMap.values());
  185. todoAdapter.setTodos(todoList, integerBookHashMap);
  186. }
  187. }
  188. });
  189.  
  190. scheduleViewModel.getSchedulesByDay().observe(getViewLifecycleOwner(), new Observer<HashMap<Integer, Schedule>>() {
  191. @Override
  192. public void onChanged(HashMap<Integer, Schedule> idScheduleHashMap) {
  193. if(idScheduleHashMap != null) {
  194. scheduleList = new ArrayList<>(idScheduleHashMap.values());
  195. scheduleAdapter.setSchedules(scheduleList, integerBookHashMap);
  196. }
  197. }
  198. });
  199.  
  200. FloatingActionButton todoAddButton = view.findViewById(R.id.todo_add_button);
  201. todoAddButton.setOnClickListener(v -> {
  202. ((MainActivity) getActivity()).showFragment(new CreateTodoFragment());
  203. });
  204.  
  205. FloatingActionButton scheduleAddButton = view.findViewById(R.id.schedule_add_button);
  206. scheduleAddButton.setOnClickListener(v -> {
  207. ((MainActivity) getActivity()).showFragment(new CreateScheduleFragment());
  208. });
  209.  
  210. // FloatingActionButton prevBotton = view.findViewById(R.id.prev_day_botton);
  211. // prevBotton.setOnClickListener(v -> {
  212. // openDate = openDate.minusDays(1);
  213. // year = openDate.getYear();
  214. // month = openDate.getMonthValue();
  215. // day = openDate.getDayOfMonth();
  216. // });
  217.  
  218. }
  219.  
  220. }
  221.  
  222. class MyTodoshelfAdapter extends RecyclerView.Adapter<MyTodoshelfAdapter.MyTodoViewHolder> {
  223. private List<Todo> todoList;
  224. private HashMap<Integer, Book> idBookHashMap;
  225. private TodosViewModel todosViewModel;
  226. private Citrus citrus;
  227. private String accountId;
  228. private String token;
  229.  
  230. MyTodoshelfAdapter(List<Todo> todos, HashMap<Integer, Book> idBookHashMap, TodosViewModel todosViewModel, Citrus citrus) {
  231. this.citrus = citrus;
  232. this.token = citrus.getToken();
  233. this.accountId = citrus.getAccountId();
  234. this.todoList = todos;
  235. this.todosViewModel = todosViewModel;
  236. if (idBookHashMap != null) {
  237. this.idBookHashMap = new HashMap<>(idBookHashMap);
  238. } else {
  239. this.idBookHashMap = new HashMap<>(); // 空のHashMapを作成
  240. }
  241. }
  242.  
  243. public void setTodos(List<Todo> todos ,HashMap<Integer, Book> idBookHashMap) {
  244. todoList = todos;
  245. if (idBookHashMap != null) {
  246. this.idBookHashMap = new HashMap<>(idBookHashMap);
  247. } else {
  248. this.idBookHashMap = new HashMap<>(); // 空のHashMapを作成
  249. }
  250. notifyDataSetChanged();
  251. }
  252.  
  253. @NonNull
  254. @Override
  255. public MyTodoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  256. View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.a_todo, parent, false);
  257. return new MyTodoViewHolder(view);
  258. }
  259.  
  260. @Override
  261. public void onBindViewHolder(@NonNull MyTodoViewHolder holder, int position) {
  262. Todo todoData = this.todoList.get(position);
  263. holder.todoButton.setText(todoData.getTitle());
  264. holder.todoCheckBox.setChecked(todoData.getCheck());
  265. Book book = idBookHashMap.get(todoData.getBookId());
  266. if(book != null) {
  267. int red = Integer.parseInt(book.getColor().substring(1, 3), 16);
  268. int green = Integer.parseInt(book.getColor().substring(3, 5), 16);
  269. int blue = Integer.parseInt(book.getColor().substring(5, 7), 16);
  270. holder.todoButton.setBackgroundColor(Color.rgb(red, green, blue));
  271. holder.todoButton.setTextColor(Color.rgb(255 - red, 255 - green, 255 - blue));
  272. }
  273.  
  274. holder.todoCheckBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
  275. // チェック状態が変更された時の処理を記述
  276. if (isChecked) {
  277. // チェックされた時の処理
  278. todosViewModel.setCheck(accountId, book.getBookId(), todoData.getYear(), todoData.getMonth(), todoData.getDay(), todoData.getTodoId(), true, token);
  279. } else {
  280. // チェックが外された時の処理
  281. todosViewModel.setCheck(accountId, book.getBookId(), todoData.getYear(), todoData.getMonth(), todoData.getDay(), todoData.getTodoId(), false, token);
  282. }
  283. });
  284.  
  285. }
  286.  
  287. @Override
  288. public int getItemCount() {
  289. return todoList.size();
  290. }
  291.  
  292. static class MyTodoViewHolder extends RecyclerView.ViewHolder {
  293. Button todoButton;
  294. CheckBox todoCheckBox;
  295. public MyTodoViewHolder(@NonNull View itemView) {
  296. super(itemView);
  297. todoButton = itemView.findViewById(R.id.todo_button);
  298. todoCheckBox = itemView.findViewById(R.id.todo_checkBox);
  299. }
  300. }
  301. }
  302.  
  303. class MyScheduleshelfAdapter extends RecyclerView.Adapter<MyScheduleshelfAdapter.MyScheduleViewHolder> {
  304.  
  305. private List<Schedule> scheduleList;
  306. private HashMap<Integer, Book> idBookHashMap;
  307.  
  308. MyScheduleshelfAdapter(List<Schedule> schedules, HashMap<Integer, Book> idBookHashMap) {
  309. this.scheduleList = schedules;
  310. if (idBookHashMap != null) {
  311. this.idBookHashMap = new HashMap<>(idBookHashMap);
  312. } else {
  313. this.idBookHashMap = new HashMap<>(); // 空のHashMapを作成
  314. } }
  315.  
  316. public void setSchedules(List<Schedule> schedules, HashMap<Integer, Book> idBookHashMap) {
  317. scheduleList = schedules;
  318. if (idBookHashMap != null) {
  319. this.idBookHashMap = new HashMap<>(idBookHashMap);
  320. } else {
  321. this.idBookHashMap = new HashMap<>(); // 空のHashMapを作成
  322. } notifyDataSetChanged();
  323. }
  324.  
  325. @NonNull
  326. @Override
  327. public MyScheduleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  328. View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.a_schedule, parent, false);
  329. return new MyScheduleViewHolder(view);
  330. }
  331.  
  332. @Override
  333. public void onBindViewHolder(@NonNull MyScheduleViewHolder holder, int position) {
  334. int red = 169;
  335. int green = 169;
  336. int blue = 169;
  337. Schedule scheduleData = this.scheduleList.get(position);
  338. holder.scheduleButton.setText(scheduleData.getTitle());
  339. Book book = idBookHashMap.get(scheduleData.getBookId());
  340. if(book != null) {
  341. red = Integer.parseInt(book.getColor().substring(1, 3), 16);
  342. green = Integer.parseInt(book.getColor().substring(3, 5), 16);
  343. blue = Integer.parseInt(book.getColor().substring(5, 7), 16);
  344. }
  345. holder.scheduleButton.setBackgroundColor(Color.rgb(red, green, blue));
  346. holder.scheduleButton.setTextColor(Color.rgb(255 - red, 255 - green, 255 - blue));
  347.  
  348. }
  349.  
  350.  
  351. @Override
  352. public int getItemCount() {
  353. return scheduleList.size();
  354. }
  355.  
  356. static class MyScheduleViewHolder extends RecyclerView.ViewHolder {
  357. Button scheduleButton;
  358. public MyScheduleViewHolder(@NonNull View itemView) {
  359. super(itemView);
  360. scheduleButton = itemView.findViewById(R.id.schedule_button);
  361. }
  362. }
  363. }