package com.casic.dcms.mvp.model;

import com.casic.dcms.bean.HomePageBean;
import com.casic.dcms.utils.retrofit.RetrofitServiceManager;

import rx.Observable;
import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author : Pengxh
 * @time : 2021/4/13 14:15
 * @email : 290677893@qq.com
 **/
public class HomePageModelImpl implements IHomePageModel {

    private OnGetPageResultListener listener;

    public HomePageModelImpl(OnGetPageResultListener onGetPageResultListener) {
        this.listener = onGetPageResultListener;
    }

    /**
     * 数据回调接口
     */
    public interface OnGetPageResultListener {
        void onSuccess(HomePageBean resultBean);

        void onFailure(Throwable throwable);
    }

    @Override
    public Subscription sendRetrofitRequest(String resourceType) {
        Observable<HomePageBean> observable = RetrofitServiceManager.getHomePageResult(resourceType);
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<HomePageBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                if (listener != null) {
                    listener.onFailure(e);
                }
            }

            @Override
            public void onNext(HomePageBean resultBean) {
                if (resultBean != null) {
                    listener.onSuccess(resultBean);
                }
            }
        });
    }
}
