From f2e80a86c7e1133084b79027b4166fad43bbeae7 Mon Sep 17 00:00:00 2001 From: HuanCheng65 <22636177+HuanCheng65@users.noreply.github.com> Date: Wed, 15 Feb 2023 21:39:35 +0800 Subject: [PATCH] =?UTF-8?q?pref:=20=E6=96=B0=E8=A1=A8=E6=83=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/huanchengfly/tieba/post/api/Utils.kt | 7 +++- .../tieba/post/utils/EmoticonManager.kt | 36 ++++++++++--------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/com/huanchengfly/tieba/post/api/Utils.kt b/app/src/main/java/com/huanchengfly/tieba/post/api/Utils.kt index 14c7d7af..9df4a89b 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/api/Utils.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/api/Utils.kt @@ -2,6 +2,7 @@ package com.huanchengfly.tieba.post.api import com.huanchengfly.tieba.post.App.ScreenInfo import com.huanchengfly.tieba.post.api.models.protos.ThreadInfo +import com.huanchengfly.tieba.post.utils.EmoticonManager fun getScreenHeight(): Int = ScreenInfo.EXACT_SCREEN_HEIGHT @@ -13,7 +14,11 @@ val ThreadInfo.abstractText: String get() = richAbstract.joinToString(separator = "") { when (it.type) { 0 -> it.text.replace(Regex(" {2,}"), " ") - 2 -> "#(${it.c})" + 2 -> { + EmoticonManager.registerEmoticon(it.text, it.c) + "#(${it.c})" + } + else -> "" } } diff --git a/app/src/main/java/com/huanchengfly/tieba/post/utils/EmoticonManager.kt b/app/src/main/java/com/huanchengfly/tieba/post/utils/EmoticonManager.kt index 82c473ff..5763c6f8 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/utils/EmoticonManager.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/utils/EmoticonManager.kt @@ -20,7 +20,8 @@ import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.rememberTextMeasurer import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp -import com.bumptech.glide.Glide +import com.github.panpf.sketch.request.LoadRequest +import com.github.panpf.sketch.request.LoadResult import com.google.accompanist.drawablepainter.rememberDrawablePainter import com.huanchengfly.tieba.post.App import com.huanchengfly.tieba.post.R @@ -28,9 +29,11 @@ import com.huanchengfly.tieba.post.models.EmoticonCache import com.huanchengfly.tieba.post.pxToDp import com.huanchengfly.tieba.post.pxToSp import com.huanchengfly.tieba.post.toJson +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch import java.io.File import java.lang.ref.WeakReference -import java.util.concurrent.Executors object EmoticonManager { private val DEFAULT_EMOTICON_MAPPING: Map = mapOf( @@ -99,8 +102,7 @@ object EmoticonManager { private val emoticonIds: MutableList = mutableListOf() private val emoticonMapping: MutableMap = mutableMapOf() private val drawableCache: MutableMap = mutableMapOf() - - private val executor by lazy { Executors.newCachedThreadPool() } + private val coroutineScope: CoroutineScope = CoroutineScope(Dispatchers.IO) @OptIn(ExperimentalTextApi::class) @Composable @@ -164,7 +166,7 @@ object EmoticonManager { emoticonMapping.putAll(emoticonCache.mapping) } updateCache() - executor.submit { + coroutineScope.launch { fetchEmoticons(context) } } @@ -249,25 +251,21 @@ object EmoticonManager { ).also { drawableCache[id] = it } } - private fun fetchEmoticons(context: Context) { + private suspend fun fetchEmoticons(context: Context) { emoticonIds.forEach { val resId = getEmoticonResId(context, it) val emoticonFile = getEmoticonFile(it) if (resId == 0 && !emoticonFile.exists()) { - try { - val emoticonBitmap = - Glide.with(getContext()) - .asBitmap() - .load("http://static.tieba.baidu.com/tb/editor/images/client/$it.png") - .submit() - .get() + val loadEmoticonResult = LoadRequest( + context, + "http://static.tieba.baidu.com/tb/editor/images/client/$it.png" + ).execute() + if (loadEmoticonResult is LoadResult.Success) { ImageUtil.bitmapToFile( - emoticonBitmap, + loadEmoticonResult.bitmap, emoticonFile, Bitmap.CompressFormat.PNG ) - } catch (e: Exception) { - e.printStackTrace() } } } @@ -284,6 +282,10 @@ object EmoticonManager { emoticonMapping[name] = realId changed = true } - if (changed) updateCache() + if (changed) { + coroutineScope.launch { + updateCache() + } + } } } \ No newline at end of file