Newer
Older
CitrusClient / app / src / main / java / com / example / citrusclient / views / CreateScheduleFragment.java
k-sakoda on 23 Jul 10 KB バグ修正
  1. package com.example.citrusclient.views;
  2.  
  3. import android.app.DatePickerDialog;
  4. import android.app.Dialog;
  5. import android.app.TimePickerDialog;
  6. import android.content.Context;
  7. import android.graphics.Color;
  8. import android.os.Bundle;
  9.  
  10. import androidx.annotation.NonNull;
  11. import androidx.annotation.Nullable;
  12. import androidx.fragment.app.DialogFragment;
  13. import androidx.fragment.app.Fragment;
  14. import androidx.lifecycle.Observer;
  15. import androidx.lifecycle.ViewModelProvider;
  16.  
  17. import android.text.Editable;
  18. import android.util.Log;
  19. import android.view.LayoutInflater;
  20. import android.view.View;
  21. import android.view.ViewGroup;
  22. import android.widget.ArrayAdapter;
  23. import android.widget.Button;
  24. import android.widget.DatePicker;
  25. import android.widget.EditText;
  26. import android.widget.Spinner;
  27. import android.widget.Switch;
  28. import android.widget.TimePicker;
  29.  
  30. import com.example.citrusclient.Citrus;
  31. import com.example.citrusclient.R;
  32. import com.example.citrusclient.models.Book;
  33. import com.example.citrusclient.viewmodels.BooksViewModel;
  34. import com.example.citrusclient.viewmodels.ScheduleViewModel;
  35. import com.google.android.material.snackbar.Snackbar;
  36.  
  37. import java.util.ArrayList;
  38. import java.util.Calendar;
  39. import java.util.HashMap;
  40. import java.util.List;
  41.  
  42. /**
  43. * A simple {@link Fragment} subclass.
  44. * Use the {@link CreateScheduleFragment#newInstance} factory method to
  45. * create an instance of this fragment.
  46. */
  47. public class CreateScheduleFragment extends Fragment {
  48.  
  49. // TODO: Rename parameter arguments, choose names that match
  50. // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
  51. private static final String ARG_PARAM1 = "param1";
  52. private static final String ARG_PARAM2 = "param2";
  53.  
  54. // TODO: Rename and change types of parameters
  55. private String mParam1;
  56. private String mParam2;
  57.  
  58. public CreateScheduleFragment() {
  59. // Required empty public constructor
  60. }
  61.  
  62. /**
  63. * Use this factory method to create a new instance of
  64. * this fragment using the provided parameters.
  65. *
  66. * @param param1 Parameter 1.
  67. * @param param2 Parameter 2.
  68. * @return A new instance of fragment CreateScheduleFragment.
  69. */
  70. // TODO: Rename and change types and number of parameters
  71. public static CreateScheduleFragment newInstance(String param1, String param2) {
  72. CreateScheduleFragment fragment = new CreateScheduleFragment();
  73. Bundle args = new Bundle();
  74. args.putString(ARG_PARAM1, param1);
  75. args.putString(ARG_PARAM2, param2);
  76. fragment.setArguments(args);
  77. return fragment;
  78. }
  79.  
  80. @Override
  81. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  82. Bundle savedInstanceState) {
  83. // Inflate the layout for this fragment
  84. return inflater.inflate(R.layout.fragment_create_schedule, container, false);
  85. }
  86.  
  87.  
  88. BooksViewModel booksViewModel;
  89. ScheduleViewModel scheduleViewModel;
  90.  
  91.  
  92. List<String> bookTitles;
  93.  
  94. int year;
  95. int month;
  96. int day;
  97. int hour;
  98. int minute;
  99.  
  100. String accountId;
  101. String token;
  102.  
  103.  
  104. @Override
  105. public void onCreate(Bundle savedInstanceState) {
  106. super.onCreate(savedInstanceState);
  107. if (getArguments() != null) {
  108. mParam1 = getArguments().getString(ARG_PARAM1);
  109. mParam2 = getArguments().getString(ARG_PARAM2);
  110. }
  111.  
  112. booksViewModel = new ViewModelProvider(this).get(BooksViewModel.class);
  113. scheduleViewModel = new ViewModelProvider(this).get(ScheduleViewModel.class);
  114. Calendar c = Calendar.getInstance();
  115. year = c.get(Calendar.YEAR);
  116. month = c.get(Calendar.MONTH);
  117. day = c.get(Calendar.DAY_OF_MONTH);
  118. hour = c.get(Calendar.HOUR);
  119. minute = c.get(Calendar.MINUTE);
  120.  
  121. Citrus citrus = (Citrus)(getActivity().getApplication());
  122. accountId = citrus.getAccountId();
  123. token = citrus.getToken();
  124.  
  125. }
  126.  
  127.  
  128. @Override
  129. public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
  130. super.onViewCreated(view, savedInstanceState);
  131.  
  132. Button startDateButton = view.findViewById(R.id.start_date_button);
  133. Button startTimeButton = view.findViewById(R.id.start_time_button);
  134. Button endDateButton = view.findViewById(R.id.end_date_button);
  135. Button endTimeButton = view.findViewById(R.id.end_time_button);
  136.  
  137. startDateButton.setText(year + "/" + String.format("%02d", (month + 1)) + "/" + String.format("%02d", day));
  138. startTimeButton.setText("00:00");
  139. endDateButton.setText(year + "/" + String.format("%02d", (month + 1)) + "/" + String.format("%02d", day));
  140. endTimeButton.setText("23:59");
  141.  
  142. startDateButton.setOnClickListener(v->{
  143. DatePickerDialog datePickerDialog = new DatePickerDialog(requireContext(), new DatePickerDialog.OnDateSetListener() {
  144. @Override
  145. public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
  146. String date = year + "/" + String.format("%02d", (month + 1)) + "/" + String.format("%02d", dayOfMonth);
  147. startDateButton.setText(date);
  148. }
  149. }, year, month, day);
  150. datePickerDialog.show();
  151. });
  152. startTimeButton.setOnClickListener(v -> {
  153. TimePickerDialog timePickerDialog = new TimePickerDialog(requireContext(), new TimePickerDialog.OnTimeSetListener() {
  154. @Override
  155. public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
  156. String time = String.format("%02d", (hourOfDay)) + ":" + String.format("%02d", (minute));
  157. startTimeButton.setText(time);
  158. }
  159. }, hour, minute, true);
  160. timePickerDialog.show();
  161. });
  162. endDateButton.setOnClickListener(v->{
  163. DatePickerDialog datePickerDialog = new DatePickerDialog(requireContext(), new DatePickerDialog.OnDateSetListener() {
  164. @Override
  165. public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
  166. String date = year + "/" + String.format("%02d", (month + 1)) + "/" + String.format("%02d", dayOfMonth);
  167. endDateButton.setText(date);
  168. }
  169. }, year, month, day);
  170. datePickerDialog.show();
  171. });
  172. endTimeButton.setOnClickListener(v -> {
  173. TimePickerDialog timePickerDialog = new TimePickerDialog(requireContext(), new TimePickerDialog.OnTimeSetListener() {
  174. @Override
  175. public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
  176. String time = String.format("%02d", (hourOfDay)) + ":" + String.format("%02d", (minute));
  177. endTimeButton.setText(time);
  178. }
  179. }, hour, minute, true);
  180. timePickerDialog.show();
  181. });
  182.  
  183. Switch allDaySwitch = view.findViewById(R.id.all_day_switch);
  184. allDaySwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
  185. if(isChecked){
  186. startTimeButton.setTextColor(Color.GRAY);
  187. startTimeButton.setText("00:00");
  188. endDateButton.setTextColor(Color.GRAY);
  189. endDateButton.setText(startDateButton.getText());
  190. endTimeButton.setTextColor(Color.GRAY);
  191. endTimeButton.setText("23:59");
  192. }else{
  193. startTimeButton.setTextColor(Color.BLACK);
  194. endDateButton.setTextColor(Color.BLACK);
  195. endTimeButton.setTextColor(Color.BLACK);
  196. }
  197. startTimeButton.setEnabled(!isChecked);
  198. endDateButton.setEnabled(!isChecked);
  199. endTimeButton.setEnabled(!isChecked);
  200. });
  201.  
  202. Button cancelButton = view.findViewById(R.id.schedule_cancel_button);
  203. cancelButton.setOnClickListener(v->{
  204. ((MainActivity)getActivity()).backFragment();
  205. });
  206.  
  207. Spinner spinner = view.findViewById(R.id.select_book_spinner);
  208. bookTitles = new ArrayList<>();
  209. bookTitles.add("本を選択しない");
  210. booksViewModel.getBookLiveData().observe(getViewLifecycleOwner(), new Observer<HashMap<Integer, Book>>() {
  211. @Override
  212. public void onChanged(HashMap<Integer, Book> integerBookHashMap) {
  213. if(integerBookHashMap != null){
  214. for(int i : integerBookHashMap.keySet()){
  215. bookTitles.add(integerBookHashMap.get(i).getTitle());
  216. }
  217. }else{
  218. bookTitles = new ArrayList<>();
  219. }
  220. }
  221. });
  222.  
  223. booksViewModel.loadBooks(accountId, token);
  224.  
  225.  
  226. ArrayAdapter<String> adapter = new ArrayAdapter<>(requireContext(), androidx.appcompat.R.layout.support_simple_spinner_dropdown_item, bookTitles);
  227. adapter.setDropDownViewResource(androidx.appcompat.R.layout.support_simple_spinner_dropdown_item);
  228. spinner.setAdapter(adapter);
  229.  
  230.  
  231. EditText scheduleTitle = view.findViewById(R.id.schedule_title);
  232. Button confirmButton = view.findViewById(R.id.schedule_create_confirm);
  233. confirmButton.setOnClickListener(v -> {
  234. String[] dates = ((String)startDateButton.getText()).split("/");
  235. int year = Integer.parseInt(dates[0]);
  236. int month = Integer.parseInt(dates[1]);
  237. int day = Integer.parseInt(dates[2]);
  238. Editable tmp = scheduleTitle.getText();
  239. if(tmp == null){
  240. Snackbar.make(view, "タイトルを入力してください", Snackbar.LENGTH_SHORT).show();
  241. return;
  242. }
  243. String title = scheduleTitle.getText().toString();
  244. if(title == null || title.isEmpty()){
  245. Snackbar.make(view, "タイトルを入力してください", Snackbar.LENGTH_SHORT).show();
  246. return;
  247. }
  248. //
  249. String startDateTime = startDateButton.getText() + " " + startTimeButton.getText();
  250. String endDateTime = endDateButton.getText() + " " + endTimeButton.getText();
  251. Log.i("lkjfda", startDateTime + ":" + endDateTime);
  252. Log.i("lkjsa", "" + spinner.getSelectedItemPosition());
  253. scheduleViewModel.createSchedule(accountId, year, month, day, token, title, startDateTime, endDateTime, spinner.getSelectedItemPosition());
  254. ((MainActivity)getActivity()).backFragment();
  255. });
  256.  
  257.  
  258. }
  259.  
  260. }