-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppDatabase.kt
More file actions
84 lines (64 loc) · 2.66 KB
/
Copy pathAppDatabase.kt
File metadata and controls
84 lines (64 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.animationstudio.data
import androidx.room.*
import com.animationstudio.data.models.*
import kotlinx.coroutines.flow.Flow
@Database(
entities = [
AnimationProject::class,
ProjectLayer::class,
ProjectKeyframe::class,
BrushPreset::class,
ColorPalette::class
],
version = 1,
exportSchema = false
)
@TypeConverters(Converters::class)
abstract class AppDatabase : RoomDatabase() {
abstract fun projectDao(): ProjectDao
}
@Dao
interface ProjectDao {
@Query("SELECT * FROM animation_projects ORDER BY lastModified DESC")
fun getAllProjects(): Flow<List<AnimationProject>>
@Query("SELECT * FROM animation_projects WHERE id = :id")
suspend fun getProject(id: Long): AnimationProject?
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertProject(project: AnimationProject): Long
@Update
suspend fun updateProject(project: AnimationProject)
@Delete
suspend fun deleteProject(project: AnimationProject)
@Query("DELETE FROM project_layers WHERE projectId = :projectId")
suspend fun deleteLayersForProject(projectId: Long)
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertLayers(layers: List<ProjectLayer>)
@Query("SELECT * FROM project_layers WHERE projectId = :projectId ORDER BY layerOrder")
suspend fun getLayersForProject(projectId: Long): List<ProjectLayer>
@Query("SELECT * FROM project_keyframes WHERE projectId = :projectId AND layerId = :layerId ORDER BY frameIndex")
suspend fun getKeyframes(projectId: Long, layerId: Long): List<ProjectKeyframe>
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertKeyframes(keyframes: List<ProjectKeyframe>)
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertBrushPreset(preset: BrushPreset)
@Query("SELECT * FROM brush_presets ORDER BY name")
fun getBrushPresets(): Flow<List<BrushPreset>>
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertColorPalette(palette: ColorPalette)
@Query("SELECT * FROM color_palettes ORDER BY name")
fun getColorPalettes(): Flow<List<ColorPalette>>
}
class Converters {
@TypeConverter
fun fromIntList(value: List<Int>): String = value.joinToString(",")
@TypeConverter
fun toIntList(value: String): List<Int> =
if (value.isEmpty()) emptyList()
else value.split(",").map { it.toInt() }
@TypeConverter
fun fromFloatList(value: List<Float>): String = value.joinToString(",")
@TypeConverter
fun toFloatList(value: String): List<Float> =
if (value.isEmpty()) emptyList()
else value.split(",").map { it.toFloat() }
}