Add home transition to music widget album cover

This commit is contained in:
MM20 2022-10-25 15:57:56 +02:00
parent de4e2c15db
commit f87a4ab257
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389
3 changed files with 55 additions and 6 deletions

View File

@ -38,6 +38,8 @@ interface MusicRepository {
val album: Flow<String?> val album: Flow<String?>
val albumArt: Flow<Bitmap?> val albumArt: Flow<Bitmap?>
val lastPlayerPackage: String?
fun next() fun next()
fun previous() fun previous()
fun pause() fun pause()
@ -62,7 +64,7 @@ internal class MusicRepositoryImpl(
context.getSharedPreferences(PREFS, Context.MODE_PRIVATE) context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
} }
private var lastPlayerPackage: String? = null override var lastPlayerPackage: String? = null
get() { get() {
if (field == null) { if (field == null) {
field = preferences.getString(PREFS_KEY_LAST_PLAYER, null) field = preferences.getString(PREFS_KEY_LAST_PLAYER, null)

View File

@ -11,6 +11,7 @@ import androidx.compose.foundation.Image
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.combinedClickable import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CornerSize
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Audiotrack import androidx.compose.material.icons.rounded.Audiotrack
import androidx.compose.material.icons.rounded.MusicNote import androidx.compose.material.icons.rounded.MusicNote
@ -23,10 +24,17 @@ import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.graphics.asImageBitmap import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.layout.boundsInWindow
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.text.style.TextOverflow
@ -36,6 +44,9 @@ import de.mm20.launcher2.music.PlaybackState
import de.mm20.launcher2.ui.R import de.mm20.launcher2.ui.R
import de.mm20.launcher2.ui.component.MissingPermissionBanner import de.mm20.launcher2.ui.component.MissingPermissionBanner
import de.mm20.launcher2.ui.ktx.conditional import de.mm20.launcher2.ui.ktx.conditional
import de.mm20.launcher2.ui.launcher.transitions.HandleHomeTransition
import de.mm20.launcher2.ui.launcher.transitions.HomeTransitionParams
import de.mm20.launcher2.ui.locals.LocalWindowSize
@Composable @Composable
fun MusicWidget() { fun MusicWidget() {
@ -155,14 +166,48 @@ fun MusicWidget() {
contentAlignment = Alignment.Center contentAlignment = Alignment.Center
) { ) {
if (albumArt != null) { if (albumArt != null) {
albumArt?.let { albumArt?.let { art ->
val windowSize = LocalWindowSize.current
var bounds by remember { mutableStateOf(Rect.Zero) }
Image( Image(
bitmap = it.asImageBitmap(), bitmap = art.asImageBitmap(),
modifier = Modifier modifier = Modifier
.fillMaxSize(), .fillMaxSize()
.onGloballyPositioned {
bounds = it.boundsInWindow()
},
contentDescription = null, contentDescription = null,
contentScale = ContentScale.Crop contentScale = ContentScale.Crop
) )
HandleHomeTransition {
if (
it.componentName.packageName == viewModel.currentPlayerPackage &&
bounds.right > 0f && bounds.left < windowSize.width &&
bounds.bottom > 0f && bounds.top < windowSize.height
) {
return@HandleHomeTransition HomeTransitionParams(
bounds
) { _, _ ->
val shape = MaterialTheme.shapes.medium
Image(
bitmap = art.asImageBitmap(),
modifier = Modifier
.size(144.dp)
.graphicsLayer {
this.clip = true
this.shape = shape.copy(
topStart = CornerSize(0f),
bottomStart = CornerSize(0f),
)
}
,
contentDescription = null,
contentScale = ContentScale.Crop
)
}
}
return@HandleHomeTransition null
}
} }
} else { } else {
Icon( Icon(

View File

@ -27,6 +27,9 @@ class MusicWidgetVM: ViewModel(), KoinComponent {
val hasPermission = permissionsManager.hasPermission(PermissionGroup.Notifications).asLiveData() val hasPermission = permissionsManager.hasPermission(PermissionGroup.Notifications).asLiveData()
val currentPlayerPackage
get() = musicRepository.lastPlayerPackage
fun skipPrevious() { fun skipPrevious() {
musicRepository.previous() musicRepository.previous()
} }
@ -41,8 +44,7 @@ class MusicWidgetVM: ViewModel(), KoinComponent {
fun openPlayer() { fun openPlayer() {
try { try {
musicRepository. musicRepository.openPlayer()?.send()
openPlayer()?.send()
} catch (e: PendingIntent.CanceledException) { } catch (e: PendingIntent.CanceledException) {
CrashReporter.logException(e) CrashReporter.logException(e)
} }