package com.gh.common.util; import android.content.Context; import android.os.Build; import android.text.TextUtils; import com.gh.gamecenter.R; import com.gh.gamecenter.common.exposure.meta.MetaUtil; import com.gh.gamecenter.common.retrofit.JSONObjectResponse; import com.gh.gamecenter.common.retrofit.Response; import com.gh.gamecenter.feature.entity.CommentEntity; import com.gh.gamecenter.retrofit.RetrofitManager; import com.lightgame.utils.Utils; import org.json.JSONObject; import io.reactivex.Observable; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.schedulers.Schedulers; import okhttp3.MediaType; import okhttp3.RequestBody; import okhttp3.ResponseBody; import retrofit2.HttpException; /** * Created by khy on 2016/11/9. */ public class PostCommentUtils { public static void addCommentData(final Context context, final String newsId, final JSONObject content, final CommentEntity commentEntity, final PostCommentListener listener) { try { JSONObject device = new JSONObject(); device.put("os", "Android"); device.put("model", Build.MODEL); device.put("manufacturer", Build.MANUFACTURER); device.put("android_version", android.os.Build.VERSION.RELEASE); device.put("rom", MetaUtil.INSTANCE.getRom().name() + " " + MetaUtil.INSTANCE.getRom().getVersionName()); content.put("device", device); } catch (Exception e) { e.printStackTrace(); } RequestBody body = RequestBody.create(MediaType.parse("application/json"), content.toString()); Observable observable; if (commentEntity != null) { observable = RetrofitManager.getInstance().getApi().postReplyComment(commentEntity.getId(), body); } else { observable = RetrofitManager.getInstance().getApi().postNewsComment(newsId, body); } observable.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new JSONObjectResponse() { @Override public void onResponse(JSONObject response) { if (response.length() != 0) { if (listener != null) { listener.postSuccess(response); } } else { Utils.toast(context, R.string.post_failure_hint); } } @Override public void onFailure(HttpException e) { if (listener != null) { listener.postFailed(e); } } }); } public static void likeComment(final String answerId, String articleId, String videoId, String questionId, final String commentId, final PostCommentListener listener) { Observable observable; if (!TextUtils.isEmpty(answerId)) { observable = RetrofitManager.getInstance().getApi().postVoteAnswerComment(answerId, commentId); } else if (!TextUtils.isEmpty(articleId)) { observable = RetrofitManager.getInstance().getApi().postVoteCommunityArticleComment(commentId); } else if (!TextUtils.isEmpty(questionId)) { observable = RetrofitManager.getInstance().getApi().postVoteQuestionComment(questionId, commentId); } else { observable = RetrofitManager.getInstance().getApi().postVoteToVideo(videoId, commentId); } observable .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Response() { @Override public void onResponse(ResponseBody response) { if (listener != null) { listener.postSuccess(null); } } @Override public void onFailure(HttpException e) { if (listener != null) { listener.postFailed(e); } } }); } public static void unLikeComment( String articleId, String articleCommunityId, String videoId, String questionId, final String commentId, final PostCommentListener listener) { Observable observable; if (!TextUtils.isEmpty(questionId)) { observable = RetrofitManager.getInstance().getApi().postUnVoteQuestionComment(questionId, commentId); } else if (!TextUtils.isEmpty(articleId)) { observable = RetrofitManager.getInstance().getApi().postUnVoteArticleComment(commentId); } else { observable = RetrofitManager.getInstance().getApi().postUnVoteVideoComment(videoId, commentId); } observable .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Response() { @Override public void onResponse(ResponseBody response) { if (listener != null) { listener.postSuccess(null); } } @Override public void onFailure(HttpException e) { if (listener != null) { listener.postFailed(e); } } }); } public static void addCommentVote(final String commentId, final PostCommentListener listener) { RetrofitManager.getInstance().getApi() .postCommentVote(commentId) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Response() { @Override public void onResponse(ResponseBody response) { if (listener != null) { listener.postSuccess(null); } } @Override public void onFailure(HttpException e) { if (listener != null) { listener.postFailed(e); } } }); } public static void addReportData(final String commentId, final String reportData, final PostCommentListener listener) { RequestBody body = RequestBody.create(MediaType.parse("application/json"), reportData); RetrofitManager.getInstance().getApi() .postReportData(commentId, body) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Response() { @Override public void onResponse(ResponseBody response) { listener.postSuccess(null); } @Override public void onFailure(HttpException e) { listener.postFailed(e); } }); } public static void postAnswerReportData(final String commentId, final String answerId, final String reportData, final PostCommentListener listener) { RequestBody body = RequestBody.create(MediaType.parse("application/json"), reportData); RetrofitManager.getInstance().getApi() .postReportOfAnswerComment(answerId, commentId, body) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Response() { @Override public void onResponse(ResponseBody response) { listener.postSuccess(null); } @Override public void onFailure(HttpException e) { listener.postFailed(e); } }); } public static void reportCommunityArticleComment(final String commentId, final String reportData, final PostCommentListener listener) { RequestBody body = RequestBody.create(MediaType.parse("application/json"), reportData); RetrofitManager.getInstance().getApi() .postCommunityArticleCommentReport(commentId, body) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Response() { @Override public void onResponse(ResponseBody response) { listener.postSuccess(null); } @Override public void onFailure(HttpException e) { listener.postFailed(e); } }); } public static void reportVideoComment(final String videoId, final String commentId, final String reportData, final PostCommentListener listener) { RequestBody body = RequestBody.create(MediaType.parse("application/json"), reportData); RetrofitManager.getInstance().getApi() .postVideoCommentReport(videoId, commentId, body) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Response() { @Override public void onResponse(ResponseBody response) { listener.postSuccess(null); } @Override public void onFailure(HttpException e) { listener.postFailed(e); } }); } public static void reportQuestionComment(final String questionId, final String commentId, final String reportData, final PostCommentListener listener) { RequestBody body = RequestBody.create(MediaType.parse("application/json"), reportData); RetrofitManager.getInstance().getApi() .postQuestionCommentReport(questionId, commentId, body) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Response() { @Override public void onResponse(ResponseBody response) { listener.postSuccess(null); } @Override public void onFailure(HttpException e) { listener.postFailed(e); } }); } public static void reportGameCollectionComment(final String gameCollectionId, final String commentId, final String reportData, final PostCommentListener listener) { RequestBody body = RequestBody.create(MediaType.parse("application/json"), reportData); RetrofitManager.getInstance().getApi() .reportGameCollectionComment(gameCollectionId, commentId, body) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Response() { @Override public void onResponse(ResponseBody response) { listener.postSuccess(null); } @Override public void onFailure(HttpException e) { listener.postFailed(e); } }); } public interface PostCommentListener { void postSuccess(JSONObject response); void postFailed(Throwable error); } }