Skip to content

Commit

Permalink
90% FullscreenPlayer
Browse files Browse the repository at this point in the history
  • Loading branch information
maxrave-dev committed Dec 15, 2024
1 parent b3489cc commit 315d51c
Show file tree
Hide file tree
Showing 10 changed files with 814 additions and 765 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/com/maxrave/simpmusic/extension/AllExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,8 @@ fun LocalDateTime.formatTimeAgo(context: Context): String {
}
}

fun formatDuration(duration: Long): String {
fun formatDuration(duration: Long, context: Context): String {
if (duration < 0L) return context.getString(R.string.na_na)
val minutes: Long = TimeUnit.MINUTES.convert(duration, TimeUnit.MILLISECONDS)
val seconds: Long = (
TimeUnit.SECONDS.convert(duration, TimeUnit.MILLISECONDS) -
Expand Down
23 changes: 21 additions & 2 deletions app/src/main/java/com/maxrave/simpmusic/extension/UIExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyListItemInfo
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.runtime.Composable
Expand Down Expand Up @@ -47,6 +48,7 @@ import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.core.graphics.ColorUtils
import com.kmpalette.palette.graphics.Palette
Expand Down Expand Up @@ -393,7 +395,10 @@ fun LazyListState.animateScrollAndCentralizeItem(
}

@Composable
fun KeepScreenOn() = AndroidView({ View(it).apply { keepScreenOn = true } })
fun KeepScreenOn() = AndroidView(
factory = { View(it).apply { keepScreenOn = true } },
modifier = Modifier.size(0.1.dp)
)

@Composable
fun LazyListState.isScrollingUp(): Boolean {
Expand Down Expand Up @@ -463,4 +468,18 @@ fun Palette?.getColorFromPalette(): Color {
}
}
return Color(ColorUtils.setAlphaComponent(startColor, 255))
}
}

fun Context.findActivity(): Activity? = when (this) {
is Activity -> this
is ContextWrapper -> baseContext.findActivity()
else -> null
}

@RequiresOptIn(
level = RequiresOptIn.Level.WARNING,
message = "This will be migrate to Compose. I use this to mark which fragment need to be migrate to Compose",
)
@Retention(AnnotationRetention.BINARY)
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
annotation class IntermediaryMigrateApi
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
Expand Down Expand Up @@ -255,6 +256,8 @@ fun FullscreenLyricsSheet(
color: Color = Color(0xFF242424),
onDismiss: () -> Unit,
) {
val context = LocalContext.current

val screenDataState by sharedViewModel.nowPlayingScreenData.collectAsState()
val timelineState by sharedViewModel.timeline.collectAsState()
val controllerState by sharedViewModel.controllerState.collectAsState()
Expand Down Expand Up @@ -528,18 +531,13 @@ fun FullscreenLyricsSheet(
.padding(horizontal = 40.dp),
) {
Text(
text =
if (timelineState.current >= 0L) {
formatDuration(timelineState.current)
} else {
stringResource(id = R.string.na_na)
},
text = formatDuration(timelineState.current, context),
style = typo.bodyMedium,
modifier = Modifier.weight(1f),
textAlign = TextAlign.Left,
)
Text(
text = if (timelineState.total >= 0L) formatDuration(timelineState.total) else stringResource(id = R.string.na_na),
text = formatDuration(timelineState.total, context),
style = typo.bodyMedium,
modifier = Modifier.weight(1f),
textAlign = TextAlign.Right,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.maxrave.simpmusic.ui.component

import android.util.Log
import android.view.TextureView
import androidx.compose.animation.Crossfade
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxHeight
Expand All @@ -20,6 +21,7 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.viewinterop.AndroidView
Expand All @@ -36,6 +38,11 @@ import androidx.media3.datasource.cache.CacheDataSource
import androidx.media3.datasource.cache.SimpleCache
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.exoplayer.source.DefaultMediaSourceFactory
import coil3.compose.AsyncImage
import coil3.request.CachePolicy
import coil3.request.ImageRequest
import coil3.request.crossfade
import coil3.toCoilUri
import com.maxrave.simpmusic.common.Config
import com.maxrave.simpmusic.extension.KeepScreenOn
import com.maxrave.simpmusic.extension.getScreenSizeInfo
Expand Down Expand Up @@ -168,14 +175,21 @@ fun MediaPlayerView(
mutableStateOf(false)
}

var showArtwork by rememberSaveable {
mutableStateOf(false)
}

val playerListener =
remember {
object : Player.Listener {
override fun onVideoSizeChanged(videoSize: VideoSize) {
super.onVideoSizeChanged(videoSize)
Log.w("MediaPlayerView", "Video size changed: ${videoSize.width} / ${videoSize.height}")
if (videoSize.width != 0 && videoSize.height != 0) {
showArtwork = false
videoRatio = videoSize.width.toFloat() / videoSize.height.toFloat()
} else if (videoSize.width == 0) {
showArtwork = true
}
}

Expand All @@ -195,24 +209,54 @@ fun MediaPlayerView(
LaunchedEffect(player) {
player.addListener(playerListener)
}

if (keepScreenOn) {
KeepScreenOn()
LaunchedEffect(true) {
player.videoSize.let {
if (it.width == 0) {
showArtwork = true
} else {
showArtwork = false
}
}
}

Box(modifier) {
AndroidView(
factory = { ctx ->
TextureView(ctx).also {
player.setVideoTextureView(it)
player.videoScalingMode = C.VIDEO_SCALING_MODE_DEFAULT
}
},
modifier =
Modifier
.wrapContentSize()
.aspectRatio(if (videoRatio > 0f) videoRatio else 16f / 9)
.align(Alignment.Center),
)
Box(
modifier,
contentAlignment = Alignment.Center
) {
if (keepScreenOn) {
KeepScreenOn()
}
Crossfade(showArtwork) {
if (it) {
AsyncImage(
model =
ImageRequest
.Builder(LocalContext.current)
.data(player.currentMediaItem?.mediaMetadata?.artworkUri?.toCoilUri())
.diskCachePolicy(CachePolicy.ENABLED)
.diskCacheKey(player.currentMediaItem?.mediaMetadata?.artworkUri?.toString())
.crossfade(550)
.build(),
contentDescription = null,
contentScale = ContentScale.FillHeight,
modifier = Modifier.fillMaxHeight()
.align(Alignment.Center),
)
} else {
AndroidView(
factory = { ctx ->
TextureView(ctx).also {
player.setVideoTextureView(it)
player.videoScalingMode = C.VIDEO_SCALING_MODE_DEFAULT
}
},
modifier =
Modifier
.wrapContentSize()
.aspectRatio(if (videoRatio > 0f) videoRatio else 16f / 9)
.align(Alignment.Center),
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
Expand Down Expand Up @@ -62,8 +64,10 @@ import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.rememberNestedScrollInteropConnection
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.KeyboardType
Expand Down Expand Up @@ -282,6 +286,8 @@ fun NowPlayingBottomSheet(
)
}

val nestedScrollInterop = rememberNestedScrollInteropConnection()

if (isBottomSheetVisible) {
ModalBottomSheet(
onDismissRequest = onDismiss,
Expand All @@ -295,12 +301,14 @@ fun NowPlayingBottomSheet(
modifier =
Modifier
.fillMaxWidth()
.wrapContentHeight(),
.wrapContentHeight()
.nestedScroll(nestedScrollInterop),
shape = RoundedCornerShape(topStart = 8.dp, topEnd = 8.dp),
colors = CardDefaults.cardColors().copy(containerColor = Color(0xFF242424)),
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier.verticalScroll(rememberScrollState())
) {
Spacer(modifier = Modifier.height(5.dp))
Card(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.appcompat.widget.SearchView
import androidx.compose.runtime.Composable
import androidx.compose.ui.viewinterop.AndroidViewBinding
import androidx.core.net.toUri
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
Expand Down Expand Up @@ -51,6 +53,7 @@ import com.maxrave.simpmusic.databinding.BottomSheetAddToAPlaylistBinding
import com.maxrave.simpmusic.databinding.BottomSheetNowPlayingBinding
import com.maxrave.simpmusic.databinding.BottomSheetSeeArtistOfNowPlayingBinding
import com.maxrave.simpmusic.databinding.FragmentSearchBinding
import com.maxrave.simpmusic.extension.IntermediaryMigrateApi
import com.maxrave.simpmusic.extension.connectArtists
import com.maxrave.simpmusic.extension.navigateSafe
import com.maxrave.simpmusic.extension.removeConflicts
Expand Down Expand Up @@ -1561,4 +1564,10 @@ class SearchFragment : Fragment() {
}
}
}
}

@Composable
@IntermediaryMigrateApi
fun SearchFragmentComposable() {
AndroidViewBinding(FragmentSearchBinding::inflate)
}
Loading

0 comments on commit 315d51c

Please sign in to comment.