Files
assistant-android/app/src/main/java/com/gh/common/util/CommentHelper.kt

327 lines
13 KiB
Kotlin

package com.gh.common.util
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import com.gh.common.util.CommentUtils.copyText
import com.gh.gamecenter.CommentDetailActivity
import com.gh.gamecenter.R
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.json.json
import com.gh.gamecenter.common.utils.ifLogin
import com.gh.gamecenter.common.utils.showAutoOrientation
import com.gh.gamecenter.common.view.BugFixedPopupWindow
import com.gh.gamecenter.feature.entity.CommentEntity
import com.gh.gamecenter.feature.entity.Permissions
import com.gh.gamecenter.login.user.UserManager
import com.gh.gamecenter.qa.comment.OnCommentOptionClickListener
import com.halo.assistant.HaloApp
import com.lightgame.utils.Utils
import org.json.JSONObject
import retrofit2.HttpException
object CommentHelper {
@JvmStatic
fun showCommunityArticleCommentOptions(
view: View,
commentEntity: CommentEntity,
showConversation: Boolean,
articleId: String,
communityId: String,
isShowTop: Boolean = false,
ignoreModerator: Boolean = false,
listener: OnCommentOptionClickListener?
) {
showCommentOptions(
view = view,
commentEntity = commentEntity,
showConversation = showConversation,
articleId = articleId,
communityId = communityId,
isShowTopOrAccept = isShowTop,
ignoreModerator = ignoreModerator,
listener = listener
)
}
@JvmStatic
fun showAnswerCommentOptions(
view: View,
commentEntity: CommentEntity,
showConversation: Boolean,
answerId: String,
listener: OnCommentOptionClickListener?
) {
showCommentOptions(
view = view,
commentEntity = commentEntity,
showConversation = showConversation,
answerId = answerId,
listener = listener
)
}
@JvmStatic
fun showVideoCommentOptions(
view: View,
commentEntity: CommentEntity,
showConversation: Boolean,
videoId: String,
isVideoAuthor: Boolean,
isShowTop: Boolean = false,
listener: OnCommentOptionClickListener?
) {
showCommentOptions(
view = view,
commentEntity = commentEntity,
showConversation = showConversation,
videoId = videoId,
isVideoAuthor = isVideoAuthor,
isShowTopOrAccept = isShowTop,
listener = listener
)
}
fun showQuestionCommentOption(
view: View,
commentEntity: CommentEntity,
questionId: String,
isShowTop: Boolean = false,
listener: OnCommentOptionClickListener?
) {
showCommentOptions(
view = view,
commentEntity = commentEntity,
showConversation = false,
questionId = questionId,
isShowTopOrAccept = isShowTop,
ignoreModerator = true,
listener = listener
)
}
fun showGameCollectionCommentOption(
view: View,
commentEntity: CommentEntity,
gameCollectionId: String,
listener: OnCommentOptionClickListener?
) {
showCommentOptions(
view = view,
commentEntity = commentEntity,
gameCollectionId = gameCollectionId,
showConversation = false,
ignoreModerator = true,
listener = listener
)
}
private fun showCommentOptions(
view: View,
commentEntity: CommentEntity,
showConversation: Boolean,
articleId: String? = null,
communityId: String? = null,
answerId: String? = null,
questionId: String? = null,
videoId: String? = null,
gameCollectionId: String? = null,
isShowTopOrAccept: Boolean = false,
ignoreModerator: Boolean = false,
isVideoAuthor: Boolean = false,
listener: OnCommentOptionClickListener? = null
) {
val context = view.context
val dialogOptions = ArrayList<String>()
val isContentAuthor = commentEntity.me?.isContentAuthor == true
if (isShowTopOrAccept && (articleId != null || questionId != null || videoId != null) && isContentAuthor) {
dialogOptions.add(if (commentEntity.isTop) "取消置顶" else "置顶")
}
if (isShowTopOrAccept && questionId != null && isContentAuthor) {
if (commentEntity.accept) {
dialogOptions.add("取消采纳")
} else {
dialogOptions.add("采纳")
}
}
dialogOptions.add("复制")
if (commentEntity.user.id != UserManager.getInstance().userId) {
dialogOptions.add("投诉")
}
if (questionId != null &&
commentEntity.me?.isModerator == true &&
(commentEntity.me?.moderatorPermissions?.highlightAnswer
?: Permissions.GUEST) > Permissions.GUEST && !commentEntity.choiceness
) {
dialogOptions.add("加精选")
}
if (questionId != null &&
commentEntity.me?.isModerator == true &&
(commentEntity.me?.moderatorPermissions?.cancelChoicenessAnswer
?: Permissions.GUEST) > Permissions.GUEST &&
commentEntity.choiceness
) {
dialogOptions.add("取消精选")
}
if (commentEntity.user.id == UserManager.getInstance().userId || commentEntity.me?.isModerator == true || isContentAuthor) {
dialogOptions.add("删除评论")
}
if (commentEntity.parentUser != null && showConversation) {
dialogOptions.add("查看对话")
}
val inflater = LayoutInflater.from(context)
val layout = inflater.inflate(R.layout.layout_popup_container, null)
val popupWindow = BugFixedPopupWindow(
layout,
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
)
val container = layout.findViewById<LinearLayout>(R.id.container)
for (text in dialogOptions) {
val item = inflater.inflate(R.layout.layout_popup_option_item, container, false)
container.addView(item)
val hitText = item.findViewById<TextView>(R.id.hint_text)
hitText.text = text
item.setOnClickListener {
popupWindow.dismiss()
listener?.onCommentOptionClick(commentEntity, text)
when (text) {
"复制" -> copyText(commentEntity.content, context)
"投诉" -> {
context.ifLogin("回答详情-评论-投诉") {
showReportTypeDialog(context) { reportType ->
val commentListener =
object : PostCommentUtils.PostCommentListener {
override fun postSuccess(response: JSONObject?) {
Utils.toast(context, "感谢您的投诉")
}
override fun postFailed(error: Throwable?) {
if (error == null) {
Utils.toast(context, "投诉失败,请稍后重试")
} else {
if (error is HttpException) {
ErrorHelper.handleError(
HaloApp.getInstance().application,
error.response().errorBody()?.string()
)
}
}
}
}
when {
answerId != null -> {
PostCommentUtils.postAnswerReportData(
commentEntity.id,
answerId,
reportType,
commentListener
)
}
articleId != null -> {
PostCommentUtils.reportCommunityArticleComment(
commentEntity.id,
reportType,
commentListener
)
}
questionId != null -> {
PostCommentUtils.reportQuestionComment(
questionId,
commentEntity.id,
reportType,
commentListener
)
}
gameCollectionId != null -> {
PostCommentUtils.reportGameCollectionComment(
gameCollectionId,
commentEntity.id,
reportType,
commentListener
)
}
else -> {
PostCommentUtils.reportVideoComment(
videoId,
commentEntity.id,
reportType,
commentListener
)
}
}
}
}
}
"查看对话" -> {
if (answerId != null) {
context.startActivity(
CommentDetailActivity
.getAnswerCommentIntent(
context,
commentEntity.id,
answerId,
null
)
)
} else if (articleId != null) {
context.startActivity(
CommentDetailActivity
.getCommunityArticleCommentIntent(
context,
articleId,
commentEntity.id,
communityId,
null
)
)
} else {
context.startActivity(
CommentDetailActivity
.getVideoCommentIntent(
context,
commentEntity.id,
videoId,
isVideoAuthor,
null
)
)
}
}
}
}
}
popupWindow.isTouchable = true
popupWindow.isFocusable = true
popupWindow.showAutoOrientation(view)
}
private fun showReportTypeDialog(
context: Context,
reportCallback: (reportType: String) -> Unit
) {
DialogUtils.showReportReasonDialog(
context,
Constants.REPORT_LIST.toList() as ArrayList<String>
) { reason, desc ->
val json = json {
"reason" to if (reason != "其他原因") reason else desc
}
reportCallback.invoke(json.toString())
}
}
}