From 4879f9d3fc40aa83f817a32cdf84b161f01199d8 Mon Sep 17 00:00:00 2001 From: HuanCheng65 <22636177+HuanCheng65@users.noreply.github.com> Date: Thu, 26 Jan 2023 14:59:27 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20Kotlin=20=E9=87=8D=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tieba/post/api/models/SearchThreadBean.kt | 48 +++++++++---------- .../FailureResponseInterceptor.kt | 24 ++++------ .../ui/page/photoview/PhotoViewActivity.kt | 5 +- 3 files changed, 33 insertions(+), 44 deletions(-) diff --git a/app/src/main/java/com/huanchengfly/tieba/post/api/models/SearchThreadBean.kt b/app/src/main/java/com/huanchengfly/tieba/post/api/models/SearchThreadBean.kt index cd22165a..654a085e 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/api/models/SearchThreadBean.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/api/models/SearchThreadBean.kt @@ -2,47 +2,43 @@ package com.huanchengfly.tieba.post.api.models import com.google.gson.annotations.SerializedName -class SearchThreadBean { +data class SearchThreadBean( @SerializedName("no") - val errorCode: Int? = null - + val errorCode: Int, @SerializedName("error") - val errorMsg: String? = null + val errorMsg: String, val data: DataBean? = null - - class DataBean { +) { + data class DataBean( @SerializedName("has_more") - val hasMore: Int? = null - + val hasMore: Int? = null, @SerializedName("current_page") - val currentPage: Int? = null - + val currentPage: Int? = null, @SerializedName("post_list") val postList: List? = null - } + ) - class ThreadInfoBean { - val tid: String? = null - val pid: String? = null - val title: String? = null - val content: String? = null - val time: String? = null + data class ThreadInfoBean( + val tid: String? = null, + val pid: String? = null, + val title: String? = null, + val content: String? = null, + val time: String? = null, @SerializedName("post_num") - val postNum: String? = null + val postNum: String? = null, @SerializedName("forum_name") - val forumName: String? = null - val user: UserInfoBean? = null + val forumName: String? = null, + val user: UserInfoBean? = null, val type: Int? = null - } + ) - class UserInfoBean { + data class UserInfoBean( @SerializedName("user_name") - val userName: String? = null - + val userName: String? = null, @SerializedName("user_id") - val userId: String? = null + val userId: String? = null, val portrait: String? = null - } + ) } \ No newline at end of file diff --git a/app/src/main/java/com/huanchengfly/tieba/post/api/retrofit/interceptors/FailureResponseInterceptor.kt b/app/src/main/java/com/huanchengfly/tieba/post/api/retrofit/interceptors/FailureResponseInterceptor.kt index be6713b2..1706f011 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/api/retrofit/interceptors/FailureResponseInterceptor.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/api/retrofit/interceptors/FailureResponseInterceptor.kt @@ -26,21 +26,17 @@ object FailureResponseInterceptor : Interceptor { it.request(Long.MAX_VALUE) }.buffer.clone().inputStream().reader(charset) - val jsonObject = try { - gson.fromJson( - gson.newJsonReader(inputStreamReader), - CommonResponse::class.java - ) - } catch (exception: Exception) { - //如果返回内容解析失败, 说明它不是一个合法的 json - //如果在拦截器抛出 MalformedJsonException 会导致 Retrofit 的异步请求一直卡着直到超时 - return response - } finally { - inputStreamReader.close() - } + val commonResponse = inputStreamReader.use { + runCatching { + gson.fromJson( + gson.newJsonReader(inputStreamReader), + CommonResponse::class.java + ) + }.getOrNull() + } ?: return response - if (jsonObject?.errorCode != null && jsonObject.errorCode != 0) { - throw TiebaApiException(jsonObject) + if (commonResponse.errorCode != null && commonResponse.errorCode != 0) { + throw TiebaApiException(commonResponse) } return response } diff --git a/app/src/main/java/com/huanchengfly/tieba/post/ui/page/photoview/PhotoViewActivity.kt b/app/src/main/java/com/huanchengfly/tieba/post/ui/page/photoview/PhotoViewActivity.kt index 2a463cf3..004c3493 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/ui/page/photoview/PhotoViewActivity.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/ui/page/photoview/PhotoViewActivity.kt @@ -23,7 +23,6 @@ import androidx.compose.material.icons.rounded.Download import androidx.compose.material.icons.rounded.Share import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect -import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember @@ -164,9 +163,7 @@ class PhotoViewActivity : BaseComposeActivityWithParcelable() { prop1 = PhotoViewUiState::hasNext, initial = false ) - val pageCount by derivedStateOf { - items.size - } + val pageCount = items.size Surface(color = Color.Black) { if (items.isNotEmpty()) { val coroutineScope = rememberCoroutineScope()