Skip to content

Commit

Permalink
feat: add embed lyrics in file button
Browse files Browse the repository at this point in the history
Adds a button to embed lyrics in the song file.
Removes the embed lyrics in file preference.
  • Loading branch information
BobbyESP committed Aug 13, 2024
1 parent 57a22ae commit 46a5e1a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 36 deletions.
29 changes: 13 additions & 16 deletions app/src/main/java/pl/lambada/songsync/ui/screens/HomeScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1224,24 +1224,21 @@ fun BatchDownloadLyrics(songs: List<Song>, 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
}
Expand Down
68 changes: 48 additions & 20 deletions app/src/main/java/pl/lambada/songsync/ui/screens/SearchScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(),
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 @@ -120,4 +120,6 @@
<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>
<string name="embed_lyrics_in_file">Embed lyrics to file</string>
<string name="embedded_lyrics_in_file">Lyrics has been embeded to the song file</string>
</resources>

0 comments on commit 46a5e1a

Please sign in to comment.