diff --git a/app/src/main/java/com/example/citrusclient/models/Todo.java b/app/src/main/java/com/example/citrusclient/models/Todo.java index ef088fb..10a9733 100644 --- a/app/src/main/java/com/example/citrusclient/models/Todo.java +++ b/app/src/main/java/com/example/citrusclient/models/Todo.java @@ -8,15 +8,17 @@ int month; int day; Integer todoId; + Integer bookId; //コンストラクタ - public Todo(String t, boolean c, int y, int m, int d, Integer tid) { - title = t; - check = c; - year = y; - month = m; - day = d; + public Todo(String title, boolean check, int year, int month, int day, Integer tid, Integer bid) { + this.title = title; + this.check = check; + this.year = year; + this.month = month; + this.day = day; todoId = tid; + this.bookId = bid; } public Todo(){} @@ -24,12 +26,13 @@ //セッター - public void setTitle(String t) {title = t;} - public void setCheck(boolean c) {check = c;} - public void setYear(int y) {year = y;} - public void setMonth(int m) {month = m;} - public void setDay(int d) {day = d;} - public void setTodoId(Integer t) {todoId = t;} + public void setTitle(String title) {this.title = title;} + public void setCheck(boolean check) {this.check = check;} + public void setYear(int year) {this.year = year;} + public void setMonth(int month) {this.month = month;} + public void setDay(int day) {this.day = day;} + public void setTodoId(Integer tid) {this.todoId = tid;} + public void setBookId(Integer bid) {this.bookId = bid; } //ゲッター public String getTitle() {return title;} @@ -45,6 +48,7 @@ public Integer getTodoId() { return todoId; } + public Integer getBookId(){return bookId;} } diff --git a/app/src/main/java/com/example/citrusclient/views/HomeFragment.java b/app/src/main/java/com/example/citrusclient/views/HomeFragment.java index 252b9dc..91d0f1f 100644 --- a/app/src/main/java/com/example/citrusclient/views/HomeFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/HomeFragment.java @@ -23,10 +23,12 @@ 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; @@ -79,6 +81,7 @@ todosViewModel = new ViewModelProvider(this).get(TodosViewModel.class); scheduleViewModel = new ViewModelProvider(this).get(ScheduleViewModel.class); + booksViewModel = new ViewModelProvider(this).get(BooksViewModel.class); } @@ -97,6 +100,9 @@ private List scheduleList; ScheduleViewModel scheduleViewModel; + private HashMap integerBookHashMap; + BooksViewModel booksViewModel; + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); @@ -104,33 +110,53 @@ 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>() { + @Override + public void onChanged(HashMap 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>() { @Override public void onChanged(HashMap idTodoHashMap) { if(idTodoHashMap != null) { - todoList = new ArrayList<>(idTodoHashMap.values()); - todoAdapter.setTodos(todoList); + todoList.addAll(idTodoHashMap.values()); + todoAdapter.setTodos(todoList, integerBookHashMap); } } }); @@ -140,27 +166,37 @@ public void onChanged(HashMap 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 { private List todoList; - - MyTodoshelfAdapter(List todos) { + private HashMap idBookHashMap; + MyTodoshelfAdapter(List todos, HashMap idBookHashMap) { this.todoList = todos; - } + if (idBookHashMap != null) { + this.idBookHashMap = new HashMap<>(idBookHashMap); + } else { + this.idBookHashMap = new HashMap<>(); // 空のHashMapを作成 + } } - public void setTodos(List todos) { + public void setTodos(List todos ,HashMap idBookHashMap) { todoList = todos; + if (idBookHashMap != null) { + this.idBookHashMap = new HashMap<>(idBookHashMap); + } else { + this.idBookHashMap = new HashMap<>(); // 空のHashMapを作成 + } notifyDataSetChanged(); } @@ -176,6 +212,15 @@ 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 @@ -197,14 +242,23 @@ class MyScheduleshelfAdapter extends RecyclerView.Adapter { private List scheduleList; + private HashMap idBookHashMap; - MyScheduleshelfAdapter(List schedules) { + MyScheduleshelfAdapter(List schedules, HashMap idBookHashMap) { this.scheduleList = schedules; - } + if (idBookHashMap != null) { + this.idBookHashMap = new HashMap<>(idBookHashMap); + } else { + this.idBookHashMap = new HashMap<>(); // 空のHashMapを作成 + } } - public void setSchedules(List schedules) { + public void setSchedules(List schedules, HashMap idBookHashMap) { scheduleList = schedules; - notifyDataSetChanged(); + if (idBookHashMap != null) { + this.idBookHashMap = new HashMap<>(idBookHashMap); + } else { + this.idBookHashMap = new HashMap<>(); // 空のHashMapを作成 + } notifyDataSetChanged(); } @NonNull @@ -218,8 +272,18 @@ 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(); diff --git a/app/src/main/res/layout/a_schedule.xml b/app/src/main/res/layout/a_schedule.xml index 46671e2..bd36c3c 100644 --- a/app/src/main/res/layout/a_schedule.xml +++ b/app/src/main/res/layout/a_schedule.xml @@ -8,7 +8,7 @@