From 46a5e1af02e1b02efacf5441355409f6324904e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Font=C3=A1n?= Date: Tue, 13 Aug 2024 18:27:19 +0200 Subject: [PATCH] feat: add embed lyrics in file button Adds a button to embed lyrics in the song file. Removes the embed lyrics in file preference. --- .../lambada/songsync/ui/screens/HomeScreen.kt | 29 ++++---- .../songsync/ui/screens/SearchScreen.kt | 68 +++++++++++++------ app/src/main/res/values/strings.xml | 2 + 3 files changed, 63 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/pl/lambada/songsync/ui/screens/HomeScreen.kt b/app/src/main/java/pl/lambada/songsync/ui/screens/HomeScreen.kt index 46390a3..fcbfe57 100644 --- a/app/src/main/java/pl/lambada/songsync/ui/screens/HomeScreen.kt +++ b/app/src/main/java/pl/lambada/songsync/ui/screens/HomeScreen.kt @@ -1224,24 +1224,21 @@ fun BatchDownloadLyrics(songs: List, viewModel: MainViewModel, onDone: () } } } - if (viewModel.embedLyricsInFile) { - viewModel.embedLyricsInFile(context, song.filePath, lrc) - } else { - sdCardFiles?.listFiles()?.forEach { - if (it.name == file.name) { - it.delete() - return@forEach - } - } - sdCardFiles?.createFile( - "text/lrc", file.name - )?.let { - val outputStream = - context.contentResolver.openOutputStream(it.uri) - outputStream?.write(lrc.toByteArray()) - outputStream?.close() + //In here we won't try to embed lyrics in file because if it failed before, it will fail again + sdCardFiles?.listFiles()?.forEach { + if (it.name == file.name) { + it.delete() + return@forEach } } + sdCardFiles?.createFile( + "text/lrc", file.name + )?.let { + val outputStream = + context.contentResolver.openOutputStream(it.uri) + outputStream?.write(lrc.toByteArray()) + outputStream?.close() + } } else { throw e } diff --git a/app/src/main/java/pl/lambada/songsync/ui/screens/SearchScreen.kt b/app/src/main/java/pl/lambada/songsync/ui/screens/SearchScreen.kt index 7a022ef..d5eb50a 100644 --- a/app/src/main/java/pl/lambada/songsync/ui/screens/SearchScreen.kt +++ b/app/src/main/java/pl/lambada/songsync/ui/screens/SearchScreen.kt @@ -343,11 +343,7 @@ fun SharedTransitionScope.SearchScreen( "SongSync/${result.songName} - ${result.artistName}.lrc" ) if (!isLegacyVersion || isInternalStorage) { - if (viewModel.embedLyricsInFile) viewModel.embedLyricsInFile( - context, filePath - ?: throw IllegalArgumentException("File path must not be null"), - lyrics = lrc - ) else file.writeText(lrc) + file.writeText(lrc) } else { val sd = context.externalCacheDirs[1].absolutePath.substring( @@ -400,26 +396,58 @@ fun SharedTransitionScope.SearchScreen( ) { Text(text = stringResource(R.string.save_lrc_file)) } - val clipboardManager = LocalClipboardManager.current - val copiedString = - stringResource(R.string.lyrics_copied_to_clipboard) - OutlinedButton( + Button( onClick = { - clipboardManager.setText(AnnotatedString(lyrics)) - Toast.makeText( - context, - copiedString, - Toast.LENGTH_SHORT - ).show() - } + val lrc = + "[ti:${result.songName}]\n" + "[ar:${result.artistName}]\n" + "[by:$generatedUsingString]\n" + lyrics + + val embeddedToFile = kotlin.runCatching { + viewModel.embedLyricsInFile( + context, + filePath ?: throw NullPointerException("filePath is null"), + lrc + ) + } + + if(embeddedToFile.isFailure) { + Toast.makeText( + context, + embeddedToFile.exceptionOrNull()?.message ?: context.getString(R.string.error), + Toast.LENGTH_LONG + ).show() + return@Button + } else { + Toast.makeText( + context, + context.getString(R.string.embedded_lyrics_in_file), + Toast.LENGTH_LONG + ).show() + } + }, ) { - Icon( - imageVector = Icons.Default.ContentCopy, - contentDescription = stringResource(R.string.copy_lyrics_to_clipboard) - ) + Text(text = stringResource(R.string.embed_lyrics_in_file)) } } + val clipboardManager = LocalClipboardManager.current + val copiedString = + stringResource(R.string.lyrics_copied_to_clipboard) + OutlinedButton( + onClick = { + clipboardManager.setText(AnnotatedString(lyrics)) + Toast.makeText( + context, + copiedString, + Toast.LENGTH_SHORT + ).show() + } + ) { + Icon( + imageVector = Icons.Default.ContentCopy, + contentDescription = stringResource(R.string.copy_lyrics_to_clipboard) + ) + } + Spacer(modifier = Modifier.height(6.dp)) OutlinedCard( modifier = Modifier.fillMaxWidth(), diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5319ad2..57d7aa7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -120,4 +120,6 @@ Disable moving text animation in app Include translation Include translated lyrics when getting song lyrics from Netease provider + Embed lyrics to file + Lyrics has been embeded to the song file \ No newline at end of file