Skip to content

Commit

Permalink
feat(dataStore): Add endless queue setting
Browse files Browse the repository at this point in the history
feat(ui): Implement endless queue switch in settings

feat(strings): Add endless queue string resource

feat(service): Implement endless queue logic in service
  • Loading branch information
maxrave-dev committed Jan 29, 2025
1 parent d5ffc4a commit 8d4408f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,25 @@ class DataStoreManager(
}
}

val endlessQueue =
settingsDataStore.data.map { preferences ->
preferences[ENDLESS_QUEUE] ?: FALSE
}

suspend fun setEndlessQueue(endlessQueue: Boolean) {
withContext(Dispatchers.IO) {
if (endlessQueue) {
settingsDataStore.edit { settings ->
settings[ENDLESS_QUEUE] = TRUE
}
} else {
settingsDataStore.edit { settings ->
settings[ENDLESS_QUEUE] = FALSE
}
}
}
}

companion object Settings {
val COOKIE = stringPreferencesKey("cookie")
val LOGGED_IN = stringPreferencesKey("logged_in")
Expand Down Expand Up @@ -691,6 +710,7 @@ class DataStoreManager(
val PROXY_TYPE = stringPreferencesKey("proxy_type")
val PROXY_HOST = stringPreferencesKey("proxy_host")
val PROXY_PORT = intPreferencesKey("proxy_port")
val ENDLESS_QUEUE = stringPreferencesKey("endless_queue")
const val REPEAT_MODE_OFF = "REPEAT_MODE_OFF"
const val REPEAT_ONE = "REPEAT_ONE"
const val REPEAT_ALL = "REPEAT_ALL"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1039,15 +1039,28 @@ class SimpleMediaServiceHandler(
if (list != null) {
Log.w("Check loadMore response", response.toString())
loadMoreCatalog(list)
_queueData.value =
_queueData.value?.copy(
continuation = response.second,
)
} else {
_queueData.value =
_queueData.value?.copy(
continuation = null,
)
if (runBlocking { dataStoreManager.endlessQueue.first() } == TRUE) {
Log.w(TAG, "loadMore: Endless Queue")
val lastTrack = queueData.value?.listTracks?.lastOrNull() ?: return@launch
getRelated(lastTrack.videoId)
}
}
_queueData.value =
_queueData.value?.copy(
continuation = response?.second,
)
// loadingMore.value = false
}
}
}
} else if (runBlocking { dataStoreManager.endlessQueue.first() } == TRUE) {
Log.w(TAG, "loadMore: Endless Queue")
val lastTrack = queueData.value?.listTracks?.lastOrNull() ?: return
getRelated(lastTrack.videoId)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import androidx.compose.material3.IconButton
import androidx.compose.material3.ModalBottomSheet
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.RadioButton
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.TopAppBar
Expand Down Expand Up @@ -128,6 +129,7 @@ fun QueueBottomSheet(
onDismiss: () -> Unit,
sharedViewModel: SharedViewModel = viewModel<SharedViewModel>(),
musicServiceHandler: SimpleMediaServiceHandler = koinInject(),
dataStoreManager: DataStoreManager = koinInject(),
) {
val coroutineScope = rememberCoroutineScope()
val localDensity = LocalDensity.current
Expand All @@ -146,6 +148,7 @@ fun QueueBottomSheet(
.mapLatest { it?.listTracks?.toList() ?: emptyList() }
.collectAsState(emptyList())
val loadMoreState by musicServiceHandler.stateFlow.collectAsState()
val endlessQueueEnable by dataStoreManager.endlessQueue.map { it == DataStoreManager.TRUE }.collectAsState(false)

val shouldLoadMore =
remember {
Expand Down Expand Up @@ -288,11 +291,32 @@ fun QueueBottomSheet(
isPlaying = false,
modifier = Modifier.fillMaxWidth().padding(10.dp),
)
Text(
text = stringResource(R.string.queue),
style = typo.titleMedium,
modifier = Modifier.padding(horizontal = 20.dp),
)
Row(verticalAlignment = Alignment.CenterVertically) {
Text(
text = stringResource(R.string.queue),
style = typo.titleMedium,
modifier =
Modifier
.padding(horizontal = 20.dp)
.weight(1f),
)
Row(verticalAlignment = Alignment.CenterVertically) {
Text(
text = stringResource(R.string.endless_queue),
style = typo.bodySmall,
modifier = Modifier.padding(horizontal = 8.dp),
)
Switch(
checked = endlessQueueEnable,
onCheckedChange = {
coroutineScope.launch {
dataStoreManager.setEndlessQueue(it)
}
},
)
Spacer(modifier = Modifier.width(10.dp))
}
}
Spacer(modifier = Modifier.height(10.dp))
LazyColumn(
horizontalAlignment = Alignment.Start,
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -378,4 +378,5 @@
<string name="lrclib" translatable="false">LRCLIB</string>
<string name="lyrics_provider_lrc">Lyrics provided by LRCLIB</string>
<string name="sync_first">Sync this playlist to YouTube Music first</string>
<string name="endless_queue">Endless queue</string>
</resources>

0 comments on commit 8d4408f

Please sign in to comment.