package com.example.iot_controlhost.base;

import android.app.Application;

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

import com.example.iot_controlhost.utils.PollingTask;
import com.example.iot_controlhost.utils.log.MyLog;

/**
 * @Description ViewModel 逻辑层
 * @Author DuanKaiji
 * @CreateTime 2024年01月17日 09:32
 */
public abstract class BasePresenter extends AndroidViewModel {

    private MutableLiveData<String> isLoadingLiveData = new MutableLiveData<>();
    private MutableLiveData<String> errorTips = new MutableLiveData<>();
    private MutableLiveData<String> successTips = new MutableLiveData<>();
    protected PollingTask pollingTask;
    public BasePresenter(@NonNull Application application) {
        super(application);
        pollingTask = PollingTask.getInstance(getClass().getSimpleName()); // 使用类名作为ID
    }

    public MutableLiveData<String> getIsLoadingLiveData() {
        return isLoadingLiveData;
    }

    public MutableLiveData<String> getErrorTips() {
        return errorTips;
    }

    public MutableLiveData<String> getSuccessTips() {
        return successTips;
    }

    public void showLoadingDialog(String content) {
        isLoadingLiveData.setValue(content);
    }

    public void showLoadingDialogInIO(String content) {
        isLoadingLiveData.postValue(content);
    }

    public void hideLoadingDialog() {
        isLoadingLiveData.setValue(null);
    }

    public void errorTips() {
        errorTips(null);
    }

    public void errorTips(String msg) {
        errorTips.setValue(msg);
    }

    public void successTips() {
        successTips(null);
    }

    public void successTips(String msg) {
        successTips.setValue(msg);
    }

    public String getString(int id) {
        return getApplication().getString(id);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        pollingTask.stopAllPollingTasks();
    }
}
