From 4db6ec60ce29c27ef2e8f1a6682d3bfc775c44be Mon Sep 17 00:00:00 2001 From: HuanChengFly <609486518@qq.com> Date: Sun, 5 Apr 2020 16:21:00 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E7=A7=BB=E9=99=A4=E6=97=A0=E7=94=A8?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/huanchengfly/tieba/api/Extensions.kt | 12 +++++----- .../interceptors/SortAndSignInterceptor.kt | 12 +++++----- .../interceptors/AddCookieInterceptor.kt | 8 +++---- .../interceptors/CommonHeaderInterceptor.kt | 2 +- .../interceptors/CommonParamInterceptor.kt | 23 ++++++++----------- .../FailureResponseInterceptor.kt | 2 +- .../interceptors/ForceLoginInterceptor.kt | 8 +++---- .../interceptors/StParamInterceptor.kt | 16 ++++++------- .../huanchengfly/tieba/post/FloorActivity.kt | 2 +- .../tieba/post/adapters/ForumAdapter.java | 4 +--- .../adapters/PersonalizedFeedAdapter.java | 2 +- .../post/adapters/RecyclerThreadAdapter.java | 2 +- .../main/res/layout/layout_video_player.xml | 4 ++-- 13 files changed, 45 insertions(+), 52 deletions(-) diff --git a/app/src/main/java/com/huanchengfly/tieba/api/Extensions.kt b/app/src/main/java/com/huanchengfly/tieba/api/Extensions.kt index 8449ad6a..c9e0ee6d 100644 --- a/app/src/main/java/com/huanchengfly/tieba/api/Extensions.kt +++ b/app/src/main/java/com/huanchengfly/tieba/api/Extensions.kt @@ -24,21 +24,21 @@ fun String.urlDecode() : String { } fun FormBody.containsEncodedName(name: String): Boolean { - repeat(size()) { + repeat(size) { if (encodedName(it) == name) return true } return false } inline fun FormBody.forEach(block: (String, String) -> Unit) { - repeat(size()) { + repeat(size) { block(encodedName(it), encodedValue(it)) } } fun FormBody.raw() = StringBuilder().apply { - repeat(size()) { + repeat(size) { if (it != 0) append('&') append(encodedName(it)) append('=') @@ -48,7 +48,7 @@ fun FormBody.raw() = fun FormBody.sortedEncodedRaw(separator: Boolean = true): String { val nameAndValue = ArrayList() - repeat(size()) { + repeat(size) { nameAndValue.add("${encodedName(it)}=${encodedValue(it)}") } if (separator) return nameAndValue.sorted().joinToString(separator = "&") @@ -57,7 +57,7 @@ fun FormBody.sortedEncodedRaw(separator: Boolean = true): String { fun FormBody.sortedRaw(separator: Boolean = true): String { val nameAndValue = ArrayList() - repeat(size()) { + repeat(size) { nameAndValue.add("${name(it)}=${value(it)}") } if (separator) return nameAndValue.sorted().joinToString(separator = "&") @@ -66,7 +66,7 @@ fun FormBody.sortedRaw(separator: Boolean = true): String { fun FormBody.Builder.addAllEncoded(formBody: FormBody): FormBody.Builder { with(formBody) { - repeat(size()) { + repeat(size) { addEncoded(encodedName(it), encodedValue(it)) } } diff --git a/app/src/main/java/com/huanchengfly/tieba/api/interceptors/SortAndSignInterceptor.kt b/app/src/main/java/com/huanchengfly/tieba/api/interceptors/SortAndSignInterceptor.kt index 35eef772..18705b92 100644 --- a/app/src/main/java/com/huanchengfly/tieba/api/interceptors/SortAndSignInterceptor.kt +++ b/app/src/main/java/com/huanchengfly/tieba/api/interceptors/SortAndSignInterceptor.kt @@ -1,8 +1,8 @@ package com.huanchengfly.tieba.api.interceptors import android.util.Log -import com.huanchengfly.tieba.api.containsEncodedName import com.huanchengfly.tieba.api.Param +import com.huanchengfly.tieba.api.containsEncodedName import com.huanchengfly.tieba.api.sortedEncodedRaw import com.huanchengfly.tieba.api.sortedRaw import com.huanchengfly.toMD5 @@ -20,14 +20,14 @@ import okhttp3.Response class SortAndSignInterceptor(private val appSecret: String) : Interceptor { override fun intercept(chain: Interceptor.Chain): Response { var request = chain.request() - val url = request.url() - val body = request.body() + val url = request.url + val body = request.body request = when { url.queryParameter("BDUSS") != null && url.queryParameter(Param.SIGN) == null -> { Log.i("SortAndSign", "get") - val sortedQuery = url.query()!!.split('&').sorted().joinToString(separator = "") - val sortedEncodedQuery = url.encodedQuery()!!.split('&').sorted().joinToString(separator = "&") + val sortedQuery = url.query!!.split('&').sorted().joinToString(separator = "") + val sortedEncodedQuery = url.encodedQuery!!.split('&').sorted().joinToString(separator = "&") request.newBuilder() .url(url.newBuilder() .encodedQuery("$sortedEncodedQuery&${Param.SIGN}=${calculateSign(sortedQuery, appSecret)}") @@ -47,7 +47,7 @@ class SortAndSignInterceptor(private val appSecret: String) : Interceptor { addEncoded(Param.SIGN, calculateSign(body.sortedRaw(false), appSecret)) }.build() request.newBuilder() - .method(request.method(), formBody) + .method(request.method, formBody) .build() } diff --git a/app/src/main/java/com/huanchengfly/tieba/api/retrofit/interceptors/AddCookieInterceptor.kt b/app/src/main/java/com/huanchengfly/tieba/api/retrofit/interceptors/AddCookieInterceptor.kt index 81690d3d..bc37360b 100644 --- a/app/src/main/java/com/huanchengfly/tieba/api/retrofit/interceptors/AddCookieInterceptor.kt +++ b/app/src/main/java/com/huanchengfly/tieba/api/retrofit/interceptors/AddCookieInterceptor.kt @@ -9,9 +9,9 @@ import okhttp3.Response object AddCookieInterceptor : Interceptor { override fun intercept(chain: Interceptor.Chain): Response { val request = chain.request() - var headers = request.headers() - val httpUrl = request.url() - val body = request.body() + var headers = request.headers + val httpUrl = request.url + val body = request.body var addCookie = true val addCookieHeader = headers[Header.ADD_COOKIE] @@ -33,7 +33,7 @@ object AddCookieInterceptor : Interceptor { request.newBuilder() .headers(headers) .url(httpUrl) - .method(request.method(), body) + .method(request.method, body) .build() ) } diff --git a/app/src/main/java/com/huanchengfly/tieba/api/retrofit/interceptors/CommonHeaderInterceptor.kt b/app/src/main/java/com/huanchengfly/tieba/api/retrofit/interceptors/CommonHeaderInterceptor.kt index b40278c4..91867ea8 100644 --- a/app/src/main/java/com/huanchengfly/tieba/api/retrofit/interceptors/CommonHeaderInterceptor.kt +++ b/app/src/main/java/com/huanchengfly/tieba/api/retrofit/interceptors/CommonHeaderInterceptor.kt @@ -8,7 +8,7 @@ import okhttp3.Response class CommonHeaderInterceptor(private vararg val additionHeaders: ParamExpression) : Interceptor { override fun intercept(chain: Interceptor.Chain): Response { val request = chain.request() - val headers = request.headers() + val headers = request.headers return chain.proceed(request.newBuilder().apply { additionHeaders.forEachNonNull { name, value -> diff --git a/app/src/main/java/com/huanchengfly/tieba/api/retrofit/interceptors/CommonParamInterceptor.kt b/app/src/main/java/com/huanchengfly/tieba/api/retrofit/interceptors/CommonParamInterceptor.kt index d5696c20..ba6b7c48 100644 --- a/app/src/main/java/com/huanchengfly/tieba/api/retrofit/interceptors/CommonParamInterceptor.kt +++ b/app/src/main/java/com/huanchengfly/tieba/api/retrofit/interceptors/CommonParamInterceptor.kt @@ -1,11 +1,6 @@ package com.huanchengfly.tieba.api.retrofit.interceptors -import com.huanchengfly.tieba.api.ParamExpression -import com.huanchengfly.tieba.api.addAllEncoded -import com.huanchengfly.tieba.api.containsEncodedName -import com.huanchengfly.tieba.api.forEachNonNull -import com.huanchengfly.tieba.api.Header -import com.huanchengfly.tieba.api.Method +import com.huanchengfly.tieba.api.* import okhttp3.FormBody import okhttp3.Interceptor import okhttp3.Response @@ -13,9 +8,9 @@ import okhttp3.Response class CommonParamInterceptor(private vararg val additionParams: ParamExpression) : Interceptor { override fun intercept(chain: Interceptor.Chain): Response { val request = chain.request() - var headers = request.headers() - var httpUrl = request.url() - var body = request.body() + var headers = request.headers + var httpUrl = request.url + var body = request.body //是否强制加到 Query(暂不存在强制加到 FormBody 的情况) var forceQuery = false @@ -27,10 +22,10 @@ class CommonParamInterceptor(private vararg val additionParams: ParamExpression) when { //如果是 GET 则添加到 Query - request.method() == Method.GET || forceQuery -> { - httpUrl = request.url().newBuilder().apply { + request.method == Method.GET || forceQuery -> { + httpUrl = request.url.newBuilder().apply { additionParams.forEachNonNull { name, value -> - if (request.url().queryParameter(name) == null) addQueryParameter(name, value) + if (request.url.queryParameter(name) == null) addQueryParameter(name, value) } }.build() } @@ -48,7 +43,7 @@ class CommonParamInterceptor(private vararg val additionParams: ParamExpression) body is FormBody -> { body = FormBody.Builder().addAllEncoded(body).apply { additionParams.forEachNonNull { name, value -> - if (!(request.body() as FormBody).containsEncodedName(name)) add(name, value) + if (!(request.body as FormBody).containsEncodedName(name)) add(name, value) } }.build() } @@ -62,7 +57,7 @@ class CommonParamInterceptor(private vararg val additionParams: ParamExpression) request.newBuilder() .headers(headers) .url(httpUrl) - .method(request.method(), body) + .method(request.method, body) .build() ) } diff --git a/app/src/main/java/com/huanchengfly/tieba/api/retrofit/interceptors/FailureResponseInterceptor.kt b/app/src/main/java/com/huanchengfly/tieba/api/retrofit/interceptors/FailureResponseInterceptor.kt index 01b22d2e..04ec6a45 100644 --- a/app/src/main/java/com/huanchengfly/tieba/api/retrofit/interceptors/FailureResponseInterceptor.kt +++ b/app/src/main/java/com/huanchengfly/tieba/api/retrofit/interceptors/FailureResponseInterceptor.kt @@ -11,7 +11,7 @@ object FailureResponseInterceptor : Interceptor { override fun intercept(chain: Interceptor.Chain): Response { val response = chain.proceed(chain.request()) - val body = response.body() + val body = response.body if (!response.isSuccessful || body == null || body.contentLength() == 0L) return response //获取字符集 diff --git a/app/src/main/java/com/huanchengfly/tieba/api/retrofit/interceptors/ForceLoginInterceptor.kt b/app/src/main/java/com/huanchengfly/tieba/api/retrofit/interceptors/ForceLoginInterceptor.kt index a4f8477f..dc759c4d 100644 --- a/app/src/main/java/com/huanchengfly/tieba/api/retrofit/interceptors/ForceLoginInterceptor.kt +++ b/app/src/main/java/com/huanchengfly/tieba/api/retrofit/interceptors/ForceLoginInterceptor.kt @@ -11,9 +11,9 @@ import okhttp3.Response object ForceLoginInterceptor : Interceptor { override fun intercept(chain: Interceptor.Chain): Response { val request = chain.request() - var headers = request.headers() - var httpUrl = request.url() - var body = request.body() + var headers = request.headers + var httpUrl = request.url + var body = request.body //是否强制登录 var forceLogin = false @@ -31,7 +31,7 @@ object ForceLoginInterceptor : Interceptor { request.newBuilder() .headers(headers) .url(httpUrl) - .method(request.method(), body) + .method(request.method, body) .build() ) } diff --git a/app/src/main/java/com/huanchengfly/tieba/api/retrofit/interceptors/StParamInterceptor.kt b/app/src/main/java/com/huanchengfly/tieba/api/retrofit/interceptors/StParamInterceptor.kt index 226efde9..5b294b19 100644 --- a/app/src/main/java/com/huanchengfly/tieba/api/retrofit/interceptors/StParamInterceptor.kt +++ b/app/src/main/java/com/huanchengfly/tieba/api/retrofit/interceptors/StParamInterceptor.kt @@ -1,9 +1,9 @@ package com.huanchengfly.tieba.api.retrofit.interceptors -import com.huanchengfly.tieba.api.addAllEncoded -import com.huanchengfly.tieba.api.forEachNonNull import com.huanchengfly.tieba.api.Header import com.huanchengfly.tieba.api.Method +import com.huanchengfly.tieba.api.addAllEncoded +import com.huanchengfly.tieba.api.forEachNonNull import okhttp3.FormBody import okhttp3.Interceptor import okhttp3.Response @@ -13,9 +13,9 @@ import kotlin.math.roundToInt class StParamInterceptor(private val method: Boolean = false) : Interceptor { override fun intercept(chain: Interceptor.Chain): Response { val request = chain.request() - var headers = request.headers() - var httpUrl = request.url() - var body = request.body() + var headers = request.headers + var httpUrl = request.url + var body = request.body //是否强制加到 Query(暂不存在强制加到 FormBody 的情况) var forceQuery = false @@ -52,8 +52,8 @@ class StParamInterceptor(private val method: Boolean = false) : Interceptor { when { //如果是 GET 则添加到 Query - request.method() == Method.GET || forceQuery -> { - httpUrl = request.url().newBuilder().apply { + request.method == Method.GET || forceQuery -> { + httpUrl = request.url.newBuilder().apply { additionParams.forEachNonNull { name, value -> addQueryParameter(name, value) } @@ -87,7 +87,7 @@ class StParamInterceptor(private val method: Boolean = false) : Interceptor { request.newBuilder() .headers(headers) .url(httpUrl) - .method(request.method(), body) + .method(request.method, body) .build() ) } diff --git a/app/src/main/java/com/huanchengfly/tieba/post/FloorActivity.kt b/app/src/main/java/com/huanchengfly/tieba/post/FloorActivity.kt index c060fa5a..1eaa617f 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/FloorActivity.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/FloorActivity.kt @@ -177,7 +177,7 @@ class FloorActivity : BaseActivity() { hasMore = false recyclerViewAdapter!!.loadEnd() } - toolbar.title = getString(R.string.title_floor_loaded, subFloorListBean.post!!.floor) + toolbar.title = getString(R.string.title_floor_loaded, subFloorListBean.post.floor) dataBean = subFloorListBean recyclerViewAdapter!!.setData(subFloorListBean) refreshLayout.isRefreshing = false diff --git a/app/src/main/java/com/huanchengfly/tieba/post/adapters/ForumAdapter.java b/app/src/main/java/com/huanchengfly/tieba/post/adapters/ForumAdapter.java index 3bcbc8c7..08da10e5 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/adapters/ForumAdapter.java +++ b/app/src/main/java/com/huanchengfly/tieba/post/adapters/ForumAdapter.java @@ -53,7 +53,6 @@ public class ForumAdapter extends MultiBaseAdapter { public static final int TYPE_THREAD_VIDEO = 14; private ForumPageBean data; private Map userBeanMap; - private NavigationHelper navigationHelper; private GoodClassifyAdapter goodClassifyAdapter; private List ids; private boolean good; @@ -62,7 +61,6 @@ public class ForumAdapter extends MultiBaseAdapter { super(context, null, true); ids = new ArrayList<>(); userBeanMap = new HashMap<>(); - navigationHelper = NavigationHelper.newInstance(mContext); good = isGood; if (isGood) { View goodView = Util.inflate(mContext, R.layout.layout_header_forum_good); @@ -303,7 +301,7 @@ public class ForumAdapter extends MultiBaseAdapter { VideoPlayerStandard videoPlayerStandard = viewHolder.getView(R.id.forum_item_content_video); videoPlayerStandard.setLayoutParams(getLayoutParams((RelativeLayout.LayoutParams) videoPlayerStandard.getLayoutParams())); videoPlayerStandard.setUp(threadBean.getVideoInfo().getVideoUrl(), ""); - ImageUtil.load(videoPlayerStandard.thumbImageView, ImageUtil.LOAD_TYPE_SMALL_PIC, threadBean.getVideoInfo().getThumbnailUrl(), true); + ImageUtil.load(videoPlayerStandard.posterImageView, ImageUtil.LOAD_TYPE_SMALL_PIC, threadBean.getVideoInfo().getThumbnailUrl(), true); break; } } diff --git a/app/src/main/java/com/huanchengfly/tieba/post/adapters/PersonalizedFeedAdapter.java b/app/src/main/java/com/huanchengfly/tieba/post/adapters/PersonalizedFeedAdapter.java index 8d57838d..c796d8ee 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/adapters/PersonalizedFeedAdapter.java +++ b/app/src/main/java/com/huanchengfly/tieba/post/adapters/PersonalizedFeedAdapter.java @@ -264,7 +264,7 @@ public class PersonalizedFeedAdapter extends MultiBaseAdapter