package com.daya.live_teaching.common;

import com.daya.live_teaching.model.RequestState;
import com.daya.live_teaching.utils.ToastUtils;

import androidx.lifecycle.Observer;

/**
 * 用于请求结果出错时弹出提示用监听
 */
public class ShowToastObserver implements Observer<RequestState> {
    private ToastBySelfComponent component;

    public ShowToastObserver() {
    }

    public ShowToastObserver(Object toastComponent) {
        if(toastComponent instanceof ToastBySelfComponent) {
            this.component = (ToastBySelfComponent)toastComponent;
        }
    }

    @Override
    public void onChanged(RequestState state) {
        if (state.getState() == RequestState.State.FAILED) {
            if (component != null) {
                component.showToast(state.getErrorCode().getMessageResId());
            } else {
                ToastUtils.showToast(state.getErrorCode().getMessageResId());
            }
        }
    }
}
