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.ScheduleViewModel; 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); scheduleViewModel = new ViewModelProvider(this).get(ScheduleViewModel.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; 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, 7, 2, 1)); scheduleList = new ArrayList<>(); scheduleList.add(new Schedule("a", "1", "1", 1, 1)); 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); 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); scheduleRecyclerView.setAdapter(scheduleAdapter); 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); } } }); 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); } } }); scheduleViewModel.updateSchedulesByDay("bird", 2024, 5, 28, "xyz"); todosViewModel.loadTodosByDay("bird", 1, 2024, 7, 2, "xyz"); } } class MyTodoshelfAdapter extends RecyclerView.Adapter<MyTodoshelfAdapter.MyTodoViewHolder> { private List<Todo> todoList; MyTodoshelfAdapter(List<Todo> todos) { this.todoList = todos; } 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); } } } class MyScheduleshelfAdapter extends RecyclerView.Adapter<MyScheduleshelfAdapter.MyScheduleViewHolder> { private List<Schedule> scheduleList; MyScheduleshelfAdapter(List<Schedule> schedules) { this.scheduleList = schedules; } public void setSchedules(List<Schedule> schedules) { scheduleList = schedules; 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()); } @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); } } }