From 2003a42920241f8403f12dbf76ac62127df01550 Mon Sep 17 00:00:00 2001 From: hpp2334 Date: Mon, 2 Dec 2024 20:41:08 +0800 Subject: [PATCH] fix: EaseImage sync load image --- .../easemusicplayer/components/EaseImage.kt | 14 ++++- .../kutedev/easemusicplayer/core/UIBridge.kt | 62 +++++++++---------- 2 files changed, 42 insertions(+), 34 deletions(-) diff --git a/android/app/src/main/java/com/kutedev/easemusicplayer/components/EaseImage.kt b/android/app/src/main/java/com/kutedev/easemusicplayer/components/EaseImage.kt index b2ef8ae5..7c04f02d 100644 --- a/android/app/src/main/java/com/kutedev/easemusicplayer/components/EaseImage.kt +++ b/android/app/src/main/java/com/kutedev/easemusicplayer/components/EaseImage.kt @@ -8,6 +8,7 @@ import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.ImageBitmap import androidx.compose.ui.layout.ContentScale +import com.kutedev.easemusicplayer.core.DataSourceKeyH import com.kutedev.easemusicplayer.core.UIBridgeController import uniffi.ease_client_shared.DataSourceKey @@ -18,10 +19,17 @@ fun EaseImage( contentScale: ContentScale ) { val bridge = UIBridgeController.current - var bitmap by remember { mutableStateOf(bridge.bitmapDataSources.get(dataSourceKey)) } + var oldKey by remember { mutableStateOf(DataSourceKeyH(dataSourceKey)) } + val key = DataSourceKeyH(dataSourceKey) + var bitmap by remember { mutableStateOf(bridge.bitmapDataSources.get(key)) } - LaunchedEffect(dataSourceKey) { - val data = bridge.bitmapDataSources.load(dataSourceKey) + if (oldKey != key) { + oldKey = key; + bitmap = bridge.bitmapDataSources.get(key) + } + + LaunchedEffect(key) { + val data = bridge.bitmapDataSources.load(key) bitmap = data } diff --git a/android/app/src/main/java/com/kutedev/easemusicplayer/core/UIBridge.kt b/android/app/src/main/java/com/kutedev/easemusicplayer/core/UIBridge.kt index be2689de..b7d5192e 100644 --- a/android/app/src/main/java/com/kutedev/easemusicplayer/core/UIBridge.kt +++ b/android/app/src/main/java/com/kutedev/easemusicplayer/core/UIBridge.kt @@ -18,9 +18,6 @@ import androidx.media3.session.SessionToken import androidx.navigation.NavHostController import com.google.common.util.concurrent.MoreExecutors import com.kutedev.easemusicplayer.utils.nextTickOnMain -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.launch import uniffi.ease_client.MainAction import uniffi.ease_client_android.IPermissionServiceForeign import uniffi.ease_client_android.IRouterServiceForeign @@ -45,13 +42,11 @@ import uniffi.ease_client.ViewAction import uniffi.ease_client.Widget import uniffi.ease_client.WidgetAction import uniffi.ease_client.WidgetActionType -import uniffi.ease_client_android.IAsyncAdapterForeign import uniffi.ease_client_android.apiBuildClient import uniffi.ease_client_android.apiDestroyClient import uniffi.ease_client_android.apiEmitViewAction import uniffi.ease_client_android.apiLoadAsset import uniffi.ease_client_android.apiStartClient -import uniffi.ease_client_shared.ArgInitializeApp import uniffi.ease_client_shared.DataSourceKey @@ -145,49 +140,54 @@ private class PermissionService : IPermissionServiceForeign { } } -class BitmapDataSources { - class K(key: DataSourceKey) { - private val _key = key; - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (other !is K) return false +class DataSourceKeyH(key: DataSourceKey) { + private val _key = key; - if (this._key is DataSourceKey.Music && other._key is DataSourceKey.Music) { - return this._key.id == other._key.id - } - if (this._key is DataSourceKey.Cover && other._key is DataSourceKey.Cover) { - return this._key.id == other._key.id - } - if (this._key is DataSourceKey.AnyEntry && other._key is DataSourceKey.AnyEntry) { - return this._key.entry.storageId == other._key.entry.storageId && this._key.entry.path == other._key.entry.path; - } - return false; - } + fun value(): DataSourceKey { + return this._key + } + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other !is DataSourceKeyH) return false - override fun hashCode(): Int { - return _key.hashCode() + if (this._key is DataSourceKey.Music && other._key is DataSourceKey.Music) { + return this._key.id == other._key.id } + if (this._key is DataSourceKey.Cover && other._key is DataSourceKey.Cover) { + return this._key.id == other._key.id + } + if (this._key is DataSourceKey.AnyEntry && other._key is DataSourceKey.AnyEntry) { + return this._key.entry.storageId == other._key.entry.storageId && this._key.entry.path == other._key.entry.path; + } + return false; + } + + override fun hashCode(): Int { + return _key.hashCode() } +} - private val _map = HashMap() +class BitmapDataSources { + private val _map = HashMap() - fun get(key: DataSourceKey): ImageBitmap? { - return this._map[K(key)] + fun get(key: DataSourceKeyH): ImageBitmap? { + return this._map[key] } - suspend fun load(key: DataSourceKey): ImageBitmap? { - val cached = this._map[K(key)] + suspend fun load(key: DataSourceKeyH): ImageBitmap? { + val cached = this._map[key] if (cached != null) { return cached } - val data = apiLoadAsset(key) + val data = apiLoadAsset(key.value()) val decoded = data?.let { val bitmap = android.graphics.BitmapFactory.decodeByteArray(it, 0, it.size) bitmap?.asImageBitmap() } - this._map[K(key)] = decoded + this._map[key] = decoded return decoded } }