package com.hd.live.wallpapers.actfrag;

import android.os.Bundle;

import androidx.lifecycle.Observer;
import androidx.paging.PagingData;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hd.live.wallpapers.alladapter.AdaHome;
import com.hd.live.wallpapers.base.BaseFragment;
import com.hd.live.wallpapers.bean.ResultData;
import com.hd.live.wallpapers.databinding.FragHomeBinding;
import com.hd.live.wallpapers.others.ItemUtils;
import com.hd.live.wallpapers.vm.VmHome;

public class FragHome extends BaseFragment<FragHomeBinding> {


    private static final String ARG_PARAM1 = "param1";


    private int wallpaperType;

    private VmHome vmHome;

    public FragHome() {

    }
    public static FragHome newInstance(int type) {
        FragHome fragment = new FragHome();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            wallpaperType = getArguments().getInt(ARG_PARAM1);
        }
    }


    @Override
    protected FragHomeBinding getFragmentVb() {
        return FragHomeBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        AdaHome adaHome = new AdaHome(requireActivity());
        Vb.recycler.setAdapter(adaHome);
        Vb.recycler.setLayoutManager(new GridLayoutManager(requireContext(),2, RecyclerView.VERTICAL, false));
        Vb.recycler.addItemDecoration(new ItemUtils(requireContext(),15,15,10));
        vmHome = getFragmentScopeViewModel(VmHome.class);
        vmHome.getPagingData(wallpaperType).observe(getViewLifecycleOwner(), new Observer<PagingData<ResultData>>() {
            @Override
            public void onChanged(PagingData<ResultData> resultDataPagingData) {
                adaHome.submitData(requireActivity().getLifecycle(),resultDataPagingData);
            }
        });

    }
}