Skip to content

Commit

Permalink
Support overriding current item notification
Browse files Browse the repository at this point in the history
  • Loading branch information
dcvz committed Aug 9, 2023
1 parent 48b586a commit fcbf10d
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 161 deletions.
6 changes: 3 additions & 3 deletions kotlin-audio-example/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ dependencies {
}

implementation(project(":kotlin-audio"))
implementation("androidx.core:core-ktx:1.9.0")
implementation("androidx.core:core-ktx:1.10.1")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.1")
implementation("androidx.activity:activity-compose:1.7.0")
implementation("androidx.activity:activity-compose:1.7.2")
implementation(platform("androidx.compose:compose-bom:2023.03.00"))
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-graphics")
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.compose.material3:material3")
implementation("androidx.compose.material3:material3:1.1.1")
implementation("androidx.compose.material:material-icons-extended")
implementation("io.coil-kt:coil-compose:2.4.0")
testImplementation("junit:junit:4.13.2")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import com.doublesymmetry.kotlinaudio.models.NotificationConfig
import com.doublesymmetry.kotlinaudio.models.RepeatMode
import com.doublesymmetry.kotlinaudio.models.PlayerConfig
import com.doublesymmetry.kotlinaudio.players.QueuedAudioPlayer
import com.example.kotlin_audio_example.ui.component.ActionBottomSheet
import com.example.kotlin_audio_example.ui.component.PlayerControls
import com.example.kotlin_audio_example.ui.component.TrackDisplay
import com.example.kotlin_audio_example.ui.theme.KotlinAudioTheme
Expand All @@ -51,6 +52,7 @@ import kotlin.time.Duration.Companion.seconds
class MainActivity : ComponentActivity() {
private lateinit var player: QueuedAudioPlayer

@OptIn(ExperimentalMaterial3Api::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Expand All @@ -76,25 +78,43 @@ class MainActivity : ComponentActivity() {
var duration by remember { mutableStateOf(0L) }
var isLive by remember { mutableStateOf(false) }

Inner(
title = title,
artist = artist,
artwork = artwork,
position = position,
duration = duration,
isLive = isLive,
onPrevious = { player.previous() },
onNext = { player.next() },
isPaused = state.value != AudioPlayerState.PLAYING,
onPlayPause = {
if (player.playerState == AudioPlayerState.PLAYING) {
player.pause()
} else {
player.play()
var showSheet by remember { mutableStateOf(false) }

if (showSheet) {
ActionBottomSheet(
onDismiss = { showSheet = false },
onRandomMetadata = {
val currentIndex = player.currentIndex
val track = tracks[currentIndex]
track.title = "Random Title - ${System.currentTimeMillis()}"
track.artwork = "https://picsum.photos/200/300"
player.replaceItem(currentIndex, track)
}
},
onSeek = { player.seek(it, TimeUnit.MILLISECONDS) }
)
)
}

KotlinAudioTheme {
MainScreen(
title = title,
artist = artist,
artwork = artwork,
position = position,
duration = duration,
isLive = isLive,
onPrevious = { player.previous() },
onNext = { player.next() },
isPaused = state.value != AudioPlayerState.PLAYING,
onTopBarAction = { showSheet = true },
onPlayPause = {
if (player.playerState == AudioPlayerState.PLAYING) {
player.pause()
} else {
player.play()
}
},
onSeek = { player.seek(it, TimeUnit.MILLISECONDS) }
)
}

LaunchedEffect(key1 = player, key2 = player.event.audioItemTransition, key3 = player.event.onPlayerActionTriggeredExternally) {
player.event.audioItemTransition
Expand Down Expand Up @@ -202,7 +222,7 @@ class MainActivity : ComponentActivity() {

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun Inner(
fun MainScreen(
title: String,
artist: String,
artwork: String,
Expand All @@ -212,64 +232,61 @@ fun Inner(
onPrevious: () -> Unit = {},
onNext: () -> Unit = {},
isPaused: Boolean,
onTopBarAction: () -> Unit = {},
onPlayPause: () -> Unit = {},
onSeek: (Long) -> Unit = {},
) {
KotlinAudioTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Column(modifier = Modifier.fillMaxSize()) {
TopAppBar(
title = {
Text(
text = "Kotlin Audio Example",
color = MaterialTheme.colorScheme.onPrimary
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Column(modifier = Modifier.fillMaxSize()) {
TopAppBar(
title = {
Text(
text = "Kotlin Audio Example",
color = MaterialTheme.colorScheme.onPrimary
)
},
actions = {
IconButton(onClick = onTopBarAction) {
Icon(
Icons.Default.MoreVert,
contentDescription = "Settings",
tint = MaterialTheme.colorScheme.onPrimary
)
},
actions = {
IconButton(onClick = {

}) {
Icon(
Icons.Default.MoreVert,
contentDescription = "Settings",
tint = MaterialTheme.colorScheme.onPrimary
)
}
},
colors = TopAppBarDefaults.smallTopAppBarColors(containerColor = MaterialTheme.colorScheme.primary)
)
TrackDisplay(
title = title,
artist = artist,
artwork = artwork,
position = position,
duration = duration,
isLive = isLive,
onSeek = onSeek,
modifier = Modifier.padding(top = 46.dp)
)
Spacer(modifier = Modifier.weight(1f))
PlayerControls(
onPrevious = onPrevious,
onNext = onNext,
isPaused = isPaused,
onPlayPause = onPlayPause,
modifier = Modifier
.fillMaxWidth()
.padding(bottom = 60.dp))
}
}
},
colors = TopAppBarDefaults.smallTopAppBarColors(containerColor = MaterialTheme.colorScheme.primary)
)
TrackDisplay(
title = title,
artist = artist,
artwork = artwork,
position = position,
duration = duration,
isLive = isLive,
onSeek = onSeek,
modifier = Modifier.padding(top = 46.dp)
)
Spacer(modifier = Modifier.weight(1f))
PlayerControls(
onPrevious = onPrevious,
onNext = onNext,
isPaused = isPaused,
onPlayPause = onPlayPause,
modifier = Modifier
.fillMaxWidth()
.padding(bottom = 60.dp)
)
}
}
}
@Preview(showBackground = true, uiMode = UI_MODE_NIGHT_YES)
@Composable
fun ContentPreview() {
KotlinAudioTheme {
Inner(
MainScreen(
title = "Title",
artist = "Artist",
artwork = "",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.example.kotlin_audio_example.ui.component

import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.BottomSheetDefaults
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ModalBottomSheet
import androidx.compose.material3.Text
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp

@Composable
@ExperimentalMaterial3Api
fun ActionBottomSheet(
onDismiss: () -> Unit,
onRandomMetadata: () -> Unit,
) {
val modalBottomSheetState = rememberModalBottomSheetState()

ModalBottomSheet(
onDismissRequest = { onDismiss() },
sheetState = modalBottomSheetState,
dragHandle = { BottomSheetDefaults.DragHandle() },
) {
InnerSheet(onRandomMetadata = onRandomMetadata)
}
}

@Composable
fun InnerSheet(onRandomMetadata: () -> Unit = {}) {
// Add a button to perform an action when clicked
Button(
onClick = onRandomMetadata,
modifier = Modifier
.padding(16.dp)
.fillMaxWidth()
) {
Text("Metadata: Update Title Randomly")
}
}

@Preview
@ExperimentalMaterial3Api
@Composable
fun ActionBottomSheetPreview() {
InnerSheet()
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ data class DefaultAudioItem(
override var artist: String? = null,
override var title: String? = null,
override var albumTitle: String? = null,
override val artwork: String? = null,
override var artwork: String? = null,
override val duration: Long = -1,
override val options: AudioItemOptions? = null,
) : AudioItem
Expand Down
Loading

0 comments on commit fcbf10d

Please sign in to comment.