Skip to content

Commit

Permalink
Merge branch 'master' into feat/offset
Browse files Browse the repository at this point in the history
  • Loading branch information
Lambada10 authored Oct 24, 2024
2 parents 8cd1484 + 7e9a4cf commit 4080640
Show file tree
Hide file tree
Showing 21 changed files with 273 additions and 87 deletions.
3 changes: 2 additions & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>

</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class UserSettingsController(private val dataStore: DataStore<Preferences>) {
var multiPersonWordByWord by mutableStateOf(dataStore.get(multiPersonWordByWordKey, false))
private set

var syncedMusixmatch by mutableStateOf(dataStore.get(syncedMusixmatchKey, true))
private set

var pureBlack by mutableStateOf(dataStore.get(pureBlackKey, false))
private set

Expand All @@ -44,6 +47,9 @@ class UserSettingsController(private val dataStore: DataStore<Preferences>) {
var sdCardPath by mutableStateOf(dataStore.get(sdCardPathKey, null))
private set

var showPath by mutableStateOf(dataStore.get(showPathKey, false))
private set

fun updateEmbedLyrics(to: Boolean) {
dataStore.set(embedKey, to)
embedLyricsIntoFiles = to
Expand Down Expand Up @@ -74,6 +80,11 @@ class UserSettingsController(private val dataStore: DataStore<Preferences>) {
multiPersonWordByWord = to
}

fun updateSyncedMusixmatch(to: Boolean) {
dataStore.set(syncedMusixmatchKey, to)
syncedMusixmatch = to
}

fun updateDisableMarquee(to: Boolean) {
dataStore.set(disableMarqueeKey, to)
disableMarquee = to
Expand All @@ -88,6 +99,11 @@ class UserSettingsController(private val dataStore: DataStore<Preferences>) {
dataStore.set(sdCardPathKey, to)
sdCardPath = to
}

fun updateShowPath(to: Boolean) {
dataStore.set(showPathKey, to)
showPath = to
}
}

private val embedKey = booleanPreferencesKey("embed_lyrics")
Expand All @@ -96,6 +112,8 @@ private val blacklistedFoldersKey = stringPreferencesKey("blacklist")
private val hideLyricsKey = booleanPreferencesKey("hide_lyrics")
private val includeTranslationKey = booleanPreferencesKey("include_translation")
private val multiPersonWordByWordKey = booleanPreferencesKey("multi_person_word_by_word")
private val syncedMusixmatchKey = booleanPreferencesKey("synced_lyrics")
private val disableMarqueeKey = booleanPreferencesKey("marquee_disable")
private val pureBlackKey = booleanPreferencesKey("pure_black")
private val sdCardPathKey = stringPreferencesKey("sd_card_path")
private val sdCardPathKey = stringPreferencesKey("sd_card_path")
private val showPathKey = booleanPreferencesKey("show_path")
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class LyricsProviderService {
// Apple Track ID
private var appleID = 0L

// Musixmatch Track ID
private var musixmatchID = 0L
// Musixmatch Song Info
private var musixmatchSongInfo: SongInfo? = null
// TODO: Use values from SongInfo object returned by search instead of storing them here

/**
Expand All @@ -52,11 +52,11 @@ class LyricsProviderService {
UnknownHostException::class, FileNotFoundException::class, NoTrackFoundException::class,
EmptyQueryException::class, InternalErrorException::class
)
suspend fun getSongInfo(query: SongInfo, offset: Int? = 0, provider: Providers): SongInfo? {
suspend fun getSongInfo(query: SongInfo, offset: Int = 0, provider: Providers): SongInfo? {
return try {
when (provider) {
Providers.SPOTIFY -> spotifyAPI.getSongInfo(query, offset)
Providers.LRCLIB -> LRCLibAPI().getSongInfo(query).also {
Providers.LRCLIB -> LRCLibAPI().getSongInfo(query, offset).also {
lrcLibID = it?.lrcLibID ?: 0
} ?: throw NoTrackFoundException()

Expand All @@ -68,8 +68,8 @@ class LyricsProviderService {
appleID = it?.appleID ?: 0
} ?: throw NoTrackFoundException()

Providers.MUSIXMATCH -> MusixmatchAPI().getSongInfo(query).also {
musixmatchID = it?.musixmatchID ?: 0
Providers.MUSIXMATCH -> MusixmatchAPI().getSongInfo(query, offset).also {
musixmatchSongInfo = it
} ?: throw NoTrackFoundException()
}
} catch (e: InternalErrorException) {
Expand All @@ -94,7 +94,8 @@ class LyricsProviderService {
provider: Providers,
// TODO providers could be a sealed interface to include such parameters
includeTranslationNetEase: Boolean = false,
multiPersonWordByWord: Boolean = false
multiPersonWordByWord: Boolean = false,
syncedMusixmatch: Boolean = true
): String? {
return try {
when (provider) {
Expand All @@ -108,7 +109,10 @@ class LyricsProviderService {
appleID,
multiPersonWordByWord
)
Providers.MUSIXMATCH -> MusixmatchAPI().getSyncedLyrics(musixmatchID)
Providers.MUSIXMATCH -> MusixmatchAPI().getLyrics(
musixmatchSongInfo,
syncedMusixmatch
)
}
} catch (e: Exception) {
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class LRCLibAPI {
* @param query The SongInfo object with songName and artistName fields filled.
* @return Search result as a SongInfo object.
*/
suspend fun getSongInfo(query: SongInfo): SongInfo? {
suspend fun getSongInfo(query: SongInfo, offset: Int = 0): SongInfo? {
val search = withContext(Dispatchers.IO) {
URLEncoder.encode(
"${query.songName}", // it doesn't work with artist name and song name together
Expand All @@ -41,10 +41,16 @@ class LRCLibAPI {

val json = json.decodeFromString<List<LRCLibResponse>>(responseBody)

val song = try {
json[offset]
} catch (e: IndexOutOfBoundsException) {
return null
}

return SongInfo(
songName = json[0].trackName,
artistName = json[0].artistName,
lrcLibID = json[0].id
songName = song.trackName,
artistName = song.artistName,
lrcLibID = song.id
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import io.ktor.client.statement.bodyAsText
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import pl.lambada.songsync.domain.model.SongInfo
import pl.lambada.songsync.domain.model.lyrics_providers.others.MusixmatchLyricsResponse
import pl.lambada.songsync.domain.model.lyrics_providers.others.MusixmatchSearchResponse
import pl.lambada.songsync.util.EmptyQueryException
import pl.lambada.songsync.util.networking.Ktor.client
Expand All @@ -20,53 +19,54 @@ class MusixmatchAPI {
* @param query The SongInfo object with songName and artistName fields filled.
* @return Search result as a SongInfo object.
*/
suspend fun getSongInfo(query: SongInfo): SongInfo? {
val search = withContext(Dispatchers.IO) {
suspend fun getSongInfo(query: SongInfo, offset: Int = 0): SongInfo? {
val artistName = withContext(Dispatchers.IO) {
URLEncoder.encode(
"${query.songName} ${query.artistName}",
"${query.artistName}",
Charsets.UTF_8.toString()
)
}

if (search == " ")
val songName = withContext(Dispatchers.IO) {
URLEncoder.encode(
"${query.songName}",
Charsets.UTF_8.toString()
)
}

if (artistName == "" || songName == "")
throw EmptyQueryException()

val response = client.get(
"$baseURL/search?q=$search"
"$baseURL/full?artist=$artistName&track=$songName"
)
val responseBody = response.bodyAsText(Charsets.UTF_8)

if (response.status.value !in 200..299 || responseBody == "[]")
if (response.status.value !in 200..299)
return null

val json = json.decodeFromString<List<MusixmatchSearchResponse>>(responseBody)

val result = json[0]
val result = json.decodeFromString<MusixmatchSearchResponse>(responseBody)

return SongInfo(
songName = result.songName,
artistName = result.artistName,
songLink = result.url,
albumCoverLink = result.artwork,
musixmatchID = result.id,
hasSyncedLyrics = result.hasSyncedLyrics,
hasUnsyncedLyrics = result.hasSyncedLyrics,
syncedLyrics = result.syncedLyrics?.lyrics,
unsyncedLyrics = result.unsyncedLyrics?.lyrics
)
}

/**
* Searches for synced lyrics using the song name and artist name.
* @param id The ID of the song from search results.
* @return The synced lyrics as a string.
* Returns the lyrics.
* @param songInfo The SongInfo of the song from search results.
* @return The lyrics as a string or null if the lyrics were not found.
*/
suspend fun getSyncedLyrics(id: Long): String? {
val response = client.get(
"$baseURL/lyrics?id=$id"
)
val responseBody = response.bodyAsText(Charsets.UTF_8)

if (response.status.value !in 200..299)
return null

val json = json.decodeFromString<MusixmatchLyricsResponse>(responseBody)
return json.lyrics
fun getLyrics(songInfo: SongInfo?, synced: Boolean = true): String? {
return if(synced) songInfo?.syncedLyrics
else songInfo?.unsyncedLyrics
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ data class SongInfo(
var neteaseID: Long? = null, // Netease-only
var appleID: Long? = null, // Apple-only
var musixmatchID: Long? = null, // Musixmatch-only
var hasSyncedLyrics: Boolean? = null, // Musixmatch-only
var hasUnsyncedLyrics: Boolean? = null, // Musixmatch-only
var syncedLyrics: String? = null, // Musixmatch-only
var unsyncedLyrics: String? = null, // Musixmatch-only
) : Parcelable
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,24 @@ data class MusixmatchSearchResponse(
val url: String,
val albumId: Long,
val hasSyncedLyrics: Boolean,
val hasUnsyncedLyrics: Boolean
val hasUnsyncedLyrics: Boolean,
val syncedLyrics: SyncedLyricsResponse? = null,
val unsyncedLyrics: UnsyncedLyricsResponse? = null
)

@Serializable
data class MusixmatchLyricsResponse(
data class SyncedLyricsResponse(
val id: Long,
val duration: Int,
val language: String,
val updatedTime: String,
val lyrics: String
)

@Serializable
data class UnsyncedLyricsResponse(
val id: Long,
val language: String,
val updatedTime: String,
val lyrics: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.compose.animation.AnimatedVisibilityScope
import androidx.compose.animation.ExperimentalSharedTransitionApi
import androidx.compose.animation.SharedTransitionScope
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
Expand All @@ -28,6 +29,7 @@ import coil.compose.rememberAsyncImagePainter
import coil.imageLoader
import coil.request.ImageRequest
import pl.lambada.songsync.R
import pl.lambada.songsync.util.openFileFromPath

@OptIn(ExperimentalSharedTransitionApi::class)
@Composable
Expand All @@ -41,11 +43,16 @@ fun SharedTransitionScope.SongCard(
animatedVisibilityScope: AnimatedVisibilityScope,
) {
val unknownArtistString = stringResource(R.string.unknown)
val context = LocalContext.current

OutlinedCard(
shape = RoundedCornerShape(10.dp),
modifier = CombinedModifier(
outer = Modifier.fillMaxWidth(),
outer = Modifier
.fillMaxWidth()
.clickable(filePath != null) {
openFileFromPath(context, filePath!!)
},
inner = modifier
)
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ import pl.lambada.songsync.ui.screens.about.components.MarqueeSwitch
import pl.lambada.songsync.ui.screens.about.components.MultiPersonSwitch
import pl.lambada.songsync.ui.screens.about.components.PureBlackThemeSwitch
import pl.lambada.songsync.ui.screens.about.components.SdCardPathSetting
import pl.lambada.songsync.ui.screens.about.components.ShowPathSwitch
import pl.lambada.songsync.ui.screens.about.components.SupportSection
import pl.lambada.songsync.ui.screens.about.components.SyncedLyricsSwitch
import pl.lambada.songsync.ui.screens.about.components.TranslationSwitch
import pl.lambada.songsync.ui.screens.about.components.UpdateAvailableDialog
import pl.lambada.songsync.util.ext.getVersion
Expand Down Expand Up @@ -75,6 +77,13 @@ fun AboutScreen(
)
}

item {
ShowPathSwitch(
selected = userSettingsController.showPath,
onToggle = { userSettingsController.updateShowPath(it) }
)
}

item {
TranslationSwitch(
selected = userSettingsController.includeTranslation,
Expand All @@ -89,6 +98,13 @@ fun AboutScreen(
)
}

item {
SyncedLyricsSwitch(
selected = userSettingsController.syncedMusixmatch,
onToggle = { userSettingsController.updateSyncedMusixmatch(it) }
)
}

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
item {
SdCardPathSetting(
Expand Down
Loading

0 comments on commit 4080640

Please sign in to comment.