Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add database #443

Merged
merged 1 commit into from
Jul 29, 2022
Merged

Add database #443

merged 1 commit into from
Jul 29, 2022

Conversation

luizgrp
Copy link
Member

@luizgrp luizgrp commented Jul 28, 2022

WHAT

  • Add database to media app sample;
  • Store playlist, media, and media download status in the database;
  • Added temporary approach for "offline first": it only hits the API if playlist table is empty;

WHY

In order to allow implementing offline first strategy.

HOW

  • Add MediaDatabase, MediaDownloadDao and PlaylistDao;
  • Add MediaDownloadEntity, MediaEntity, PlaylistEntity, PlaylistMediaEntity;

Checklist 📋

  • Add explicit visibility modifier and explicit return types for public declarations
  • Run spotless check
  • Run tests
  • Update metalava's signature text files

suspend fun getCount(): Int

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if there is a more straight forward approach with room where a single entity/pojo could be passed

@OptIn(FlowPreview::class)
override fun get(playlistId: String): Flow<PlaylistDownload> =
playlistLocalDataSource.getPopulated(playlistId).flatMapMerge { populatedPlaylist ->
combine(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this flatMapMerge + combine could potentially be removed if figuring out a way for the DAOs to return a pojo with the combined data from PlaylistEntity + MediaEntity + MediaDownloadEntity

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 We can follow up, but be nice to start using this, so maybe follow up?

override fun getPlaylists(): Flow<Result<List<Playlist>>> = playlistCache
override fun getAllPopulated(): Flow<List<Playlist>> = flow {
emitAll(
if (playlistLocalDataSource.isEmpty()) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

temporary approach for offline first, to be improved

@luizgrp luizgrp requested a review from yschimke July 28, 2022 16:40
Copy link
Collaborator

@yschimke yschimke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great.

import androidx.room.PrimaryKey

@Entity
data class PlaylistEntity(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before this gets pulled up from samples, we should discuss this name. Playlist might be quite specific compare to album, so maybe there is a more general name like collection, with a type field?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good 👍

@ColumnInfo val mediaUrl: String,
@ColumnInfo val artworkUrl: String,
@ColumnInfo val title: String?,
@ColumnInfo val artist: String?,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For later: Will we add some mechanism to have additional fields, say in a child table? We may not need them and the cost of loading/parsing for most screens. But we can assume that each app has additional fields they will want to show. Think a podcast?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea, will address when moving to the lib 👍

)
suspend fun getCount(): Int

@Insert(onConflict = OnConflictStrategy.REPLACE)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is REPLACE here for the mediaEntity list?

For the others, they shouldn't already exist, or if they do we are building up a bigger set over time, as we are not removing existing entries?

I wonder if it's better to define this as three operations like the first sample here https://developer.android.com/reference/androidx/room/Transaction

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I thought about this too, will improve this once implementing the database sync mechanism 👍

Copy link
Member Author

@luizgrp luizgrp Aug 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just checked the implementation of this function in PlaylistDao_Impl.java and it looks like it uses a transaction:

        __db.beginTransaction();
        try {
          __insertionAdapterOfPlaylistEntity.insert(playlistEntity);
          __insertionAdapterOfMediaEntity.insert(mediaEntityList);
          __insertionAdapterOfPlaylistMediaEntity.insert(playlistMediaEntityList);
          __db.setTransactionSuccessful();
          return Unit.INSTANCE;
        } finally {
          __db.endTransaction();
        }

still can't use the function as it is as we need different strategy per table but just a FYI/TIL

)
data class PlaylistDownloadEntity(
data class PlaylistMediaEntity(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For later: Should playlists or albums have some sort of ordering concept?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I see there is a ordering field coming from the API, but it's per album and not per "playlist" so using that would conflict as we have more than one album per playlist, something to implement separately and discuss the strategy to take, but I agree to add this feature 👍

@OptIn(FlowPreview::class)
override fun get(playlistId: String): Flow<PlaylistDownload> =
playlistLocalDataSource.getPopulated(playlistId).flatMapMerge { populatedPlaylist ->
combine(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 We can follow up, but be nice to start using this, so maybe follow up?

@luizgrp
Copy link
Member Author

luizgrp commented Jul 29, 2022

Landing and will follow up on comments

@luizgrp luizgrp merged commit 2b4e7c4 into google:main Jul 29, 2022
@luizgrp luizgrp deleted the mediasample_download_db branch July 29, 2022 08:07
@luizgrp luizgrp self-assigned this Dec 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants