feat(data): add Room entities, DAOs and AppDatabase
This commit is contained in:
17
app/src/main/java/com/radiola/data/local/AppDatabase.kt
Normal file
17
app/src/main/java/com/radiola/data/local/AppDatabase.kt
Normal file
@@ -0,0 +1,17 @@
|
||||
package com.radiola.data.local
|
||||
|
||||
import androidx.room.Database
|
||||
import androidx.room.RoomDatabase
|
||||
import com.radiola.data.local.dao.StationDao
|
||||
import com.radiola.data.local.dao.TrackHistoryDao
|
||||
import com.radiola.data.local.entity.StationEntity
|
||||
import com.radiola.data.local.entity.TrackHistoryEntity
|
||||
|
||||
@Database(
|
||||
entities = [StationEntity::class, TrackHistoryEntity::class],
|
||||
version = 1
|
||||
)
|
||||
abstract class AppDatabase : RoomDatabase() {
|
||||
abstract fun stationDao(): StationDao
|
||||
abstract fun trackHistoryDao(): TrackHistoryDao
|
||||
}
|
||||
34
app/src/main/java/com/radiola/data/local/dao/StationDao.kt
Normal file
34
app/src/main/java/com/radiola/data/local/dao/StationDao.kt
Normal file
@@ -0,0 +1,34 @@
|
||||
package com.radiola.data.local.dao
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Insert
|
||||
import androidx.room.OnConflictStrategy
|
||||
import androidx.room.Query
|
||||
import androidx.room.Update
|
||||
import com.radiola.data.local.entity.StationEntity
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
interface StationDao {
|
||||
|
||||
@Query("SELECT * FROM stations ORDER BY sortOrder ASC")
|
||||
fun getAll(): Flow<List<StationEntity>>
|
||||
|
||||
@Query("SELECT * FROM stations WHERE isFavorite = 1 ORDER BY sortOrder ASC")
|
||||
fun getFavorites(): Flow<List<StationEntity>>
|
||||
|
||||
@Query("SELECT * FROM stations WHERE id = :id")
|
||||
fun getById(id: Int): Flow<StationEntity?>
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
suspend fun insertAll(stations: List<StationEntity>)
|
||||
|
||||
@Update
|
||||
suspend fun update(station: StationEntity)
|
||||
|
||||
@Query("UPDATE stations SET isFavorite = :isFavorite WHERE id = :id")
|
||||
suspend fun setFavorite(id: Int, isFavorite: Boolean)
|
||||
|
||||
@Query("SELECT isFavorite FROM stations WHERE id = :id")
|
||||
fun isFavorite(id: Int): Flow<Boolean>
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.radiola.data.local.dao
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Insert
|
||||
import androidx.room.Query
|
||||
import com.radiola.data.local.entity.TrackHistoryEntity
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
interface TrackHistoryDao {
|
||||
|
||||
@Query("SELECT * FROM track_history ORDER BY timestamp DESC LIMIT 200")
|
||||
fun getAll(): Flow<List<TrackHistoryEntity>>
|
||||
|
||||
@Insert
|
||||
suspend fun insert(track: TrackHistoryEntity)
|
||||
|
||||
@Query("DELETE FROM track_history WHERE id = :id")
|
||||
suspend fun deleteById(id: Int)
|
||||
|
||||
@Query("DELETE FROM track_history WHERE id NOT IN (SELECT id FROM track_history ORDER BY timestamp DESC LIMIT 200)")
|
||||
suspend fun cleanupOld()
|
||||
|
||||
@Query("SELECT * FROM track_history WHERE artist LIKE '%' || :query || '%' OR song LIKE '%' || :query || '%' ORDER BY timestamp DESC")
|
||||
suspend fun search(query: String): List<TrackHistoryEntity>
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.radiola.data.local.entity
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
@Entity(tableName = "stations")
|
||||
data class StationEntity(
|
||||
@PrimaryKey val id: Int,
|
||||
val name: String,
|
||||
val prefix: String,
|
||||
val streamUrl: String,
|
||||
val coverUrl: String,
|
||||
val genre: String,
|
||||
val tags: String,
|
||||
val sortOrder: Int,
|
||||
val isFavorite: Boolean = false
|
||||
)
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.radiola.data.local.entity
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
@Entity(tableName = "track_history")
|
||||
data class TrackHistoryEntity(
|
||||
@PrimaryKey(autoGenerate = true) val id: Int = 0,
|
||||
val artist: String,
|
||||
val song: String,
|
||||
val stationName: String,
|
||||
val coverUrl: String?,
|
||||
val timestamp: Long
|
||||
)
|
||||
Reference in New Issue
Block a user