Skip to content

Commit

Permalink
refactor: better persistent & cache database
Browse files Browse the repository at this point in the history
  • Loading branch information
zyrouge committed Oct 26, 2024
1 parent 25338c9 commit 3e0605d
Show file tree
Hide file tree
Showing 67 changed files with 776 additions and 768 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ app/src/main/assets/i18n
dist
secrets
.kotlin
app/room-schemas/

10 changes: 10 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ plugins {
alias(libs.plugins.android.kotlin)
alias(libs.plugins.compose.compiler)
alias(libs.plugins.kotlin.serialization)
alias(libs.plugins.ksp)
alias(libs.plugins.room)
}

android {
Expand Down Expand Up @@ -32,6 +34,7 @@ android {
keyPassword = System.getenv("SIGNING_KEY_PASSWORD")
}
}

buildTypes {
getByName("release") {
isMinifyEnabled = true
Expand Down Expand Up @@ -76,6 +79,10 @@ android {
it.useJUnitPlatform()
}
}

room {
schemaDirectory("$projectDir/room-schemas")
}
}

dependencies {
Expand All @@ -93,6 +100,9 @@ dependencies {
implementation(libs.lifecycle.runtime)
implementation(libs.media)
implementation(libs.okhttp3)
ksp(libs.room.compiler)
implementation(libs.room.ktx)
implementation(libs.room.runtime)

debugImplementation(libs.compose.ui.tooling)
debugImplementation(libs.compose.ui.test.manifest)
Expand Down
58 changes: 29 additions & 29 deletions app/src/main/java/io/github/zyrouge/symphony/services/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import android.net.Uri
import android.os.Environment
import androidx.core.content.edit
import io.github.zyrouge.symphony.Symphony
import io.github.zyrouge.symphony.services.groove.AlbumArtistSortBy
import io.github.zyrouge.symphony.services.groove.AlbumSortBy
import io.github.zyrouge.symphony.services.groove.ArtistSortBy
import io.github.zyrouge.symphony.services.groove.GenreSortBy
import io.github.zyrouge.symphony.services.groove.PathSortBy
import io.github.zyrouge.symphony.services.groove.PlaylistSortBy
import io.github.zyrouge.symphony.services.groove.SongSortBy
import io.github.zyrouge.symphony.services.groove.AlbumArtistRepository
import io.github.zyrouge.symphony.services.groove.AlbumRepository
import io.github.zyrouge.symphony.services.groove.ArtistRepository
import io.github.zyrouge.symphony.services.groove.GenreRepository
import io.github.zyrouge.symphony.services.groove.PlaylistRepository
import io.github.zyrouge.symphony.services.groove.SongRepository
import io.github.zyrouge.symphony.services.radio.RadioQueue
import io.github.zyrouge.symphony.ui.theme.ThemeMode
import io.github.zyrouge.symphony.ui.view.HomePageBottomBarLabelVisibility
import io.github.zyrouge.symphony.ui.view.HomePages
import io.github.zyrouge.symphony.ui.view.NowPlayingControlsLayout
import io.github.zyrouge.symphony.ui.view.NowPlayingLyricsLayout
import io.github.zyrouge.symphony.ui.view.home.ForYou
import io.github.zyrouge.symphony.utils.StringListUtils
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
Expand Down Expand Up @@ -91,17 +91,17 @@ object SettingsKeys {
object SettingsDefaults {
val themeMode = ThemeMode.SYSTEM
const val useMaterialYou = true
val lastUsedSongSortBy = SongSortBy.TITLE
val lastUsedArtistsSortBy = ArtistSortBy.ARTIST_NAME
val lastUsedAlbumArtistsSortBy = AlbumArtistSortBy.ARTIST_NAME
val lastUsedAlbumsSortBy = AlbumSortBy.ALBUM_NAME
val lastUsedGenresSortBy = GenreSortBy.GENRE
val lastUsedBrowserSortBy = SongSortBy.FILENAME
val lastUsedPlaylistsSortBy = PlaylistSortBy.TITLE
val lastUsedPlaylistSongsSortBy = SongSortBy.CUSTOM
val lastUsedAlbumSongsSortBy = SongSortBy.TRACK_NUMBER
val lastUsedTreePathSortBy = PathSortBy.NAME
val lastUsedFoldersSortBy = PathSortBy.NAME
val lastUsedSongSortBy = SongRepository.SortBy.TITLE
val lastUsedArtistsSortBy = ArtistRepository.SortBy.ARTIST_NAME
val lastUsedAlbumArtistsSortBy = AlbumArtistRepository.SortBy.ARTIST_NAME
val lastUsedAlbumsSortBy = AlbumRepository.SortBy.ALBUM_NAME
val lastUsedGenresSortBy = GenreRepository.SortBy.GENRE
val lastUsedBrowserSortBy = SongRepository.SortBy.FILENAME
val lastUsedPlaylistsSortBy = PlaylistRepository.SortBy.TITLE
val lastUsedPlaylistSongsSortBy = SongRepository.SortBy.CUSTOM
val lastUsedAlbumSongsSortBy = SongRepository.SortBy.TRACK_NUMBER
val lastUsedTreePathSortBy = StringListUtils.SortBy.NAME
val lastUsedFoldersSortBy = StringListUtils.SortBy.NAME
const val checkForUpdates = false
const val fadePlayback = false
const val requireAudioFocus = true
Expand Down Expand Up @@ -363,7 +363,7 @@ class SettingsManager(private val symphony: Symphony) {
.getEnum(SettingsKeys.lastUsedSongsSortBy, null)
?: SettingsDefaults.lastUsedSongSortBy

fun setLastUsedSongsSortBy(sortBy: SongSortBy) {
fun setLastUsedSongsSortBy(sortBy: SongRepository.SortBy) {
getSharedPreferences().edit {
putEnum(SettingsKeys.lastUsedSongsSortBy, sortBy)
}
Expand All @@ -384,7 +384,7 @@ class SettingsManager(private val symphony: Symphony) {
.getEnum(SettingsKeys.lastUsedArtistsSortBy, null)
?: SettingsDefaults.lastUsedArtistsSortBy

fun setLastUsedArtistsSortBy(sortBy: ArtistSortBy) {
fun setLastUsedArtistsSortBy(sortBy: ArtistRepository.SortBy) {
getSharedPreferences().edit {
putEnum(SettingsKeys.lastUsedArtistsSortBy, sortBy)
}
Expand All @@ -405,7 +405,7 @@ class SettingsManager(private val symphony: Symphony) {
.getEnum(SettingsKeys.lastUsedAlbumArtistsSortBy, null)
?: SettingsDefaults.lastUsedAlbumArtistsSortBy

fun setLastUsedAlbumArtistsSortBy(sortBy: AlbumArtistSortBy) {
fun setLastUsedAlbumArtistsSortBy(sortBy: AlbumArtistRepository.SortBy) {
getSharedPreferences().edit {
putEnum(SettingsKeys.lastUsedAlbumArtistsSortBy, sortBy)
}
Expand All @@ -426,7 +426,7 @@ class SettingsManager(private val symphony: Symphony) {
.getEnum(SettingsKeys.lastUsedAlbumsSortBy, null)
?: SettingsDefaults.lastUsedAlbumsSortBy

fun setLastUsedAlbumsSortBy(sortBy: AlbumSortBy) {
fun setLastUsedAlbumsSortBy(sortBy: AlbumRepository.SortBy) {
getSharedPreferences().edit {
putEnum(SettingsKeys.lastUsedAlbumsSortBy, sortBy)
}
Expand All @@ -447,7 +447,7 @@ class SettingsManager(private val symphony: Symphony) {
.getEnum(SettingsKeys.lastUsedGenresSortBy, null)
?: SettingsDefaults.lastUsedGenresSortBy

fun setLastUsedGenresSortBy(sortBy: GenreSortBy) {
fun setLastUsedGenresSortBy(sortBy: GenreRepository.SortBy) {
getSharedPreferences().edit {
putEnum(SettingsKeys.lastUsedGenresSortBy, sortBy)
}
Expand All @@ -468,7 +468,7 @@ class SettingsManager(private val symphony: Symphony) {
.getEnum(SettingsKeys.lastUsedBrowserSortBy, null)
?: SettingsDefaults.lastUsedBrowserSortBy

fun setLastUsedBrowserSortBy(sortBy: SongSortBy) {
fun setLastUsedBrowserSortBy(sortBy: SongRepository.SortBy) {
getSharedPreferences().edit {
putEnum(SettingsKeys.lastUsedBrowserSortBy, sortBy)
}
Expand Down Expand Up @@ -500,7 +500,7 @@ class SettingsManager(private val symphony: Symphony) {
.getEnum(SettingsKeys.lastUsedPlaylistsSortBy, null)
?: SettingsDefaults.lastUsedPlaylistsSortBy

fun setLastUsedPlaylistsSortBy(sortBy: PlaylistSortBy) {
fun setLastUsedPlaylistsSortBy(sortBy: PlaylistRepository.SortBy) {
getSharedPreferences().edit {
putEnum(SettingsKeys.lastUsedPlaylistsSortBy, sortBy)
}
Expand All @@ -521,7 +521,7 @@ class SettingsManager(private val symphony: Symphony) {
.getEnum(SettingsKeys.lastUsedPlaylistSongsSortBy, null)
?: SettingsDefaults.lastUsedPlaylistSongsSortBy

fun setLastUsedPlaylistSongsSortBy(sortBy: SongSortBy) {
fun setLastUsedPlaylistSongsSortBy(sortBy: SongRepository.SortBy) {
getSharedPreferences().edit {
putEnum(SettingsKeys.lastUsedPlaylistSongsSortBy, sortBy)
}
Expand All @@ -542,7 +542,7 @@ class SettingsManager(private val symphony: Symphony) {
.getEnum(SettingsKeys.lastUsedAlbumSongsSortBy, null)
?: SettingsDefaults.lastUsedAlbumSongsSortBy

fun setLastUsedAlbumSongsSortBy(sortBy: SongSortBy) {
fun setLastUsedAlbumSongsSortBy(sortBy: SongRepository.SortBy) {
getSharedPreferences().edit {
putEnum(SettingsKeys.lastUsedAlbumSongsSortBy, sortBy)
}
Expand All @@ -563,7 +563,7 @@ class SettingsManager(private val symphony: Symphony) {
.getEnum(SettingsKeys.lastUsedTreePathSortBy, null)
?: SettingsDefaults.lastUsedTreePathSortBy

fun setLastUsedTreePathSortBy(sortBy: PathSortBy) {
fun setLastUsedTreePathSortBy(sortBy: StringListUtils.SortBy) {
getSharedPreferences().edit {
putEnum(SettingsKeys.lastUsedTreePathSortBy, sortBy)
}
Expand All @@ -584,7 +584,7 @@ class SettingsManager(private val symphony: Symphony) {
.getEnum(SettingsKeys.lastUsedFoldersSortBy, null)
?: SettingsDefaults.lastUsedFoldersSortBy

fun setLastUsedFoldersSortBy(sortBy: PathSortBy) {
fun setLastUsedFoldersSortBy(sortBy: StringListUtils.SortBy) {
getSharedPreferences().edit {
putEnum(SettingsKeys.lastUsedFoldersSortBy, sortBy)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.github.zyrouge.symphony.services.database

import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import io.github.zyrouge.symphony.Symphony
import io.github.zyrouge.symphony.services.database.store.SongCacheStore
import io.github.zyrouge.symphony.services.groove.Song
import io.github.zyrouge.symphony.utils.RoomConvertors

@Database(entities = [Song::class], version = 1)
@TypeConverters(RoomConvertors::class)
abstract class CacheDatabase : RoomDatabase() {
abstract fun songs(): SongCacheStore

companion object {
fun create(symphony: Symphony) = Room
.databaseBuilder(
symphony.applicationContext,
CacheDatabase::class.java,
"cache"
)
.build()
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package io.github.zyrouge.symphony.services.database

import io.github.zyrouge.symphony.Symphony
import io.github.zyrouge.symphony.services.database.store.ArtworkCacheStore
import io.github.zyrouge.symphony.services.database.store.LyricsCacheStore

class Database(symphony: Symphony) {
val songCache = SongCache(symphony)
val artworkCache = ArtworkCache(symphony)
val lyricsCache = LyricsCache(symphony)
val playlists = PlaylistBox(symphony)
private val cache = CacheDatabase.create(symphony)
private val persistent = PersistentDatabase.create(symphony)

val artworkCache = ArtworkCacheStore(symphony)
val lyricsCache = LyricsCacheStore(symphony)
val songCache get() = cache.songs()
val playlists get() = persistent.playlists()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.github.zyrouge.symphony.services.database

import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import io.github.zyrouge.symphony.Symphony
import io.github.zyrouge.symphony.services.database.store.PlaylistStore
import io.github.zyrouge.symphony.services.groove.Playlist
import io.github.zyrouge.symphony.utils.RoomConvertors

@Database(entities = [Playlist::class], version = 1)
@TypeConverters(RoomConvertors::class)
abstract class PersistentDatabase : RoomDatabase() {
abstract fun playlists(): PlaylistStore

companion object {
fun create(symphony: Symphony) = Room
.databaseBuilder(
symphony.applicationContext,
PersistentDatabase::class.java,
"persistent"
)
.build()
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package io.github.zyrouge.symphony.services.database
package io.github.zyrouge.symphony.services.database.store

import io.github.zyrouge.symphony.Symphony
import io.github.zyrouge.symphony.services.database.adapters.FileTreeDatabaseAdapter
import java.nio.file.Paths

class ArtworkCache(val symphony: Symphony) {
class ArtworkCacheStore(val symphony: Symphony) {
private val adapter = FileTreeDatabaseAdapter(
Paths
.get(symphony.applicationContext.dataDir.absolutePath, "covers")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package io.github.zyrouge.symphony.services.database
package io.github.zyrouge.symphony.services.database.store

import io.github.zyrouge.symphony.Symphony
import io.github.zyrouge.symphony.services.database.adapters.SQLiteKeyValueDatabaseAdapter

class LyricsCache(val symphony: Symphony) {
class LyricsCacheStore(val symphony: Symphony) {
private val adapter = SQLiteKeyValueDatabaseAdapter(
SQLiteKeyValueDatabaseAdapter.Transformer.AsString(),
SQLiteKeyValueDatabaseAdapter.CacheOpenHelper(symphony.applicationContext, "lyrics", 1)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.github.zyrouge.symphony.services.database.store

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.MapColumn
import androidx.room.Query
import androidx.room.Update
import io.github.zyrouge.symphony.services.groove.Playlist

@Dao
interface PlaylistStore {
@Insert
suspend fun insert(vararg playlist: Playlist)

@Update
suspend fun update(vararg playlist: Playlist)

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

@Query("SELECT * FROM playlists")
suspend fun entries(): Map<@MapColumn("id") String, Playlist>
}
Loading

0 comments on commit 3e0605d

Please sign in to comment.