Skip to content

Commit

Permalink
Optimising Synced lyrics
Browse files Browse the repository at this point in the history
  • Loading branch information
shub39 committed Aug 5, 2024
1 parent ca57ae0 commit a9244df
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 21 deletions.
Binary file modified app/release/app-release.apk
Binary file not shown.
Binary file modified app/release/baselineProfiles/0/app-release.dm
Binary file not shown.
Binary file modified app/release/baselineProfiles/1/app-release.dm
Binary file not shown.
2 changes: 1 addition & 1 deletion app/src/main/java/com/shub39/rush/database/LrcLibSong.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ data class LrcLibSong (
val albumName: String,
val duration: Double,
val instrumental: Boolean,
val plainLyrics: String,
val plainLyrics: String?,
val syncedLyrics: String?
)
4 changes: 2 additions & 2 deletions app/src/main/java/com/shub39/rush/lyrics/LyricsFetcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ object LyricsFetcher {
val jsonSong = response.body()
if (!jsonSong.isNullOrEmpty()) {
val song = jsonSong[0]
val lyrics = song.plainLyrics
val syncedLyrics = song.syncedLyrics
val lyrics = song.plainLyrics ?: jsonSong[1].plainLyrics ?: ""
val syncedLyrics = song.syncedLyrics ?: if (jsonSong.size > 1) jsonSong[1].syncedLyrics else null
return Pair(lyrics, syncedLyrics)
}
}
Expand Down
48 changes: 30 additions & 18 deletions app/src/main/java/com/shub39/rush/page/LyricsPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,22 @@ fun LyricsPage(
}
if (
nonNullSong.syncedLyrics != null
&& (currentPlayingSong?.first ?: "") == nonNullSong.title
&&
(currentPlayingSong?.first ?: "").trim()
.lowercase() == nonNullSong.title.trim()
.lowercase()
) {
syncedAvailable = true
sync = true
}
}

LaunchedEffect(currentPlayingSong) {
syncedAvailable = (nonNullSong.syncedLyrics != null
&& (currentPlayingSong?.first ?: "") == nonNullSong.title)
syncedAvailable = (
nonNullSong.syncedLyrics != null
&& (currentPlayingSong?.first ?: "").trim()
.lowercase() == nonNullSong.title.trim().lowercase()
)
sync = syncedAvailable
}

Expand Down Expand Up @@ -378,9 +384,9 @@ fun LyricsPage(
}

}
} else {
} else if (nonNullSong.syncedLyrics != null) {
val syncedLazyList = rememberLazyListState()
val parsedSyncedLyrics = parseLyrics(nonNullSong.syncedLyrics!!)
val parsedSyncedLyrics = parseLyrics(nonNullSong.syncedLyrics)

LaunchedEffect(currentSongPosition) {
coroutineScope.launch {
Expand All @@ -394,37 +400,43 @@ fun LyricsPage(
modifier = Modifier.padding(end = 16.dp, start = 16.dp, bottom = 16.dp),
state = syncedLazyList
) {
items(parsedSyncedLyrics, key = { it.time }) {
items(parsedSyncedLyrics, key = { it.time }) { lyric ->
Row(
modifier = Modifier
.fillMaxWidth()
modifier = Modifier.fillMaxWidth()
) {
Card(
modifier = Modifier
.padding(6.dp),
colors = CardDefaults.cardColors(
val color = if (lyric.time <= currentSongPosition + 2000) {
CardDefaults.cardColors(
containerColor = MaterialTheme.colorScheme.primary,
contentColor = MaterialTheme.colorScheme.onPrimary
)
} else {
CardDefaults.cardColors(
containerColor = MaterialTheme.colorScheme.primaryContainer,
contentColor = MaterialTheme.colorScheme.onPrimaryContainer
)
) {
if (it.text.isNotEmpty()) {
}

Card(
modifier = Modifier.padding(6.dp),
colors = color,
shape = MaterialTheme.shapes.small,
) {
if (lyric.text.isNotEmpty()) {
Text(
text = it.text,
text = lyric.text,
style = MaterialTheme.typography.bodyLarge,
fontWeight = FontWeight.Bold,
modifier = Modifier.padding(6.dp)
)
} else {
Icon(
painter = painterResource(id = R.drawable.round_music_note_24),
contentDescription = null,
modifier = Modifier.padding(6.dp)
contentDescription = null
)
}
}
}
}

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class RushViewModel(
_songs.value = songDao.getAllSongs()
MediaListener.songInfoFlow.collect { songInfo ->
_currentPlayingSongInfo.value = songInfo
Log.d("RushViewModel", "SongInfo: $songInfo")
}
}
viewModelScope.launch {
Expand Down

0 comments on commit a9244df

Please sign in to comment.