Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Random songs screen #354

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ dependencies {
implementation project(":core:room")
implementation project(":core:properties")
implementation project(":shared:player")
implementation project(":shared:favorites")
implementation project(":shared:song:random")
implementation project(':feature:album:list')
implementation project(':feature:album:info')
implementation project(':feature:artist:list')
Expand All @@ -95,6 +97,7 @@ dependencies {
implementation project(":feature:personal")
implementation project(":feature:explore")
implementation project(":feature:library")
implementation project(":feature:song:random")
implementation(libs.kotlin.serialization.core)
implementation(libs.bundles.koin)
implementation(libs.navigation.compose)
Expand Down
49 changes: 26 additions & 23 deletions app/src/main/java/ru/stersh/youamp/ApiProviderImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,23 @@ package ru.stersh.youamp
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapNotNull
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import ru.stersh.youamp.core.api.ApiDefaults
import ru.stersh.youamp.core.api.SubsonicApi
import ru.stersh.youamp.core.api.provider.ApiProvider
import ru.stersh.youamp.core.api.provider.NoActiveServerSettingsFound
import ru.stersh.youamp.core.room.server.SubsonicServerDao
import ru.stersh.youamp.core.room.server.SubsonicServerDb
import java.util.concurrent.ConcurrentHashMap

internal class ApiProviderImpl(
private val subsonicServerDao: SubsonicServerDao
) : ApiProvider {
private val mutex = Mutex()
private var apiSonic: SubsonicApi? = null

override suspend fun getApi(): SubsonicApi = mutex.withLock {
private val apiCache = ConcurrentHashMap<Long, SubsonicApi>()

override suspend fun getApi(): SubsonicApi {
val currentServerSettings = subsonicServerDao.getActive() ?: throw NoActiveServerSettingsFound()
return@withLock requireApi(currentServerSettings)
return requireApi(currentServerSettings)
}

override suspend fun requireApiId(): Long {
Expand All @@ -43,22 +42,34 @@ internal class ApiProviderImpl(
return subsonicServerDao
.flowActive()
.map {
getApiOrNull(it)
if (it == null) {
null
} else {
getApi(it.id)
}
}
}

private fun getApiOrNull(subsonicServer: SubsonicServerDb?): SubsonicApi? {
if (subsonicServer == null) {
return null
override fun flowApiId(): Flow<Long?> {
return subsonicServerDao
.flowActive()
.map { it?.id }
}

override suspend fun getApi(id: Long): SubsonicApi? {
return apiCache.getOrPut(id) {
subsonicServerDao.getServer(id)?.let {
createNewApi(it)
}
}
return requireApi(subsonicServer)
}

override suspend fun requireApi(id: Long): SubsonicApi {
return requireNotNull(getApi(id))
}

private fun requireApi(subsonicServer: SubsonicServerDb): SubsonicApi {
if (!isSameSettings(subsonicServer)) {
apiSonic = createNewApi(subsonicServer)
}
return requireNotNull(apiSonic)
return apiCache.getOrPut(subsonicServer.id) { createNewApi(subsonicServer) }
}

private fun createNewApi(subsonicServer: SubsonicServerDb): SubsonicApi {
Expand All @@ -71,12 +82,4 @@ internal class ApiProviderImpl(
useLegacyAuth = subsonicServer.useLegacyAuth
)
}

private fun isSameSettings(subsonicServer: SubsonicServerDb): Boolean {
val currentApiSonic = apiSonic ?: return false
return currentApiSonic.username == subsonicServer.username &&
currentApiSonic.password == subsonicServer.password &&
currentApiSonic.url == subsonicServer.url &&
currentApiSonic.useLegacyAuth == subsonicServer.useLegacyAuth
}
}
12 changes: 8 additions & 4 deletions app/src/main/java/ru/stersh/youamp/Di.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ import ru.stresh.youamp.feature.about.aboutModule
import ru.stresh.youamp.feature.explore.exploreModule
import ru.stresh.youamp.feature.favorite.list.favoriteListModule
import ru.stresh.youamp.feature.library.libraryModule
import ru.stresh.youamp.feature.song.random.songRandomModule
import ru.stresh.youamp.shared.favorites.favoritesSharedModule
import ru.stresh.youamp.shared.song.random.songRandomSharedModule

internal fun setupDi(application: Application) {
startKoin {
Expand All @@ -50,7 +53,9 @@ private val core = listOf(
)

private val shared = listOf(
playerSharedModule
playerSharedModule,
favoritesSharedModule,
songRandomSharedModule
)

private val feature = listOf(
Expand All @@ -72,16 +77,15 @@ private val feature = listOf(
exploreModule,
mainModule,
libraryModule,
songInfoModule
songInfoModule,
songRandomModule
)

private val impl = module {
single {
AppProperties(
name = get<Context>().getString(R.string.app_name),
version = BuildConfig.VERSION_NAME,
googlePlayAppUri = "market://details?id=ru.stersh.youamp".toUri(),
googlePlayBrowserUri = "https://play.google.com/store/apps/details?id=ru.stersh.youamp".toUri(),
githubUri = "https://github.com/siper/Youamp".toUri(),
fdroidUri = "https://f-droid.org/packages/ru.stersh.youamp/".toUri(),
crwodinUri = "https://crowdin.com/project/youamp".toUri()
Expand Down
24 changes: 24 additions & 0 deletions app/src/main/java/ru/stersh/youamp/main/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import ru.stresh.youamp.feature.explore.ui.ExploreScreen
import ru.stresh.youamp.feature.favorite.list.ui.FavoriteSongsScreen
import ru.stresh.youamp.feature.library.ui.LibraryScreen
import ru.stresh.youamp.feature.settings.ui.SettingsScreen
import ru.stresh.youamp.feature.song.random.ui.RandomSongsScreen

class MainActivity : ComponentActivity() {

Expand Down Expand Up @@ -150,6 +151,9 @@ class MainActivity : ComponentActivity() {
onSearchClick = {
rootNavController.navigate(Search)
},
onRandomSongsClick = {
rootNavController.navigate(RandomSongs)
},
onSongClick = {
songInfoProperties = SongInfoProperties(
songId = it,
Expand Down Expand Up @@ -412,6 +416,26 @@ class MainActivity : ComponentActivity() {
)
}
}
composable<RandomSongs> {
ScreenWithMiniPlayer(
viewModelStoreOwner = viewModelStoreOwner,
onMiniPlayerClick = {
rootNavController.navigate(Player)
}
) {
RandomSongsScreen(
onBackClick = {
rootNavController.popBackStack()
},
onSongClick = {
songInfoProperties = SongInfoProperties(
songId = it,
showAlbum = true
)
}
)
}
}
}
songInfoProperties?.let { songProperties ->
ModalBottomSheet(
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/ru/stersh/youamp/main/ui/Navigation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,7 @@ object Artists
object Playlists

@Serializable
object FavoriteSongs
object FavoriteSongs

@Serializable
object RandomSongs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ interface ApiProvider {

suspend fun requireApiId(): Long

suspend fun getApi(id: Long): SubsonicApi?

suspend fun requireApi(id: Long): SubsonicApi

fun flowApi(): Flow<SubsonicApi>

fun flowApiOrNull(): Flow<SubsonicApi?>

fun flowApiId(): Flow<Long?>
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import android.net.Uri
data class AppProperties(
val name: String,
val version: String,
val googlePlayAppUri: Uri,
val googlePlayBrowserUri: Uri,
val githubUri: Uri,
val fdroidUri: Uri,
val crwodinUri: Uri
Expand Down
3 changes: 1 addition & 2 deletions core/ui/src/main/java/ru/stersh/youamp/core/ui/Artist.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import androidx.compose.material.icons.rounded.Person
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.NonRestartableComposable
import androidx.compose.runtime.Stable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
Expand Down Expand Up @@ -98,7 +97,7 @@ private fun ArtistArtwork(
}

@Composable
@NonRestartableComposable

private fun PlayButtonBackground(modifier: Modifier = Modifier) {
Box(
modifier = modifier
Expand Down
54 changes: 54 additions & 0 deletions core/ui/src/main/java/ru/stersh/youamp/core/ui/Header.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package ru.stersh.youamp.core.ui

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp


@Composable
fun HeaderLayout(
title: @Composable () -> Unit,
actions: @Composable RowScope.() -> Unit,
modifier: Modifier = Modifier
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(16.dp),
modifier = modifier
.padding(horizontal = 24.dp)
.padding(bottom = 12.dp)
) {
title()
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceEvenly
) {
actions()
}
}
}

@Composable
fun HeaderTitle(
text: String,
modifier: Modifier = Modifier
) {
Text(
text = text,
textAlign = TextAlign.Center,
style = MaterialTheme.typography.headlineLarge,
maxLines = 1,
minLines = 1,
modifier = modifier.padding(vertical = 100.dp, horizontal = 24.dp)
)
}
3 changes: 1 addition & 2 deletions core/ui/src/main/java/ru/stersh/youamp/core/ui/PlayButton.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import androidx.compose.material3.IconButton
import androidx.compose.material3.IconButtonDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.NonRestartableComposable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
Expand All @@ -39,7 +38,7 @@ fun PlayButtonOutlined(
}

@Composable
@NonRestartableComposable

fun PlayButton(
isPlaying: Boolean,
onClick: () -> Unit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ internal class AboutAppViewModel(appPropertiesStorage: AppPropertiesStorage) : V
return AboutStateUi(
name = name,
version = version,
googlePlayAppUri = googlePlayAppUri,
googlePlayBrowserUri = googlePlayBrowserUri,
githubUri = githubUri,
fdroidUri = fdroidUri,
crwodinUri = crwodinUri
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,6 @@ private fun AboutScreenPreview() {
state = AboutStateUi(
name = "Youamp",
version = "1.0.0-alpha04",
googlePlayAppUri = Uri.EMPTY,
googlePlayBrowserUri = Uri.EMPTY,
githubUri = Uri.EMPTY,
fdroidUri = Uri.EMPTY,
crwodinUri = Uri.EMPTY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import androidx.compose.runtime.Immutable
internal data class AboutStateUi(
val name: String,
val version: String,
val googlePlayAppUri: Uri,
val googlePlayBrowserUri: Uri,
val githubUri: Uri,
val fdroidUri: Uri,
val crwodinUri: Uri
Expand Down
1 change: 1 addition & 0 deletions feature/album/info/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies {
implementation project(":core:api")
implementation project(":core:utils")
implementation project(":shared:player")
implementation project(":shared:favorites")
implementation(libs.bundles.lifecycle)
implementation(libs.coil.compose)
implementation(libs.bundles.koin)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import ru.stersh.youamp.feature.album.domain.AlbumInfoRepository
import ru.stersh.youamp.feature.album.ui.AlbumInfoViewModel

val albumInfoModule = module {
single<AlbumInfoRepository> { AlbumInfoRepositoryImpl(get()) }
single<AlbumInfoRepository> { AlbumInfoRepositoryImpl(get(), get()) }
single<AlbumFavoriteRepository> { AlbumFavoriteRepositoryImpl(get()) }

viewModel { (id: String) ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
package ru.stersh.youamp.feature.album.data

import ru.stersh.youamp.core.api.provider.ApiProvider
import ru.stersh.youamp.feature.album.domain.AlbumFavoriteRepository
import ru.stresh.youamp.shared.favorites.AlbumFavoritesStorage

internal class AlbumFavoriteRepositoryImpl(
private val apiProvider: ApiProvider
private val albumFavoritesStorage: AlbumFavoritesStorage
) : AlbumFavoriteRepository {

override suspend fun setFavorite(id: String, isFavorite: Boolean) {
val api = apiProvider.getApi()
if (isFavorite) {
api.starAlbum(id)
} else {
api.unstarAlbum(id)
}
albumFavoritesStorage.setAlbumFavorite(id, isFavorite)
}
}
Loading
Loading