Skip to content

Commit

Permalink
Merge pull request #102 from Lambada10/offset-mode
Browse files Browse the repository at this point in the history
Offset mode
  • Loading branch information
Lambada10 authored Nov 1, 2024
2 parents 7fb1a85 + 7ce22bc commit 98dfb34
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 11 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 @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import androidx.navigation.NavController
import pl.lambada.songsync.R
import pl.lambada.songsync.data.remote.UpdateState
import pl.lambada.songsync.data.remote.UserSettingsController
import pl.lambada.songsync.ui.screens.about.components.OffsetModeSwitch
import pl.lambada.songsync.ui.screens.settings.components.SettingsScreenTopBar
import pl.lambada.songsync.ui.screens.settings.components.AppInfoSection
import pl.lambada.songsync.ui.screens.settings.components.ContributorsSection
Expand Down Expand Up @@ -105,6 +106,13 @@ fun SettingsScreen(
)
}

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.SettingsItem
import pl.lambada.songsync.ui.components.SwitchItem

@Composable
fun OffsetModeSwitch(
selected: Boolean,
onToggle: (Boolean) -> Unit
) {
SettingsItem(label = stringResource(R.string.offset_mode)) {
SwitchItem(
label = stringResource(R.string.offset_mode_summary),
selected = selected,
onClick = { onToggle(!selected) }
)
}
}
22 changes: 15 additions & 7 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 Expand Up @@ -316,7 +324,7 @@ fun applyOffsetToLyrics(lyrics: String, offset: Int): String {

fun applyOffset(minute: Int, second: Int, millisecond: Int): String {
val totalMilliseconds = (minute * 60 * 1000) + (second * 1000) + (millisecond * 10) + offset
if (totalMilliseconds < 0) return "[00:00.000]" // Prevent negative times
if (totalMilliseconds < 0) return "00:00.000" // Prevent negative times

val newMinutes = (totalMilliseconds / 60000) % 60
val newSeconds = (totalMilliseconds / 1000) % 60
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,5 +138,7 @@
<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>
<string name="settings">Settings</string>
</resources>

0 comments on commit 98dfb34

Please sign in to comment.