Skip to content

Commit

Permalink
feat(android): record spotify listen history (#888)
Browse files Browse the repository at this point in the history
  • Loading branch information
lydavid committed May 2, 2024
1 parent 3e9c3e6 commit b867b39
Show file tree
Hide file tree
Showing 51 changed files with 1,739 additions and 1,097 deletions.
3 changes: 3 additions & 0 deletions android/feature/spotify/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies {
implementation(projects.core.models)
implementation(projects.ui.core)
implementation(projects.ui.common)
implementation(projects.domain)
implementation(projects.strings)
implementation(libs.androidx.core)
implementation(libs.circuit.foundation)
Expand All @@ -21,6 +22,8 @@ dependencies {
implementation(libs.compose.material3)
implementation(libs.compose.ui)
implementation(libs.compose.ui.preview)
implementation(libs.paging.compose)
implementation(libs.paging.common)

debugImplementation(libs.compose.ui.tooling)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.ui.platform.LocalContext
import androidx.core.content.ContextCompat
import kotlinx.datetime.Instant
import ly.david.musicsearch.core.models.history.SpotifyHistory

private object BroadcastTypes {
private const val SPOTIFY_PACKAGE = "com.spotify.music"
Expand All @@ -18,7 +20,7 @@ private object BroadcastTypes {

@Composable
internal fun SpotifyBroadcastReceiver(
onMetadataChange: (SpotifyMetadata) -> Unit,
onMetadataChange: (SpotifyHistory) -> Unit,
) {
// Grab the current context in this part of the UI tree
val context = LocalContext.current
Expand All @@ -45,28 +47,29 @@ internal fun SpotifyBroadcastReceiver(
)
when (intent.action) {
BroadcastTypes.METADATA_CHANGED -> {
val trackId = intent.getStringExtra("id")
val trackId = intent.getStringExtra("id") ?: return
val artistName = intent.getStringExtra("artist")
val albumName = intent.getStringExtra("album")
val trackName = intent.getStringExtra("track")
val trackLengthInSec = intent.getIntExtra(
val trackLengthMilliseconds = intent.getIntExtra(
"length",
0,
)
onMetadataChange(
SpotifyMetadata(
SpotifyHistory(
trackId = trackId,
artistName = artistName,
albumName = albumName,
trackName = trackName,
trackLengthInSec = trackLengthInSec,
timeSentInMs = timeSentInMs,
trackLengthMilliseconds = trackLengthMilliseconds,
lastListened = Instant.fromEpochMilliseconds(timeSentInMs),
),
)
}

BroadcastTypes.PLAYBACK_STATE_CHANGED -> {
// Nothing at the moment.
// val playing = intent.getBooleanExtra("playing", false)
// val playbackPosition = intent.getIntExtra("playbackPosition", 0)
}

BroadcastTypes.QUEUE_CHANGED -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,32 @@ package ly.david.musicsearch.android.feature.spotify
import com.slack.circuit.runtime.presenter.Presenter
import com.slack.circuit.runtime.ui.Ui
import com.slack.circuit.runtime.ui.ui
import ly.david.ui.common.screen.SpotifyPlayingScreen
import ly.david.musicsearch.android.feature.spotify.history.SpotifyHistoryPresenter
import ly.david.musicsearch.android.feature.spotify.history.SpotifyHistoryUi
import ly.david.musicsearch.android.feature.spotify.history.SpotifyUiState
import ly.david.ui.common.screen.SpotifyHistoryScreen
import org.koin.core.qualifier.named
import org.koin.dsl.module

val spotifyFeatureModule = module {
single(named("SpotifyPlayingScreen")) {
single(named("SpotifyHistoryScreen")) {
Presenter.Factory { screen, navigator, _ ->
when (screen) {
is SpotifyPlayingScreen -> SpotifyPresenter(
is SpotifyHistoryScreen -> SpotifyHistoryPresenter(
navigator = navigator,
spotifyHistoryRepository = get(),
)

else -> null
}
}
}
single(named("SpotifyPlayingScreen")) {
single(named("SpotifyHistoryScreen")) {
Ui.Factory { screen, _ ->
when (screen) {
is SpotifyPlayingScreen -> {
is SpotifyHistoryScreen -> {
ui<SpotifyUiState> { state, modifier ->
SpotifyUi(
SpotifyHistoryUi(
state = state,
modifier = modifier,
)
Expand Down

This file was deleted.

This file was deleted.

0 comments on commit b867b39

Please sign in to comment.