Skip to content

Commit

Permalink
refactor: revamp db operations
Browse files Browse the repository at this point in the history
  • Loading branch information
zyrouge committed Dec 7, 2024
1 parent f8bdd78 commit e3a4c91
Show file tree
Hide file tree
Showing 14 changed files with 95 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class Settings(private val symphony: Symphony) {
val lastUsedPlaylistsSortBy = EnumEntry(
"last_used_playlists_sort_by",
enumEntries<PlaylistRepository.SortBy>(),
PlaylistRepository.SortBy.TITLE,
PlaylistRepository.SortBy.CUSTOM,
)
val lastUsedPlaylistsSortReverse = BooleanEntry("last_used_playlists_sort_reverse", false)
val lastUsedPlaylistsHorizontalGridColumns = IntEntry(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ interface PlaylistStore {
@Update
suspend fun update(vararg playlist: Playlist): Int

@Query("DELETE FROM playlists WHERE id IN (:playlistIds)")
suspend fun delete(playlistIds: Collection<String>): Int
@Query("DELETE FROM playlists WHERE id = :playlistId")
suspend fun delete(playlistId: String): Int

@Query("SELECT * FROM playlists")
suspend fun entries(): Map<@MapColumn("id") String, Playlist>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,27 @@ class Groove(private val symphony: Symphony) : Symphony.Hooks {
}.join()
}

suspend fun refetch() {
reset()
fetch()
private suspend fun clearCache() {
symphony.database.songCache.clear()
symphony.database.artworkCache.clear()
symphony.database.lyricsCache.clear()
}

data class FetchOptions(
val resetInMemoryCache: Boolean = false,
val resetPersistentCache: Boolean = false,
)

fun fetch(options: FetchOptions) {
coroutineScope.launch {
if (options.resetInMemoryCache) {
reset()
}
if (options.resetPersistentCache) {
clearCache()
}
fetch()
}
}

override fun onSymphonyReady() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ data class Playlist(
companion object {
private const val PRIMARY_STORAGE = "primary:"

fun parse(symphony: Symphony, uri: Uri): Playlist {
fun parse(symphony: Symphony, playlistId: String?, uri: Uri): Playlist {
val file = DocumentFileX.fromSingleUri(symphony.applicationContext, uri)!!
val content = symphony.applicationContext.contentResolver.openInputStream(uri)
?.use { String(it.readBytes()) } ?: ""
val songPaths = content.lineSequence()
.map { it.trim() }
.filter { it.isNotEmpty() && it[0] != '#' }
.toList()
val id = symphony.groove.playlist.idGenerator.next()
val id = playlistId ?: symphony.groove.playlist.idGenerator.next()
val path = DocumentFileX.getParentPathOfSingleUri(file.uri) ?: file.name
return Playlist(
id = id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class PlaylistRepository(private val symphony: Symphony) {
val playlist = when {
x.isLocal -> {
ActivityUtils.makePersistableReadableUri(context, x.uri!!)
Playlist.parse(symphony, x.uri)
Playlist.parse(symphony, x.id, x.uri)
}

else -> x
Expand Down Expand Up @@ -140,33 +140,39 @@ class PlaylistRepository(private val symphony: Symphony) {
path = null,
)

suspend fun add(playlist: Playlist) {
fun add(playlist: Playlist) {
cache[playlist.id] = playlist
_all.update {
it + playlist.id
}
emitUpdateId()
emitCount()
symphony.database.playlists.insert(playlist)
symphony.groove.coroutineScope.launch {
symphony.database.playlists.insert(playlist)
}
}

suspend fun delete(id: String) {
cache.remove(id)?.let {
it.uri?.let { uri ->
runCatching {
ActivityUtils.makePersistableReadableUri(symphony.applicationContext, uri)
}
fun delete(id: String) {
Logger.error(
"PlaylistRepository",
"cache ${cache.containsKey(id)}"
)
cache.remove(id)?.uri?.let {
runCatching {
ActivityUtils.makePersistableReadableUri(symphony.applicationContext, it)
}
}
_all.update {
it - id
}
emitUpdateId()
emitCount()
symphony.database.playlists.delete(listOf(id))
symphony.groove.coroutineScope.launch {
symphony.database.playlists.delete(id)
}
}

suspend fun update(id: String, songIds: List<String>) {
fun update(id: String, songIds: List<String>) {
val playlist = get(id) ?: return
val updated = Playlist(
id = id,
Expand All @@ -183,7 +189,9 @@ class PlaylistRepository(private val symphony: Symphony) {
songIds
}
}
symphony.database.playlists.update(updated)
symphony.groove.coroutineScope.launch {
symphony.database.playlists.update(updated)
}
}

// NOTE: maybe we shouldn't use groove's coroutine scope?
Expand All @@ -193,9 +201,7 @@ class PlaylistRepository(private val symphony: Symphony) {
if (songIds.contains(songId)) {
return
}
symphony.groove.coroutineScope.launch {
update(favorites.id, songIds.mutate { add(songId) })
}
update(favorites.id, songIds.mutate { add(songId) })
}

fun unfavorite(songId: String) {
Expand All @@ -204,9 +210,7 @@ class PlaylistRepository(private val symphony: Symphony) {
if (!songIds.contains(songId)) {
return
}
symphony.groove.coroutineScope.launch {
update(favorites.id, songIds.mutate { remove(songId) })
}
update(favorites.id, songIds.mutate { remove(songId) })
}

fun isFavoritesPlaylist(playlist: Playlist) = playlist.id == FAVORITE_PLAYLIST
Expand All @@ -220,11 +224,13 @@ class PlaylistRepository(private val symphony: Symphony) {
}
}

suspend fun renamePlaylist(playlist: Playlist, title: String) {
fun renamePlaylist(playlist: Playlist, title: String) {
val renamed = playlist.withTitle(title)
cache[playlist.id] = renamed
emitUpdateId()
symphony.database.playlists.update(renamed)
symphony.groove.coroutineScope.launch {
symphony.database.playlists.update(renamed)
}
}

internal fun onScanFinish() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,19 @@ import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.runtime.toMutableStateList
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import io.github.zyrouge.symphony.ui.helpers.ViewContext
import io.github.zyrouge.symphony.utils.mutate
import kotlinx.coroutines.launch

@Composable
fun AddToPlaylistDialog(
context: ViewContext,
songIds: List<String>,
onDismissRequest: () -> Unit,
) {
val coroutineScope = rememberCoroutineScope()
var showNewPlaylistDialog by remember { mutableStateOf(false) }
val allPlaylistsIds by context.symphony.groove.playlist.all.collectAsState()
val playlists by remember(allPlaylistsIds) {
Expand Down Expand Up @@ -83,13 +80,11 @@ fun AddToPlaylistDialog(
)
},
onClick = {
coroutineScope.launch {
context.symphony.groove.playlist.update(
playlist.id,
playlistSongIds.mutate { addAll(songIds) },
)
onDismissRequest()
}
context.symphony.groove.playlist.update(
playlist.id,
playlistSongIds.mutate { addAll(songIds) },
)
onDismissRequest()
}
)
}
Expand All @@ -115,9 +110,7 @@ fun AddToPlaylistDialog(
context = context,
onDone = { playlist ->
showNewPlaylistDialog = false
coroutineScope.launch {
context.symphony.groove.playlist.add(playlist)
}
context.symphony.groove.playlist.add(playlist)
},
onDismissRequest = {
showNewPlaylistDialog = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ fun PlaylistInformationDialog(
InformationDialog(
context,
content = {
InformationKeyValue(context.symphony.t.Id) {
LongPressCopyableText(context, playlist.id)
}
InformationKeyValue(context.symphony.t.Title) {
LongPressCopyableText(context, playlist.title)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand All @@ -51,7 +50,6 @@ import io.github.zyrouge.symphony.ui.helpers.ViewContext
import io.github.zyrouge.symphony.ui.theme.ThemeColors
import io.github.zyrouge.symphony.ui.view.PlaylistViewRoute
import io.github.zyrouge.symphony.utils.Logger
import kotlinx.coroutines.launch

@Composable
fun PlaylistTile(context: ViewContext, playlist: Playlist) {
Expand Down Expand Up @@ -139,7 +137,6 @@ fun PlaylistDropdownMenu(
onDelete: (() -> Unit) = {},
onDismissRequest: () -> Unit,
) {
val coroutineScope = rememberCoroutineScope()
val savePlaylistLauncher = rememberLauncherForActivityResult(
ActivityResultContracts.CreateDocument(MediaExposer.MIMETYPE_M3U)
) { uri ->
Expand Down Expand Up @@ -314,11 +311,9 @@ fun PlaylistDropdownMenu(
context,
selectedSongIds = playlist.getSongIds(context.symphony),
onDone = {
coroutineScope.launch {
context.symphony.groove.playlist.update(playlist.id, it)
onSongsChanged()
showSongsPicker = false
}
context.symphony.groove.playlist.update(playlist.id, it)
onSongsChanged()
showSongsPicker = false
}
)
}
Expand All @@ -333,12 +328,10 @@ fun PlaylistDropdownMenu(
Text(context.symphony.t.AreYouSureThatYouWantToDeleteThisPlaylist)
},
onResult = { result ->
coroutineScope.launch {
showDeleteDialog = false
if (result) {
onDelete()
context.symphony.groove.playlist.delete(playlist.id)
}
showDeleteDialog = false
if (result) {
onDelete()
context.symphony.groove.playlist.delete(playlist.id)
}
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
Expand All @@ -21,7 +20,6 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import io.github.zyrouge.symphony.services.groove.Playlist
import io.github.zyrouge.symphony.ui.helpers.ViewContext
import kotlinx.coroutines.launch

@Composable
fun RenamePlaylistDialog(
Expand All @@ -30,7 +28,6 @@ fun RenamePlaylistDialog(
onRename: () -> Unit = {},
onDismissRequest: () -> Unit,
) {
val coroutineScope = rememberCoroutineScope()
var input by remember { mutableStateOf(playlist.title) }
val focusRequester = remember { FocusRequester() }

Expand Down Expand Up @@ -70,9 +67,7 @@ fun RenamePlaylistDialog(
onClick = {
onRename()
onDismissRequest()
coroutineScope.launch {
context.symphony.groove.playlist.renamePlaylist(playlist, input)
}
context.symphony.groove.playlist.renamePlaylist(playlist, input)
}
) {
Text(context.symphony.t.Done)
Expand Down
7 changes: 3 additions & 4 deletions app/src/main/java/io/github/zyrouge/symphony/ui/view/Home.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ import io.github.zyrouge.symphony.ui.view.home.GenresView
import io.github.zyrouge.symphony.ui.view.home.PlaylistsView
import io.github.zyrouge.symphony.ui.view.home.SongsView
import io.github.zyrouge.symphony.ui.view.home.TreeView
import kotlinx.coroutines.launch
import kotlinx.serialization.Serializable

enum class HomePage(
Expand Down Expand Up @@ -229,9 +228,9 @@ fun HomeView(context: ViewContext) {
onClick = {
showOptionsDropdown = false
context.symphony.radio.stop()
coroutineScope.launch {
context.symphony.groove.refetch()
}
context.symphony.groove.fetch(
Groove.FetchOptions(resetInMemoryCache = true),
)
}
)
DropdownMenuItem(
Expand Down
13 changes: 4 additions & 9 deletions app/src/main/java/io/github/zyrouge/symphony/ui/view/Playlist.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
Expand All @@ -36,7 +35,6 @@ import io.github.zyrouge.symphony.ui.components.TopAppBarMinimalTitle
import io.github.zyrouge.symphony.ui.helpers.ViewContext
import io.github.zyrouge.symphony.ui.theme.ThemeColors
import io.github.zyrouge.symphony.utils.mutate
import kotlinx.coroutines.launch
import kotlinx.serialization.Serializable

@Serializable
Expand All @@ -45,7 +43,6 @@ data class PlaylistViewRoute(val playlistId: String)
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun PlaylistView(context: ViewContext, route: PlaylistViewRoute) {
val coroutineScope = rememberCoroutineScope()
val allPlaylistIds by context.symphony.groove.playlist.all.collectAsState()
val updateId by context.symphony.groove.playlist.updateId.collectAsState()
var updateCounter by remember { mutableIntStateOf(0) }
Expand Down Expand Up @@ -148,12 +145,10 @@ fun PlaylistView(context: ViewContext, route: PlaylistViewRoute) {
},
onClick = {
onDismissRequest()
coroutineScope.launch {
context.symphony.groove.playlist.update(
it.id,
songIds.mutate { remove(song.id) },
)
}
context.symphony.groove.playlist.update(
it.id,
songIds.mutate { remove(song.id) },
)
}
)
}
Expand Down
Loading

0 comments on commit e3a4c91

Please sign in to comment.