package com.example.citrusclient.views; import android.app.DatePickerDialog; import android.app.Dialog; import android.app.TimePickerDialog; import android.content.Context; import android.graphics.Color; import android.os.Bundle; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.DialogFragment; import androidx.fragment.app.Fragment; import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModelProvider; import android.text.Editable; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.DatePicker; import android.widget.EditText; import android.widget.Spinner; import android.widget.Switch; import android.widget.TimePicker; 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.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 CreateScheduleFragment#newInstance} factory method to * create an instance of this fragment. */ public class CreateScheduleFragment extends Fragment { // 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; 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(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_create_schedule, container, false); } BooksViewModel booksViewModel; ScheduleViewModel scheduleViewModel; List<String> bookTitles; int year; int month; int day; int hour; int minute; String accountId; String token; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } 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(); token = citrus.getToken(); } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); Button startDateButton = view.findViewById(R.id.start_date_button); Button startTimeButton = view.findViewById(R.id.start_time_button); Button endDateButton = view.findViewById(R.id.end_date_button); Button endTimeButton = view.findViewById(R.id.end_time_button); startDateButton.setText(year + "/" + (month + 1) + "/" + day); startTimeButton.setText("00:00"); endDateButton.setText(year + "/" + (month + 1) + "/" + day); endTimeButton.setText("23:59"); startDateButton.setOnClickListener(v->{ DatePickerDialog datePickerDialog = new DatePickerDialog(requireContext(), new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { String date = year + "/" + (month + 1) + "/" + dayOfMonth; startDateButton.setText(date); } }, year, month, day); datePickerDialog.show(); }); startTimeButton.setOnClickListener(v -> { TimePickerDialog timePickerDialog = new TimePickerDialog(requireContext(), new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { String time = hourOfDay + ":" + minute; startTimeButton.setText(time); } }, hour, minute, true); timePickerDialog.show(); }); endDateButton.setOnClickListener(v->{ DatePickerDialog datePickerDialog = new DatePickerDialog(requireContext(), new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { String date = year + "/" + (month + 1) + "/" + dayOfMonth; endDateButton.setText(date); } }, year, month, day); datePickerDialog.show(); }); endTimeButton.setOnClickListener(v -> { TimePickerDialog timePickerDialog = new TimePickerDialog(requireContext(), new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { String time = hourOfDay + ":" + minute; endTimeButton.setText(time); } }, hour, minute, true); timePickerDialog.show(); }); Switch allDaySwitch = view.findViewById(R.id.all_day_switch); allDaySwitch.setOnCheckedChangeListener((buttonView, isChecked) -> { if(isChecked){ startTimeButton.setTextColor(Color.GRAY); startTimeButton.setText("00:00"); endDateButton.setTextColor(Color.GRAY); endDateButton.setText(startDateButton.getText()); endTimeButton.setTextColor(Color.GRAY); endTimeButton.setText("23:59"); }else{ startTimeButton.setTextColor(Color.BLACK); endDateButton.setTextColor(Color.BLACK); endTimeButton.setTextColor(Color.BLACK); } startTimeButton.setEnabled(!isChecked); endDateButton.setEnabled(!isChecked); 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); 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); EditText scheduleTitle = view.findViewById(R.id.schedule_title); Button confirmButton = view.findViewById(R.id.schedule_create_confirm); confirmButton.setOnClickListener(v -> { String[] dates = ((String)startDateButton.getText()).split("/"); int year = Integer.parseInt(dates[0]); int month = Integer.parseInt(dates[1]); int day = Integer.parseInt(dates[2]); Editable tmp = scheduleTitle.getText(); if(tmp == null){ Snackbar.make(view, "タイトルを入力してください", Snackbar.LENGTH_SHORT).show(); return; } String title = scheduleTitle.getText().toString(); if(title == null || title.isEmpty()){ Snackbar.make(view, "タイトルを入力してください", Snackbar.LENGTH_SHORT).show(); return; } // String startDateTime = startDateButton.getText() + " " + startTimeButton.getText(); String endDateTime = endDateButton.getText() + " " + endTimeButton.getText(); Log.i("lkjsa", "" + spinner.getSelectedItemPosition()); scheduleViewModel.createSchedule(accountId, year, month, day, token, title, startDateTime, endDateTime, spinner.getSelectedItemPosition()); ((MainActivity)getActivity()).backFragment(); }); } }