package com.example.citrusclient.views; import android.os.Bundle; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModelProvider; import android.text.Editable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.NumberPicker; import android.widget.Spinner; import com.example.citrusclient.Citrus; import com.example.citrusclient.R; import com.example.citrusclient.models.Book; 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; import java.util.HashMap; import java.util.List; /** * A simple {@link Fragment} subclass. * Use the {@link CreateTodoFragment#newInstance} factory method to * create an instance of this fragment. */ public class CreateTodoFragment extends Fragment { private TodosViewModel todosViewModel; private BooksViewModel booksViewModel; // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM2 = "param2"; // TODO: Rename and change types of parameters private String mParam1; private String mParam2; List<String> bookTitles; private Citrus citrus; private String accountId; private String token; private int selectBookNum; public CreateTodoFragment() { // 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 CreateTodoFragment. */ // TODO: Rename and change types and number of parameters public static CreateTodoFragment newInstance(String param1, String param2) { CreateTodoFragment fragment = new CreateTodoFragment(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } citrus = (Citrus)(getActivity().getApplication()); accountId = citrus.getAccountId(); token = citrus.getToken(); todosViewModel = new ViewModelProvider(this).get(TodosViewModel.class); booksViewModel = new ViewModelProvider(this).get(BooksViewModel.class); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_create_todo, container, false); } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { selectBookNum = 0; EditText titleField = view.findViewById(R.id.todo_title_inputField); NumberPicker numberPickerYear = view.findViewById(R.id.numberPickerYear); numberPickerYear.setMinValue(citrus.getCurYear()); numberPickerYear.setMaxValue(citrus.getCurYear() + 30); numberPickerYear.setValue(citrus.getCurYear()); // 年の初期値を設定 NumberPicker numberPickerMonth = view.findViewById(R.id.numberPickerMonth); numberPickerMonth.setMinValue(1); numberPickerMonth.setMaxValue(12); numberPickerMonth.setValue(citrus.getCurMonth()); // 月の初期値を設定 NumberPicker numberPickerDay = view.findViewById(R.id.numberPickerDay); numberPickerDay.setMinValue(1); 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("本を選択"); booksViewModel.getBookLiveData().observe(getViewLifecycleOwner(), new Observer<HashMap<Integer, Book>>() { @Override public void onChanged(HashMap<Integer, Book> integerBookHashMap) { if(integerBookHashMap != null){ for(int i : integerBookHashMap.keySet()){ bookTitles.add(integerBookHashMap.get(i).getTitle()); } }else{ bookTitles = new ArrayList<>(); } } }); booksViewModel.loadBooks(accountId, token); ArrayAdapter<String> 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.todo_cancel_button); cancelButton.setOnClickListener(v->{ ((MainActivity)getActivity()).showFragment(new HomeFragment()); }); Button createButton = view.findViewById(R.id.todo_create_button); createButton.setOnClickListener(v->{ int year = numberPickerYear.getValue(); // 設定年を取得 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; } if(spinner.getSelectedItem().toString().equals("本を選択")){ Snackbar.make(view, "本を選択してください", Snackbar.LENGTH_SHORT).show(); return; } todosViewModel.createTodo(accountId,spinner.getSelectedItemPosition(),year,month,day,title,token); ((MainActivity)getActivity()).showFragment(new HomeFragment()); }); } 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); } }