Skip to content

Commit

Permalink
Add offset modes (direct/lrc tag)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lambada10 committed Oct 29, 2024
1 parent e0d0732 commit 412fafc
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class UserSettingsController(private val dataStore: DataStore<Preferences>) {
var showPath by mutableStateOf(dataStore.get(showPathKey, false))
private set

var directlyModifyTimestamps by mutableStateOf(dataStore.get(directlyModifyTimestampsKey, false))
private set

var sortOrder by mutableStateOf(
SortOrders.entries
.find { it.queryName == dataStore.get(sortOrderKey, SortOrders.ASCENDING.queryName) }!!
Expand Down Expand Up @@ -119,6 +122,11 @@ class UserSettingsController(private val dataStore: DataStore<Preferences>) {
showPath = to
}

fun updateDirectlyModifyTimestamps(to: Boolean) {
dataStore.set(directlyModifyTimestampsKey, to)
directlyModifyTimestamps = to
}

fun updateSortOrder(to: SortOrders) {
dataStore.set(sortOrderKey, to.queryName)
sortOrder = to
Expand All @@ -142,4 +150,5 @@ private val pureBlackKey = booleanPreferencesKey("pure_black")
private val sdCardPathKey = stringPreferencesKey("sd_card_path")
private val showPathKey = booleanPreferencesKey("show_path")
private val sortOrderKey = stringPreferencesKey("sort_order")
private val sortByKey = stringPreferencesKey("sort_by")
private val sortByKey = stringPreferencesKey("sort_by")
private val directlyModifyTimestampsKey = booleanPreferencesKey("directly_modify_timestamps")
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import pl.lambada.songsync.ui.screens.about.components.CreditsSection
import pl.lambada.songsync.ui.screens.about.components.ExternalLinkSection
import pl.lambada.songsync.ui.screens.about.components.MarqueeSwitch
import pl.lambada.songsync.ui.screens.about.components.MultiPersonSwitch
import pl.lambada.songsync.ui.screens.about.components.OffsetModeSwitch
import pl.lambada.songsync.ui.screens.about.components.PureBlackThemeSwitch
import pl.lambada.songsync.ui.screens.about.components.SdCardPathSetting
import pl.lambada.songsync.ui.screens.about.components.ShowPathSwitch
Expand Down Expand Up @@ -105,6 +106,13 @@ fun AboutScreen(
)
}

item {
OffsetModeSwitch(
selected = userSettingsController.directlyModifyTimestamps,
onToggle = { userSettingsController.updateDirectlyModifyTimestamps(it) }
)
}

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
item {
SdCardPathSetting(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package pl.lambada.songsync.ui.screens.about.components

import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource
import pl.lambada.songsync.R
import pl.lambada.songsync.ui.components.AboutItem
import pl.lambada.songsync.ui.components.SwitchItem

@Composable
fun OffsetModeSwitch(
selected: Boolean,
onToggle: (Boolean) -> Unit
) {
AboutItem(label = stringResource(R.string.offset_mode)) {
SwitchItem(
label = stringResource(R.string.offset_mode_summary),
selected = selected,
onClick = { onToggle(!selected) }
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ fun SharedTransitionScope.LyricsFetchScreen(
viewModel.lrcOffset = 0
viewModel.queryState = QueryStatus.NotSubmitted
},
directOffset = viewModel.userSettingsController.directlyModifyTimestamps,
offset = viewModel.lrcOffset,
onSetOffset = { viewModel.lrcOffset = it },
onSaveLyrics = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class LyricsFetchViewModel(
context: Context,
generatedUsingString: String
) {
val lrcContent = generateLrcContent(song, lyrics, generatedUsingString, lrcOffset)
val lrcContent = generateLrcContent(song, lyrics, generatedUsingString, lrcOffset, userSettingsController.directlyModifyTimestamps)
val file = newLyricsFilePath(filePath, song)

if (!isLegacyFileAccessRequired(filePath)) {
Expand Down Expand Up @@ -127,7 +127,7 @@ class LyricsFetchViewModel(
context: Context,
song: SongInfo
) {
val lrcContent = generateLrcContent(song, lyrics, context.getString(R.string.generated_using), lrcOffset)
val lrcContent = generateLrcContent(song, lyrics, context.getString(R.string.generated_using), lrcOffset, userSettingsController.directlyModifyTimestamps)

runCatching {
embedLyricsInFile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ fun SharedTransitionScope.SuccessContent(
result: SongInfo,
onTryAgain: () -> Unit,
onEdit: () -> Unit,
directOffset: Boolean,
offset: Int,
onSetOffset: (Int) -> Unit,
onSaveLyrics: (String) -> Unit,
Expand Down Expand Up @@ -103,7 +104,13 @@ fun SharedTransitionScope.SuccessContent(
LyricsFetchState.NotSubmitted -> { /* nothing */ }

is LyricsFetchState.Success -> LyricsSuccessContent(
lyrics = applyOffsetToLyrics(it.lyrics, offset),
lyrics = it.lyrics.let {
if (offset != 0 && directOffset) {
applyOffsetToLyrics(it, offset)
} else {
it
}
},
offset = offset,
onSetOffset = onSetOffset,
onSaveLyrics = { onSaveLyrics(it.lyrics) },
Expand Down
20 changes: 14 additions & 6 deletions app/src/main/java/pl/lambada/songsync/util/LyricsUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,17 @@ fun generateLrcContent(
lyrics: String,
generatedUsingString: String,
offset: Int = 0,
directOffset: Boolean
): String {
return ("[ti:${song.songName}]\n" +
val offsetSign = if (offset >= 0) "+" else ""
val offsetStr = if (!directOffset) "[offset:${offsetSign}${offset}]\n" else ""
val lyrics = if (directOffset && offset != 0) applyOffsetToLyrics(lyrics, offset) else lyrics

return "[ti:${song.songName}]\n" +
"[ar:${song.artistName}]\n" +
"[by:$generatedUsingString]\n" + lyrics).let {
if (offset != 0) applyOffsetToLyrics(it, offset) else it
}
offsetStr +
"[by:$generatedUsingString]\n" +
lyrics
}

fun newLyricsFilePath(filePath: String?, song: SongInfo): File {
Expand Down Expand Up @@ -251,7 +256,8 @@ private suspend fun downloadLyricsForSong(
context,
viewModel.userSettingsController.sdCardPath,
songInfo,
it
it,
viewModel.userSettingsController.directlyModifyTimestamps
)

onLyricsSaved()
Expand All @@ -265,13 +271,15 @@ private fun formatAndSaveLyricsForSong(
sdCardPath: String?,
songInfo: SongInfo,
lyrics: String,
directOffset: Boolean
) {
val targetFile = song.filePath.toLrcFile()

val lrcContent = generateLrcContent(
songInfo,
lyrics,
context.getString(R.string.generated_using)
context.getString(R.string.generated_using),
directOffset = directOffset
)

writeLyricsToFile(targetFile, lrcContent, context, song, sdCardPath)
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 @@ -138,4 +138,6 @@
<string name="ascending">Ascending</string>
<string name="descending">Descending</string>
<string name="sort">Sort</string>
<string name="offset_mode">Offset mode</string>
<string name="offset_mode_summary">Directly modify the lyrics timestamps instead of using offset LRC tag</string>
</resources>

0 comments on commit 412fafc

Please sign in to comment.