Skip to content

Commit

Permalink
feat(lyricsProviders): Extract lyrics providers to a separate module.…
Browse files Browse the repository at this point in the history
… Separate LRCLIB as a new lyrics provider
  • Loading branch information
maxrave-dev committed Jan 12, 2025
1 parent 4c8ce1d commit 8325e55
Show file tree
Hide file tree
Showing 38 changed files with 1,458 additions and 1,307 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ android {

dependencies {

implementation(project(":lyricsProviders"))
// Compose
val composeBom = platform(libs.compose.bom)
implementation(composeBom)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ class DataStoreManager(
settingsDataStore.data.map { preferences ->
preferences[USING_PROXY] ?: FALSE
}

suspend fun setUsingProxy(usingProxy: Boolean) {
withContext(Dispatchers.IO) {
if (usingProxy) {
Expand All @@ -575,41 +576,49 @@ class DataStoreManager(
}
}
}

val proxyType =
settingsDataStore.data.map { preferences ->
preferences[PROXY_TYPE]
}.map {
when (it) {
PROXY_TYPE_HTTP -> ProxyType.PROXY_TYPE_HTTP
PROXY_TYPE_SOCKS -> ProxyType.PROXY_TYPE_SOCKS
else -> ProxyType.PROXY_TYPE_HTTP
settingsDataStore.data
.map { preferences ->
preferences[PROXY_TYPE]
}.map {
when (it) {
PROXY_TYPE_HTTP -> ProxyType.PROXY_TYPE_HTTP
PROXY_TYPE_SOCKS -> ProxyType.PROXY_TYPE_SOCKS
else -> ProxyType.PROXY_TYPE_HTTP
}
}
}

suspend fun setProxyType(proxyType: ProxyType) {
withContext(Dispatchers.IO) {
settingsDataStore.edit { settings ->
settings[PROXY_TYPE] = when (proxyType) {
ProxyType.PROXY_TYPE_HTTP -> PROXY_TYPE_HTTP
ProxyType.PROXY_TYPE_SOCKS -> PROXY_TYPE_SOCKS
}
settings[PROXY_TYPE] =
when (proxyType) {
ProxyType.PROXY_TYPE_HTTP -> PROXY_TYPE_HTTP
ProxyType.PROXY_TYPE_SOCKS -> PROXY_TYPE_SOCKS
}
}
}
}

val proxyHost =
settingsDataStore.data.map { preferences ->
preferences[PROXY_HOST] ?: ""
}

suspend fun setProxyHost(proxyHost: String) {
withContext(Dispatchers.IO) {
settingsDataStore.edit { settings ->
settings[PROXY_HOST] = proxyHost
}
}
}

val proxyPort =
settingsDataStore.data.map { preferences ->
preferences[PROXY_PORT] ?: 8000
}

suspend fun setProxyPort(proxyPort: Int) {
withContext(Dispatchers.IO) {
settingsDataStore.edit { settings ->
Expand All @@ -630,7 +639,7 @@ class DataStoreManager(
ProxyType.PROXY_TYPE_HTTP -> Proxy.Type.HTTP
ProxyType.PROXY_TYPE_SOCKS -> Proxy.Type.SOCKS
},
java.net.InetSocketAddress(proxyHost, proxyPort)
java.net.InetSocketAddress(proxyHost, proxyPort),
)
} else {
return@runBlocking null
Expand Down Expand Up @@ -659,6 +668,7 @@ class DataStoreManager(
val MUSIXMATCH_LOGGED_IN = stringPreferencesKey("musixmatch_logged_in")
const val YOUTUBE = "youtube"
const val MUSIXMATCH = "musixmatch"
const val LRCLIB = "lrclib"
val LYRICS_PROVIDER = stringPreferencesKey("lyrics_provider")
val TRANSLATION_LANGUAGE = stringPreferencesKey("translation_language")
val USE_TRANSLATION_LANGUAGE = stringPreferencesKey("use_translation_language")
Expand Down Expand Up @@ -688,10 +698,11 @@ class DataStoreManager(
const val FALSE = "FALSE"
const val PROXY_TYPE_HTTP = "http"
const val PROXY_TYPE_SOCKS = "socks"

// Proxy type
enum class ProxyType {
PROXY_TYPE_HTTP,
PROXY_TYPE_SOCKS
PROXY_TYPE_SOCKS,
}
}
}

Large diffs are not rendered by default.

15 changes: 14 additions & 1 deletion app/src/main/java/com/maxrave/simpmusic/di/DatabaseModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.sqlite.db.SupportSQLiteDatabase
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.maxrave.kotlinytmusicscraper.YouTube
import com.maxrave.lyricsproviders.LyricsClient
import com.maxrave.simpmusic.common.DB_NAME
import com.maxrave.simpmusic.data.dataStore.DataStoreManager
import com.maxrave.simpmusic.data.db.Converters
Expand Down Expand Up @@ -185,9 +186,21 @@ val databaseModule =
Spotify()
}

single(createdAtStart = true) {
LyricsClient(androidContext())
}

// MainRepository
single(createdAtStart = true) {
MainRepository(get<LocalDataSource>(), get<DataStoreManager>(), get<YouTube>(), get<Spotify>(), get<MusicDatabase>(), androidContext())
MainRepository(
get<LocalDataSource>(),
get<DataStoreManager>(),
get<YouTube>(),
get<Spotify>(),
get<LyricsClient>(),
get<MusicDatabase>(),
androidContext(),
)
}
// List of managers

Expand Down
9 changes: 6 additions & 3 deletions app/src/main/java/com/maxrave/simpmusic/extension/AllExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import androidx.navigation.NavController
import androidx.sqlite.db.SimpleSQLiteQuery
import com.maxrave.kotlinytmusicscraper.models.SongItem
import com.maxrave.kotlinytmusicscraper.models.VideoItem
import com.maxrave.kotlinytmusicscraper.models.musixmatch.MusixmatchTranslationLyricsResponse
import com.maxrave.kotlinytmusicscraper.models.response.PipedResponse
import com.maxrave.kotlinytmusicscraper.models.youtube.Transcript
import com.maxrave.kotlinytmusicscraper.models.youtube.YouTubeInitialPage
import com.maxrave.lyricsproviders.models.response.MusixmatchTranslationLyricsResponse
import com.maxrave.simpmusic.R
import com.maxrave.simpmusic.common.DownloadState
import com.maxrave.simpmusic.common.SETTINGS_FILENAME
Expand Down Expand Up @@ -593,7 +593,7 @@ fun <T> Iterable<T>.indexMap(): Map<T, Int> {
return map
}

fun com.maxrave.kotlinytmusicscraper.models.lyrics.Lyrics.toLyrics(): Lyrics {
fun com.maxrave.lyricsproviders.models.lyrics.Lyrics.toLyrics(): Lyrics {
val lines: ArrayList<Line> = arrayListOf()
if (this.lyrics != null) {
this.lyrics?.lines?.forEach {
Expand Down Expand Up @@ -865,7 +865,10 @@ fun LocalDateTime.formatTimeAgo(context: Context): String {
}
}

fun formatDuration(duration: Long, context: Context): String {
fun formatDuration(
duration: Long,
context: Context,
): String {
if (duration < 0L) return context.getString(R.string.na_na)
val minutes: Long = TimeUnit.MINUTES.convert(duration, TimeUnit.MILLISECONDS)
val seconds: Long = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.maxrave.simpmusic.ui.component

import android.app.Activity
import android.content.Intent
import android.content.res.Configuration
import android.os.Build
import android.os.Bundle
import android.util.Log
Expand Down Expand Up @@ -74,7 +73,6 @@ import androidx.compose.ui.platform.rememberNestedScrollInteropConnection
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.core.text.isDigitsOnly
import androidx.media3.common.util.UnstableApi
Expand Down Expand Up @@ -216,7 +214,12 @@ fun NowPlayingBottomSheet(
if (mainLyricsProvider) {
var selected by remember {
mutableIntStateOf(
if (uiState.mainLyricsProvider == DataStoreManager.MUSIXMATCH) 0 else 1,
when (uiState.mainLyricsProvider) {
DataStoreManager.MUSIXMATCH -> 0
DataStoreManager.YOUTUBE -> 1
DataStoreManager.LRCLIB -> 2
else -> 0
},
)
}

Expand Down Expand Up @@ -263,14 +266,35 @@ fun NowPlayingBottomSheet(
Spacer(modifier = Modifier.size(10.dp))
Text(text = stringResource(id = R.string.youtube_transcript), style = typo.labelSmall)
}
Row(
modifier =
Modifier
.padding(horizontal = 4.dp)
.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically,
) {
RadioButton(
selected = selected == 2,
onClick = {
selected = 2
},
)
Spacer(modifier = Modifier.size(10.dp))
Text(text = stringResource(id = R.string.lrclib), style = typo.labelSmall)
}
}
},
confirmButton = {
TextButton(
onClick = {
viewModel.onUIEvent(
NowPlayingBottomSheetUIEvent.ChangeLyricsProvider(
if (selected == 0) DataStoreManager.MUSIXMATCH else DataStoreManager.YOUTUBE,
when (selected) {
0 -> DataStoreManager.MUSIXMATCH
1 -> DataStoreManager.YOUTUBE
2 -> DataStoreManager.LRCLIB
else -> DataStoreManager.MUSIXMATCH
},
),
)
mainLyricsProvider = false
Expand Down Expand Up @@ -299,7 +323,7 @@ fun NowPlayingBottomSheet(
contentColor = Color.Transparent,
dragHandle = null,
scrimColor = Color.Black.copy(alpha = .5f),
contentWindowInsets = { WindowInsets(0, 0, 0, 0) }
contentWindowInsets = { WindowInsets(0, 0, 0, 0) },
) {
Card(
modifier =
Expand All @@ -312,7 +336,7 @@ fun NowPlayingBottomSheet(
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier.verticalScroll(rememberScrollState())
modifier = Modifier.verticalScroll(rememberScrollState()),
) {
Spacer(modifier = Modifier.height(5.dp))
Card(
Expand Down Expand Up @@ -719,7 +743,7 @@ fun AddToPlaylistModalBottomSheet(
contentColor = Color.Transparent,
dragHandle = null,
scrimColor = Color.Black.copy(alpha = .5f),
contentWindowInsets = { WindowInsets(0, 0, 0, 0) }
contentWindowInsets = { WindowInsets(0, 0, 0, 0) },
) {
Card(
modifier =
Expand Down Expand Up @@ -1207,7 +1231,13 @@ fun EndOfModalBottomSheet() {
modifier =
Modifier
.fillMaxWidth()
.height(WindowInsets.navigationBars.asPaddingValues()
.calculateBottomPadding().value.toInt().dp + 8.dp),
.height(
WindowInsets.navigationBars
.asPaddingValues()
.calculateBottomPadding()
.value
.toInt()
.dp + 8.dp,
),
) {}
}
Loading

0 comments on commit 8325e55

Please sign in to comment.