pref: 新表情

This commit is contained in:
HuanCheng65 2023-02-15 21:39:35 +08:00
parent af407efdb9
commit f2e80a86c7
No known key found for this signature in database
GPG Key ID: E9031EF91A805148
2 changed files with 25 additions and 18 deletions

View File

@ -2,6 +2,7 @@ package com.huanchengfly.tieba.post.api
import com.huanchengfly.tieba.post.App.ScreenInfo import com.huanchengfly.tieba.post.App.ScreenInfo
import com.huanchengfly.tieba.post.api.models.protos.ThreadInfo import com.huanchengfly.tieba.post.api.models.protos.ThreadInfo
import com.huanchengfly.tieba.post.utils.EmoticonManager
fun getScreenHeight(): Int = ScreenInfo.EXACT_SCREEN_HEIGHT fun getScreenHeight(): Int = ScreenInfo.EXACT_SCREEN_HEIGHT
@ -13,7 +14,11 @@ val ThreadInfo.abstractText: String
get() = richAbstract.joinToString(separator = "") { get() = richAbstract.joinToString(separator = "") {
when (it.type) { when (it.type) {
0 -> it.text.replace(Regex(" {2,}"), " ") 0 -> it.text.replace(Regex(" {2,}"), " ")
2 -> "#(${it.c})" 2 -> {
EmoticonManager.registerEmoticon(it.text, it.c)
"#(${it.c})"
}
else -> "" else -> ""
} }
} }

View File

@ -20,7 +20,8 @@ import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.rememberTextMeasurer import androidx.compose.ui.text.rememberTextMeasurer
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp 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.google.accompanist.drawablepainter.rememberDrawablePainter
import com.huanchengfly.tieba.post.App import com.huanchengfly.tieba.post.App
import com.huanchengfly.tieba.post.R 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.pxToDp
import com.huanchengfly.tieba.post.pxToSp import com.huanchengfly.tieba.post.pxToSp
import com.huanchengfly.tieba.post.toJson import com.huanchengfly.tieba.post.toJson
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File import java.io.File
import java.lang.ref.WeakReference import java.lang.ref.WeakReference
import java.util.concurrent.Executors
object EmoticonManager { object EmoticonManager {
private val DEFAULT_EMOTICON_MAPPING: Map<String, String> = mapOf( private val DEFAULT_EMOTICON_MAPPING: Map<String, String> = mapOf(
@ -99,8 +102,7 @@ object EmoticonManager {
private val emoticonIds: MutableList<String> = mutableListOf() private val emoticonIds: MutableList<String> = mutableListOf()
private val emoticonMapping: MutableMap<String, String> = mutableMapOf() private val emoticonMapping: MutableMap<String, String> = mutableMapOf()
private val drawableCache: MutableMap<String, Drawable> = mutableMapOf() private val drawableCache: MutableMap<String, Drawable> = mutableMapOf()
private val coroutineScope: CoroutineScope = CoroutineScope(Dispatchers.IO)
private val executor by lazy { Executors.newCachedThreadPool() }
@OptIn(ExperimentalTextApi::class) @OptIn(ExperimentalTextApi::class)
@Composable @Composable
@ -164,7 +166,7 @@ object EmoticonManager {
emoticonMapping.putAll(emoticonCache.mapping) emoticonMapping.putAll(emoticonCache.mapping)
} }
updateCache() updateCache()
executor.submit { coroutineScope.launch {
fetchEmoticons(context) fetchEmoticons(context)
} }
} }
@ -249,25 +251,21 @@ object EmoticonManager {
).also { drawableCache[id] = it } ).also { drawableCache[id] = it }
} }
private fun fetchEmoticons(context: Context) { private suspend fun fetchEmoticons(context: Context) {
emoticonIds.forEach { emoticonIds.forEach {
val resId = getEmoticonResId(context, it) val resId = getEmoticonResId(context, it)
val emoticonFile = getEmoticonFile(it) val emoticonFile = getEmoticonFile(it)
if (resId == 0 && !emoticonFile.exists()) { if (resId == 0 && !emoticonFile.exists()) {
try { val loadEmoticonResult = LoadRequest(
val emoticonBitmap = context,
Glide.with(getContext()) "http://static.tieba.baidu.com/tb/editor/images/client/$it.png"
.asBitmap() ).execute()
.load("http://static.tieba.baidu.com/tb/editor/images/client/$it.png") if (loadEmoticonResult is LoadResult.Success) {
.submit()
.get()
ImageUtil.bitmapToFile( ImageUtil.bitmapToFile(
emoticonBitmap, loadEmoticonResult.bitmap,
emoticonFile, emoticonFile,
Bitmap.CompressFormat.PNG Bitmap.CompressFormat.PNG
) )
} catch (e: Exception) {
e.printStackTrace()
} }
} }
} }
@ -284,6 +282,10 @@ object EmoticonManager {
emoticonMapping[name] = realId emoticonMapping[name] = realId
changed = true changed = true
} }
if (changed) updateCache() if (changed) {
coroutineScope.launch {
updateCache()
}
}
} }
} }