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 8fc862c..00a6502 100644 --- a/app/src/main/java/com/example/citrusclient/views/CreateScheduleFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/CreateScheduleFragment.java @@ -11,6 +11,8 @@ import androidx.annotation.Nullable; import androidx.fragment.app.DialogFragment; import androidx.fragment.app.Fragment; +import androidx.lifecycle.Observer; +import androidx.lifecycle.ViewModelProvider; import android.util.Log; import android.view.LayoutInflater; @@ -19,13 +21,21 @@ 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 java.util.ArrayList; import java.util.Calendar; +import java.util.HashMap; +import java.util.List; /** * A simple {@link Fragment} subclass. @@ -66,40 +76,61 @@ } @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - if (getArguments() != null) { - mParam1 = getArguments().getString(ARG_PARAM1); - mParam2 = getArguments().getString(ARG_PARAM2); - } - } - - @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); } - String startDate; - String startTime; - String endDate; - String endTime; + + BooksViewModel booksViewModel; + ScheduleViewModel scheduleViewModel; + + + List 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); - Calendar c = Calendar.getInstance(); + 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); - int year = c.get(Calendar.YEAR); - int month = c.get(Calendar.MONTH); - int day = c.get(Calendar.DAY_OF_MONTH); - int hour = c.get(Calendar.HOUR); - int minute = c.get(Calendar.MINUTE); startDateButton.setText(year + "/" + (month + 1) + "/" + day); startTimeButton.setText("00:00"); @@ -168,14 +199,45 @@ ((MainActivity)getActivity()).backFragment(); }); - Spinner spinner; - spinner = view.findViewById(R.id.select_book_spinner); - String[] books = {"book1", "book2", "book3"}; + Spinner spinner = view.findViewById(R.id.select_book_spinner); + bookTitles = new ArrayList<>(); + bookTitles.add("本を選択しない"); + 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()); + } + }else{ + bookTitles = new ArrayList<>(); + } + } + }); - ArrayAdapter adapter = new ArrayAdapter<>(requireContext(), androidx.appcompat.R.layout.support_simple_spinner_dropdown_item, books); + booksViewModel.loadBooks(accountId, token); + + + 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); + + 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]); + String title = scheduleTitle.getText().toString(); + String startDateTime = startDateButton.getText() + " " + startTimeButton.getText(); + String endDateTime = endDateButton.getText() + " " + endTimeButton.getText(); + scheduleViewModel.createSchedule(accountId, year, month, day, token, title, startDateTime, endDateTime, spinner.getSelectedItemPosition()); + ((MainActivity)getActivity()).backFragment(); + }); + + } }