Newer
Older
CitrusClient / app / src / main / java / com / example / citrusclient / views / CreateTodoFragment.java
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.ViewModelProvider;

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.viewmodels.BooksViewModel;
import com.example.citrusclient.viewmodels.ScheduleViewModel;
import com.example.citrusclient.viewmodels.TodosViewModel;

import java.util.Calendar;

/**
 * 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;

    private Citrus citrus;
    private int year;
    private int month;
    private int day;
    private String title;
    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());
        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);
        title = titleField.getText().toString();

        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.getCurMouth()); // 月の初期値を設定


        NumberPicker numberPickerDay = view.findViewById(R.id.numberPickerDay);
        numberPickerDay.setMinValue(1);
        numberPickerDay.setMaxValue(31);
        numberPickerDay.setValue(citrus.getCurDay()); // 日の初期値を設定


        Button cancelButton = view.findViewById(R.id.todo_cancel_button);
        cancelButton.setOnClickListener(v->{
            ((MainActivity)getActivity()).backFragment();
        });

        Button createButton = view.findViewById(R.id.todo_create_button);
        createButton.setOnClickListener(v->{
            year = numberPickerYear.getValue(); // 設定年を取得
            month = numberPickerMonth.getValue(); // 設定月を取得
            day = numberPickerDay.getValue(); // 設定日を取得
            createTodo();
            ((MainActivity)getActivity()).backFragment();
        });

        Spinner spinner;
        spinner = view.findViewById(R.id.select_book_spinner);
        String[] books = {"book1", "book2", "book3"};
        ArrayAdapter<String> adapter = new ArrayAdapter<>(requireContext(), androidx.appcompat.R.layout.support_simple_spinner_dropdown_item, books);
        adapter.setDropDownViewResource(androidx.appcompat.R.layout.support_simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
    }

    //Todo追加時の処理
    private void createTodo()
    {
        todosViewModel.createTodo(citrus.getAccountId(),selectBookNum,year,month,day,title,citrus.getToken());
    }

}