package com.gh.gamecenter.info;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MediatorLiveData;

import com.gh.gamecenter.common.retrofit.Response;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import retrofit2.HttpException;

/**
 * Created by khy on 19/03/18.
 */

public class NewsViewsRepository<T> {

    private final MediatorLiveData<List<T>> result = new MediatorLiveData<>();

    // 获取新闻阅读量
    public void getNewsViews(Observable<List<T>> observable) {
        observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Response<List<T>>() {
                    @Override
                    public void onResponse(List<T> response) {
                        result.postValue(response);
                    }

                    @Override
                    public void onFailure(HttpException e) {
                        result.postValue(null);
                    }
                });

    }

    public LiveData<List<T>> asLiveData() {
        return result;
    }
}
