package com.hd.live.wallpapers.base;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModel;
import androidx.viewbinding.ViewBinding;

import com.hd.live.wallpapers.vm.VMScope;


public abstract class BaseFragment<T extends ViewBinding> extends Fragment {
    private final VMScope mVMScope = new VMScope();
    protected  T Vb;

    protected AppCompatActivity mActivity;
//    protected VMApplication vmApplication;
    public BaseFragment() {

    }
    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        mActivity = (AppCompatActivity) context;
    }
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Vb = getFragmentVb();
//        vmApplication = getApplicationScopeViewModel(VMApplication.class);
        initView();
        return Vb.getRoot();
    }

    protected abstract T getFragmentVb();
    protected  abstract void  initView();

    protected <K extends ViewModel> K getFragmentScopeViewModel(@NonNull Class<K> modelClass) {
        return mVMScope.getFragmentScopeViewModel(this, modelClass);
    }
    protected <K extends ViewModel> K getActivityScopeViewModel(@NonNull Class<K> modelClass) {
        return mVMScope.getActivityScopeViewModel(mActivity, modelClass);
    }

    protected <K extends ViewModel> K getApplicationScopeViewModel(@NonNull Class<K> modelClass) {
        return mVMScope.getApplicationScopeViewModel(modelClass);
    }
}