package com.exquisite.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.exquisite.adapter.ThumAdapter;
import com.exquisite.data.Mydata;
import com.exquisite.data.MydataBase;
import com.exquisite.R;
import com.exquisite.databinding.FragmentLikeBinding;
import com.exquisite.tools.Mytools;
import com.exquisite.tools.SpaceItem;

import java.util.ArrayList;
import java.util.List;


public class LikeFragment extends Fragment {
    FragmentLikeBinding binding;
    private SpaceItem spaceItem = new SpaceItem(12, 12, 12);
    private List<Mydata> likelist = new ArrayList<>();
    private ThumAdapter thumAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentLikeBinding.inflate(getLayoutInflater());

        initData();
        binding.likeRv.addItemDecoration(spaceItem);

        return binding.getRoot();
    }

    private void initData() {
        MydataBase.getInstance().myDAO().getlikelist(true).observe(getViewLifecycleOwner(), new Observer<List<Mydata>>() {
            @Override
            public void onChanged(List<Mydata> mydata) {
                if (mydata.isEmpty()) {
                    binding.nolikeText.setVisibility(View.VISIBLE);
                } else {
                    binding.nolikeText.setVisibility(View.GONE);
                }
                thumAdapter = new ThumAdapter(requireContext(), mydata);
                binding.likeRv.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
                binding.likeRv.setAdapter(thumAdapter);
            }
        });
    }
}