added room and starting making database classes

master
Isaac Iwasaki 3 months ago
parent 1956170e33
commit 6fcb1a7d39

@ -3,6 +3,7 @@ plugins {
alias(libs.plugins.kotlin.android) alias(libs.plugins.kotlin.android)
alias(libs.plugins.compose.compiler) alias(libs.plugins.compose.compiler)
id("org.jetbrains.kotlin.plugin.serialization") version "2.0.20" id("org.jetbrains.kotlin.plugin.serialization") version "2.0.20"
id("com.google.devtools.ksp")
} }
android { android {
@ -64,6 +65,12 @@ dependencies {
implementation(libs.androidx.media3.ui) implementation(libs.androidx.media3.ui)
implementation(libs.androidx.media3.common) implementation(libs.androidx.media3.common)
implementation(libs.androidx.media3.session) implementation(libs.androidx.media3.session)
implementation(libs.androidx.room.runtime)
implementation(libs.com.google.devtools.ksp.gradle.plugin)
ksp(libs.androidx.room.compiler)
annotationProcessor(libs.androidx.room.compiler)
debugImplementation(libs.androidx.ui.tooling) debugImplementation(libs.androidx.ui.tooling)
debugImplementation(libs.androidx.ui.test.manifest) debugImplementation(libs.androidx.ui.test.manifest)

@ -36,8 +36,11 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import androidx.media3.common.util.UnstableApi import androidx.media3.common.util.UnstableApi
import androidx.room.Room
import kotlinx.serialization.encodeToString import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import usr.empty.player.database.AppDatabase
import usr.empty.player.database.Track
import usr.empty.player.ui.theme.PlayerTheme import usr.empty.player.ui.theme.PlayerTheme
@ -52,6 +55,12 @@ class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
// Log.d("meow", "${filesDir.toPath().resolve("all_tracks.data")}")
val db = Room.databaseBuilder(
applicationContext, AppDatabase::class.java, "local"
).allowMainThreadQueries().build()
Log.d("meow", "${db.trackDao().getAll()}")
if (!Environment.isExternalStorageManager()) { if (!Environment.isExternalStorageManager()) {
val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION) val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
val uri = Uri.fromParts("package", packageName, null) val uri = Uri.fromParts("package", packageName, null)
@ -92,12 +101,21 @@ fun MainLayout(modifier: Modifier = Modifier) {
audioUri?.run { audioUri?.run {
uriToPath(this).let { uriToPath(this).let {
MediaMetadataRetriever().apply { MediaMetadataRetriever().apply {
setDataSource(it) setDataSource(it) // notas.add(NotaDescriptor(name = nullifyException {
notas.add(NotaDescriptor(name = nullifyException { // extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE)
// } ?: it.split('/').last().split('.').first(), artist = nullifyException {
// extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST)
// } ?: "Unknown Artist", sourceType = NotaDescriptor.Source.LOCAL, source = it))
val db = Room.databaseBuilder(
context.applicationContext, AppDatabase::class.java, "local"
).allowMainThreadQueries().build()
db.trackDao().insertTrack(Track(title = nullifyException {
extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE) extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE)
} ?: it.split('/').last().split('.').first(), artist = nullifyException { } ?: it.split('/').last().split('.').first(),
extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST) artistId = 0,
} ?: "Unknown Artist", sourceType = NotaDescriptor.Source.LOCAL, source = it)) albumId = null,
sourceType = NotaDescriptor.Source.LOCAL,
source = it))
} }
} }
} }

@ -0,0 +1,11 @@
package usr.empty.player.database
import androidx.room.Database
import androidx.room.RoomDatabase
@Database(
entities = [Track::class], version = 1
)
abstract class AppDatabase : RoomDatabase() {
abstract fun trackDao(): TrackDao
}

@ -0,0 +1,41 @@
package usr.empty.player.database
import androidx.room.ColumnInfo
import androidx.room.Dao
import androidx.room.Entity
import androidx.room.Insert
import androidx.room.PrimaryKey
import androidx.room.Query
import usr.empty.player.NotaDescriptor
import java.util.UUID
// a.k.a. Nota
@Entity
data class Track(
@ColumnInfo val title: String,
@ColumnInfo val artistId: Int,
@ColumnInfo val albumId: Int?,
@ColumnInfo val sourceType: NotaDescriptor.Source,
@ColumnInfo val source: String,
) {
@PrimaryKey(autoGenerate = true)
var uid: Int = 0
@ColumnInfo
var uuid: UUID = UUID.nameUUIDFromBytes("${title}${artistId}".toByteArray())
}
@Dao
interface TrackDao {
@Insert
fun insertTrack(track: Track)
@Query("SELECT * FROM track")
fun getAll(): List<Track>
@Query("SELECT * FROM track WHERE :artistId = artistId")
fun getByArtistId(artistId: Int): List<Track>
@Query("SELECT * FROM track WHERE :albumId = albumId")
fun getByAlbumId(albumId: Int): List<Track>
}

@ -2,4 +2,5 @@
plugins { plugins {
alias(libs.plugins.android.application) apply false alias(libs.plugins.android.application) apply false
alias(libs.plugins.kotlin.android) apply false alias(libs.plugins.kotlin.android) apply false
id("com.google.devtools.ksp") version "2.0.20-1.0.25" apply false
} }

@ -5,8 +5,9 @@ coreKtx = "1.13.1"
kotlinxSerializationJson = "1.7.3" kotlinxSerializationJson = "1.7.3"
lifecycleRuntimeKtx = "2.8.6" lifecycleRuntimeKtx = "2.8.6"
activityCompose = "1.9.2" activityCompose = "1.9.2"
composeBom = "2024.09.02" composeBom = "2024.09.03"
media3Exoplayer = "1.4.1" media3Exoplayer = "1.4.1"
roomRuntime = "2.6.1"
[libraries] [libraries]
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" } androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
@ -17,13 +18,16 @@ androidx-media3-ui = { module = "androidx.media3:media3-ui", version.ref = "medi
androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycleRuntimeKtx" } androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycleRuntimeKtx" }
androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" } androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" }
androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" } androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" }
androidx-room-compiler = { module = "androidx.room:room-compiler", version.ref = "roomRuntime" }
androidx-room-runtime = { module = "androidx.room:room-runtime", version.ref = "roomRuntime" }
androidx-ui = { group = "androidx.compose.ui", name = "ui" } androidx-ui = { group = "androidx.compose.ui", name = "ui" }
androidx-ui-graphics = { group = "androidx.compose.ui", name = "ui-graphics" } androidx-ui-graphics = { group = "androidx.compose.ui", name = "ui-graphics" }
androidx-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" } androidx-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" }
androidx-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" } androidx-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" }
androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" } androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest", version = "1.7.3" }
androidx-material3 = { group = "androidx.compose.material3", name = "material3" } androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinxSerializationJson" } kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinxSerializationJson" }
com-google-devtools-ksp-gradle-plugin = { group = "com.google.devtools.ksp", name = "com.google.devtools.ksp.gradle.plugin", version = "2.0.20-1.0.25" }
[plugins] [plugins]
android-application = { id = "com.android.application", version.ref = "agp" } android-application = { id = "com.android.application", version.ref = "agp" }

Loading…
Cancel
Save