refactor: Kotlin 重写

This commit is contained in:
HuanCheng65 2023-01-26 14:59:27 +08:00
parent ada00cfed8
commit 4879f9d3fc
No known key found for this signature in database
GPG Key ID: E9031EF91A805148
3 changed files with 33 additions and 44 deletions

View File

@ -2,47 +2,43 @@ package com.huanchengfly.tieba.post.api.models
import com.google.gson.annotations.SerializedName import com.google.gson.annotations.SerializedName
class SearchThreadBean { data class SearchThreadBean(
@SerializedName("no") @SerializedName("no")
val errorCode: Int? = null val errorCode: Int,
@SerializedName("error") @SerializedName("error")
val errorMsg: String? = null val errorMsg: String,
val data: DataBean? = null val data: DataBean? = null
) {
class DataBean { data class DataBean(
@SerializedName("has_more") @SerializedName("has_more")
val hasMore: Int? = null val hasMore: Int? = null,
@SerializedName("current_page") @SerializedName("current_page")
val currentPage: Int? = null val currentPage: Int? = null,
@SerializedName("post_list") @SerializedName("post_list")
val postList: List<ThreadInfoBean>? = null val postList: List<ThreadInfoBean>? = null
} )
class ThreadInfoBean { data class ThreadInfoBean(
val tid: String? = null val tid: String? = null,
val pid: String? = null val pid: String? = null,
val title: String? = null val title: String? = null,
val content: String? = null val content: String? = null,
val time: String? = null val time: String? = null,
@SerializedName("post_num") @SerializedName("post_num")
val postNum: String? = null val postNum: String? = null,
@SerializedName("forum_name") @SerializedName("forum_name")
val forumName: String? = null val forumName: String? = null,
val user: UserInfoBean? = null val user: UserInfoBean? = null,
val type: Int? = null val type: Int? = null
} )
class UserInfoBean { data class UserInfoBean(
@SerializedName("user_name") @SerializedName("user_name")
val userName: String? = null val userName: String? = null,
@SerializedName("user_id") @SerializedName("user_id")
val userId: String? = null val userId: String? = null,
val portrait: String? = null val portrait: String? = null
} )
} }

View File

@ -26,21 +26,17 @@ object FailureResponseInterceptor : Interceptor {
it.request(Long.MAX_VALUE) it.request(Long.MAX_VALUE)
}.buffer.clone().inputStream().reader(charset) }.buffer.clone().inputStream().reader(charset)
val jsonObject = try { val commonResponse = inputStreamReader.use {
gson.fromJson<CommonResponse>( runCatching {
gson.newJsonReader(inputStreamReader), gson.fromJson<CommonResponse>(
CommonResponse::class.java gson.newJsonReader(inputStreamReader),
) CommonResponse::class.java
} catch (exception: Exception) { )
//如果返回内容解析失败, 说明它不是一个合法的 json }.getOrNull()
//如果在拦截器抛出 MalformedJsonException 会导致 Retrofit 的异步请求一直卡着直到超时 } ?: return response
return response
} finally {
inputStreamReader.close()
}
if (jsonObject?.errorCode != null && jsonObject.errorCode != 0) { if (commonResponse.errorCode != null && commonResponse.errorCode != 0) {
throw TiebaApiException(jsonObject) throw TiebaApiException(commonResponse)
} }
return response return response
} }

View File

@ -23,7 +23,6 @@ import androidx.compose.material.icons.rounded.Download
import androidx.compose.material.icons.rounded.Share import androidx.compose.material.icons.rounded.Share
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
@ -164,9 +163,7 @@ class PhotoViewActivity : BaseComposeActivityWithParcelable<PhotoViewData>() {
prop1 = PhotoViewUiState::hasNext, prop1 = PhotoViewUiState::hasNext,
initial = false initial = false
) )
val pageCount by derivedStateOf { val pageCount = items.size
items.size
}
Surface(color = Color.Black) { Surface(color = Color.Black) {
if (items.isNotEmpty()) { if (items.isNotEmpty()) {
val coroutineScope = rememberCoroutineScope() val coroutineScope = rememberCoroutineScope()