Skip to content

Commit

Permalink
Add database (#443)
Browse files Browse the repository at this point in the history
  • Loading branch information
luizgrp authored Jul 29, 2022
1 parent fe8e654 commit 2b4e7c4
Show file tree
Hide file tree
Showing 37 changed files with 828 additions and 388 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.android.horologist.mediasample.data.database

import androidx.room.Database
import androidx.room.RoomDatabase
import com.google.android.horologist.mediasample.data.database.dao.MediaDownloadDao
import com.google.android.horologist.mediasample.data.database.dao.PlaylistDao
import com.google.android.horologist.mediasample.data.database.model.MediaDownloadEntity
import com.google.android.horologist.mediasample.data.database.model.MediaEntity
import com.google.android.horologist.mediasample.data.database.model.PlaylistEntity
import com.google.android.horologist.mediasample.data.database.model.PlaylistMediaEntity

@Database(
entities = [
MediaDownloadEntity::class,
MediaEntity::class,
PlaylistEntity::class,
PlaylistMediaEntity::class,
],
version = 1,
exportSchema = false
)
abstract class MediaDatabase : RoomDatabase() {

abstract fun mediaDownloadDao(): MediaDownloadDao

abstract fun playlistDao(): PlaylistDao
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,38 +20,38 @@ import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.google.android.horologist.mediasample.data.database.model.PlaylistDownloadEntity
import com.google.android.horologist.mediasample.data.database.model.PlaylistDownloadState
import com.google.android.horologist.mediasample.data.database.model.MediaDownloadEntity
import com.google.android.horologist.mediasample.data.database.model.MediaDownloadEntityStatus
import kotlinx.coroutines.flow.Flow

@Dao
interface PlaylistDownloadDao {
interface MediaDownloadDao {

@Query(
value = """
SELECT * FROM playlist_download
WHERE playlistId = :playlistId
SELECT * FROM mediadownloadentity
WHERE mediaId in (:mediaIds)
"""
)
fun getStream(playlistId: String): Flow<List<PlaylistDownloadEntity>>
fun getList(mediaIds: List<String>): Flow<List<MediaDownloadEntity>>

@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insert(playlistDownloadEntities: PlaylistDownloadEntity): Long
suspend fun insert(mediaDownloadEntity: MediaDownloadEntity): Long

@Query(
value = """
DELETE FROM playlist_download
WHERE mediaItemId = :mediaItemId
"""
UPDATE mediadownloadentity
SET status = :status
WHERE mediaId = :mediaId
"""
)
suspend fun deleteByMediaItemId(mediaItemId: String)
suspend fun updateStatus(mediaId: String, status: MediaDownloadEntityStatus)

@Query(
value = """
UPDATE playlist_download
SET status = :status
WHERE mediaItemId = :mediaItemId
"""
DELETE FROM mediadownloadentity
WHERE mediaId = :mediaId
"""
)
suspend fun updateStatusByMediaItemId(mediaItemId: String, status: PlaylistDownloadState)
suspend fun delete(mediaId: String)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.android.horologist.mediasample.data.database.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Transaction
import com.google.android.horologist.mediasample.data.database.model.MediaEntity
import com.google.android.horologist.mediasample.data.database.model.PlaylistEntity
import com.google.android.horologist.mediasample.data.database.model.PlaylistMediaEntity
import com.google.android.horologist.mediasample.data.database.model.PopulatedPlaylist
import kotlinx.coroutines.flow.Flow

@Dao
interface PlaylistDao {

@Query(
value = """
SELECT COUNT(*) FROM playlistentity
"""
)
suspend fun getCount(): Int

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(
playlistEntity: PlaylistEntity,
mediaEntityList: List<MediaEntity>,
playlistMediaEntityList: List<PlaylistMediaEntity>
)

@Transaction
@Query(
value = """
SELECT * FROM playlistentity
WHERE playlistId = :playlistId
"""
)
fun getPopulated(playlistId: String): Flow<PopulatedPlaylist>

@Transaction
@Query(
value = """
SELECT * FROM playlistentity
"""
)
fun getAllPopulated(): Flow<List<PopulatedPlaylist>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@
* limitations under the License.
*/

package com.google.android.horologist.mediasample.data.mapper
package com.google.android.horologist.mediasample.data.database.mapper

import androidx.media3.exoplayer.offline.Download
import com.google.android.horologist.mediasample.data.database.model.PlaylistDownloadState
import com.google.android.horologist.mediasample.data.database.model.MediaDownloadEntityStatus

object PlaylistDownloadStateMapper {
object MediaDownloadEntityStatusMapper {

fun map(@Download.State state: Int): PlaylistDownloadState = when (state) {
Download.STATE_QUEUED, Download.STATE_DOWNLOADING, Download.STATE_RESTARTING -> PlaylistDownloadState.Downloading
Download.STATE_COMPLETED -> PlaylistDownloadState.Downloaded
Download.STATE_FAILED -> PlaylistDownloadState.Failed
else -> PlaylistDownloadState.NotDownloaded
fun map(@Download.State state: Int): MediaDownloadEntityStatus = when (state) {
Download.STATE_QUEUED,
Download.STATE_DOWNLOADING,
Download.STATE_RESTARTING -> MediaDownloadEntityStatus.Downloading
Download.STATE_COMPLETED -> MediaDownloadEntityStatus.Downloaded
Download.STATE_FAILED -> MediaDownloadEntityStatus.Failed
else -> MediaDownloadEntityStatus.NotDownloaded
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,18 @@
* limitations under the License.
*/

package com.google.android.horologist.mediasample.data.database
package com.google.android.horologist.mediasample.data.database.mapper

import androidx.room.Database
import androidx.room.RoomDatabase
import com.google.android.horologist.mediasample.data.database.dao.PlaylistDownloadDao
import com.google.android.horologist.mediasample.data.database.model.PlaylistDownloadEntity
import com.google.android.horologist.media.model.Media
import com.google.android.horologist.mediasample.data.database.model.MediaEntity

@Database(
entities = [PlaylistDownloadEntity::class],
version = 1,
exportSchema = false
)
abstract class DownloadDatabase : RoomDatabase() {
object MediaEntityMapper {

abstract fun playlistDownloadDao(): PlaylistDownloadDao
fun map(media: Media): MediaEntity = MediaEntity(
mediaId = media.id,
mediaUrl = media.uri,
artworkUrl = media.artworkUri ?: "",
title = media.title,
artist = media.artist,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@
* limitations under the License.
*/

package com.google.android.horologist.mediasample.data.mapper
package com.google.android.horologist.mediasample.data.database.mapper

import com.google.android.horologist.mediasample.data.database.model.PlaylistDownloadState
import com.google.android.horologist.mediasample.domain.model.PlaylistDownload
import com.google.android.horologist.mediasample.data.database.model.PlaylistEntity
import com.google.android.horologist.mediasample.domain.model.Playlist

object PlaylistDownloadStatusMapper {
object PlaylistEntityMapper {

fun map(playlistDownloadState: PlaylistDownloadState): PlaylistDownload.Status = when (playlistDownloadState) {
PlaylistDownloadState.NotDownloaded -> PlaylistDownload.Status.Idle
PlaylistDownloadState.Downloading -> PlaylistDownload.Status.InProgress
PlaylistDownloadState.Downloaded -> PlaylistDownload.Status.Completed
PlaylistDownloadState.Failed -> PlaylistDownload.Status.Idle
}
fun map(playlist: Playlist): PlaylistEntity = PlaylistEntity(
playlistId = playlist.id,
name = playlist.name,
artworkUri = playlist.artworkUri,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.android.horologist.mediasample.data.database.mapper

import com.google.android.horologist.media.model.Media
import com.google.android.horologist.mediasample.data.database.model.PlaylistMediaEntity
import com.google.android.horologist.mediasample.domain.model.Playlist

object PlaylistMediaEntityMapper {

fun map(playlist: Playlist, media: Media) = PlaylistMediaEntity(
playlistId = playlist.id,
mediaId = media.id
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.android.horologist.mediasample.data.database.model

import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity
data class MediaDownloadEntity(
@PrimaryKey val mediaId: String,
val status: MediaDownloadEntityStatus,
)

enum class MediaDownloadEntityStatus {
NotDownloaded, Downloading, Downloaded, Failed
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.android.horologist.mediasample.data.database.model

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity
data class MediaEntity(
@PrimaryKey val mediaId: String,
@ColumnInfo val mediaUrl: String,
@ColumnInfo val artworkUrl: String,
@ColumnInfo val title: String?,
@ColumnInfo val artist: String?,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.android.horologist.mediasample.data.database.model

import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity
data class PlaylistEntity(
@PrimaryKey val playlistId: String,
val name: String,
val artworkUri: String?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,15 @@
package com.google.android.horologist.mediasample.data.database.model

import androidx.room.Entity
import androidx.room.Index

@Entity(
tableName = "playlist_download",
primaryKeys = ["playlistId", "mediaItemId"]
primaryKeys = ["playlistId", "mediaId"],
indices = [
Index(value = ["playlistId"]),
]
)
data class PlaylistDownloadEntity(
data class PlaylistMediaEntity(
val playlistId: String,
val mediaItemId: String,
val status: PlaylistDownloadState,
val mediaId: String,
)

enum class PlaylistDownloadState {
NotDownloaded,
Downloading,
Downloaded,
Failed
}
Loading

0 comments on commit 2b4e7c4

Please sign in to comment.