Skip to content

Commit

Permalink
Refactor Track's fromDb a bit (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
chvp authored Nov 27, 2021
1 parent 2c4ce96 commit 6960096
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 37 deletions.
6 changes: 3 additions & 3 deletions app/src/main/java/me/vanpetegem/accentor/data/tracks/Track.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<MutableList<TrackArtist>>, trackGenres: SparseArray<MutableList<Int>>) =
fun fromDb(t: DbTrack, trackArtists: List<TrackArtist>, trackGenres: List<Int>) =
Track(
t.id,
t.title,
Expand All @@ -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,
Expand Down
44 changes: 10 additions & 34 deletions app/src/main/java/me/vanpetegem/accentor/data/tracks/TrackDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int>): LiveData<List<Track>> = 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())) }
}
}
}
Expand All @@ -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 },
)
}
}
Expand All @@ -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())) }
}
}
}
Expand All @@ -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())) }
}
}
}
Expand All @@ -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 },
)
}

Expand Down

0 comments on commit 6960096

Please sign in to comment.