diff --git a/app/src/main/java/com/example/citrusclient/views/CreateScheduleFragment.java b/app/src/main/java/com/example/citrusclient/views/CreateScheduleFragment.java index 3c51052..5c566b5 100644 --- a/app/src/main/java/com/example/citrusclient/views/CreateScheduleFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/CreateScheduleFragment.java @@ -30,10 +30,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.viewmodels.BooksViewModel; import com.example.citrusclient.viewmodels.ScheduleViewModel; import com.google.android.material.snackbar.Snackbar; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; import java.util.ArrayList; import java.util.Calendar; import java.util.HashMap; @@ -55,21 +59,9 @@ private String mParam1; private String mParam2; - public CreateScheduleFragment() { - // Required empty public constructor - } - - /** - * Use this factory method to create a new instance of - * this fragment using the provided parameters. - * - * @param param1 Parameter 1. - * @param param2 Parameter 2. - * @return A new instance of fragment CreateScheduleFragment. - */ // TODO: Rename and change types and number of parameters public static CreateScheduleFragment newInstance(String param1, String param2) { - CreateScheduleFragment fragment = new CreateScheduleFragment(); + CreateScheduleFragment fragment = new CreateScheduleFragment(0,0,0); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); @@ -91,15 +83,65 @@ List bookTitles; - int year; - int month; - int day; - int hour; - int minute; String accountId; String token; + private int selectBookNum; + //編集用変数 + private boolean isEdit; + private Schedule editSchedule; + private Book editBook; + private String viwTitle; + private int viwStartYear; + private int viwStartMonth; + private int viwStartDay; + private int viwStartHour; + private int viwStartMin; + private int viwEndYear; + private int viwEndMonth; + private int viwEndDay; + private int viwEndHour; + private int viwEndMin; + + + //追加用コンストラクタ + public CreateScheduleFragment(int viwYear, int viwMonth, int viwDay) { + this.isEdit = false; + this.viwStartYear = viwYear; + this.viwStartMonth = viwMonth; + this.viwStartDay = viwDay;//Homeからは表示している日付、calendarからは今日の日付 + viwStartHour = 0; + viwStartMin = 0; + viwEndYear = viwYear; + viwEndMonth = viwMonth; + viwEndDay = viwDay; + viwEndHour = 23; + viwEndMin = 59; + viwTitle = ""; + } + + //編集用コンストラクタ + public CreateScheduleFragment(Schedule editSchedule, Book editBook){ + this.isEdit = true; + this.editSchedule = editSchedule; + this.editBook = editBook; + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm"); + LocalDateTime startTime = LocalDateTime.parse(this.editSchedule.getStartTime(), formatter); + viwStartYear = startTime.getYear(); + viwStartMonth = startTime.getMonthValue(); + viwStartDay = startTime.getDayOfMonth(); + viwStartHour = startTime.getHour(); + viwStartMin = startTime.getMinute(); + LocalDateTime endTime = LocalDateTime.parse(this.editSchedule.getEndTime(), formatter); + viwEndYear = endTime.getYear(); + viwEndMonth = endTime.getMonthValue(); + viwEndDay = endTime.getDayOfMonth(); + viwEndHour = endTime.getHour(); + viwEndMin = endTime.getMinute(); + viwTitle = editSchedule.getTitle(); + + } @Override public void onCreate(Bundle savedInstanceState) { @@ -111,12 +153,6 @@ booksViewModel = new ViewModelProvider(this).get(BooksViewModel.class); scheduleViewModel = new ViewModelProvider(this).get(ScheduleViewModel.class); - Calendar c = Calendar.getInstance(); - year = c.get(Calendar.YEAR); - month = c.get(Calendar.MONTH); - day = c.get(Calendar.DAY_OF_MONTH); - hour = c.get(Calendar.HOUR); - minute = c.get(Calendar.MINUTE); Citrus citrus = (Citrus)(getActivity().getApplication()); accountId = citrus.getAccountId(); @@ -127,6 +163,7 @@ @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + selectBookNum = 0; super.onViewCreated(view, savedInstanceState); Button startDateButton = view.findViewById(R.id.start_date_button); @@ -134,10 +171,18 @@ Button endDateButton = view.findViewById(R.id.end_date_button); Button endTimeButton = view.findViewById(R.id.end_time_button); - startDateButton.setText(year + "/" + String.format("%02d", (month + 1)) + "/" + String.format("%02d", day)); - startTimeButton.setText("00:00"); - endDateButton.setText(year + "/" + String.format("%02d", (month + 1)) + "/" + String.format("%02d", day)); - endTimeButton.setText("23:59"); + startDateButton.setText(viwStartYear + "/" + String.format("%02d", (viwStartMonth)) + "/" + String.format("%02d", viwStartDay)); + if(!isEdit) { + startTimeButton.setText("00:00"); + }else{ + startTimeButton.setText(String.format("%02d", viwStartHour) + ":" + String.format("%02d", viwStartMin)); + } + endDateButton.setText(viwEndYear + "/" + String.format("%02d", (viwEndMonth)) + "/" + String.format("%02d", viwEndDay)); + if(!isEdit) { + endTimeButton.setText("23:59"); + }else{ + endTimeButton.setText(String.format("%02d", viwEndHour) + ":" + String.format("%02d", viwEndMin)); + } startDateButton.setOnClickListener(v->{ DatePickerDialog datePickerDialog = new DatePickerDialog(requireContext(), new DatePickerDialog.OnDateSetListener() { @@ -146,7 +191,7 @@ String date = year + "/" + String.format("%02d", (month + 1)) + "/" + String.format("%02d", dayOfMonth); startDateButton.setText(date); } - }, year, month, day); + }, viwStartYear, viwStartMonth, viwStartDay); datePickerDialog.show(); }); startTimeButton.setOnClickListener(v -> { @@ -156,7 +201,7 @@ String time = String.format("%02d", (hourOfDay)) + ":" + String.format("%02d", (minute)); startTimeButton.setText(time); } - }, hour, minute, true); + }, viwStartHour, viwStartMin, true); timePickerDialog.show(); }); endDateButton.setOnClickListener(v->{ @@ -166,7 +211,7 @@ String date = year + "/" + String.format("%02d", (month + 1)) + "/" + String.format("%02d", dayOfMonth); endDateButton.setText(date); } - }, year, month, day); + }, viwEndYear, viwEndMonth, viwEndDay); datePickerDialog.show(); }); endTimeButton.setOnClickListener(v -> { @@ -176,7 +221,7 @@ String time = String.format("%02d", (hourOfDay)) + ":" + String.format("%02d", (minute)); endTimeButton.setText(time); } - }, hour, minute, true); + }, viwEndHour, viwEndMin, true); timePickerDialog.show(); }); @@ -199,37 +244,77 @@ endTimeButton.setEnabled(!isChecked); }); - Button cancelButton = view.findViewById(R.id.schedule_cancel_button); - cancelButton.setOnClickListener(v->{ - ((MainActivity)getActivity()).backFragment(); - }); - - Spinner spinner = view.findViewById(R.id.select_book_spinner); + Spinner spinner = view.findViewById(R.id.select_schedule_spinner); bookTitles = new ArrayList<>(); bookTitles.add("本を選択しない"); + + // Adapterの初期化を先に行う + ArrayAdapter adapter = new ArrayAdapter<>(requireContext(), androidx.appcompat.R.layout.support_simple_spinner_dropdown_item, bookTitles); + adapter.setDropDownViewResource(androidx.appcompat.R.layout.support_simple_spinner_dropdown_item); + spinner.setAdapter(adapter); + booksViewModel.getBookLiveData().observe(getViewLifecycleOwner(), new Observer>() { @Override public void onChanged(HashMap integerBookHashMap) { if(integerBookHashMap != null){ for(int i : integerBookHashMap.keySet()){ bookTitles.add(integerBookHashMap.get(i).getTitle()); + // 編集の場合、BookIdから対応するタイトルを探す + if(editBook == null) break; //本に所属していない場合はばいばい + if (isEdit && integerBookHashMap.get(i).getTitle().equals(editBook.getTitle())){ + selectBookNum = bookTitles.size() - 1; // 現在追加した位置を保持 + } } - }else{ + // ArrayAdapterの更新を通知 + adapter.notifyDataSetChanged(); + + // データがセットされた後にSpinnerに選択状態を設定 + if (isEdit) { + spinner.setSelection(selectBookNum); + } + } else { bookTitles = new ArrayList<>(); } } }); booksViewModel.loadBooks(accountId, token); + // 編集モードの場合、事前に本が選択されるようにSpinnerに設定 + if (isEdit) { + spinner.setSelection(selectBookNum); + } - ArrayAdapter adapter = new ArrayAdapter<>(requireContext(), androidx.appcompat.R.layout.support_simple_spinner_dropdown_item, bookTitles); - adapter.setDropDownViewResource(androidx.appcompat.R.layout.support_simple_spinner_dropdown_item); - spinner.setAdapter(adapter); + //キャンセルボタン処理 + Button cancelButton = view.findViewById(R.id.schedule_cancel_button); + cancelButton.setOnClickListener(v->{ + ((MainActivity)getActivity()).backFragment(); + }); + //削除ボタン処理 + Button deleteButton = view.findViewById(R.id.schedule_delete_button); + if (isEdit) { + deleteButton.setVisibility(View.VISIBLE); // 編集時ボタンを表示 + } else { + deleteButton.setVisibility(View.INVISIBLE); // 追加時ボタンを非表示 + } + deleteButton.setOnClickListener(v->{ + scheduleViewModel.deleteScheduleById(accountId,viwStartYear,viwStartMonth,viwStartDay, + editSchedule.getScheduleId(), token); + ((MainActivity)getActivity()).backFragment(); + }); + + //追加ボタン処理 + Button confirmButton = view.findViewById(R.id.schedule_create_confirm); + if (isEdit) { + confirmButton.setText("完了"); //編集時 + } else { + confirmButton.setText("作成"); //追加時 + } EditText scheduleTitle = view.findViewById(R.id.schedule_title); - Button confirmButton = view.findViewById(R.id.schedule_create_confirm); + scheduleTitle.setText(viwTitle); // 年の初期値を設定 + confirmButton.setOnClickListener(v -> { String[] dates = ((String)startDateButton.getText()).split("/"); int year = Integer.parseInt(dates[0]); @@ -250,8 +335,20 @@ String endDateTime = endDateButton.getText() + " " + endTimeButton.getText(); Log.i("lkjfda", startDateTime + ":" + endDateTime); Log.i("lkjsa", "" + spinner.getSelectedItemPosition()); - scheduleViewModel.createSchedule(accountId, year, month, day, token, title, startDateTime, endDateTime, spinner.getSelectedItemPosition()); - ((MainActivity)getActivity()).backFragment(); + + if(isEdit){ + //編集時 + scheduleViewModel.deleteScheduleById(accountId,viwStartYear,viwStartMonth,viwStartDay, + editSchedule.getScheduleId(), token); + scheduleViewModel.createSchedule(accountId, year, month, day, token, title, startDateTime, endDateTime, + spinner.getSelectedItemPosition()); + ((MainActivity)getActivity()).backFragment(); + }else{ + //追加時 + scheduleViewModel.createSchedule(accountId, year, month, day, token, title, startDateTime, endDateTime, + spinner.getSelectedItemPosition()); + ((MainActivity)getActivity()).backFragment(); + } });