diff --git a/app/src/main/java/com/example/citrusclient/views/CreateTodoFragment.java b/app/src/main/java/com/example/citrusclient/views/CreateTodoFragment.java index cd80709..92d4908 100644 --- a/app/src/main/java/com/example/citrusclient/views/CreateTodoFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/CreateTodoFragment.java @@ -21,6 +21,7 @@ import com.example.citrusclient.Citrus; import com.example.citrusclient.R; import com.example.citrusclient.models.Book; +import com.example.citrusclient.models.Todo; import com.example.citrusclient.viewmodels.BooksViewModel; import com.example.citrusclient.viewmodels.ScheduleViewModel; import com.example.citrusclient.viewmodels.TodosViewModel; @@ -41,6 +42,8 @@ private TodosViewModel todosViewModel; private BooksViewModel booksViewModel; + private Todo todo; + // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER private static final String ARG_PARAM1 = "param1"; @@ -57,8 +60,32 @@ private String token; private int selectBookNum; - public CreateTodoFragment() { - // Required empty public constructor + //編集用 + private boolean isEdit; + private Todo editTodo; + private int viwYear; + private int viwMonth; + private int viwDay; + private String viwTitle; + + //追加用コンストラクタ + public CreateTodoFragment(int viwYear, int viwMonth, int viwDay) { + this.isEdit = false; + this.viwYear = viwYear; + this.viwMonth = viwMonth; + this.viwDay = viwDay; + this.viwTitle = ""; + } + + //編集用コンストラクタ + public CreateTodoFragment(Todo editTodo) { + this.editTodo = editTodo; + this.isEdit = true; + this.viwYear = editTodo.getYear(); + this.viwMonth = editTodo.getMonth(); + this.viwDay = editTodo.getDay(); + this.viwTitle = editTodo.getTitle(); + } /** @@ -71,7 +98,7 @@ */ // TODO: Rename and change types and number of parameters public static CreateTodoFragment newInstance(String param1, String param2) { - CreateTodoFragment fragment = new CreateTodoFragment(); + CreateTodoFragment fragment = new CreateTodoFragment(0,0,0); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); @@ -111,17 +138,17 @@ NumberPicker numberPickerYear = view.findViewById(R.id.numberPickerYear); numberPickerYear.setMinValue(citrus.getCurYear()); numberPickerYear.setMaxValue(citrus.getCurYear() + 30); - numberPickerYear.setValue(citrus.getCurYear()); // 年の初期値を設定 + numberPickerYear.setValue(viwYear); // 年の初期値を設定 NumberPicker numberPickerMonth = view.findViewById(R.id.numberPickerMonth); numberPickerMonth.setMinValue(1); numberPickerMonth.setMaxValue(12); - numberPickerMonth.setValue(citrus.getCurMonth()); // 月の初期値を設定 + numberPickerMonth.setValue(viwMonth); // 月の初期値を設定 NumberPicker numberPickerDay = view.findViewById(R.id.numberPickerDay); numberPickerDay.setMinValue(1); numberPickerDay.setMaxValue(getDaysInMonth(citrus.getCurYear(), citrus.getCurMonth())); // 日の初期値を設定 - numberPickerDay.setValue(citrus.getCurDay()); // 日の初期値を設定 + numberPickerDay.setValue(viwDay); // 日の初期値を設定 // 月が変更されたとき numberPickerMonth.setOnValueChangedListener((picker, oldVal, newVal) -> { @@ -159,12 +186,31 @@ adapter.setDropDownViewResource(androidx.appcompat.R.layout.support_simple_spinner_dropdown_item); spinner.setAdapter(adapter); + //キャンセルボタン処理 Button cancelButton = view.findViewById(R.id.todo_cancel_button); cancelButton.setOnClickListener(v->{ - ((MainActivity)getActivity()).showFragment(new HomeFragment()); + ((MainActivity)getActivity()).backFragment(); }); + //削除ボタン処理 + Button deleteButton = view.findViewById(R.id.todo_delete_button); + if (isEdit) { + deleteButton.setVisibility(View.VISIBLE); // 編集時ボタンを表示 + } else { + deleteButton.setVisibility(View.INVISIBLE); // 追加時ボタンを非表示 + } + deleteButton.setOnClickListener(v->{ + //ここにdelete処理か確認ダイアログ(いるかな) + }); + + //追加ボタン処理 Button createButton = view.findViewById(R.id.todo_create_button); + if (isEdit) { + createButton.setText("完了"); //編集時 + } else { + createButton.setText("追加"); //追加時 + } + createButton.setOnClickListener(v->{ int year = numberPickerYear.getValue(); // 設定年を取得 int month = numberPickerMonth.getValue(); // 設定月を取得 @@ -184,7 +230,7 @@ return; } todosViewModel.createTodo(accountId,spinner.getSelectedItemPosition(),year,month,day,title,token); - ((MainActivity)getActivity()).showFragment(new HomeFragment()); + ((MainActivity)getActivity()).backFragment(); }); }