Newer
Older
CitrusClient / app / src / main / java / com / example / citrusclient / views / HomeFragment.java
package com.example.citrusclient.views;

import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;

import com.example.citrusclient.Citrus;
import com.example.citrusclient.R;
import com.example.citrusclient.models.Book;
import com.example.citrusclient.models.Schedule;
import com.example.citrusclient.models.Todo;
import com.example.citrusclient.viewmodels.BooksViewModel;
import com.example.citrusclient.viewmodels.ScheduleViewModel;
import com.example.citrusclient.viewmodels.TodosViewModel;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.security.PrivateKey;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment 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 HomeFragment() {
        // 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 HomeFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static HomeFragment newInstance(String param1, String param2) {
        HomeFragment fragment = new HomeFragment();
        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);
        }

        todosViewModel = new ViewModelProvider(this).get(TodosViewModel.class);
        scheduleViewModel = new ViewModelProvider(this).get(ScheduleViewModel.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_home, container, false);
    }

    private List<Todo> todoList;
    TodosViewModel todosViewModel;
    private List<Schedule> scheduleList;
    ScheduleViewModel scheduleViewModel;

    private HashMap<Integer, Book> integerBookHashMap;
    BooksViewModel booksViewModel;

    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        Citrus citrus = (Citrus)(getActivity().getApplication());
        String token = citrus.getToken();
        String accountId = citrus.getAccountId();
        int year = citrus.getCurYear();
        int month = citrus.getCurMouth();
        int day = citrus.getCurDay();
        int curBookId = citrus.getCurBookId();

        todoList = new ArrayList<>();

        scheduleList = new ArrayList<>();


        RecyclerView todoRecyclerView = view.findViewById(R.id.my_todos_list);
        todoRecyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(view.getContext());
        todoRecyclerView.setLayoutManager(layoutManager);
        MyTodoshelfAdapter todoAdapter = new MyTodoshelfAdapter(todoList, integerBookHashMap);
        todoRecyclerView.setAdapter(todoAdapter);

        RecyclerView scheduleRecyclerView = view.findViewById(R.id.my_schedule_list);
        scheduleRecyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager scheduleLayoutManager = new LinearLayoutManager(view.getContext());
        scheduleRecyclerView.setLayoutManager(scheduleLayoutManager);
        MyScheduleshelfAdapter scheduleAdapter = new MyScheduleshelfAdapter(scheduleList, integerBookHashMap);
        scheduleRecyclerView.setAdapter(scheduleAdapter);


        booksViewModel.loadBooks(accountId, token);
        System.out.println(token);

        booksViewModel.getBookLiveData().observe(getViewLifecycleOwner(), new Observer<HashMap<Integer, Book>>() {
            @Override
            public void onChanged(HashMap<Integer, Book> idBookHashMap) {
                if(idBookHashMap != null) {
                    integerBookHashMap = new HashMap<>(idBookHashMap);
                    for (Book book : integerBookHashMap.values()){
                        todosViewModel.loadTodosByDay(accountId, book.getBookId(), year , month, day, token);
                    }
                    scheduleViewModel.updateSchedulesByDay(accountId, year, month, day, token);
                }
            }
        });

        todosViewModel.getTodosByDayLiveData().observe(getViewLifecycleOwner(), new Observer<HashMap<Integer, Todo>>() {
            @Override
            public void onChanged(HashMap<Integer, Todo> idTodoHashMap) {
                if(idTodoHashMap != null) {
                    todoList.addAll(idTodoHashMap.values());
                    todoAdapter.setTodos(todoList, integerBookHashMap);
                }
            }
        });

        scheduleViewModel.getSchedulesByDay().observe(getViewLifecycleOwner(), new Observer<HashMap<Integer, Schedule>>() {
            @Override
            public void onChanged(HashMap<Integer, Schedule> idScheduleHashMap) {
                if(idScheduleHashMap != null) {
                    scheduleList = new ArrayList<>(idScheduleHashMap.values());
                    scheduleAdapter.setSchedules(scheduleList, integerBookHashMap);
                }
            }
        });




    }

}

class MyTodoshelfAdapter extends RecyclerView.Adapter<MyTodoshelfAdapter.MyTodoViewHolder> {

