diff --git a/app/src/main/java/com/example/citrusclient/views/OtherCalendarFragment.java b/app/src/main/java/com/example/citrusclient/views/OtherCalendarFragment.java index 84c4e88..0161adb 100644 --- a/app/src/main/java/com/example/citrusclient/views/OtherCalendarFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/OtherCalendarFragment.java @@ -11,6 +11,7 @@ import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -77,6 +78,8 @@ scheduleViewModel = new ViewModelProvider(this).get(ScheduleViewModel.class); + booksViewModel = new ViewModelProvider(this).get(BooksViewModel.class); + todosViewModel = new ViewModelProvider(this).get(TodosViewModel.class); schedules = new HashMap<>(); @@ -90,7 +93,7 @@ } - + BooksViewModel booksViewModel; private List scheduleList; ScheduleViewModel scheduleViewModel; TodosViewModel todosViewModel; @@ -101,7 +104,7 @@ int day; HashMap books; - + private Book book; HashMap> schedules; HashMap> todos; @@ -179,7 +182,24 @@ } }); - + booksViewModel.getBook().observe(getViewLifecycleOwner(), new Observer() { + @Override + public void onChanged(Book updateBook) { + book = updateBook; + if (book != null) { + TextView curMonth = view.findViewById(R.id.otherMonth); + if (todos != null) { + todos.clear(); + } else { + todos = new HashMap<>(); + } + todosViewModel.loadTodosByMonth(accountId, book.getBookId(), year, month, token); + updateCalendar(curMonth); + Log.d("Debug", "Book: " + book); + Log.d("Debug", "Color: " + (book != null ? book.getColor() : "No color")); + } + } + }); todosViewModel.getTodosByMonthLiveData().observe(getViewLifecycleOwner(), new Observer>>() { @Override @@ -207,6 +227,7 @@ // month = calendar.get(Calendar.MONTH) + 1; //現在の月 scheduleViewModel.updateSchedulesByMonthAndBookId(accountId, year, month, token, bookId); + booksViewModel.loadBook(accountId, token, bookId); todosViewModel.loadTodosByMonth(accountId, bookId, year, month, token); //現在の月の最後の日付を取得 @@ -323,7 +344,7 @@ } citrus.setCurMonth(month + 1); } - ((MainActivity) getActivity()).showFragment(new HomeFragment()); + ((MainActivity) getActivity()).showFragment(new OtherHomeFragment()); } }); RecyclerView recyclerView = new RecyclerView(requireContext()); @@ -332,7 +353,7 @@ List schedules = new ArrayList<>(); - recyclerView.setAdapter(new OtherScheduleAdapter(schedules, books)); + recyclerView.setAdapter(new OtherScheduleAdapter(schedules, book)); layout.addView(recyclerView); layout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override @@ -410,36 +431,39 @@ } } } - ((OtherScheduleAdapter) recyclerView.getAdapter()).setSchedules(schedules, books); + ((OtherScheduleAdapter) recyclerView.getAdapter()).setSchedules(schedules, book); ((TextView) layout.getChildAt(0)).setText(days[i][j]); } } } } + + class OtherScheduleAdapter extends RecyclerView.Adapter { private List scheduleList; - private HashMap integerBookHashMap; + private Book book; - OtherScheduleAdapter(List schedules, HashMap integerBookHashMap) { + OtherScheduleAdapter(List schedules, Book book) { this.scheduleList = schedules; - if(integerBookHashMap != null) { - this.integerBookHashMap = new HashMap<>(integerBookHashMap); + if(book != null) { + this.book = book; } else { - this.integerBookHashMap = new HashMap<>(); + this.book = new Book(); } } - public void setSchedules(List schedules, HashMap integerBookHashMap) { + public void setSchedules(List schedules, Book book) { scheduleList = schedules; - if(integerBookHashMap != null) { - this.integerBookHashMap = new HashMap<>(integerBookHashMap); + if(book != null) { + this.book = book; } else { - this.integerBookHashMap = new HashMap<>(); + this.book = new Book(); } notifyDataSetChanged(); } + @NonNull @Override public OtherScheduleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { @@ -454,7 +478,6 @@ int blue = 169; Task scheduleData = this.scheduleList.get(position); holder.scheduleText.setText(scheduleData.getTitle()); - Book book = integerBookHashMap.get(scheduleData.getBookId()); if(book != null) { red = Integer.parseInt(book.getColor().substring(1, 3), 16); green = Integer.parseInt(book.getColor().substring(3, 5), 16);