Create a correctly typed list from getMusicDirectory()
This commit is contained in:
parent
fbf5a63396
commit
10b83805a9
|
@ -39,15 +39,15 @@ class MusicDirectory : ArrayList<MusicDirectory.Child>() {
|
||||||
abstract var album: String?
|
abstract var album: String?
|
||||||
abstract var title: String?
|
abstract var title: String?
|
||||||
abstract override val name: String?
|
abstract override val name: String?
|
||||||
abstract val discNumber: Int?
|
abstract var discNumber: Int?
|
||||||
abstract var coverArt: String?
|
abstract var coverArt: String?
|
||||||
abstract val songCount: Long?
|
abstract var songCount: Long?
|
||||||
abstract val created: Date?
|
abstract var created: Date?
|
||||||
abstract var artist: String?
|
abstract var artist: String?
|
||||||
abstract val artistId: String?
|
abstract var artistId: String?
|
||||||
abstract val duration: Int?
|
abstract var duration: Int?
|
||||||
abstract val year: Int?
|
abstract var year: Int?
|
||||||
abstract val genre: String?
|
abstract var genre: String?
|
||||||
abstract var starred: Boolean
|
abstract var starred: Boolean
|
||||||
abstract var path: String?
|
abstract var path: String?
|
||||||
abstract var closeness: Int
|
abstract var closeness: Int
|
||||||
|
@ -120,15 +120,15 @@ class MusicDirectory : ArrayList<MusicDirectory.Child>() {
|
||||||
override var album: String? = null,
|
override var album: String? = null,
|
||||||
override var title: String? = null,
|
override var title: String? = null,
|
||||||
override val name: String? = null,
|
override val name: String? = null,
|
||||||
override val discNumber: Int = 0,
|
override var discNumber: Int? = 0,
|
||||||
override var coverArt: String? = null,
|
override var coverArt: String? = null,
|
||||||
override val songCount: Long? = null,
|
override var songCount: Long? = null,
|
||||||
override val created: Date? = null,
|
override var created: Date? = null,
|
||||||
override var artist: String? = null,
|
override var artist: String? = null,
|
||||||
override val artistId: String? = null,
|
override var artistId: String? = null,
|
||||||
override val duration: Int = 0,
|
override var duration: Int? = 0,
|
||||||
override val year: Int = 0,
|
override var year: Int? = 0,
|
||||||
override val genre: String? = null,
|
override var genre: String? = null,
|
||||||
override var starred: Boolean = false,
|
override var starred: Boolean = false,
|
||||||
override var path: String? = null,
|
override var path: String? = null,
|
||||||
override var closeness: Int = 0,
|
override var closeness: Int = 0,
|
||||||
|
|
|
@ -21,7 +21,7 @@ fun Album.toDomainEntity(): MusicDirectory.Album = MusicDirectory.Album(
|
||||||
)
|
)
|
||||||
|
|
||||||
fun Album.toMusicDirectoryDomainEntity(): MusicDirectory = MusicDirectory().apply {
|
fun Album.toMusicDirectoryDomainEntity(): MusicDirectory = MusicDirectory().apply {
|
||||||
addAll(this@toMusicDirectoryDomainEntity.songList.map { it.toDomainEntity() })
|
addAll(this@toMusicDirectoryDomainEntity.songList.map { it.toTrackEntity() })
|
||||||
}
|
}
|
||||||
|
|
||||||
fun List<Album>.toDomainEntityList(): List<MusicDirectory.Album> = this.map { it.toDomainEntity() }
|
fun List<Album>.toDomainEntityList(): List<MusicDirectory.Album> = this.map { it.toDomainEntity() }
|
||||||
|
|
|
@ -10,7 +10,7 @@ fun ApiBookmark.toDomainEntity(): Bookmark = Bookmark(
|
||||||
comment = this@toDomainEntity.comment,
|
comment = this@toDomainEntity.comment,
|
||||||
created = this@toDomainEntity.created?.time,
|
created = this@toDomainEntity.created?.time,
|
||||||
changed = this@toDomainEntity.changed?.time,
|
changed = this@toDomainEntity.changed?.time,
|
||||||
entry = this@toDomainEntity.entry.toDomainEntity()
|
entry = this@toDomainEntity.entry.toTrackEntity()
|
||||||
)
|
)
|
||||||
|
|
||||||
fun List<ApiBookmark>.toDomainEntitiesList(): List<Bookmark> = map { it.toDomainEntity() }
|
fun List<ApiBookmark>.toDomainEntitiesList(): List<Bookmark> = map { it.toDomainEntity() }
|
||||||
|
|
|
@ -1,5 +1,10 @@
|
||||||
// Converts MusicDirectory entity from [org.moire.ultrasonic.api.subsonic.SubsonicAPIClient]
|
/*
|
||||||
// to app domain entities.
|
* APIMusicDirectoryConverter.kt
|
||||||
|
* Copyright (C) 2009-2021 Ultrasonic developers
|
||||||
|
*
|
||||||
|
* Distributed under terms of the GNU GPLv3 license.
|
||||||
|
*/
|
||||||
|
|
||||||
@file:JvmName("APIMusicDirectoryConverter")
|
@file:JvmName("APIMusicDirectoryConverter")
|
||||||
package org.moire.ultrasonic.domain
|
package org.moire.ultrasonic.domain
|
||||||
|
|
||||||
|
@ -9,48 +14,90 @@ import java.util.Locale
|
||||||
import org.moire.ultrasonic.api.subsonic.models.MusicDirectory as APIMusicDirectory
|
import org.moire.ultrasonic.api.subsonic.models.MusicDirectory as APIMusicDirectory
|
||||||
import org.moire.ultrasonic.api.subsonic.models.MusicDirectoryChild
|
import org.moire.ultrasonic.api.subsonic.models.MusicDirectoryChild
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Converts MusicDirectory entity from [org.moire.ultrasonic.api.subsonic.SubsonicAPIClient]
|
||||||
|
* to app domain entities.
|
||||||
|
*
|
||||||
|
* Unlike other API endpoints getMusicDirectory doesn't return instances of Albums or Songs,
|
||||||
|
* but just children, which can be albums or songs.
|
||||||
|
*/
|
||||||
|
|
||||||
internal val dateFormat: DateFormat by lazy {
|
internal val dateFormat: DateFormat by lazy {
|
||||||
SimpleDateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.getDefault())
|
SimpleDateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.getDefault())
|
||||||
}
|
}
|
||||||
|
|
||||||
fun MusicDirectoryChild.toDomainEntity(): MusicDirectory.Entry = MusicDirectory.Entry(id).apply {
|
fun MusicDirectoryChild.toTrackEntity(): MusicDirectory.Entry = MusicDirectory.Entry(id).apply {
|
||||||
parent = this@toDomainEntity.parent
|
populateCommonProps(this, this@toTrackEntity)
|
||||||
isDirectory = this@toDomainEntity.isDir
|
populateTrackProps(this, this@toTrackEntity)
|
||||||
title = this@toDomainEntity.title
|
|
||||||
album = this@toDomainEntity.album
|
|
||||||
albumId = this@toDomainEntity.albumId
|
|
||||||
artist = this@toDomainEntity.artist
|
|
||||||
artistId = this@toDomainEntity.artistId
|
|
||||||
track = this@toDomainEntity.track
|
|
||||||
year = this@toDomainEntity.year
|
|
||||||
genre = this@toDomainEntity.genre
|
|
||||||
contentType = this@toDomainEntity.contentType
|
|
||||||
suffix = this@toDomainEntity.suffix
|
|
||||||
transcodedContentType = this@toDomainEntity.transcodedContentType
|
|
||||||
transcodedSuffix = this@toDomainEntity.transcodedSuffix
|
|
||||||
coverArt = this@toDomainEntity.coverArt
|
|
||||||
size = this@toDomainEntity.size
|
|
||||||
duration = this@toDomainEntity.duration
|
|
||||||
bitRate = this@toDomainEntity.bitRate
|
|
||||||
path = this@toDomainEntity.path
|
|
||||||
isVideo = this@toDomainEntity.isVideo
|
|
||||||
created = this@toDomainEntity.created?.time
|
|
||||||
starred = this@toDomainEntity.starred != null
|
|
||||||
discNumber = this@toDomainEntity.discNumber
|
|
||||||
type = this@toDomainEntity.type
|
|
||||||
if (this@toDomainEntity.streamId.isNotBlank()) {
|
|
||||||
id = this@toDomainEntity.streamId
|
|
||||||
}
|
|
||||||
if (this@toDomainEntity.publishDate != null) {
|
|
||||||
artist = dateFormat.format(this@toDomainEntity.publishDate!!.time)
|
|
||||||
}
|
|
||||||
userRating = this@toDomainEntity.userRating
|
|
||||||
averageRating = this@toDomainEntity.averageRating
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun List<MusicDirectoryChild>.toDomainEntityList() = this.map { it.toDomainEntity() }
|
fun MusicDirectoryChild.toAlbumEntity(): MusicDirectory.Album = MusicDirectory.Album(id).apply {
|
||||||
|
populateCommonProps(this, this@toAlbumEntity)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun populateCommonProps(
|
||||||
|
entry: MusicDirectory.Child,
|
||||||
|
source: MusicDirectoryChild
|
||||||
|
) {
|
||||||
|
entry.parent = source.parent
|
||||||
|
entry.isDirectory = source.isDir
|
||||||
|
entry.title = source.title
|
||||||
|
entry.album = source.album
|
||||||
|
entry.artist = source.artist
|
||||||
|
entry.artistId = source.artistId
|
||||||
|
entry.year = source.year
|
||||||
|
entry.genre = source.genre
|
||||||
|
entry.coverArt = source.coverArt
|
||||||
|
entry.duration = source.duration
|
||||||
|
entry.path = source.path
|
||||||
|
entry.isVideo = source.isVideo
|
||||||
|
entry.created = source.created?.time
|
||||||
|
entry.starred = source.starred != null
|
||||||
|
entry.discNumber = source.discNumber
|
||||||
|
|
||||||
|
if (source.streamId.isNotBlank()) {
|
||||||
|
entry.id = source.streamId
|
||||||
|
}
|
||||||
|
if (source.publishDate != null) {
|
||||||
|
entry.artist = dateFormat.format(source.publishDate!!.time)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun populateTrackProps(
|
||||||
|
entry: MusicDirectory.Entry,
|
||||||
|
source: MusicDirectoryChild
|
||||||
|
) {
|
||||||
|
entry.size = source.size
|
||||||
|
entry.contentType = source.contentType
|
||||||
|
entry.suffix = source.suffix
|
||||||
|
entry.transcodedContentType = source.transcodedContentType
|
||||||
|
entry.transcodedSuffix = source.transcodedSuffix
|
||||||
|
entry.track = source.track
|
||||||
|
entry.albumId = source.albumId
|
||||||
|
entry.bitRate = source.bitRate
|
||||||
|
entry.type = source.type
|
||||||
|
entry.userRating = source.userRating
|
||||||
|
entry.averageRating = source.averageRating
|
||||||
|
}
|
||||||
|
|
||||||
|
fun List<MusicDirectoryChild>.toDomainEntityList(): List<MusicDirectory.Child> {
|
||||||
|
val newList: MutableList<MusicDirectory.Child> = mutableListOf()
|
||||||
|
|
||||||
|
forEach {
|
||||||
|
if (it.isDir)
|
||||||
|
newList.add(it.toAlbumEntity())
|
||||||
|
else
|
||||||
|
newList.add(it.toTrackEntity())
|
||||||
|
}
|
||||||
|
|
||||||
|
return newList
|
||||||
|
}
|
||||||
|
|
||||||
|
fun List<MusicDirectoryChild>.toTrackList(): List<MusicDirectory.Entry> = this.map {
|
||||||
|
it.toTrackEntity()
|
||||||
|
}
|
||||||
|
|
||||||
fun APIMusicDirectory.toDomainEntity(): MusicDirectory = MusicDirectory().apply {
|
fun APIMusicDirectory.toDomainEntity(): MusicDirectory = MusicDirectory().apply {
|
||||||
name = this@toDomainEntity.name
|
name = this@toDomainEntity.name
|
||||||
addAll(this@toDomainEntity.childList.map { it.toDomainEntity() })
|
addAll(this@toDomainEntity.childList.toDomainEntityList())
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ internal val playlistDateFormat by lazy(NONE) { SimpleDateFormat.getInstance() }
|
||||||
|
|
||||||
fun APIPlaylist.toMusicDirectoryDomainEntity(): MusicDirectory = MusicDirectory().apply {
|
fun APIPlaylist.toMusicDirectoryDomainEntity(): MusicDirectory = MusicDirectory().apply {
|
||||||
name = this@toMusicDirectoryDomainEntity.name
|
name = this@toMusicDirectoryDomainEntity.name
|
||||||
addAll(this@toMusicDirectoryDomainEntity.entriesList.map { it.toDomainEntity() })
|
addAll(this@toMusicDirectoryDomainEntity.entriesList.map { it.toTrackEntity() })
|
||||||
}
|
}
|
||||||
|
|
||||||
fun APIPlaylist.toDomainEntity(): Playlist = Playlist(
|
fun APIPlaylist.toDomainEntity(): Playlist = Playlist(
|
||||||
|
|
|
@ -9,17 +9,17 @@ import org.moire.ultrasonic.api.subsonic.models.SearchTwoResult
|
||||||
|
|
||||||
fun APISearchResult.toDomainEntity(): SearchResult = SearchResult(
|
fun APISearchResult.toDomainEntity(): SearchResult = SearchResult(
|
||||||
emptyList(), emptyList(),
|
emptyList(), emptyList(),
|
||||||
this.matchList.map { it.toDomainEntity() }
|
this.matchList.map { it.toTrackEntity() }
|
||||||
)
|
)
|
||||||
|
|
||||||
fun SearchTwoResult.toDomainEntity(): SearchResult = SearchResult(
|
fun SearchTwoResult.toDomainEntity(): SearchResult = SearchResult(
|
||||||
this.artistList.map { it.toDomainEntity() },
|
this.artistList.map { it.toDomainEntity() },
|
||||||
this.albumList.map { it.toDomainEntity() },
|
this.albumList.map { it.toDomainEntity() },
|
||||||
this.songList.map { it.toDomainEntity() }
|
this.songList.map { it.toTrackEntity() }
|
||||||
)
|
)
|
||||||
|
|
||||||
fun SearchThreeResult.toDomainEntity(): SearchResult = SearchResult(
|
fun SearchThreeResult.toDomainEntity(): SearchResult = SearchResult(
|
||||||
this.artistList.map { it.toDomainEntity() },
|
this.artistList.map { it.toDomainEntity() },
|
||||||
this.albumList.map { it.toDomainEntity() },
|
this.albumList.map { it.toDomainEntity() },
|
||||||
this.songList.map { it.toDomainEntity() }
|
this.songList.map { it.toTrackEntity() }
|
||||||
)
|
)
|
||||||
|
|
|
@ -22,5 +22,5 @@ fun APIShare.toDomainEntity(): Share = Share(
|
||||||
url = this@toDomainEntity.url,
|
url = this@toDomainEntity.url,
|
||||||
username = this@toDomainEntity.username,
|
username = this@toDomainEntity.username,
|
||||||
visitCount = this@toDomainEntity.visitCount.toLong(),
|
visitCount = this@toDomainEntity.visitCount.toLong(),
|
||||||
entries = this@toDomainEntity.items.toDomainEntityList().toMutableList()
|
entries = this@toDomainEntity.items.toTrackList().toMutableList()
|
||||||
)
|
)
|
||||||
|
|
|
@ -41,6 +41,7 @@ import org.moire.ultrasonic.domain.toDomainEntity
|
||||||
import org.moire.ultrasonic.domain.toDomainEntityList
|
import org.moire.ultrasonic.domain.toDomainEntityList
|
||||||
import org.moire.ultrasonic.domain.toIndexList
|
import org.moire.ultrasonic.domain.toIndexList
|
||||||
import org.moire.ultrasonic.domain.toMusicDirectoryDomainEntity
|
import org.moire.ultrasonic.domain.toMusicDirectoryDomainEntity
|
||||||
|
import org.moire.ultrasonic.domain.toTrackEntity
|
||||||
import org.moire.ultrasonic.util.FileUtil
|
import org.moire.ultrasonic.util.FileUtil
|
||||||
import org.moire.ultrasonic.util.Settings
|
import org.moire.ultrasonic.util.Settings
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
@ -317,7 +318,7 @@ open class RESTMusicService(
|
||||||
"skipped" != podcastEntry.status &&
|
"skipped" != podcastEntry.status &&
|
||||||
"error" != podcastEntry.status
|
"error" != podcastEntry.status
|
||||||
) {
|
) {
|
||||||
val entry = podcastEntry.toDomainEntity()
|
val entry = podcastEntry.toTrackEntity()
|
||||||
entry.track = null
|
entry.track = null
|
||||||
musicDirectory.add(entry)
|
musicDirectory.add(entry)
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,7 +51,7 @@ class APIAlbumConverterTest {
|
||||||
with(convertedEntity) {
|
with(convertedEntity) {
|
||||||
name `should be equal to` null
|
name `should be equal to` null
|
||||||
size `should be equal to` entity.songList.size
|
size `should be equal to` entity.songList.size
|
||||||
this[0] `should be equal to` entity.songList[0].toDomainEntity()
|
this[0] `should be equal to` entity.songList[0].toTrackEntity()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,7 +27,7 @@ class APIBookmarkConverterTest {
|
||||||
comment `should be equal to` entity.comment
|
comment `should be equal to` entity.comment
|
||||||
created `should be equal to` entity.created?.time
|
created `should be equal to` entity.created?.time
|
||||||
changed `should be equal to` entity.changed?.time
|
changed `should be equal to` entity.changed?.time
|
||||||
entry `should be equal to` entity.entry.toDomainEntity()
|
entry `should be equal to` entity.entry.toTrackEntity()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,7 @@ class APIMusicDirectoryConverterTest {
|
||||||
name `should be equal to` entity.name
|
name `should be equal to` entity.name
|
||||||
size `should be equal to` entity.childList.size
|
size `should be equal to` entity.childList.size
|
||||||
getChildren() `should be equal to` entity.childList
|
getChildren() `should be equal to` entity.childList
|
||||||
.map { it.toDomainEntity() }.toMutableList()
|
.map { it.toTrackEntity() }.toMutableList()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,7 +44,7 @@ class APIMusicDirectoryConverterTest {
|
||||||
starred = Calendar.getInstance(), userRating = 3, averageRating = 2.99F
|
starred = Calendar.getInstance(), userRating = 3, averageRating = 2.99F
|
||||||
)
|
)
|
||||||
|
|
||||||
val convertedEntity = entity.toDomainEntity()
|
val convertedEntity = entity.toTrackEntity()
|
||||||
|
|
||||||
with(convertedEntity) {
|
with(convertedEntity) {
|
||||||
id `should be equal to` entity.id
|
id `should be equal to` entity.id
|
||||||
|
@ -84,7 +84,7 @@ class APIMusicDirectoryConverterTest {
|
||||||
artist = "some-artist", publishDate = Calendar.getInstance()
|
artist = "some-artist", publishDate = Calendar.getInstance()
|
||||||
)
|
)
|
||||||
|
|
||||||
val convertedEntity = entity.toDomainEntity()
|
val convertedEntity = entity.toTrackEntity()
|
||||||
|
|
||||||
with(convertedEntity) {
|
with(convertedEntity) {
|
||||||
id `should be equal to` entity.streamId
|
id `should be equal to` entity.streamId
|
||||||
|
@ -100,7 +100,7 @@ class APIMusicDirectoryConverterTest {
|
||||||
|
|
||||||
domainList.size `should be equal to` entitiesList.size
|
domainList.size `should be equal to` entitiesList.size
|
||||||
domainList.forEachIndexed { index, entry ->
|
domainList.forEachIndexed { index, entry ->
|
||||||
entry `should be equal to` entitiesList[index].toDomainEntity()
|
entry `should be equal to` entitiesList[index].toTrackEntity()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,8 +27,8 @@ class APIPlaylistConverterTest {
|
||||||
with(convertedEntity) {
|
with(convertedEntity) {
|
||||||
name `should be equal to` entity.name
|
name `should be equal to` entity.name
|
||||||
size `should be equal to` entity.entriesList.size
|
size `should be equal to` entity.entriesList.size
|
||||||
this[0] `should be equal to` entity.entriesList[0].toDomainEntity()
|
this[0] `should be equal to` entity.entriesList[0].toTrackEntity()
|
||||||
this[1] `should be equal to` entity.entriesList[1].toDomainEntity()
|
this[1] `should be equal to` entity.entriesList[1].toTrackEntity()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -34,7 +34,7 @@ class APISearchConverterTest {
|
||||||
artists `should not be equal to` null
|
artists `should not be equal to` null
|
||||||
artists.size `should be equal to` 0
|
artists.size `should be equal to` 0
|
||||||
songs.size `should be equal to` entity.matchList.size
|
songs.size `should be equal to` entity.matchList.size
|
||||||
songs[0] `should be equal to` entity.matchList[0].toDomainEntity()
|
songs[0] `should be equal to` entity.matchList[0].toTrackEntity()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,7 +54,7 @@ class APISearchConverterTest {
|
||||||
albums.size `should be equal to` entity.albumList.size
|
albums.size `should be equal to` entity.albumList.size
|
||||||
albums[0] `should be equal to` entity.albumList[0].toDomainEntity()
|
albums[0] `should be equal to` entity.albumList[0].toDomainEntity()
|
||||||
songs.size `should be equal to` entity.songList.size
|
songs.size `should be equal to` entity.songList.size
|
||||||
songs[0] `should be equal to` entity.songList[0].toDomainEntity()
|
songs[0] `should be equal to` entity.songList[0].toTrackEntity()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,7 +74,7 @@ class APISearchConverterTest {
|
||||||
albums.size `should be equal to` entity.albumList.size
|
albums.size `should be equal to` entity.albumList.size
|
||||||
albums[0] `should be equal to` entity.albumList[0].toDomainEntity()
|
albums[0] `should be equal to` entity.albumList[0].toDomainEntity()
|
||||||
songs.size `should be equal to` entity.songList.size
|
songs.size `should be equal to` entity.songList.size
|
||||||
songs[0] `should be equal to` entity.songList[0].toDomainEntity()
|
songs[0] `should be equal to` entity.songList[0].toTrackEntity()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue