OtherHomeFragmentのtodoとscheduleの表示 #193

Merged t-watanabe merged 1 commit into nitta-lab-2024:master from nitta-lab-2024:OtherHomeFragment on 15 Oct
Showing 1 changed file
View
216
app/src/main/java/com/example/citrusclient/views/OtherHomeFragment.java
public View onCreateView(LayoutInflater inflater, ViewGroup container,
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);
}
 
BooksViewModel booksViewModel;
TodosViewModel todosViewModel;
ScheduleViewModel scheduleViewModel;
 
private Book book;
private List<Todo> todoList;
TodosViewModel todosViewModel;
private List<Schedule> scheduleList;
ScheduleViewModel scheduleViewModel;
 
private HashMap<Integer, Book> integerBookHashMap;
BooksViewModel booksViewModel;
 
LocalDate curDate;
 
int year ;
 
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);
 
todoList = new ArrayList<>();
RecyclerView todoRecyclerView = view.findViewById(R.id.my_todos_list);
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);
dateButton.setOnClickListener(v -> {
year = curDate.getYear();
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);
nextBotton.setOnClickListener(v -> {
year = curDate.getYear();
month = curDate.getMonthValue();
day = curDate.getDayOfMonth();
curDateButton.setText(year + "年" + month + "月" + day + "日");
 
scheduleAdapter.updateCurDate(curDate);
updateTodoSchedule(integerBookHashMap);
 
});
 
 
booksViewModel.loadBooks(accountId, token);
scheduleViewModel.updateSchedulesByDay(accountId, year, month, day, token);
 
clearTodoSchedule(todoAdapter, scheduleAdapter);
updateTodoSchedule(book);
 
});
 
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<HashMap<Integer, Book>>() {
booksViewModel.getBook().observe(getViewLifecycleOwner(), new Observer<Book>() {
@Override
public void onChanged(HashMap<Integer, Book> idBookHashMap) {
updateTodoSchedule(idBookHashMap);
public void onChanged(Book book) {
clearTodoSchedule(todoAdapter, scheduleAdapter);
updateTodoSchedule(book);
}
});
 
todosViewModel.getTodosByDayLiveData().observe(getViewLifecycleOwner(), new Observer<HashMap<Integer, Todo>>() {
@Override
public void onChanged(HashMap<Integer, Todo> 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);
}
}
});
 
@Override
public void onChanged(HashMap<Integer, Schedule> 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);
}
}
});
 
 
}
 
 
private void updateTodoSchedule(HashMap<Integer, Book> 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);
}
}
}
scheduleViewModel.updateSchedulesByDay(accountId, year, month, day, token);
if (book != null) {
this.book = book;
}
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);
}
 
 
 
}
 
class MyOtherTodoshelfAdapter extends RecyclerView.Adapter<MyOtherTodoshelfAdapter.MyTodoViewHolder> {
private List<Todo> todoList;
private HashMap<Integer, Book> idBookHashMap;
private Book book;
private TodosViewModel todosViewModel;
private Context context;
 
 
MyOtherTodoshelfAdapter(List<Todo> todos, HashMap<Integer, Book> idBookHashMap, TodosViewModel todosViewModel, Context context) {
MyOtherTodoshelfAdapter(List<Todo> 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<Todo> todos ,HashMap<Integer, Book> idBookHashMap) {
todoList = todos;
if (idBookHashMap != null) {
this.idBookHashMap = new HashMap<>(idBookHashMap);
} else {
this.idBookHashMap = new HashMap<>(); // 空のHashMapを作成
}
public void setTodos(List<Todo> todos ,Book book) {
this.todoList = todos;
this.book = book;
 
sortTodosById();
notifyDataSetChanged();
}
}
 
@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);
int blue = Integer.parseInt(book.getColor().substring(5, 7), 16);
holder.todoButton.setBackgroundColor(Color.rgb(red, green, blue));
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);
// }
// });
 
}
 
@Override
 
class MyOtherScheduleshelfAdapter extends RecyclerView.Adapter<MyOtherScheduleshelfAdapter.MyScheduleViewHolder> {
 
private List<Schedule> scheduleList;
private HashMap<Integer, Book> idBookHashMap;
Book book;
private Context context;
private LocalDate curDate;
 
MyOtherScheduleshelfAdapter(List<Schedule> schedules, HashMap<Integer, Book> idBookHashMap, LocalDate curDate, Context context) {
MyOtherScheduleshelfAdapter(List<Schedule> 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<Schedule> schedules, HashMap<Integer, Book> idBookHashMap) {
public void setSchedules(List<Schedule> schedules, Book book) {
scheduleList = schedules;
if (idBookHashMap != null) {
this.idBookHashMap = new HashMap<>(idBookHashMap);
} else {
this.idBookHashMap = new HashMap<>(); // 空のHashMapを作成
}
this.book = book;
sortScheduleByTime();
notifyDataSetChanged();
}
 
}
 
@Override
public void onBindViewHolder(@NonNull MyScheduleViewHolder holder, int position) {
Activity activity = (Activity) context;
// Activity activity = (Activity) context;
 
int red = 169;
int green = 169;
int blue = 169;
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);
blue = Integer.parseInt(book.getColor().substring(5, 7), 16);
}
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));
// });
}