Skip to content

Commit

Permalink
Add Netease translations support, closes #77
Browse files Browse the repository at this point in the history
  • Loading branch information
Lambada10 committed Aug 12, 2024
1 parent 4ce11c5 commit 2baac44
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 4 deletions.
3 changes: 3 additions & 0 deletions app/src/main/java/pl/lambada/songsync/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ class MainActivity : ComponentActivity() {
viewModel.sdCardPath = sdCardPath
}

val includeTranslation = dataStore.get(booleanPreferencesKey("include_translation"), false)
viewModel.includeTranslation = includeTranslation

val blacklist = dataStore.get(stringPreferencesKey("blacklist"), null)
if (blacklist != null) {
viewModel.blacklistedFolders = blacklist.split(",").toMutableList()
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/pl/lambada/songsync/data/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ class MainViewModel : ViewModel() {
// LRCLib Track ID
private var lrcLibID = 0

// Netease Track ID
// Netease Track ID and stuff
private var neteaseID = 0L
var includeTranslation = false

// Apple Track ID
private var appleID = 0L
Expand Down Expand Up @@ -123,7 +124,7 @@ class MainViewModel : ViewModel() {
when (this.provider) {
Providers.SPOTIFY -> SpotifyLyricsAPI().getSyncedLyrics(songLink, version)
Providers.LRCLIB -> LRCLibAPI().getSyncedLyrics(this.lrcLibID)
Providers.NETEASE -> NeteaseAPI().getSyncedLyrics(this.neteaseID)
Providers.NETEASE -> NeteaseAPI().getSyncedLyrics(this.neteaseID, includeTranslation)
Providers.APPLE -> AppleAPI().getSyncedLyrics(this.appleID)
}
} catch (e: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class NeteaseAPI {
* @param id The ID of the song from search results.
* @return The synced lyrics as a string.
*/
suspend fun getSyncedLyrics(id: Long): String? {
suspend fun getSyncedLyrics(id: Long, includeTranslation: Boolean = false): String? {
val response = client.get(
baseURL + "song/lyric"
) {
Expand All @@ -96,13 +96,22 @@ class NeteaseAPI {
}
parameter("id", id)
parameter("lv", 1)
parameter("tv", 1)
}
val responseBody = response.bodyAsText(Charsets.UTF_8)

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

val json = json.decodeFromString<NeteaseLyricsResponse>(responseBody)
return if (json.lrc.lyric != "") json.lrc.lyric else null

if (json.lrc.lyric == "")
return null

if (includeTranslation && json.tlyric != null && json.tlyric.lyric != "") {
return json.lrc.lyric + "\n\n" + json.tlyric.lyric
}

return json.lrc.lyric
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ data class NeteaseArtist(
@Serializable
data class NeteaseLyricsResponse(
val lrc: NeteaseLyrics,
val tlyric: NeteaseLyrics?,
val code: Int
)

Expand Down
15 changes: 15 additions & 0 deletions app/src/main/java/pl/lambada/songsync/ui/screens/AboutScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,21 @@ fun AboutScreen(
}
}

item {
AboutItem(label = stringResource(id = R.string.include_translation)) {
val includeTranslation = viewModel.includeTranslation
var selected by remember { mutableStateOf(includeTranslation) }
SwitchItem(
label = stringResource(id = R.string.include_translation_summary),
selected = selected
) {
viewModel.includeTranslation = !selected
selected = !selected
dataStore.set(key = booleanPreferencesKey("include_translation"), value = selected)
}
}
}

item {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
var picker by remember { mutableStateOf(false) }
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,6 @@
<string name="internal_error">Internal error: %s</string>
<string name="disable_marquee">Disable Marquee Text</string>
<string name="disable_marquee_summary">Disable moving text animation in app</string>
<string name="include_translation">Include translation</string>
<string name="include_translation_summary">Include translated lyrics when getting song lyrics from Netease provider</string>
</resources>

0 comments on commit 2baac44

Please sign in to comment.