From 488cac247893e07d91470e9255c826e83dad9906 Mon Sep 17 00:00:00 2001 From: Iliano101 Date: Wed, 18 Dec 2024 16:51:00 -0500 Subject: [PATCH] Fix context error and refactor --- .../simpmusic/ui/component/FullWidthItems.kt | 275 ++++++++++-------- .../simpmusic/ui/component/MediaPlayerView.kt | 9 +- .../ui/fragment/home/MoodFragment.kt | 4 - .../ui/screen/player/NowPlayingScreen.kt | 221 +++++++------- 4 files changed, 263 insertions(+), 246 deletions(-) diff --git a/app/src/main/java/com/maxrave/simpmusic/ui/component/FullWidthItems.kt b/app/src/main/java/com/maxrave/simpmusic/ui/component/FullWidthItems.kt index 54119280..5197a70b 100644 --- a/app/src/main/java/com/maxrave/simpmusic/ui/component/FullWidthItems.kt +++ b/app/src/main/java/com/maxrave/simpmusic/ui/component/FullWidthItems.kt @@ -80,10 +80,11 @@ fun SongFullWidthItems( ) Box( modifier = - modifier - .clickable { - onClickListener?.invoke(track?.videoId ?: songEntity?.videoId ?: "") - }.animateContentSize(), + modifier + .clickable { + onClickListener?.invoke(track?.videoId ?: songEntity?.videoId ?: "") + } + .animateContentSize(), ) { Row( Modifier @@ -102,24 +103,24 @@ fun SongFullWidthItems( val thumb = track?.thumbnails?.lastOrNull()?.url ?: songEntity?.thumbnails AsyncImage( model = - ImageRequest - .Builder(LocalContext.current) - .data(thumb) - .diskCachePolicy(CachePolicy.ENABLED) - .diskCacheKey(thumb) - .crossfade(true) - .build(), + ImageRequest + .Builder(LocalContext.current) + .data(thumb) + .diskCachePolicy(CachePolicy.ENABLED) + .diskCacheKey(thumb) + .crossfade(true) + .build(), placeholder = painterResource(R.drawable.holder), error = painterResource(R.drawable.holder), contentDescription = null, contentScale = ContentScale.FillWidth, modifier = - Modifier - .fillMaxSize(), + Modifier + .fillMaxSize(), ) } else { Text( - text = ((index ?: 0) + 1).toString(), + text = (index + 1).toString(), color = Color.White, style = typo.titleMedium, modifier = Modifier.align(Alignment.Center), @@ -139,50 +140,54 @@ fun SongFullWidthItems( maxLines = 1, color = Color.White, modifier = - Modifier - .fillMaxWidth() - .wrapContentHeight(align = Alignment.CenterVertically) - .basicMarquee( - iterations = Int.MAX_VALUE, - animationMode = MarqueeAnimationMode.Immediately, - ).focusable(), + Modifier + .fillMaxWidth() + .wrapContentHeight(align = Alignment.CenterVertically) + .basicMarquee( + iterations = Int.MAX_VALUE, + animationMode = MarqueeAnimationMode.Immediately, + ) + .focusable(), ) Row { AnimatedVisibility( visible = - if (songEntity != null || track != null) { - downloadState == DownloadState.STATE_DOWNLOADED - } else { - false - }, + if (songEntity != null || track != null) { + downloadState == DownloadState.STATE_DOWNLOADED + } else { + false + }, ) { Row { Icon( painter = painterResource(id = R.drawable.download_for_offline_white), tint = Color.White, contentDescription = "", - modifier = Modifier.size(20.dp).padding(2.dp), + modifier = Modifier + .size(20.dp) + .padding(2.dp), ) Spacer(modifier = Modifier.width(10.dp)) } } Text( text = - ( - track?.artists?.toListName()?.connectArtists() - ?: songEntity?.artistName?.connectArtists() + ( + track?.artists?.toListName()?.connectArtists() + ?: songEntity?.artistName?.connectArtists() ) ?: "", style = typo.bodyMedium, maxLines = 1, color = Color(0xC4FFFFFF), modifier = - Modifier - .fillMaxWidth() - .wrapContentHeight(align = Alignment.CenterVertically) - .basicMarquee( - iterations = Int.MAX_VALUE, - animationMode = MarqueeAnimationMode.Immediately, - ).focusable(), + Modifier + .fillMaxWidth() + .wrapContentHeight(align = Alignment.CenterVertically) + .basicMarquee( + iterations = Int.MAX_VALUE, + animationMode = MarqueeAnimationMode.Immediately, + ) + .focusable(), ) } } @@ -206,12 +211,13 @@ fun SuggestItems( ) Box( modifier = - Modifier - .clickable { - if (onClickListener != null) { - onClickListener() - } - }.animateContentSize(), + Modifier + .clickable { + if (onClickListener != null) { + onClickListener() + } + } + .animateContentSize(), ) { Row( Modifier @@ -226,21 +232,21 @@ fun SuggestItems( val thumb = track.thumbnails?.lastOrNull()?.url AsyncImage( model = - ImageRequest - .Builder(LocalContext.current) - .data(thumb) - .diskCachePolicy(CachePolicy.ENABLED) - .diskCacheKey(thumb) - .crossfade(true) - .build(), + ImageRequest + .Builder(LocalContext.current) + .data(thumb) + .diskCachePolicy(CachePolicy.ENABLED) + .diskCacheKey(thumb) + .crossfade(true) + .build(), placeholder = painterResource(R.drawable.holder), error = painterResource(R.drawable.holder), contentDescription = null, contentScale = ContentScale.FillWidth, modifier = - Modifier - .wrapContentHeight() - .fillMaxWidth(), + Modifier + .wrapContentHeight() + .fillMaxWidth(), ) } } @@ -257,38 +263,40 @@ fun SuggestItems( maxLines = 1, color = Color.White, modifier = - Modifier - .fillMaxWidth() - .wrapContentHeight(align = Alignment.CenterVertically) - .basicMarquee( - iterations = Int.MAX_VALUE, - animationMode = MarqueeAnimationMode.Immediately, - ).focusable(), + Modifier + .fillMaxWidth() + .wrapContentHeight(align = Alignment.CenterVertically) + .basicMarquee( + iterations = Int.MAX_VALUE, + animationMode = MarqueeAnimationMode.Immediately, + ) + .focusable(), ) Text( text = - ( - track.artists?.toListName()?.connectArtists() + ( + track.artists?.toListName()?.connectArtists() ) ?: "", style = typo.bodySmall, maxLines = 1, color = Color(0xC4FFFFFF), modifier = - Modifier - .fillMaxWidth() - .wrapContentHeight(align = Alignment.CenterVertically) - .basicMarquee( - iterations = Int.MAX_VALUE, - animationMode = MarqueeAnimationMode.Immediately, - ).focusable(), + Modifier + .fillMaxWidth() + .wrapContentHeight(align = Alignment.CenterVertically) + .basicMarquee( + iterations = Int.MAX_VALUE, + animationMode = MarqueeAnimationMode.Immediately, + ) + .focusable(), ) } RippleIconButton( resId = R.drawable.baseline_add_24, fillMaxSize = false, onClick = - onAddClickListener ?: { - }, + onAddClickListener ?: { + }, ) } } @@ -302,10 +310,11 @@ fun PlaylistFullWidthItems( ) { Box( modifier = - modifier - .clickable { - onClickListener?.invoke() - }.animateContentSize(), + modifier + .clickable { + onClickListener?.invoke() + } + .animateContentSize(), ) { var title = "" var thumb = "" @@ -326,16 +335,19 @@ fun PlaylistFullWidthItems( secondSubtitle = data.artistName?.connectArtists() ?: "" thirdRowSubtitle = data.year } + is PlaylistEntity -> { title = data.title thumb = data.thumbnails secondSubtitle = data.author ?: "" } + is LocalPlaylistEntity -> { title = data.title thumb = data.thumbnail ?: "" secondSubtitle = stringResource(R.string.you) } + is PlaylistsResult -> { title = data.title thumb = data.thumbnails.lastOrNull()?.url ?: "" @@ -351,20 +363,20 @@ fun PlaylistFullWidthItems( Box(modifier = Modifier.size(50.dp)) { AsyncImage( model = - ImageRequest - .Builder(LocalContext.current) - .data(thumb) - .diskCachePolicy(CachePolicy.ENABLED) - .diskCacheKey(thumb) - .crossfade(true) - .build(), + ImageRequest + .Builder(LocalContext.current) + .data(thumb) + .diskCachePolicy(CachePolicy.ENABLED) + .diskCacheKey(thumb) + .crossfade(true) + .build(), placeholder = painterResource(R.drawable.holder), error = painterResource(R.drawable.holder), contentDescription = null, contentScale = ContentScale.FillWidth, modifier = - Modifier - .fillMaxSize(), + Modifier + .fillMaxSize(), ) } Column( @@ -379,13 +391,14 @@ fun PlaylistFullWidthItems( maxLines = 1, color = Color.White, modifier = - Modifier - .fillMaxWidth() - .wrapContentHeight(align = Alignment.CenterVertically) - .basicMarquee( - iterations = Int.MAX_VALUE, - animationMode = MarqueeAnimationMode.Immediately, - ).focusable(), + Modifier + .fillMaxWidth() + .wrapContentHeight(align = Alignment.CenterVertically) + .basicMarquee( + iterations = Int.MAX_VALUE, + animationMode = MarqueeAnimationMode.Immediately, + ) + .focusable(), ) Text( @@ -394,13 +407,14 @@ fun PlaylistFullWidthItems( maxLines = 1, color = Color(0xC4FFFFFF), modifier = - Modifier - .fillMaxWidth() - .wrapContentHeight(align = Alignment.CenterVertically) - .basicMarquee( - iterations = Int.MAX_VALUE, - animationMode = MarqueeAnimationMode.Immediately, - ).focusable(), + Modifier + .fillMaxWidth() + .wrapContentHeight(align = Alignment.CenterVertically) + .basicMarquee( + iterations = Int.MAX_VALUE, + animationMode = MarqueeAnimationMode.Immediately, + ) + .focusable(), ) if (thirdRowSubtitle != null) { @@ -410,13 +424,14 @@ fun PlaylistFullWidthItems( maxLines = 1, color = Color(0xC4FFFFFF), modifier = - Modifier - .fillMaxWidth() - .wrapContentHeight(align = Alignment.CenterVertically) - .basicMarquee( - iterations = Int.MAX_VALUE, - animationMode = MarqueeAnimationMode.Immediately, - ).focusable(), + Modifier + .fillMaxWidth() + .wrapContentHeight(align = Alignment.CenterVertically) + .basicMarquee( + iterations = Int.MAX_VALUE, + animationMode = MarqueeAnimationMode.Immediately, + ) + .focusable(), ) } } @@ -445,21 +460,21 @@ fun ArtistFullWidthItems( Box(modifier = Modifier.size(50.dp)) { AsyncImage( model = - ImageRequest - .Builder(LocalContext.current) - .data(data.thumbnails) - .diskCachePolicy(CachePolicy.ENABLED) - .diskCacheKey(data.thumbnails) - .crossfade(true) - .build(), + ImageRequest + .Builder(LocalContext.current) + .data(data.thumbnails) + .diskCachePolicy(CachePolicy.ENABLED) + .diskCacheKey(data.thumbnails) + .crossfade(true) + .build(), placeholder = painterResource(R.drawable.holder), error = painterResource(R.drawable.holder), contentDescription = null, contentScale = ContentScale.FillHeight, modifier = - Modifier - .fillMaxSize() - .clip(CircleShape), + Modifier + .fillMaxSize() + .clip(CircleShape), ) } Column( @@ -474,13 +489,14 @@ fun ArtistFullWidthItems( maxLines = 1, color = Color.White, modifier = - Modifier - .fillMaxWidth() - .wrapContentHeight(align = Alignment.CenterVertically) - .basicMarquee( - iterations = Int.MAX_VALUE, - animationMode = MarqueeAnimationMode.Immediately, - ).focusable(), + Modifier + .fillMaxWidth() + .wrapContentHeight(align = Alignment.CenterVertically) + .basicMarquee( + iterations = Int.MAX_VALUE, + animationMode = MarqueeAnimationMode.Immediately, + ) + .focusable(), ) Text( @@ -489,13 +505,14 @@ fun ArtistFullWidthItems( maxLines = 1, color = Color(0xC4FFFFFF), modifier = - Modifier - .fillMaxWidth() - .wrapContentHeight(align = Alignment.CenterVertically) - .basicMarquee( - iterations = Int.MAX_VALUE, - animationMode = MarqueeAnimationMode.Immediately, - ).focusable(), + Modifier + .fillMaxWidth() + .wrapContentHeight(align = Alignment.CenterVertically) + .basicMarquee( + iterations = Int.MAX_VALUE, + animationMode = MarqueeAnimationMode.Immediately, + ) + .focusable(), ) } } diff --git a/app/src/main/java/com/maxrave/simpmusic/ui/component/MediaPlayerView.kt b/app/src/main/java/com/maxrave/simpmusic/ui/component/MediaPlayerView.kt index cedb600a..b69f573b 100644 --- a/app/src/main/java/com/maxrave/simpmusic/ui/component/MediaPlayerView.kt +++ b/app/src/main/java/com/maxrave/simpmusic/ui/component/MediaPlayerView.kt @@ -220,11 +220,7 @@ fun MediaPlayerView( } LaunchedEffect(true) { player.videoSize.let { - if (it.width == 0) { - showArtwork = true - } else { - showArtwork = false - } + showArtwork = it.width == 0 } } @@ -254,7 +250,8 @@ fun MediaPlayerView( .build(), contentDescription = null, contentScale = ContentScale.FillHeight, - modifier = Modifier.fillMaxHeight() + modifier = Modifier + .fillMaxHeight() .align(Alignment.Center), ) } else { diff --git a/app/src/main/java/com/maxrave/simpmusic/ui/fragment/home/MoodFragment.kt b/app/src/main/java/com/maxrave/simpmusic/ui/fragment/home/MoodFragment.kt index 2f800c71..350ded46 100644 --- a/app/src/main/java/com/maxrave/simpmusic/ui/fragment/home/MoodFragment.kt +++ b/app/src/main/java/com/maxrave/simpmusic/ui/fragment/home/MoodFragment.kt @@ -36,10 +36,6 @@ class MoodFragment : Fragment() { } } - override fun onDestroyView() { - super.onDestroyView() - } - override fun onViewCreated( view: View, savedInstanceState: Bundle?, diff --git a/app/src/main/java/com/maxrave/simpmusic/ui/screen/player/NowPlayingScreen.kt b/app/src/main/java/com/maxrave/simpmusic/ui/screen/player/NowPlayingScreen.kt index e2d1e20e..b31ff81b 100644 --- a/app/src/main/java/com/maxrave/simpmusic/ui/screen/player/NowPlayingScreen.kt +++ b/app/src/main/java/com/maxrave/simpmusic/ui/screen/player/NowPlayingScreen.kt @@ -154,7 +154,7 @@ fun NowPlayingScreen( @Suppress("ktlint:standard:property-naming") val TAG = "NowPlayingScreen" - LocalContext.current + val context = LocalContext.current val localDensity = LocalDensity.current val uriHandler = LocalUriHandler.current rememberCoroutineScope() @@ -222,7 +222,7 @@ fun NowPlayingScreen( } LaunchedEffect(true) { - val activity = context.findActivity() ?: return@LaunchedEffect + val activity = context.findActivity() val bottom = activity.findViewById(R.id.bottom_navigation_view) val miniplayer = activity.findViewById(R.id.miniplayer) if (bottom.visibility != View.GONE || miniplayer.visibility != View.GONE) { @@ -425,13 +425,13 @@ fun NowPlayingScreen( } else if (isVideo == false) { AsyncImage( model = - ImageRequest - .Builder(LocalContext.current) - .data(screenDataState.canvasData?.url) - .diskCachePolicy(CachePolicy.ENABLED) - .diskCacheKey(screenDataState.canvasData?.url) - .crossfade(550) - .build(), + ImageRequest + .Builder(LocalContext.current) + .data(screenDataState.canvasData?.url) + .diskCachePolicy(CachePolicy.ENABLED) + .diskCacheKey(screenDataState.canvasData?.url) + .crossfade(550) + .build(), contentDescription = null, modifier = Modifier.fillMaxSize(), ) @@ -500,13 +500,14 @@ fun NowPlayingScreen( textAlign = TextAlign.Center, maxLines = 1, modifier = - Modifier - .fillMaxWidth() - .wrapContentHeight(align = Alignment.CenterVertically) - .basicMarquee( - iterations = Int.MAX_VALUE, - animationMode = MarqueeAnimationMode.Immediately, - ).focusable(), + Modifier + .fillMaxWidth() + .wrapContentHeight(align = Alignment.CenterVertically) + .basicMarquee( + iterations = Int.MAX_VALUE, + animationMode = MarqueeAnimationMode.Immediately, + ) + .focusable(), ) } }, @@ -578,13 +579,13 @@ fun NowPlayingScreen( // IS SONG => Show Artwork AsyncImage( model = - ImageRequest - .Builder(LocalContext.current) - .data(screenDataState.thumbnailURL) - .diskCachePolicy(CachePolicy.ENABLED) - .diskCacheKey(screenDataState.thumbnailURL + "BIGGER") - .crossfade(550) - .build(), + ImageRequest + .Builder(LocalContext.current) + .data(screenDataState.thumbnailURL) + .diskCachePolicy(CachePolicy.ENABLED) + .diskCacheKey(screenDataState.thumbnailURL + "BIGGER") + .crossfade(550) + .build(), contentDescription = "", onSuccess = { bitmap = @@ -595,16 +596,18 @@ fun NowPlayingScreen( contentScale = ContentScale.Crop, placeholder = painterResource(id = R.drawable.holder), modifier = - Modifier - .align(Alignment.Center) - .fillMaxWidth() - .aspectRatio( - if (!screenDataState.isVideo) 1f else 16f / 9, - ).clip( - RoundedCornerShape(8.dp), - ).alpha( - if (!screenDataState.isVideo || !shouldShowVideo) 1f else 0f, - ), + Modifier + .align(Alignment.Center) + .fillMaxWidth() + .aspectRatio( + if (!screenDataState.isVideo) 1f else 16f / 9, + ) + .clip( + RoundedCornerShape(8.dp), + ) + .alpha( + if (!screenDataState.isVideo || !shouldShowVideo) 1f else 0f, + ), ) // IS VIDEO => Show Video @@ -777,13 +780,14 @@ fun NowPlayingScreen( maxLines = 1, color = Color.White, modifier = - Modifier - .fillMaxWidth() - .wrapContentHeight(align = Alignment.CenterVertically) - .basicMarquee( - iterations = Int.MAX_VALUE, - animationMode = MarqueeAnimationMode.Immediately, - ).focusable(), + Modifier + .fillMaxWidth() + .wrapContentHeight(align = Alignment.CenterVertically) + .basicMarquee( + iterations = Int.MAX_VALUE, + animationMode = MarqueeAnimationMode.Immediately, + ) + .focusable(), ) Spacer(modifier = Modifier.height(3.dp)) Text( @@ -791,19 +795,20 @@ fun NowPlayingScreen( style = typo.bodyMedium, maxLines = 1, modifier = - Modifier - .fillMaxWidth() - .wrapContentHeight(align = Alignment.CenterVertically) - .basicMarquee( - iterations = Int.MAX_VALUE, - animationMode = MarqueeAnimationMode.Immediately, - ).focusable() - .clickable { - navController.navigateSafe( - R.id.action_global_artistFragment, - bundleOf("channelId" to screenDataState.songInfoData?.authorId), - ) - }, + Modifier + .fillMaxWidth() + .wrapContentHeight(align = Alignment.CenterVertically) + .basicMarquee( + iterations = Int.MAX_VALUE, + animationMode = MarqueeAnimationMode.Immediately, + ) + .focusable() + .clickable { + navController.navigateSafe( + R.id.action_global_artistFragment, + bundleOf("channelId" to screenDataState.songInfoData?.authorId), + ) + }, ) } Spacer(modifier = Modifier.size(10.dp)) @@ -831,14 +836,15 @@ fun NowPlayingScreen( CompositionLocalProvider(LocalMinimumInteractiveComponentSize provides Dp.Unspecified) { LinearProgressIndicator( modifier = - Modifier - .fillMaxWidth() - .height(4.dp) - .padding( - horizontal = 3.dp, - ).clip( - RoundedCornerShape(8.dp), - ), + Modifier + .fillMaxWidth() + .height(4.dp) + .padding( + horizontal = 3.dp, + ) + .clip( + RoundedCornerShape(8.dp), + ), color = Color.Gray, trackColor = Color.DarkGray, strokeCap = StrokeCap.Round, @@ -849,14 +855,15 @@ fun NowPlayingScreen( LinearProgressIndicator( progress = { timelineState.bufferedPercent.toFloat() / 100 }, modifier = - Modifier - .fillMaxWidth() - .height(4.dp) - .padding( - horizontal = 3.dp, - ).clip( - RoundedCornerShape(8.dp), - ), + Modifier + .fillMaxWidth() + .height(4.dp) + .padding( + horizontal = 3.dp, + ) + .clip( + RoundedCornerShape(8.dp), + ), color = Color.Gray, trackColor = Color.DarkGray, strokeCap = StrokeCap.Round, @@ -885,16 +892,16 @@ fun NowPlayingScreen( track = { sliderState -> SliderDefaults.Track( modifier = - Modifier - .height(5.dp), + Modifier + .height(5.dp), enabled = true, sliderState = sliderState, colors = - SliderDefaults.colors().copy( - thumbColor = Color.White, - activeTrackColor = Color.White, - inactiveTrackColor = Color.Transparent, - ), + SliderDefaults.colors().copy( + thumbColor = Color.White, + activeTrackColor = Color.White, + inactiveTrackColor = Color.Transparent, + ), thumbTrackGapSize = 0.dp, drawTick = { _, _ -> }, drawStopIndicator = null, @@ -903,12 +910,12 @@ fun NowPlayingScreen( thumb = { SliderDefaults.Thumb( modifier = - Modifier - .height(18.dp) - .width(8.dp) - .padding( - vertical = 4.dp, - ), + Modifier + .height(18.dp) + .width(8.dp) + .padding( + vertical = 4.dp, + ), thumbSize = DpSize(8.dp, 8.dp), interactionSource = remember { @@ -1183,12 +1190,12 @@ fun NowPlayingScreen( } else { IconButton( modifier = - Modifier - .size(24.dp) - .aspectRatio(1f) - .clip( - CircleShape, - ), + Modifier + .size(24.dp) + .aspectRatio(1f) + .clip( + CircleShape, + ), onClick = { sharedViewModel.addToYouTubeLiked() }, @@ -1254,23 +1261,23 @@ fun NowPlayingScreen( val thumb = screenDataState.songInfoData?.authorThumbnail AsyncImage( model = - ImageRequest - .Builder(LocalContext.current) - .data(thumb) - .diskCachePolicy(CachePolicy.ENABLED) - .diskCacheKey(thumb) - .crossfade(550) - .build(), + ImageRequest + .Builder(LocalContext.current) + .data(thumb) + .diskCachePolicy(CachePolicy.ENABLED) + .diskCacheKey(thumb) + .crossfade(550) + .build(), placeholder = painterResource(R.drawable.holder), error = painterResource(R.drawable.holder), contentDescription = null, contentScale = ContentScale.Crop, modifier = - Modifier - .size(42.dp) - .clip( - CircleShape, - ), + Modifier + .size(42.dp) + .clip( + CircleShape, + ), ) Spacer(modifier = Modifier.size(12.dp)) Text( @@ -1436,13 +1443,13 @@ fun NowPlayingScreen( val thumb = screenDataState.songInfoData?.authorThumbnail AsyncImage( model = - ImageRequest - .Builder(LocalContext.current) - .data(thumb) - .diskCachePolicy(CachePolicy.ENABLED) - .diskCacheKey(thumb) - .crossfade(550) - .build(), + ImageRequest + .Builder(LocalContext.current) + .data(thumb) + .diskCachePolicy(CachePolicy.ENABLED) + .diskCacheKey(thumb) + .crossfade(550) + .build(), placeholder = painterResource(R.drawable.holder_video), error = painterResource(R.drawable.holder_video), contentDescription = null,