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 a980aa1..8840973 100644 --- a/app/src/main/java/com/example/citrusclient/views/CreateTodoFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/CreateTodoFragment.java @@ -8,6 +8,7 @@ import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModelProvider; +import android.text.Editable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -23,6 +24,7 @@ import com.example.citrusclient.viewmodels.BooksViewModel; import com.example.citrusclient.viewmodels.ScheduleViewModel; import com.example.citrusclient.viewmodels.TodosViewModel; +import com.google.android.material.snackbar.Snackbar; import java.util.ArrayList; import java.util.Calendar; @@ -116,12 +118,25 @@ numberPickerMonth.setMaxValue(12); numberPickerMonth.setValue(citrus.getCurMonth()); // 月の初期値を設定 - NumberPicker numberPickerDay = view.findViewById(R.id.numberPickerDay); numberPickerDay.setMinValue(1); - numberPickerDay.setMaxValue(31); + numberPickerDay.setMaxValue(getDaysInMonth(citrus.getCurYear(), citrus.getCurMonth())); // 日の初期値を設定 numberPickerDay.setValue(citrus.getCurDay()); // 日の初期値を設定 + // 月が変更されたとき + numberPickerMonth.setOnValueChangedListener((picker, oldVal, newVal) -> { + int year = numberPickerYear.getValue(); + int daysInMonth = getDaysInMonth(year, newVal); + numberPickerDay.setMaxValue(daysInMonth); + }); + + // 年が変更されたとき + numberPickerYear.setOnValueChangedListener((picker, oldVal, newVal) -> { + int month = numberPickerMonth.getValue(); + int daysInMonth = getDaysInMonth(newVal, month); + numberPickerDay.setMaxValue(daysInMonth); + }); + Spinner spinner = view.findViewById(R.id.select_todo_spinner); bookTitles = new ArrayList<>(); bookTitles.add("本を選択しない"); @@ -155,9 +170,25 @@ int month = numberPickerMonth.getValue(); // 設定月を取得 int day = numberPickerDay.getValue(); // 設定日を取得 String title = titleField.getText().toString(); + Editable tmp = titleField.getText(); + if(tmp == null){ + Snackbar.make(view, "タイトルを入力してください", Snackbar.LENGTH_SHORT).show(); + return; + } + if(title == null || title.isEmpty()){ + Snackbar.make(view, "タイトルを入力してください", Snackbar.LENGTH_SHORT).show(); + return; + } todosViewModel.createTodo(accountId,spinner.getSelectedItemPosition(),year,month,day,title,token); ((MainActivity)getActivity()).backFragment(); }); } + private int getDaysInMonth(int year, int month) { + Calendar calendar = Calendar.getInstance(); + calendar.set(Calendar.YEAR, year); + calendar.set(Calendar.MONTH, month - 1); // Calendar.MONTHは0から始まるので-1する + return calendar.getActualMaximum(Calendar.DAY_OF_MONTH); + } + } \ No newline at end of file