diff --git a/app/src/main/java/com/gh/common/util/ErrorHelper.kt b/app/src/main/java/com/gh/common/util/ErrorHelper.kt index 8708b67b38..88adcb43df 100644 --- a/app/src/main/java/com/gh/common/util/ErrorHelper.kt +++ b/app/src/main/java/com/gh/common/util/ErrorHelper.kt @@ -34,9 +34,8 @@ object ErrorHelper { if (customizedHandler(errorEntity.code ?: 0)) { return } else { - ErrorHelper.handleError(context, important, errorEntity) + handleError(context, important, errorEntity) } - } /** @@ -56,7 +55,7 @@ object ErrorHelper { return } - ErrorHelper.handleError(context, important, errorEntity) + handleError(context, important, errorEntity) } /*** diff --git a/app/src/main/java/com/gh/common/util/Extensions.kt b/app/src/main/java/com/gh/common/util/Extensions.kt index 14b4d94e99..22b46875bb 100644 --- a/app/src/main/java/com/gh/common/util/Extensions.kt +++ b/app/src/main/java/com/gh/common/util/Extensions.kt @@ -115,6 +115,14 @@ fun View.debounceActionWithInterval(interval: Long = 300, action: (() -> Unit)? debounceActionWithInterval(this.id, interval, action) } +/** + * 告诉需要返回 true or false 的外层这个事件已经被消费(即返回 true) + */ +inline fun consume(f: () -> Unit): Boolean { + f() + return true +} + /** * String related */ diff --git a/app/src/main/java/com/gh/gamecenter/qa/answer/detail/AnswerDetailFragment.kt b/app/src/main/java/com/gh/gamecenter/qa/answer/detail/AnswerDetailFragment.kt index 8e9866d9aa..59d31bcc1f 100644 --- a/app/src/main/java/com/gh/gamecenter/qa/answer/detail/AnswerDetailFragment.kt +++ b/app/src/main/java/com/gh/gamecenter/qa/answer/detail/AnswerDetailFragment.kt @@ -322,18 +322,14 @@ class AnswerDetailFragment : NormalFragment() { showFollowHint() } } else if (apiResponse.httpException != null) { - ErrorHelper.handleErrorWithCustomizedHandler(requireContext(), apiResponse.httpException.response().errorBody()!!.string(), false) { + ErrorHelper.handleErrorWithCustomizedHandler(requireContext(), apiResponse.httpException.response().errorBody()?.string(), false) { when (it) { - 403008 -> { + 403008 -> consume { toast(R.string.ask_vote_hint) answerDetail.me.isAnswerVoted = true updateLikeView(answerDetail.me.isAnswerVoted, answerDetail.vote) - true - } - 403036 -> { - toast(R.string.ask_vote_limit_hint) - true } + 403036 -> consume { toast(R.string.ask_vote_limit_hint) } else -> false } } diff --git a/app/src/main/java/com/gh/gamecenter/qa/answer/edit/AnswerEditActivity.kt b/app/src/main/java/com/gh/gamecenter/qa/answer/edit/AnswerEditActivity.kt index a83054307d..c3c22ede9a 100644 --- a/app/src/main/java/com/gh/gamecenter/qa/answer/edit/AnswerEditActivity.kt +++ b/app/src/main/java/com/gh/gamecenter/qa/answer/edit/AnswerEditActivity.kt @@ -240,16 +240,11 @@ class AnswerEditActivity : BaseRichEditorActivity() { startActivity(intent) } finish() - } else if (it?.status == Status.ERROR) { - var errorString: String? = null - val e = it.exception - if (e != null && e.code() == 403) { - try { - errorString = e.response().errorBody()!!.string() - val string = JSONObject(errorString) - val code = string.getInt("code") - if (code == 403037) { + val errorString = it.exception?.response()?.errorBody()?.string() + ErrorHelper.handleErrorWithCustomizedHandler(this, errorString, customizedHandler = { code -> + when (code) { + 403037 -> consume { if (TextUtils.isEmpty(mViewModel?.draftId)) { DialogUtils.showAlertDialog(this, "发布失败" , "问题已被删除,无法发布回答", "好吧", "", { @@ -262,16 +257,10 @@ class AnswerEditActivity : BaseRichEditorActivity() { finish() }) } - return@Observer - } else { - ErrorHelper.handleError(this, errorString, false) - return@Observer } - } catch (e1: Exception) { - e1.printStackTrace() + else -> false } - } - ErrorHelper.handleError(this, errorString, false) + }) } })