Skip to content

Commit

Permalink
fix: EaseImage sync load image
Browse files Browse the repository at this point in the history
  • Loading branch information
hpp2334 committed Dec 2, 2024
1 parent 7380598 commit 2003a42
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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


Expand Down Expand Up @@ -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<K, ImageBitmap?>()
class BitmapDataSources {
private val _map = HashMap<DataSourceKeyH, ImageBitmap?>()

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
}
}
Expand Down

0 comments on commit 2003a42

Please sign in to comment.