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..67dfac5 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; @@ -23,9 +25,15 @@ 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 java.util.ArrayList; import java.util.Calendar; +import java.util.HashMap; +import java.util.List; /** * A simple {@link Fragment} subclass. @@ -66,40 +74,63 @@ } @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); } + + BooksViewModel booksViewModel; + String startDate; String startTime; String endDate; String endTime; + 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); + 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,11 +199,27 @@ ((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("本を選択しない"); - ArrayAdapter adapter = new ArrayAdapter<>(requireContext(), androidx.appcompat.R.layout.support_simple_spinner_dropdown_item, books); + 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<>(); + } + } + }); + + 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);