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 91d0f1f..a73523a 100644 --- a/app/src/main/java/com/example/citrusclient/views/HomeFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/HomeFragment.java @@ -1,6 +1,9 @@ package com.example.citrusclient.views; +import android.app.Activity; +import android.content.Context; import android.graphics.Color; +import android.media.Image; import android.os.Bundle; import androidx.annotation.NonNull; @@ -17,6 +20,7 @@ import android.view.ViewGroup; import android.widget.Button; import android.widget.CheckBox; +import android.widget.TextView; import com.example.citrusclient.Citrus; import com.example.citrusclient.R; @@ -30,9 +34,12 @@ import java.security.PrivateKey; import java.util.ArrayList; +import java.util.Calendar; import java.util.HashMap; import java.util.List; +import java.time.LocalDate; + /** * A simple {@link Fragment} subclass. * Use the {@link HomeFragment#newInstance} factory method to @@ -103,6 +110,11 @@ private HashMap integerBookHashMap; BooksViewModel booksViewModel; + private LocalDate openDate; + int year; + int month; + int day; + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); @@ -110,21 +122,26 @@ 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(); + year = citrus.getCurYear(); + month = citrus.getCurMonth(); + day = citrus.getCurDay(); + int curBookId = citrus.getCurBookId(); - todoList = new ArrayList<>(); + openDate = LocalDate.of(year, month, day); + todoList = new ArrayList<>(); scheduleList = new ArrayList<>(); + TextView curDate = view.findViewById(R.id.year_month_day); + curDate.setText(year + "年" + month + "月" + day + "日"); + 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, integerBookHashMap); + MyTodoshelfAdapter todoAdapter = new MyTodoshelfAdapter(todoList, integerBookHashMap, todosViewModel, citrus); todoRecyclerView.setAdapter(todoAdapter); RecyclerView scheduleRecyclerView = view.findViewById(R.id.my_schedule_list); @@ -141,10 +158,19 @@ 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); + if (idBookHashMap != null) { + if(curBookId != -1) { + integerBookHashMap = new HashMap<>(); + Book book = idBookHashMap.get(curBookId); + integerBookHashMap.put(curBookId, book); + todosViewModel.loadTodosByDay(accountId, curBookId, year, month, day, token); + citrus.setCurBookId(-1); + } 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); } @@ -171,24 +197,48 @@ } }); + FloatingActionButton todoAddButton = view.findViewById(R.id.todo_add_button); + todoAddButton.setOnClickListener(v -> { + ((MainActivity) getActivity()).showFragment(new CreateTodoFragment()); + }); + FloatingActionButton scheduleAddButton = view.findViewById(R.id.schedule_add_button); + scheduleAddButton.setOnClickListener(v -> { + ((MainActivity) getActivity()).showFragment(new CreateScheduleFragment()); + }); +// FloatingActionButton prevBotton = view.findViewById(R.id.prev_day_botton); +// prevBotton.setOnClickListener(v -> { +// openDate = openDate.minusDays(1); +// year = openDate.getYear(); +// month = openDate.getMonthValue(); +// day = openDate.getDayOfMonth(); +// }); } } class MyTodoshelfAdapter extends RecyclerView.Adapter { - private List todoList; private HashMap idBookHashMap; - MyTodoshelfAdapter(List todos, HashMap idBookHashMap) { + private TodosViewModel todosViewModel; + private Citrus citrus; + private String accountId; + private String token; + + MyTodoshelfAdapter(List todos, HashMap idBookHashMap, TodosViewModel todosViewModel, Citrus citrus) { + this.citrus = citrus; + this.token = citrus.getToken(); + this.accountId = citrus.getAccountId(); this.todoList = todos; + this.todosViewModel = todosViewModel; if (idBookHashMap != null) { this.idBookHashMap = new HashMap<>(idBookHashMap); } else { this.idBookHashMap = new HashMap<>(); // 空のHashMapを作成 - } } + } + } public void setTodos(List todos ,HashMap idBookHashMap) { todoList = todos; @@ -221,6 +271,17 @@ holder.todoButton.setTextColor(Color.rgb(255 - red, 255 - green, 255 - blue)); } + 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 @@ -270,16 +331,19 @@ @Override public void onBindViewHolder(@NonNull MyScheduleViewHolder holder, int position) { + int red = 169; + int green = 169; + int blue = 169; 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)); + 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)); }