diff --git a/app/src/main/java/com/example/citrusclient/views/OtherHomeFragment.java b/app/src/main/java/com/example/citrusclient/views/OtherHomeFragment.java index 4279181..5eed4ef 100644 --- a/app/src/main/java/com/example/citrusclient/views/OtherHomeFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/OtherHomeFragment.java @@ -111,16 +111,16 @@ Bundle savedInstanceState) { // Inflate the layout for this fragment - return inflater.inflate(R.layout.fragment_home, container, false); + return inflater.inflate(R.layout.fragment_other_home, container, false); } - private List todoList; + BooksViewModel booksViewModel; TodosViewModel todosViewModel; - private List scheduleList; ScheduleViewModel scheduleViewModel; - private HashMap integerBookHashMap; - BooksViewModel booksViewModel; + private Book book; + private List todoList; + private List scheduleList; LocalDate curDate; @@ -135,6 +135,9 @@ Citrus citrus = (Citrus)(getActivity().getApplication()); String token = citrus.getToken(); String accountId = citrus.getCurLookingAccountId(); + + int curBookId = citrus.getCurLookingBookId(); + System.out.println(token); curDate = LocalDate.of(year, month, day); @@ -149,14 +152,14 @@ todoRecyclerView.setHasFixedSize(true); RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(view.getContext()); todoRecyclerView.setLayoutManager(layoutManager); - MyOtherTodoshelfAdapter todoAdapter = new MyOtherTodoshelfAdapter(todoList, integerBookHashMap, todosViewModel, getActivity()); + MyOtherTodoshelfAdapter todoAdapter = new MyOtherTodoshelfAdapter(todoList, book, todosViewModel, getActivity()); todoRecyclerView.setAdapter(todoAdapter); RecyclerView scheduleRecyclerView = view.findViewById(R.id.my_schedule_list); scheduleRecyclerView.setHasFixedSize(true); RecyclerView.LayoutManager scheduleLayoutManager = new LinearLayoutManager(view.getContext()); scheduleRecyclerView.setLayoutManager(scheduleLayoutManager); - MyOtherScheduleshelfAdapter scheduleAdapter = new MyOtherScheduleshelfAdapter(scheduleList, integerBookHashMap, curDate, getActivity()); + MyOtherScheduleshelfAdapter scheduleAdapter = new MyOtherScheduleshelfAdapter(scheduleList, book, curDate, getActivity()); scheduleRecyclerView.setAdapter(scheduleAdapter); Button dateButton = view.findViewById(R.id.date_button); @@ -171,9 +174,11 @@ month = curDate.getMonthValue(); day = curDate.getDayOfMonth(); curDateButton.setText(year + "年" + month + "月" + day + "日"); - + scheduleList.clear(); scheduleAdapter.updateCurDate(curDate); - updateTodoSchedule(integerBookHashMap); + + clearTodoSchedule(todoAdapter, scheduleAdapter); + updateTodoSchedule(book); }); ImageButton nextBotton = view.findViewById(R.id.next_day_botton); @@ -183,21 +188,23 @@ month = curDate.getMonthValue(); day = curDate.getDayOfMonth(); curDateButton.setText(year + "年" + month + "月" + day + "日"); - scheduleAdapter.updateCurDate(curDate); - updateTodoSchedule(integerBookHashMap); + + clearTodoSchedule(todoAdapter, scheduleAdapter); + updateTodoSchedule(book); }); - - booksViewModel.loadBooks(accountId, token); - scheduleViewModel.updateSchedulesByDay(accountId, year, month, day, token); + booksViewModel.loadBook(accountId, token, curBookId); + todosViewModel.loadTodosByDay(accountId, curBookId, year, month, day, token); + scheduleViewModel.updateSchedulesByDayAndBookId(accountId, year, month, day, token, curBookId); System.out.println(token); - booksViewModel.getBookLiveData().observe(getViewLifecycleOwner(), new Observer>() { + booksViewModel.getBook().observe(getViewLifecycleOwner(), new Observer() { @Override - public void onChanged(HashMap idBookHashMap) { - updateTodoSchedule(idBookHashMap); + public void onChanged(Book book) { + clearTodoSchedule(todoAdapter, scheduleAdapter); + updateTodoSchedule(book); } }); @@ -205,14 +212,10 @@ @Override public void onChanged(HashMap idTodoHashMap) { if(idTodoHashMap != null) { - for(Todo todo : idTodoHashMap.values()){ - if(!todo.containsTodo(todoList)){ - todoList.add(todo); - } - } - todoAdapter.setTodos(todoList, integerBookHashMap); + todoList = new ArrayList<>(idTodoHashMap.values()); + todoAdapter.setTodos(todoList, book); } else { - todoAdapter.setTodos(todoList, integerBookHashMap); + todoAdapter.setTodos(todoList, book); } } }); @@ -222,10 +225,10 @@ public void onChanged(HashMap idScheduleHashMap) { if(idScheduleHashMap != null) { scheduleList = new ArrayList<>(idScheduleHashMap.values()); - scheduleAdapter.setSchedules(scheduleList, integerBookHashMap); + scheduleAdapter.setSchedules(scheduleList, book); } else { scheduleList = new ArrayList<>(); - scheduleAdapter.setSchedules(scheduleList, integerBookHashMap); + scheduleAdapter.setSchedules(scheduleList, book); } } }); @@ -234,28 +237,24 @@ } - private void updateTodoSchedule(HashMap idBookHashMap) { + private void updateTodoSchedule(Book book) { Citrus citrus = (Citrus)(getActivity().getApplication()); String token = citrus.getToken(); String accountId = citrus.getCurLookingAccountId(); int curBookId = citrus.getCurLookingBookId(); - if (idBookHashMap != null) { - if(curBookId != -1) { - todoList = new ArrayList<>(); - integerBookHashMap = new HashMap<>(); - Book book = idBookHashMap.get(curBookId); - integerBookHashMap.put(curBookId, book); - todosViewModel.loadTodosByDay(accountId, curBookId, year, month, day, token); - } else { - todoList = new ArrayList<>(); - integerBookHashMap = new HashMap<>(idBookHashMap); - for (Book book : integerBookHashMap.values()) { - todosViewModel.loadTodosByDay(accountId, book.getBookId(), year, month, day, token); - } - } + if (book != null) { + this.book = book; } - scheduleViewModel.updateSchedulesByDay(accountId, year, month, day, token); + todosViewModel.loadTodosByDay(accountId, curBookId, year, month, day, token); + scheduleViewModel.updateSchedulesByDayAndBookId(accountId, year, month, day, token, curBookId); + } + + private void clearTodoSchedule(MyOtherTodoshelfAdapter todoAdapter, MyOtherScheduleshelfAdapter scheduleAdapter) { + todoList.clear(); + todoAdapter.setTodos(todoList, book); + scheduleList.clear(); + scheduleAdapter.setSchedules(scheduleList, book); } @@ -271,29 +270,21 @@ class MyOtherTodoshelfAdapter extends RecyclerView.Adapter { private List todoList; - private HashMap idBookHashMap; + private Book book; private TodosViewModel todosViewModel; private Context context; - MyOtherTodoshelfAdapter(List todos, HashMap idBookHashMap, TodosViewModel todosViewModel, Context context) { + MyOtherTodoshelfAdapter(List todos, Book book, TodosViewModel todosViewModel, Context context) { this.todoList = todos; this.todosViewModel = todosViewModel; - if (idBookHashMap != null) { - this.idBookHashMap = new HashMap<>(idBookHashMap); - } else { - this.idBookHashMap = new HashMap<>(); // 空のHashMapを作成 - } + this.book = book; this.context = context; } - public void setTodos(List todos ,HashMap idBookHashMap) { - todoList = todos; - if (idBookHashMap != null) { - this.idBookHashMap = new HashMap<>(idBookHashMap); - } else { - this.idBookHashMap = new HashMap<>(); // 空のHashMapを作成 - } + public void setTodos(List todos ,Book book) { + this.todoList = todos; + this.book = book; sortTodosById(); notifyDataSetChanged(); @@ -308,15 +299,15 @@ @Override public void onBindViewHolder(@NonNull MyTodoViewHolder holder, int position) { - Activity activity = (Activity) context; - Citrus citrus = (Citrus)(activity.getApplication()); - String token = citrus.getToken(); - String accountId = citrus.getCurLookingAccountId(); +// Activity activity = (Activity) context; +// Citrus citrus = (Citrus)(activity.getApplication()); +// String token = citrus.getToken(); +// String accountId = citrus.getCurLookingAccountId(); Todo todoData = this.todoList.get(position); holder.todoButton.setText(todoData.getTitle()); holder.todoCheckBox.setChecked(todoData.getCheck()); - Book book = idBookHashMap.get(todoData.getBookId()); + holder.todoCheckBox.setEnabled(false); if(book != null) { int red = Integer.parseInt(book.getColor().substring(1, 3), 16); int green = Integer.parseInt(book.getColor().substring(3, 5), 16); @@ -325,20 +316,20 @@ holder.todoButton.setTextColor(Color.rgb(255 - red, 255 - green, 255 - blue)); } - holder.todoButton.setOnClickListener(v -> { - ((MainActivity) activity).showFragment(new CreateTodoFragment(todoData, book)); - }); - - holder.todoCheckBox.setOnCheckedChangeListener((buttonView, isChecked) -> { - // チェック状態が変更された時の処理を記述 - if (isChecked) { - // チェックされた時の処理 - todosViewModel.setCheck(accountId, book.getBookId(), todoData.getYear(), todoData.getMonth(), todoData.getDay(), todoData.getTodoId(), true, token); - } else { - // チェックが外された時の処理 - todosViewModel.setCheck(accountId, book.getBookId(), todoData.getYear(), todoData.getMonth(), todoData.getDay(), todoData.getTodoId(), false, token); - } - }); +// holder.todoButton.setOnClickListener(v -> { +// ((MainActivity) activity).showFragment(new CreateTodoFragment(todoData, book)); +// }); +// +// holder.todoCheckBox.setOnCheckedChangeListener((buttonView, isChecked) -> { +// // チェック状態が変更された時の処理を記述 +// if (isChecked) { +// // チェックされた時の処理 +// todosViewModel.setCheck(accountId, book.getBookId(), todoData.getYear(), todoData.getMonth(), todoData.getDay(), todoData.getTodoId(), true, token); +// } else { +// // チェックが外された時の処理 +// todosViewModel.setCheck(accountId, book.getBookId(), todoData.getYear(), todoData.getMonth(), todoData.getDay(), todoData.getTodoId(), false, token); +// } +// }); } @@ -375,28 +366,20 @@ class MyOtherScheduleshelfAdapter extends RecyclerView.Adapter { private List scheduleList; - private HashMap idBookHashMap; + Book book; private Context context; private LocalDate curDate; - MyOtherScheduleshelfAdapter(List schedules, HashMap idBookHashMap, LocalDate curDate, Context context) { + MyOtherScheduleshelfAdapter(List schedules, Book book, LocalDate curDate, Context context) { this.scheduleList = schedules; - if (idBookHashMap != null) { - this.idBookHashMap = new HashMap<>(idBookHashMap); - } else { - this.idBookHashMap = new HashMap<>(); // 空のHashMapを作成 - } + this.book = book; this.curDate = curDate; this.context = context; } - public void setSchedules(List schedules, HashMap idBookHashMap) { + public void setSchedules(List schedules, Book book) { scheduleList = schedules; - if (idBookHashMap != null) { - this.idBookHashMap = new HashMap<>(idBookHashMap); - } else { - this.idBookHashMap = new HashMap<>(); // 空のHashMapを作成 - } + this.book = book; sortScheduleByTime(); notifyDataSetChanged(); } @@ -410,7 +393,7 @@ @Override public void onBindViewHolder(@NonNull MyScheduleViewHolder holder, int position) { - Activity activity = (Activity) context; +// Activity activity = (Activity) context; int red = 169; int green = 169; @@ -418,7 +401,6 @@ Schedule scheduleData = this.scheduleList.get(position); holder.scheduleButton.setText(scheduleData.getTitle()); holder.scheduleButton.setText(scheduleData.getTitle() + "\n" + extractionTime(scheduleData.getStartTime(), curDate) + " ~ " + extractionTime(scheduleData.getEndTime(), curDate)); - Book book = idBookHashMap.get(scheduleData.getBookId()); if(book != null) { red = Integer.parseInt(book.getColor().substring(1, 3), 16); green = Integer.parseInt(book.getColor().substring(3, 5), 16); @@ -427,9 +409,9 @@ holder.scheduleButton.setBackgroundColor(Color.rgb(red, green, blue)); holder.scheduleButton.setTextColor(Color.rgb(255 - red, 255 - green, 255 - blue)); - holder.scheduleButton.setOnClickListener(v -> { - ((MainActivity) activity).showFragment(new CreateScheduleFragment(scheduleData, book)); - }); +// holder.scheduleButton.setOnClickListener(v -> { +// ((MainActivity) activity).showFragment(new CreateScheduleFragment(scheduleData, book)); +// }); }