    private List<Todo> todoList;
    private HashMap<Integer, Book> idBookHashMap;
    MyTodoshelfAdapter(List<Todo> todos, HashMap<Integer, Book> idBookHashMap) {
        this.todoList = todos;
        if (idBookHashMap != null) {
            this.idBookHashMap = new HashMap<>(idBookHashMap);
        } else {
            this.idBookHashMap = new HashMap<>(); // 空のHashMapを作成
        }    }

    public void setTodos(List<Todo> todos ,HashMap<Integer, Book> idBookHashMap) {
        todoList = todos;
        if (idBookHashMap != null) {
            this.idBookHashMap = new HashMap<>(idBookHashMap);
        } else {
            this.idBookHashMap = new HashMap<>(); // 空のHashMapを作成
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MyTodoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.a_todo, parent, false);
        return new MyTodoViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyTodoViewHolder holder, int position) {
        Todo todoData = this.todoList.get(position);
        holder.todoButton.setText(todoData.getTitle());
        holder.todoCheckBox.setChecked(todoData.getCheck());
        Book book = idBookHashMap.get(todoData.getBookId());
        if(book != null) {
            int red = Integer.parseInt(book.getColor().substring(1, 3), 16);
            int green = Integer.parseInt(book.getColor().substring(3, 5), 16);
            int blue = Integer.parseInt(book.getColor().substring(5, 7), 16);
            holder.todoButton.setBackgroundColor(Color.rgb(red, green, blue));
            holder.todoButton.setTextColor(Color.rgb(255 - red, 255 - green, 255 - blue));
        }

    }

    @Override
    public int getItemCount() {
        return todoList.size();
    }

    static class MyTodoViewHolder extends RecyclerView.ViewHolder {
        Button todoButton;
        CheckBox todoCheckBox;
        public MyTodoViewHolder(@NonNull View itemView) {
            super(itemView);
            todoButton = itemView.findViewById(R.id.todo_button);
            todoCheckBox = itemView.findViewById(R.id.todo_checkBox);
        }
    }
}

class MyScheduleshelfAdapter extends RecyclerView.Adapter<MyScheduleshelfAdapter.MyScheduleViewHolder> {

    private List<Schedule> scheduleList;
    private HashMap<Integer, Book> idBookHashMap;

    MyScheduleshelfAdapter(List<Schedule> schedules, HashMap<Integer, Book> idBookHashMap) {
        this.scheduleList = schedules;
        if (idBookHashMap != null) {
            this.idBookHashMap = new HashMap<>(idBookHashMap);
        } else {
            this.idBookHashMap = new HashMap<>(); // 空のHashMapを作成
        }    }

    public void setSchedules(List<Schedule> schedules, HashMap<Integer, Book> idBookHashMap) {
        scheduleList = schedules;
        if (idBookHashMap != null) {
            this.idBookHashMap = new HashMap<>(idBookHashMap);
        } else {
            this.idBookHashMap = new HashMap<>(); // 空のHashMapを作成
        }        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MyScheduleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.a_schedule, parent, false);
        return new MyScheduleViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyScheduleViewHolder holder, int position) {
        Schedule scheduleData = this.scheduleList.get(position);
        holder.scheduleButton.setText(scheduleData.getTitle());
        Book book = idBookHashMap.get(scheduleData.getBookId());
        if(book != null) {
            int red = Integer.parseInt(book.getColor().substring(1, 3), 16);
            int green = Integer.parseInt(book.getColor().substring(3, 5), 16);
            int blue = Integer.parseInt(book.getColor().substring(5, 7), 16);
            holder.scheduleButton.setBackgroundColor(Color.rgb(red, green, blue));
            holder.scheduleButton.setTextColor(Color.rgb(255 - red, 255 - green, 255 - blue));
        }

    }


    @Override
    public int getItemCount() {
        return scheduleList.size();
    }

    static class MyScheduleViewHolder extends RecyclerView.ViewHolder {
        Button scheduleButton;
        public MyScheduleViewHolder(@NonNull View itemView) {
            super(itemView);
            scheduleButton = itemView.findViewById(R.id.schedule_button);
        }
    }
}