| |
---|
| | import com.example.citrusclient.R; |
---|
| | import com.example.citrusclient.models.Book; |
---|
| | import com.example.citrusclient.models.Schedule; |
---|
| | import com.example.citrusclient.models.Todo; |
---|
| | import com.example.citrusclient.viewmodels.BooksViewModel; |
---|
| | import com.example.citrusclient.viewmodels.ScheduleViewModel; |
---|
| | import com.example.citrusclient.viewmodels.TodosViewModel; |
---|
| | import com.google.android.material.floatingactionbutton.FloatingActionButton; |
---|
| | |
---|
| | import java.security.PrivateKey; |
---|
| | import java.util.ArrayList; |
---|
| | import java.util.HashMap; |
---|
| | import java.util.List; |
---|
| | |
---|
| |
---|
| | } |
---|
| | |
---|
| | todosViewModel = new ViewModelProvider(this).get(TodosViewModel.class); |
---|
| | scheduleViewModel = new ViewModelProvider(this).get(ScheduleViewModel.class); |
---|
| | booksViewModel = new ViewModelProvider(this).get(BooksViewModel.class); |
---|
| | |
---|
| | } |
---|
| | |
---|
| | |
---|
| |
---|
| | TodosViewModel todosViewModel; |
---|
| | private List<Schedule> scheduleList; |
---|
| | ScheduleViewModel scheduleViewModel; |
---|
| | |
---|
| | private HashMap<Integer, Book> integerBookHashMap; |
---|
| | BooksViewModel booksViewModel; |
---|
| | |
---|
| | public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { |
---|
| | super.onViewCreated(view, savedInstanceState); |
---|
| | |
---|
| | |
---|
| | Citrus citrus = (Citrus)(getActivity().getApplication()); |
---|
| | String token = citrus.getToken(); |
---|
| | String accountId = citrus.getAccountId(); |
---|
| | int year = citrus.getCurYear(); |
---|
| | int month = citrus.getCurMouth(); |
---|
| | int day = citrus.getCurDay(); |
---|
| | int curBookId = citrus.getCurBookId(); |
---|
| | |
---|
| | todoList = new ArrayList<>(); |
---|
| | todoList.add(new Todo("a", true, 2024, 7, 2, 1)); |
---|
| | |
---|
| | scheduleList = new ArrayList<>(); |
---|
| | scheduleList.add(new Schedule("a", "1", "1", 1, 1)); |
---|
| | |
---|
| | |
---|
| | RecyclerView todoRecyclerView = view.findViewById(R.id.my_todos_list); |
---|
| | todoRecyclerView.setHasFixedSize(true); |
---|
| | RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(view.getContext()); |
---|
| | todoRecyclerView.setLayoutManager(layoutManager); |
---|
| | MyTodoshelfAdapter todoAdapter = new MyTodoshelfAdapter(todoList); |
---|
| | MyTodoshelfAdapter todoAdapter = new MyTodoshelfAdapter(todoList, integerBookHashMap); |
---|
| | 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); |
---|
| | MyScheduleshelfAdapter scheduleAdapter = new MyScheduleshelfAdapter(scheduleList); |
---|
| | MyScheduleshelfAdapter scheduleAdapter = new MyScheduleshelfAdapter(scheduleList, integerBookHashMap); |
---|
| | scheduleRecyclerView.setAdapter(scheduleAdapter); |
---|
| | |
---|
| | |
---|
| | booksViewModel.loadBooks(accountId, token); |
---|
| | System.out.println(token); |
---|
| | |
---|
| | booksViewModel.getBookLiveData().observe(getViewLifecycleOwner(), new Observer<HashMap<Integer, Book>>() { |
---|
| | @Override |
---|
| | public void onChanged(HashMap<Integer, Book> idBookHashMap) { |
---|
| | if(idBookHashMap != null) { |
---|
| | 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); |
---|
| | } |
---|
| | } |
---|
| | }); |
---|
| | |
---|
| | todosViewModel.getTodosByDayLiveData().observe(getViewLifecycleOwner(), new Observer<HashMap<Integer, Todo>>() { |
---|
| | @Override |
---|
| | public void onChanged(HashMap<Integer, Todo> idTodoHashMap) { |
---|
| | if(idTodoHashMap != null) { |
---|
| | todoList = new ArrayList<>(idTodoHashMap.values()); |
---|
| | todoAdapter.setTodos(todoList); |
---|
| | todoList.addAll(idTodoHashMap.values()); |
---|
| | todoAdapter.setTodos(todoList, integerBookHashMap); |
---|
| | } |
---|
| | } |
---|
| | }); |
---|
| | |
---|
| |
---|
| | @Override |
---|
| | public void onChanged(HashMap<Integer, Schedule> idScheduleHashMap) { |
---|
| | if(idScheduleHashMap != null) { |
---|
| | scheduleList = new ArrayList<>(idScheduleHashMap.values()); |
---|
| | scheduleAdapter.setSchedules(scheduleList); |
---|
| | scheduleAdapter.setSchedules(scheduleList, integerBookHashMap); |
---|
| | } |
---|
| | } |
---|
| | }); |
---|
| | scheduleViewModel.updateSchedulesByDay("bird", 2024, 5, 28, "xyz"); |
---|
| | todosViewModel.loadTodosByDay("bird", 1, 2024, 7, 2, "xyz"); |
---|
| | |
---|
| | } |
---|
| | |
---|
| | |
---|
| | |
---|
| | |
---|
| | |
---|
| | } |
---|
| | |
---|
| | } |
---|
| | |
---|
| | class MyTodoshelfAdapter extends RecyclerView.Adapter<MyTodoshelfAdapter.MyTodoViewHolder> { |
---|
| | |
---|
| | private List<Todo> todoList; |
---|
| | |
---|
| | MyTodoshelfAdapter(List<Todo> todos) { |
---|
| | private HashMap<Integer, Book> idBookHashMap; |
---|
| | MyTodoshelfAdapter(List<Todo> todos, HashMap<Integer, Book> idBookHashMap) { |
---|
| | this.todoList = todos; |
---|
| | } |
---|
| | |
---|
| | public void setTodos(List<Todo> todos) { |
---|
| | if (idBookHashMap != null) { |
---|
| | this.idBookHashMap = new HashMap<>(idBookHashMap); |
---|
| | } else { |
---|
| | this.idBookHashMap = new HashMap<>(); // 空のHashMapを作成 |
---|
| | } } |
---|
| | |
---|
| | 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を作成 |
---|
| | } |
---|
| | notifyDataSetChanged(); |
---|
| | } |
---|
| | |
---|
| | @NonNull |
---|
| |
---|
| | public void onBindViewHolder(@NonNull MyTodoViewHolder holder, int position) { |
---|
| | Todo todoData = this.todoList.get(position); |
---|
| | holder.todoButton.setText(todoData.getTitle()); |
---|
| | holder.todoCheckBox.setChecked(todoData.getCheck()); |
---|
| | Book book = idBookHashMap.get(todoData.getBookId()); |
---|
| | 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)); |
---|
| | } |
---|
| | |
---|
| | } |
---|
| | |
---|
| | @Override |
---|
| | public int getItemCount() { |
---|
| |
---|
| | |
---|
| | class MyScheduleshelfAdapter extends RecyclerView.Adapter<MyScheduleshelfAdapter.MyScheduleViewHolder> { |
---|
| | |
---|
| | private List<Schedule> scheduleList; |
---|
| | |
---|
| | MyScheduleshelfAdapter(List<Schedule> schedules) { |
---|
| | private HashMap<Integer, Book> idBookHashMap; |
---|
| | |
---|
| | MyScheduleshelfAdapter(List<Schedule> schedules, HashMap<Integer, Book> idBookHashMap) { |
---|
| | this.scheduleList = schedules; |
---|
| | } |
---|
| | |
---|
| | public void setSchedules(List<Schedule> schedules) { |
---|
| | if (idBookHashMap != null) { |
---|
| | this.idBookHashMap = new HashMap<>(idBookHashMap); |
---|
| | } else { |
---|
| | this.idBookHashMap = new HashMap<>(); // 空のHashMapを作成 |
---|
| | } } |
---|
| | |
---|
| | public void setSchedules(List<Schedule> schedules, HashMap<Integer, Book> idBookHashMap) { |
---|
| | scheduleList = schedules; |
---|
| | notifyDataSetChanged(); |
---|
| | if (idBookHashMap != null) { |
---|
| | this.idBookHashMap = new HashMap<>(idBookHashMap); |
---|
| | } else { |
---|
| | this.idBookHashMap = new HashMap<>(); // 空のHashMapを作成 |
---|
| | } notifyDataSetChanged(); |
---|
| | } |
---|
| | |
---|
| | @NonNull |
---|
| | @Override |
---|
| |
---|
| | @Override |
---|
| | public void onBindViewHolder(@NonNull MyScheduleViewHolder holder, int position) { |
---|
| | Schedule scheduleData = this.scheduleList.get(position); |
---|
| | holder.scheduleButton.setText(scheduleData.getTitle()); |
---|
| | } |
---|
| | Book book = idBookHashMap.get(scheduleData.getBookId()); |
---|
| | 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.scheduleButton.setBackgroundColor(Color.rgb(red, green, blue)); |
---|
| | holder.scheduleButton.setTextColor(Color.rgb(255 - red, 255 - green, 255 - blue)); |
---|
| | } |
---|
| | |
---|
| | } |
---|
| | |
---|
| | |
---|
| | @Override |
---|
| | public int getItemCount() { |
---|
| | return scheduleList.size(); |
---|
| |
---|
| | |