package com.sjkj.base_lib.vm;

import android.app.Application;
import android.os.Handler;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;

import com.sjkj.base_lib.app.AppManager;
import com.sjkj.base_lib.app.BaseApp;
import com.sjkj.base_lib.weight.LoadingDialog;

public class BaseViewModel extends AndroidViewModel {
    public BaseViewModel() {
        super(BaseApp.getInstance());
    }

    public BaseViewModel(@NonNull Application application) {
        super(application);
    }

    private LoadingDialog dialog;

    public void showLoadingDialog() {
        if (dialog == null)
            dialog = new LoadingDialog(AppManager.getAppManager().currentActivity());
        dialog.showDialog();
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
    }

    public void hideLoadingDialog(int code) {
        if (code == LoadingDialog.LOADING_SUCCESS) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (null != dialog && dialog.isShowing()) {
                        dialog.hideDialog();
                    }
                }
            }, 300);

        } else {
            if (null != dialog && dialog.isShowing()) {
                dialog.hideDialog();
            }
        }


    }

}
