Skip to content

Commit

Permalink
feat/WIP: Improved the mediaplayer and added a little implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriel Fontán <[email protected]>
  • Loading branch information
BobbyESP committed Apr 17, 2024
1 parent 5c9d051 commit ee34999
Show file tree
Hide file tree
Showing 6 changed files with 343 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.media3.common.util.UnstableApi
import androidx.media3.exoplayer.ExoPlayer
Expand All @@ -23,6 +24,7 @@ class MediaNotificationManager @Inject constructor(
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

init {
Log.i("MediaNotificationManager", "init: creating notification manager")
createNotificationChannel()
}

Expand Down Expand Up @@ -58,12 +60,20 @@ class MediaNotificationManager @Inject constructor(

private fun startForegroundNotification(mediaSessionService: MediaSessionService) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Log.d(
"MediaNotificationManager",
"startForegroundNotification: creating notification for API >= 26"
)
val notification = Notification.Builder(context, NOTIFICATION_CHANNEL_ID)
.setCategory(Notification.CATEGORY_SERVICE)
.build()

mediaSessionService.startForeground(NOTIFICATION_ID, notification)
} else {
Log.d(
"MediaNotificationManager",
"startForegroundNotification: creating notification for API < 26"
)
val notification = NotificationCompat.Builder(context)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import com.bobbyesp.metadator.presentation.common.routesToNavigate
import com.bobbyesp.metadator.presentation.pages.MediaStorePageViewModel
import com.bobbyesp.metadator.presentation.pages.home.HomePage
import com.bobbyesp.metadator.presentation.pages.mediaplayer.MediaplayerPage
import com.bobbyesp.metadator.presentation.pages.mediaplayer.MediaplayerViewModel
import com.bobbyesp.metadator.presentation.pages.utilities.tageditor.ID3MetadataEditorPage
import com.bobbyesp.metadator.presentation.pages.utilities.tageditor.ID3MetadataEditorPageViewModel
import com.bobbyesp.ui.motion.animatedComposable
Expand Down Expand Up @@ -176,7 +177,8 @@ fun Navigator() {
route = Route.MediaplayerNavigator.route
) {
animatedComposable(Route.MediaplayerNavigator.Mediaplayer.route) {
MediaplayerPage()
val viewModel = hiltViewModel<MediaplayerViewModel>()
MediaplayerPage(viewModel)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,165 @@
package com.bobbyesp.metadator.presentation.components.others

import android.content.res.Configuration.UI_MODE_NIGHT_YES
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Pause
import androidx.compose.material.icons.rounded.PlayArrow
import androidx.compose.material3.Icon
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.NavigationBarDefaults
import androidx.compose.material3.surfaceColorAtElevation
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.bobbyesp.metadator.R
import com.bobbyesp.metadator.presentation.components.image.ArtworkAsyncImage
import com.bobbyesp.metadator.presentation.pages.mediaplayer.MediaplayerViewModel
import com.bobbyesp.metadator.presentation.theme.MetadatorTheme
import com.bobbyesp.model.Song
import com.bobbyesp.ui.components.bottomsheet.draggable.DraggableBottomSheet
import com.bobbyesp.ui.components.bottomsheet.draggable.DraggableBottomSheetState
import com.bobbyesp.ui.components.button.DynamicButton
import com.bobbyesp.ui.components.text.MarqueeText

@Composable
fun MediaplayerSheet(modifier: Modifier = Modifier, state: DraggableBottomSheetState) {
fun MediaplayerSheet(
modifier: Modifier = Modifier,
state: DraggableBottomSheetState,
viewModel: MediaplayerViewModel
) {

val songs = viewModel.pageViewState.value.playingSong
DraggableBottomSheet(
state = state,
collapsedContent = {

MediaplayerCollapsedContent(
queueSongs = listOf(songs ?: return@DraggableBottomSheet)
)
},
backgroundColor = MaterialTheme.colorScheme.surfaceColorAtElevation(NavigationBarDefaults.Elevation)
) {

}
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
private fun MediaplayerCollapsedContent(
modifier: Modifier = Modifier,
queueSongs: List<Song>
) {
val pagesCount = queueSongs.size

val pagerState = rememberPagerState(pageCount = {
pagesCount
})

HorizontalPager(modifier = modifier.fillMaxWidth(), state = pagerState) {
val song = queueSongs.getOrNull(it) ?: return@HorizontalPager
Column(
modifier = Modifier
.fillMaxWidth()
) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 4.dp, horizontal = 8.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(4.dp)
) {
ArtworkAsyncImage(
modifier = Modifier
.size(64.dp)
.padding(4.dp),
artworkPath = song.artworkPath
)
Column(
horizontalAlignment = Alignment.Start, modifier = Modifier
.padding(vertical = 8.dp, horizontal = 6.dp)
.weight(1f)
) {
MarqueeText(
text = song.title,
style = MaterialTheme.typography.bodyLarge,
fontWeight = FontWeight.Bold,
fontSize = 16.sp
)
MarqueeText(
text = song.artist,
style = MaterialTheme.typography.bodyMedium.copy(
color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.6f)
),
fontSize = 12.sp
)
}

DynamicButton(
modifier = Modifier
.size(42.dp)
.background(
MaterialTheme.colorScheme.primary,
MaterialTheme.shapes.small
)
.padding(4.dp),
icon = {
Icon(
imageVector = Icons.Rounded.PlayArrow,
contentDescription = stringResource(
id = R.string.play
)
)
}, icon2 = {
Icon(
imageVector = Icons.Rounded.Pause,
contentDescription = stringResource(
id = R.string.pause
)
)
}, isIcon1 = true
) {

}
}
LinearProgressIndicator(
modifier = Modifier.fillMaxWidth()
)
}
}
}

@Preview
@Preview(uiMode = UI_MODE_NIGHT_YES)
@Composable
private fun CollapsedContentPrev() {
MetadatorTheme {
MediaplayerCollapsedContent(
queueSongs = listOf(
Song(
id = 1,
title = "Bones",
artist = "Imagine Dragons",
album = "Mercury - Acts 1 & 2",
artworkPath = null,
duration = 100.0,
path = "path"
)
)
)
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,38 @@
package com.bobbyesp.metadator.presentation.pages.mediaplayer

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.bobbyesp.metadator.presentation.components.cards.songs.HorizontalSongCard
import com.bobbyesp.metadator.presentation.components.others.CollapsedPlayerHeight
import com.bobbyesp.metadator.presentation.components.others.MediaplayerSheet
import com.bobbyesp.metadator.presentation.components.others.PlayerAnimationSpec
import com.bobbyesp.ui.components.bottomsheet.draggable.rememberDraggableBottomSheetState
import com.bobbyesp.ui.components.pulltorefresh.rememberPullState
import my.nanihadesuka.compose.LazyColumnScrollbar
import my.nanihadesuka.compose.ScrollbarSelectionActionable

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun MediaplayerPage() {
fun MediaplayerPage(
viewModel: MediaplayerViewModel
) {
val mediaStoreLazyColumnState = rememberLazyListState()
val pullState = rememberPullState()

val songs = viewModel.songsFlow.collectAsStateWithLifecycle(initialValue = emptyList()).value

BoxWithConstraints(
modifier = Modifier.fillMaxSize()
) {
Expand All @@ -36,16 +51,36 @@ fun MediaplayerPage() {
.fillMaxSize()
.padding(it)
) {
val scope = rememberCoroutineScope()

Button(onClick = { mediaPlayerSheetState.expandSoft() }) {
Text(text = "Expand soft")
LazyColumnScrollbar(
listState = mediaStoreLazyColumnState,
thumbColor = MaterialTheme.colorScheme.onSurfaceVariant,
thumbSelectedColor = MaterialTheme.colorScheme.primary,
selectionActionable = ScrollbarSelectionActionable.WhenVisible,
) {
LazyColumn(
modifier = Modifier
.fillMaxSize()
.background(MaterialTheme.colorScheme.background),
state = mediaStoreLazyColumnState,
) {
items(count = songs.size,
key = { index -> songs[index].id },
contentType = { index -> songs[index].id.toString() }) { index ->
val song = songs[index]
HorizontalSongCard(song = song,
modifier = Modifier.animateItemPlacement(),
onClick = {
viewModel.playSingleSong(song)
})
}
}
}
}
}

MediaplayerSheet(
state = mediaPlayerSheetState
state = mediaPlayerSheetState,
viewModel = viewModel
)
}
}
Loading

0 comments on commit ee34999

Please sign in to comment.