From 2baac4432bf49b7ddfbc3d813bf5bc3e6918a212 Mon Sep 17 00:00:00 2001 From: Lambada10 <62511588+Lambada10@users.noreply.github.com> Date: Mon, 12 Aug 2024 17:45:28 +0200 Subject: [PATCH] Add Netease translations support, closes #77 --- .../main/java/pl/lambada/songsync/MainActivity.kt | 3 +++ .../pl/lambada/songsync/data/MainViewModel.kt | 5 +++-- .../remote/lyrics_providers/others/NeteaseAPI.kt | 13 +++++++++++-- .../model/lyrics_providers/others/Netease.kt | 1 + .../pl/lambada/songsync/ui/screens/AboutScreen.kt | 15 +++++++++++++++ app/src/main/res/values/strings.xml | 2 ++ 6 files changed, 35 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/pl/lambada/songsync/MainActivity.kt b/app/src/main/java/pl/lambada/songsync/MainActivity.kt index e57016c..24583d8 100644 --- a/app/src/main/java/pl/lambada/songsync/MainActivity.kt +++ b/app/src/main/java/pl/lambada/songsync/MainActivity.kt @@ -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() diff --git a/app/src/main/java/pl/lambada/songsync/data/MainViewModel.kt b/app/src/main/java/pl/lambada/songsync/data/MainViewModel.kt index 68eaa47..9157981 100644 --- a/app/src/main/java/pl/lambada/songsync/data/MainViewModel.kt +++ b/app/src/main/java/pl/lambada/songsync/data/MainViewModel.kt @@ -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 @@ -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) { diff --git a/app/src/main/java/pl/lambada/songsync/data/remote/lyrics_providers/others/NeteaseAPI.kt b/app/src/main/java/pl/lambada/songsync/data/remote/lyrics_providers/others/NeteaseAPI.kt index 9b5c6ec..84955d0 100644 --- a/app/src/main/java/pl/lambada/songsync/data/remote/lyrics_providers/others/NeteaseAPI.kt +++ b/app/src/main/java/pl/lambada/songsync/data/remote/lyrics_providers/others/NeteaseAPI.kt @@ -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" ) { @@ -96,6 +96,7 @@ class NeteaseAPI { } parameter("id", id) parameter("lv", 1) + parameter("tv", 1) } val responseBody = response.bodyAsText(Charsets.UTF_8) @@ -103,6 +104,14 @@ class NeteaseAPI { return null val json = json.decodeFromString(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 } } \ No newline at end of file diff --git a/app/src/main/java/pl/lambada/songsync/domain/model/lyrics_providers/others/Netease.kt b/app/src/main/java/pl/lambada/songsync/domain/model/lyrics_providers/others/Netease.kt index 39e62d1..2d0e91e 100644 --- a/app/src/main/java/pl/lambada/songsync/domain/model/lyrics_providers/others/Netease.kt +++ b/app/src/main/java/pl/lambada/songsync/domain/model/lyrics_providers/others/Netease.kt @@ -29,6 +29,7 @@ data class NeteaseArtist( @Serializable data class NeteaseLyricsResponse( val lrc: NeteaseLyrics, + val tlyric: NeteaseLyrics?, val code: Int ) diff --git a/app/src/main/java/pl/lambada/songsync/ui/screens/AboutScreen.kt b/app/src/main/java/pl/lambada/songsync/ui/screens/AboutScreen.kt index cc7e237..0cb8708 100644 --- a/app/src/main/java/pl/lambada/songsync/ui/screens/AboutScreen.kt +++ b/app/src/main/java/pl/lambada/songsync/ui/screens/AboutScreen.kt @@ -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) } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c654b26..5319ad2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -118,4 +118,6 @@ Internal error: %s Disable Marquee Text Disable moving text animation in app + Include translation + Include translated lyrics when getting song lyrics from Netease provider \ No newline at end of file