package com.phlx.anchorcollect.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.phlx.anchorcollect.BR;
import com.phlx.anchorcollect.Configs;
import com.phlx.anchorcollect.R;
import com.phlx.anchorcollect.data.VMFactory;
import com.phlx.anchorcollect.databinding.FragmentCollectListBinding;
import com.phlx.anchorcollect.ui.fragment.vm.CollectListVM;
import com.phlx.anchorcollect.ui.fragment.vm.CollectVM;
import com.phlx.anchorcollect.ui.main.MainActivity;

import me.goldze.mvvmhabit.base.BaseFragment;

public class CollectListFragment extends BaseFragment<FragmentCollectListBinding, CollectListVM> {


    private String collectType;

    public static CollectListFragment newInstance(String text) {
        CollectListFragment collectListFragment = new CollectListFragment();
        Bundle bundle = new Bundle();
        bundle.putString("collectType", text);
        collectListFragment.setArguments(bundle);
        return collectListFragment;
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_collect_list;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initParam() {
        super.initParam();

        if (getArguments() != null) {
            //取出保存的值
            collectType = getArguments().getString("collectType");
        }
    }

    @Override
    public CollectListVM initViewModel() {
        VMFactory factory = VMFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(CollectListVM.class);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        //刷新列表
        if (!hidden)
           viewModel.refreshList();
    }

    @Override
    public void initData() {
        super.initData();

        viewModel.setCollectType(collectType);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();

        viewModel.onItemClickEvent.observe(getActivity(), new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                MainActivity mainActivity = (MainActivity)getActivity();
                mainActivity.labelSwitching(0);
            }
        });
    }
}
