Skip to content

Commit

Permalink
Playlist screen refactoring (#98)
Browse files Browse the repository at this point in the history
Closes #85, closes #86
  • Loading branch information
siper committed Jun 3, 2024
1 parent 8f304e6 commit 6899d9b
Show file tree
Hide file tree
Showing 12 changed files with 403 additions and 91 deletions.
74 changes: 74 additions & 0 deletions core/ui/src/main/java/ru/stersh/youamp/core/ui/Error.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package ru.stersh.youamp.core.ui

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
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.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
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.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp


@Composable
fun ErrorLayout(
onRetry: () -> Unit,
modifier: Modifier = Modifier
) {
Box(modifier = modifier.fillMaxSize()) {
Error(
onRetry = onRetry,
modifier = Modifier.align(Alignment.Center)
)
}
}

@Composable
fun Error(
onRetry: () -> Unit,
modifier: Modifier = Modifier
) {
Column(
modifier = modifier.padding(horizontal = 48.dp),
verticalArrangement = Arrangement.spacedBy(16.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(
textAlign = TextAlign.Center,
style = MaterialTheme.typography.titleLarge,
text = stringResource(R.string.error_title)
)

Text(
textAlign = TextAlign.Center,
style = MaterialTheme.typography.bodyLarge,
color = MaterialTheme.colorScheme.outline,
text = stringResource(R.string.error_message)
)

Button(onClick = onRetry) {
Text(text = stringResource(R.string.error_button_title))
}
}
}

@Composable
@Preview
private fun ErrorLayoutPreview() {
YouAmpPlayerTheme {
Scaffold {
ErrorLayout(
onRetry = {},
modifier = Modifier.padding(it)
)
}
}
}
3 changes: 3 additions & 0 deletions core/ui/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
<string name="shuffle_title">Shuffle</string>
<string name="play_all_title">Play all</string>
<string name="placeholder_image_description">Placeholder image</string>
<string name="error_button_title">Try again</string>
<string name="error_message">Try again. If the error occurs several times, contact the developer.</string>
<string name="error_title">Oops, something went wrong</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package ru.stersh.youamp.feature.playlist

import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.dsl.module
import ru.stersh.youamp.feature.playlist.data.PlaylistInfoRepositoryImpl
import ru.stersh.youamp.feature.playlist.domain.PlaylistInfoRepository
import ru.stersh.youamp.feature.playlist.ui.PlaylistInfoViewModel

val playlistInfoModule = module {
single<PlaylistInfoRepository> { PlaylistInfoRepositoryImpl(get(), get(), get()) }
viewModel { (id: String) ->
PlaylistInfoViewModel(id, get(), get(), get(), get(), get())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package ru.stersh.youamp.feature.playlist.data

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flow
import ru.stersh.youamp.core.api.Playlist
import ru.stersh.youamp.core.api.provider.ApiProvider
import ru.stersh.youamp.feature.playlist.domain.PlaylistInfo
import ru.stersh.youamp.feature.playlist.domain.PlaylistInfoRepository
import ru.stersh.youamp.feature.playlist.domain.PlaylistSong
import ru.stersh.youamp.shared.player.metadata.CurrentSongInfoStore
import ru.stersh.youamp.shared.player.state.PlayStateStore

internal class PlaylistInfoRepositoryImpl(
private val playerStateStore: PlayStateStore,
private val apiProvider: ApiProvider,
private val currentSongInfoStore: CurrentSongInfoStore,
) : PlaylistInfoRepository {

override fun getPlaylistInfo(playlistId: String): Flow<PlaylistInfo> {
return flowPlaylist(playlistId).flatMapLatest { playlist ->
combine(
currentSongInfoStore.getCurrentSongInfo(),
playerStateStore.isPlaying()
) { currentSongInfo, isPlaying ->
val api = apiProvider.getApi()
return@combine PlaylistInfo(
title = playlist.name,
artworkUrl = api.getCoverArtUrl(playlist.coverArt),
songs = playlist
.entry
.orEmpty()
.map { entry ->
val isCurrent = currentSongInfo?.id == entry.id
PlaylistSong(
id = entry.id,
title = entry.title,
artist = entry.artist,
artworkUrl = entry.coverArt?.let { api.getCoverArtUrl(it) },
isCurrent = isCurrent,
isPlaying = isCurrent && isPlaying
)
}
)
}
}
}

private fun flowPlaylist(playlistId: String): Flow<Playlist> {
return flow {
val api = apiProvider.getApi()
emit(api.getPlaylist(playlistId))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.stersh.youamp.feature.playlist.domain

internal data class PlaylistInfo(
val artworkUrl: String?,
val title: String,
val songs: List<PlaylistSong>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.stersh.youamp.feature.playlist.domain

import kotlinx.coroutines.flow.Flow

internal interface PlaylistInfoRepository {
fun getPlaylistInfo(playlistId: String): Flow<PlaylistInfo>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ru.stersh.youamp.feature.playlist.domain

internal data class PlaylistSong(
val id: String,
val title: String,
val artist: String?,
val artworkUrl: String?,
val isCurrent: Boolean,
val isPlaying: Boolean
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ru.stersh.youamp.feature.playlist.ui

import ru.stersh.youamp.feature.playlist.domain.PlaylistInfo
import ru.stersh.youamp.feature.playlist.domain.PlaylistSong

internal fun PlaylistInfo.toUi(): PlaylistInfoUi {
return PlaylistInfoUi(
artworkUrl = artworkUrl,
title = title,
songs = songs.toUi()
)
}

internal fun List<PlaylistSong>.toUi(): List<PlaylistSongUi> = map { it.toUi() }

internal fun PlaylistSong.toUi(): PlaylistSongUi {
return PlaylistSongUi(
id = id,
title = title,
artist = artist,
artworkUrl = artworkUrl,
isCurrent = isCurrent,
isPlaying = isPlaying
)
}
Loading

0 comments on commit 6899d9b

Please sign in to comment.