Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Lambada10 committed Oct 24, 2024
1 parent 6e30fbb commit 0de0fc4
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.stringPreferencesKey
import pl.lambada.songsync.domain.model.SortOrders
import pl.lambada.songsync.domain.model.SortValues
import pl.lambada.songsync.util.Providers
import pl.lambada.songsync.util.get
import pl.lambada.songsync.util.set
Expand Down Expand Up @@ -50,6 +52,18 @@ class UserSettingsController(private val dataStore: DataStore<Preferences>) {
var showPath by mutableStateOf(dataStore.get(showPathKey, false))
private set

var sortOrder by mutableStateOf(
SortOrders.entries
.find { it.queryName == dataStore.get(sortOrderKey, SortOrders.ASCENDING.queryName) }!!
)
private set

var sortBy by mutableStateOf(
SortValues.entries
.find { it.name == dataStore.get(sortByKey, SortValues.TITLE.name) }!!
)
private set

fun updateEmbedLyrics(to: Boolean) {
dataStore.set(embedKey, to)
embedLyricsIntoFiles = to
Expand Down Expand Up @@ -104,6 +118,16 @@ class UserSettingsController(private val dataStore: DataStore<Preferences>) {
dataStore.set(showPathKey, to)
showPath = to
}

fun updateSortOrder(to: SortOrders) {
dataStore.set(sortOrderKey, to.queryName)
sortOrder = to
}

fun updateSortBy(to: SortValues) {
dataStore.set(sortByKey, to.name)
sortBy = to
}
}

private val embedKey = booleanPreferencesKey("embed_lyrics")
Expand All @@ -116,4 +140,6 @@ private val syncedMusixmatchKey = booleanPreferencesKey("synced_lyrics")
private val disableMarqueeKey = booleanPreferencesKey("marquee_disable")
private val pureBlackKey = booleanPreferencesKey("pure_black")
private val sdCardPathKey = stringPreferencesKey("sd_card_path")
private val showPathKey = booleanPreferencesKey("show_path")
private val showPathKey = booleanPreferencesKey("show_path")
private val sortOrderKey = stringPreferencesKey("sort_order")
private val sortByKey = stringPreferencesKey("sort_by")
15 changes: 15 additions & 0 deletions app/src/main/java/pl/lambada/songsync/domain/model/Sort.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package pl.lambada.songsync.domain.model

enum class SortOrders(val queryName: String) {
ASCENDING("ASC"),
DESCENDING("DESC")
}

enum class SortValues {
TITLE,
ARTIST,
ALBUM,
TRACK_NUMBER,
YEAR,
DURATION
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.Button
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
Expand All @@ -36,6 +39,8 @@ import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import pl.lambada.songsync.domain.model.SortOrders
import pl.lambada.songsync.domain.model.SortValues
import pl.lambada.songsync.ui.LyricsFetchScreen
import pl.lambada.songsync.ui.ScreenAbout
import pl.lambada.songsync.ui.screens.home.components.BatchDownloadLyrics
Expand Down Expand Up @@ -65,7 +70,9 @@ fun HomeScreen(
var isBatchDownload by remember { mutableStateOf(false) }
val context = LocalContext.current

SideEffect { viewModel.updateAllSongs(context) }
LaunchedEffect(viewModel.userSettingsController.sortBy to viewModel.userSettingsController.sortOrder) {
viewModel.updateAllSongs(context, viewModel.userSettingsController.sortBy, viewModel.userSettingsController.sortOrder)
}

Scaffold(
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
Expand Down Expand Up @@ -231,6 +238,26 @@ fun HomeScreenLoaded(
}
}

item {
//todo proper ui
Button(
onClick = {
viewModel.userSettingsController.updateSortBy(SortValues.TITLE)
viewModel.userSettingsController.updateSortOrder(SortOrders.ASCENDING)
}
) {
Text("Sort by title")
}
Button(
onClick = {
viewModel.userSettingsController.updateSortBy(SortValues.ARTIST)
viewModel.userSettingsController.updateSortOrder(SortOrders.ASCENDING)
}
) {
Text("Sort by artist")
}
}

items(viewModel.displaySongs.size) { index ->
val song = viewModel.displaySongs[index]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import pl.lambada.songsync.data.remote.UserSettingsController
import pl.lambada.songsync.data.remote.lyrics_providers.LyricsProviderService
import pl.lambada.songsync.domain.model.Song
import pl.lambada.songsync.domain.model.SongInfo
import pl.lambada.songsync.domain.model.SortOrders
import pl.lambada.songsync.domain.model.SortValues
import pl.lambada.songsync.util.downloadLyrics
import pl.lambada.songsync.util.ext.toLrcFile

Expand Down Expand Up @@ -90,16 +92,16 @@ class HomeViewModel(
}
}

fun updateAllSongs(context: Context) = viewModelScope.launch(Dispatchers.IO) {
allSongs = getAllSongs(context)
fun updateAllSongs(context: Context, sortBy: SortValues, sortOrder: SortOrders) = viewModelScope.launch(Dispatchers.IO) {
allSongs = getAllSongs(context, sortBy, sortOrder)
}

/**
* Loads all songs from the MediaStore.
* @param context The application context.
* @return A list of Song objects representing the songs.
*/
private fun getAllSongs(context: Context): List<Song> {
private fun getAllSongs(context: Context, sortBy: SortValues, sortOrder: SortOrders): List<Song> {
return cachedSongs ?: run {
val selection = MediaStore.Audio.Media.IS_MUSIC + "!= 0"
val projection = arrayOf(
Expand All @@ -109,7 +111,7 @@ class HomeViewModel(
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.ALBUM_ID,
)
val sortOrder = MediaStore.Audio.Media.TITLE + " ASC"
val sortOrder = sortBy.name + " " + sortOrder.queryName

val songs = mutableListOf<Song>()
val cursor = context.contentResolver.query(
Expand Down Expand Up @@ -189,7 +191,7 @@ class HomeViewModel(
return cachedFolders ?: run {
val folders = mutableListOf<String>()

for (song in getAllSongs(context)) {
for (song in getAllSongs(context, SortValues.TITLE, SortOrders.ASCENDING)) {
val path = song.filePath
val folder = path?.substring(0, path.lastIndexOf("/"))
if (folder != null && !folders.contains(folder))
Expand Down

0 comments on commit 0de0fc4

Please sign in to comment.