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.Todo;
import com.example.citrusclient.viewmodels.TodosViewModel;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

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);

    }



    @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;

    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();

        todoList = new ArrayList<>();
        todoList.add(new Todo("a", true, 2024, 06, 27, 1));

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

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

        todosViewModel.loadTodosByDay("bird", 1, 2024, 6, 2, "xyz");
    }
    
}
class MyTodoshelfAdapter extends RecyclerView.Adapter<MyTodoshelfAdapter.MyTodoViewHolder> {

    private List<Todo> todoList;

    MyTodoshelfAdapter(List<Todo> todo) {
        this.todoList = todo;
    }

    public void setTodos(List<Todo> todos) {
        todoList = todos;
        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());
    }

    @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);
        }
    }
}