From 6960096272541fb0f3f0c62cdfb3bc297a8f4b99 Mon Sep 17 00:00:00 2001 From: Charlotte Van Petegem Date: Sat, 27 Nov 2021 16:22:57 +0100 Subject: [PATCH] Refactor Track's fromDb a bit (#330) --- .../vanpetegem/accentor/data/tracks/Track.kt | 6 +-- .../accentor/data/tracks/TrackDao.kt | 44 +++++-------------- 2 files changed, 13 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/me/vanpetegem/accentor/data/tracks/Track.kt b/app/src/main/java/me/vanpetegem/accentor/data/tracks/Track.kt index c3d3314d..209fb056 100644 --- a/app/src/main/java/me/vanpetegem/accentor/data/tracks/Track.kt +++ b/app/src/main/java/me/vanpetegem/accentor/data/tracks/Track.kt @@ -47,7 +47,7 @@ data class Track( const val ARTIST = "me.vanpetegem.accentor.data.tracks.Track.ARTIST" const val YEAR = "me.vanpetegem.accentor.data.tracks.Track.YEAR" - fun fromDb(t: DbTrack, trackArtists: SparseArray>, trackGenres: SparseArray>) = + fun fromDb(t: DbTrack, trackArtists: List, trackGenres: List) = Track( t.id, t.title, @@ -57,8 +57,8 @@ data class Track( t.reviewComment, t.createdAt, t.updatedAt, - trackGenres.get(t.id, ArrayList()), - trackArtists.get(t.id, ArrayList()), + trackGenres, + trackArtists, t.codecId, t.length, t.bitrate, diff --git a/app/src/main/java/me/vanpetegem/accentor/data/tracks/TrackDao.kt b/app/src/main/java/me/vanpetegem/accentor/data/tracks/TrackDao.kt index 4fe46a1e..07bc7d27 100644 --- a/app/src/main/java/me/vanpetegem/accentor/data/tracks/TrackDao.kt +++ b/app/src/main/java/me/vanpetegem/accentor/data/tracks/TrackDao.kt @@ -22,13 +22,13 @@ abstract class TrackDao { val ids = tracks.map { it.id } val trackGenres = getTrackGenresByTrackIdWhereTrackIds(ids) val trackArtists = getTrackArtistsByTrackIdWhereTrackIds(ids) - return tracks.map { t -> Track.fromDb(t, trackArtists, trackGenres) } + return tracks.map { t -> Track.fromDb(t, trackArtists.get(t.id, ArrayList()), trackGenres.get(t.id, ArrayList())) } } open fun findByIds(ids: List): LiveData> = switchMap(findDbTracksByIds(ids)) { tracks -> switchMap(findTrackArtistsByTrackIdWhereTrackIds(ids)) { trackArtists -> map(findTrackGenresByTrackIdWhereTrackIds(ids)) { trackGenres -> - tracks.map { t -> Track.fromDb(t, trackArtists, trackGenres) } + tracks.map { t -> Track.fromDb(t, trackArtists.get(t.id, ArrayList()), trackGenres.get(t.id, ArrayList())) } } } } @@ -37,22 +37,10 @@ abstract class TrackDao { switchMap(findDbTrackArtistsById(id)) { trackArtists -> map(findDbTrackGenresById(id)) { trackGenres -> dbTrack?.let { - Track( - it.id, - it.title, - it.normalizedTitle, - it.number, - it.albumId, - it.reviewComment, - it.createdAt, - it.updatedAt, - trackGenres.map { it.genreId }, + Track.fromDb( + it, trackArtists.map { TrackArtist(it.artistId, it.name, it.normalizedName, it.role, it.order) }, - it.codecId, - it.length, - it.bitrate, - it.locationId, - it.fetchedAt, + trackGenres.map { it.genreId }, ) } } @@ -63,7 +51,7 @@ abstract class TrackDao { val ids = tracks.map { it.id } switchMap(findTrackArtistsByTrackIdWhereTrackIds(ids)) { trackArtists -> map(findTrackGenresByTrackIdWhereTrackIds(ids)) { trackGenres -> - tracks.map { Track.fromDb(it, trackArtists, trackGenres) } + tracks.map { Track.fromDb(it, trackArtists.get(it.id, ArrayList()), trackGenres.get(it.id, ArrayList())) } } } } @@ -72,7 +60,7 @@ abstract class TrackDao { val ids = tracks.map { it.id } switchMap(findTrackArtistsByTrackIdWhereTrackIds(ids)) { trackArtists -> map(findTrackGenresByTrackIdWhereTrackIds(ids)) { trackGenres -> - tracks.map { Track.fromDb(it, trackArtists, trackGenres) } + tracks.map { Track.fromDb(it, trackArtists.get(it.id, ArrayList()), trackGenres.get(it.id, ArrayList())) } } } } @@ -84,22 +72,10 @@ abstract class TrackDao { val trackArtists = getDbTrackArtistsById(id) val trackGenres = getDbTrackGenresById(id) - return Track( - dbTrack.id, - dbTrack.title, - dbTrack.normalizedTitle, - dbTrack.number, - dbTrack.albumId, - dbTrack.reviewComment, - dbTrack.createdAt, - dbTrack.updatedAt, - trackGenres.map { it.genreId }, + return Track.fromDb( + dbTrack, trackArtists.map { TrackArtist(it.artistId, it.name, it.normalizedName, it.role, it.order) }, - dbTrack.codecId, - dbTrack.length, - dbTrack.bitrate, - dbTrack.locationId, - dbTrack.fetchedAt, + trackGenres.map { it.genreId }, ) }