TrendsDetailInteractor.java 8.59 KB
Newer Older
konghaorui committed
1 2 3
package com.yidianling.dynamic.trendsDetail;


YKai committed
4
import androidx.appcompat.app.AppCompatActivity;
konghaorui committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226

import com.yidianling.common.tools.LogUtil;
import com.yidianling.dynamic.common.net.DynamicApiUtils;
import com.yidianling.dynamic.model.Command;
import com.yidianling.dynamic.model.CommentBean;
import com.yidianling.dynamic.model.TrendsDetailInfoBean;
import com.yidianling.dynamic.router.DynamicIn;
import com.ydl.ydlcommon.data.http.BaseAPIResponse;
import com.ydl.ydlcommon.data.http.RxUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Jim on 2017/10/28 0028.
 */

public class TrendsDetailInteractor {
    private CompositeDisposable mCompositeSubscription;
    private ITrendsDetailPresenter.OnTrendsDetailDataLoadFinishedListenner callBack;

    public void setCallBack(ITrendsDetailPresenter.OnTrendsDetailDataLoadFinishedListenner callBack) {
        this.callBack = callBack;
    }

    public TrendsDetailInteractor() {
        mCompositeSubscription = new CompositeDisposable();
    }

    public void fetchTrendsDetails(int id) {
        Command.TrendsInfo request = new Command.TrendsInfo(id);
        mCompositeSubscription.add(DynamicApiUtils.fetchTrendsDetailInfo(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(resp -> {
                    cacheCommentsData(resp);
                    if (callBack != null) {
                        callBack.onTrendsDetailFetched(resp);
                    }
                }, throwable -> {
                    if (callBack != null) {
                        callBack.onTrendsDetailFetchFailed(throwable);
                    }
                }));
    }

    private static final Integer PAGE_SIZE = 20;
    private List<List<CommentBean>> cacheComments = null;

    private void cacheCommentsData(BaseAPIResponse<TrendsDetailInfoBean> resp) {
        if(resp.data!=null&&resp.data.comments.size()>0) {
            List<CommentBean> comments = resp.data.comments;
            if(cacheComments==null) {
                cacheComments = new ArrayList<>();
            }else {
                cacheComments.clear();
            }

            Integer totalCount = comments.size();

            Integer requestCount = totalCount / PAGE_SIZE;

            for (int i = 0; i <= requestCount; i++) {
                Integer fromIndex = i * PAGE_SIZE;
                int toIndex = Math.min(totalCount, (i + 1) * PAGE_SIZE);
                List<CommentBean> subList = comments.subList(fromIndex, toIndex);
                cacheComments.add(subList);
            }
            callBack.onCommentsListFetched(cacheComments.get(0));
        }
    }


    public void fetchCommentsDatas(int id, int page, int lastId, int direction) {
        Command.TrendsComments request = new Command.TrendsComments(String.valueOf(id), String.valueOf(page), String.valueOf(lastId), String.valueOf(direction));
        mCompositeSubscription.add(DynamicApiUtils.fetchTrendsComments(request)
                .subscribeOn(Schedulers.io())
                .compose(RxUtils.resultData())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(listBaseResponse -> {
                    if (callBack != null) {
                        callBack.onCommentsListFetched(listBaseResponse);
                    }
                }, throwable -> {
                    if (callBack != null) {
                        callBack.onCommentsListFetchedFailed(throwable);
                    }
                }));

    }

    public void fetchCommentsDatas(int page) {
        if (callBack != null) {
            List<CommentBean> commentBeans = null;
            if(cacheComments.size() >= page) {
                commentBeans = cacheComments.get(page-1);
            }
            callBack.onCommentsListFetched(commentBeans);
        }
    }

    /**
     * 点赞
     */
    public void doLikeAction(int type, int id) {
        final Command.ZanAction cmd = new Command.ZanAction(String.valueOf(type), String.valueOf(id));
        mCompositeSubscription.add(DynamicApiUtils.zanAction(cmd)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(zanResultBaseResponse -> {
                    if (callBack != null) {
                        callBack.ondoLikeActionFetchedResult(zanResultBaseResponse, type);
                    }
                }, throwable -> {
                    if (callBack != null) {
                        LogUtil.d("do like action error: " + throwable.toString());
                        callBack.showDataLoadErrorView(throwable);
                    }
                }));
    }


    public void focus(String uid) {
        Command.FocusCmd cmd = new Command.FocusCmd(uid, 1 + "");//业务类型:1关注用户,2关注话题
        mCompositeSubscription.add(DynamicApiUtils.focus(cmd)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(resp -> {
                    if (callBack != null) {
                        callBack.onFoucusActionSuccessed(resp);
                    }
                }, throwable -> {
                    if (callBack != null) {
                        callBack.showDataLoadErrorView(throwable);
                    }
                }));
    }

    /**
     * //获取举报的内容列表
     */
    public void fetchReportReasonContent(int answerId, String commentContent) {
        Command.GetReportReason cmd = new Command.GetReportReason();
        mCompositeSubscription.add(DynamicApiUtils.getReportReason(cmd)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(resp -> {
                    if (callBack != null) {
                        callBack.onReportContentFetched(resp, answerId, commentContent);
                    }
                }, throwable -> {
                    callBack.showDataLoadErrorView(throwable);
                }));
    }


    //举报帖子评论
    public void report(int trendId, int type, int reasonId, int answerId, String answerContent) {
        type = 1;//举报帖子和评论都传1
        Command.ReportWorry cmd = new Command.ReportWorry(trendId, reasonId, answerId, answerContent, type);
        mCompositeSubscription.add(DynamicApiUtils.reportWorry(cmd)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(objectBaseResponse -> {
                    if (callBack != null) {
                        callBack.onReportSuccessed(objectBaseResponse);
                    }
                }, throwable -> {
                    callBack.showDataLoadErrorView(throwable);
                }));
    }


    /**
     * //删除帖子或评论
     *
     * @param type 移除类型:1移除评论,2移除动态
     * @param id   评论的id,动态id
     */
    public void delTrendsReply(int type, int id, int position) {
        Command.TrendsReplyDel command = new Command.TrendsReplyDel(type, id);
        mCompositeSubscription.add(DynamicApiUtils.removeTrendsReply(command)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(resp -> {
                    if (callBack != null) {
                        callBack.onDeleteSuccessed(resp, type, position);
                    }
                }, throwable -> {
                    callBack.showDataLoadErrorView(throwable);
                }));

    }

    public void getExpert(AppCompatActivity activity, String expertUid) {
        DynamicIn.INSTANCE.startChat(activity, expertUid);
    }

    public void commitOrReply(int type, String tid, String content, String replyContent, String toName) {
        Command.CommentOrReply cmd = new Command.CommentOrReply(String.valueOf(type), tid, content);
        mCompositeSubscription.add(DynamicApiUtils.commitOrReply(cmd)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(replyBaseResponse -> {
                    if (callBack != null) {
                        callBack.onCommentOrReplySuccessed(replyBaseResponse, type, content, replyContent, toName);
                    }
                }, throwable -> {
                    callBack.showDataLoadErrorView(throwable);
                }));
    }


    public void detachView() {
        mCompositeSubscription.dispose();
    }

}