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 d2f14ab..91d0f1f 100644 --- a/app/src/main/java/com/example/citrusclient/views/HomeFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/HomeFragment.java @@ -21,10 +21,14 @@ import com.example.citrusclient.Citrus; 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; @@ -76,6 +80,8 @@ } todosViewModel = new ViewModelProvider(this).get(TodosViewModel.class); + scheduleViewModel = new ViewModelProvider(this).get(ScheduleViewModel.class); + booksViewModel = new ViewModelProvider(this).get(BooksViewModel.class); } @@ -91,6 +97,11 @@ private List todoList; TodosViewModel todosViewModel; + private List scheduleList; + ScheduleViewModel scheduleViewModel; + + private HashMap integerBookHashMap; + BooksViewModel booksViewModel; public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); @@ -99,41 +110,93 @@ 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, 06, 27, 1)); - RecyclerView recyclerView = view.findViewById(R.id.my_todos_list); - recyclerView.setHasFixedSize(true); + scheduleList = new ArrayList<>(); + + + RecyclerView todoRecyclerView = view.findViewById(R.id.my_todos_list); + todoRecyclerView.setHasFixedSize(true); RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(view.getContext()); - recyclerView.setLayoutManager(layoutManager); - MyTodoshelfAdapter todoAdapter = new MyTodoshelfAdapter(todoList); - recyclerView.setAdapter(todoAdapter); + todoRecyclerView.setLayoutManager(layoutManager); + 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, 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); } } }); - todosViewModel.loadTodosByDay("bird", 1, 2024, 6, 2, "xyz"); + scheduleViewModel.getSchedulesByDay().observe(getViewLifecycleOwner(), new Observer>() { + @Override + public void onChanged(HashMap idScheduleHashMap) { + if(idScheduleHashMap != null) { + scheduleList = new ArrayList<>(idScheduleHashMap.values()); + scheduleAdapter.setSchedules(scheduleList, integerBookHashMap); + } + } + }); + + + + } - + } + class MyTodoshelfAdapter extends RecyclerView.Adapter { private List todoList; + 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を作成 + } } - MyTodoshelfAdapter(List todo) { - this.todoList = todo; - } - - 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(); } @@ -149,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 @@ -166,3 +238,62 @@ } } } + +class MyScheduleshelfAdapter extends RecyclerView.Adapter { + + private List scheduleList; + private HashMap idBookHashMap; + + 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, HashMap idBookHashMap) { + scheduleList = schedules; + if (idBookHashMap != null) { + this.idBookHashMap = new HashMap<>(idBookHashMap); + } else { + this.idBookHashMap = new HashMap<>(); // 空のHashMapを作成 + } notifyDataSetChanged(); + } + + @NonNull + @Override + public MyScheduleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.a_schedule, parent, false); + return new MyScheduleViewHolder(view); + } + + @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(); + } + + static class MyScheduleViewHolder extends RecyclerView.ViewHolder { + Button scheduleButton; + public MyScheduleViewHolder(@NonNull View itemView) { + super(itemView); + scheduleButton = itemView.findViewById(R.id.schedule_button); + } + } +} diff --git a/app/src/main/res/layout/a_schedule.xml b/app/src/main/res/layout/a_schedule.xml new file mode 100644 index 0000000..bd36c3c --- /dev/null +++ b/app/src/main/res/layout/a_schedule.xml @@ -0,0 +1,20 @@ + + + +