Clean up JellyfinRepositoryImpl

This commit is contained in:
Jarne Demeulemeester 2022-01-01 18:43:54 +01:00
parent 8e3c4a3a37
commit 9a4f846023
No known key found for this signature in database
GPG key ID: B61B7B150DB6A6D2
2 changed files with 101 additions and 157 deletions

View file

@ -10,21 +10,12 @@ import timber.log.Timber
import java.util.*
class JellyfinRepositoryImpl(private val jellyfinApi: JellyfinApi) : JellyfinRepository {
override suspend fun getUserViews(): List<BaseItemDto> {
val views: List<BaseItemDto>
withContext(Dispatchers.IO) {
views =
jellyfinApi.viewsApi.getUserViews(jellyfinApi.userId!!).content.items ?: emptyList()
}
return views
override suspend fun getUserViews(): List<BaseItemDto> = withContext(Dispatchers.IO) {
jellyfinApi.viewsApi.getUserViews(jellyfinApi.userId!!).content.items.orEmpty()
}
override suspend fun getItem(itemId: UUID): BaseItemDto {
val item: BaseItemDto
withContext(Dispatchers.IO) {
item = jellyfinApi.userLibraryApi.getItem(jellyfinApi.userId!!, itemId).content
}
return item
override suspend fun getItem(itemId: UUID): BaseItemDto = withContext(Dispatchers.IO) {
jellyfinApi.userLibraryApi.getItem(jellyfinApi.userId!!, itemId).content
}
override suspend fun getItems(
@ -33,181 +24,142 @@ class JellyfinRepositoryImpl(private val jellyfinApi: JellyfinApi) : JellyfinRep
recursive: Boolean,
sortBy: SortBy,
sortOrder: SortOrder
): List<BaseItemDto> {
val items: List<BaseItemDto>
Timber.d("$sortBy $sortOrder")
withContext(Dispatchers.IO) {
items = jellyfinApi.itemsApi.getItems(
jellyfinApi.userId!!,
parentId = parentId,
includeItemTypes = includeTypes,
recursive = recursive,
sortBy = listOf(sortBy.SortString),
sortOrder = listOf(sortOrder)
).content.items ?: emptyList()
}
return items
): List<BaseItemDto> = withContext(Dispatchers.IO) {
jellyfinApi.itemsApi.getItems(
jellyfinApi.userId!!,
parentId = parentId,
includeItemTypes = includeTypes,
recursive = recursive,
sortBy = listOf(sortBy.SortString),
sortOrder = listOf(sortOrder)
).content.items.orEmpty()
}
override suspend fun getPersonItems(
personIds: List<UUID>,
includeTypes: List<ContentType>?,
recursive: Boolean
): List<BaseItemDto> {
val items: List<BaseItemDto>
withContext(Dispatchers.IO) {
items = jellyfinApi.itemsApi.getItems(
jellyfinApi.userId!!,
personIds = personIds,
includeItemTypes = includeTypes?.map { it.type },
recursive = recursive
).content.items ?: emptyList()
}
return items
): List<BaseItemDto> = withContext(Dispatchers.IO) {
jellyfinApi.itemsApi.getItems(
jellyfinApi.userId!!,
personIds = personIds,
includeItemTypes = includeTypes?.map { it.type },
recursive = recursive
).content.items.orEmpty()
}
override suspend fun getFavoriteItems(): List<BaseItemDto> {
val items: List<BaseItemDto>
withContext(Dispatchers.IO) {
items = jellyfinApi.itemsApi.getItems(
jellyfinApi.userId!!,
filters = listOf(ItemFilter.IS_FAVORITE),
includeItemTypes = listOf("Movie", "Series", "Episode"),
recursive = true
).content.items ?: emptyList()
}
return items
override suspend fun getFavoriteItems(): List<BaseItemDto> = withContext(Dispatchers.IO) {
jellyfinApi.itemsApi.getItems(
jellyfinApi.userId!!,
filters = listOf(ItemFilter.IS_FAVORITE),
includeItemTypes = listOf("Movie", "Series", "Episode"),
recursive = true
).content.items.orEmpty()
}
override suspend fun getSearchItems(searchQuery: String): List<BaseItemDto> {
val items: List<BaseItemDto>
override suspend fun getSearchItems(searchQuery: String): List<BaseItemDto> =
withContext(Dispatchers.IO) {
items = jellyfinApi.itemsApi.getItems(
jellyfinApi.itemsApi.getItems(
jellyfinApi.userId!!,
searchTerm = searchQuery,
includeItemTypes = listOf("Movie", "Series", "Episode"),
recursive = true
).content.items ?: emptyList()
).content.items.orEmpty()
}
return items
override suspend fun getResumeItems(): List<BaseItemDto> = withContext(Dispatchers.IO) {
jellyfinApi.itemsApi.getResumeItems(
jellyfinApi.userId!!,
includeItemTypes = listOf("Movie", "Episode"),
).content.items.orEmpty()
}
override suspend fun getResumeItems(): List<BaseItemDto> {
val items: List<BaseItemDto>
override suspend fun getLatestMedia(parentId: UUID): List<BaseItemDto> =
withContext(Dispatchers.IO) {
items =
jellyfinApi.itemsApi.getResumeItems(
jellyfinApi.userId!!,
includeItemTypes = listOf("Movie", "Episode"),
).content.items ?: emptyList()
}
return items
}
override suspend fun getLatestMedia(parentId: UUID): List<BaseItemDto> {
val items: List<BaseItemDto>
withContext(Dispatchers.IO) {
items = jellyfinApi.userLibraryApi.getLatestMedia(
jellyfinApi.userLibraryApi.getLatestMedia(
jellyfinApi.userId!!,
parentId = parentId
).content
}
return items
}
override suspend fun getSeasons(seriesId: UUID): List<BaseItemDto> {
val seasons: List<BaseItemDto>
override suspend fun getSeasons(seriesId: UUID): List<BaseItemDto> =
withContext(Dispatchers.IO) {
seasons = jellyfinApi.showsApi.getSeasons(seriesId, jellyfinApi.userId!!).content.items
?: emptyList()
jellyfinApi.showsApi.getSeasons(seriesId, jellyfinApi.userId!!).content.items.orEmpty()
}
return seasons
}
override suspend fun getNextUp(seriesId: UUID?): List<BaseItemDto> {
val nextUpItems: List<BaseItemDto>
override suspend fun getNextUp(seriesId: UUID?): List<BaseItemDto> =
withContext(Dispatchers.IO) {
nextUpItems = jellyfinApi.showsApi.getNextUp(
jellyfinApi.showsApi.getNextUp(
jellyfinApi.userId!!,
seriesId = seriesId?.toString(),
).content.items ?: emptyList()
).content.items.orEmpty()
}
return nextUpItems
}
override suspend fun getEpisodes(
seriesId: UUID,
seasonId: UUID,
fields: List<ItemFields>?,
startItemId: UUID?
): List<BaseItemDto> {
val episodes: List<BaseItemDto>
): List<BaseItemDto> = withContext(Dispatchers.IO) {
jellyfinApi.showsApi.getEpisodes(
seriesId,
jellyfinApi.userId!!,
seasonId = seasonId,
fields = fields,
startItemId = startItemId
).content.items.orEmpty()
}
override suspend fun getMediaSources(itemId: UUID): List<MediaSourceInfo> =
withContext(Dispatchers.IO) {
episodes = jellyfinApi.showsApi.getEpisodes(
seriesId,
jellyfinApi.userId!!,
seasonId = seasonId,
fields = fields,
startItemId = startItemId
).content.items ?: emptyList()
}
return episodes
}
override suspend fun getMediaSources(itemId: UUID): List<MediaSourceInfo> {
val mediaSourceInfoList: List<MediaSourceInfo>
val mediaInfo by jellyfinApi.mediaInfoApi.getPostedPlaybackInfo(
itemId, PlaybackInfoDto(
userId = jellyfinApi.userId!!,
deviceProfile = DeviceProfile(
name = "Direct play all",
maxStaticBitrate = 1_000_000_000,
maxStreamingBitrate = 1_000_000_000,
codecProfiles = emptyList(),
containerProfiles = emptyList(),
directPlayProfiles = listOf(
DirectPlayProfile(
type = DlnaProfileType.VIDEO
), DirectPlayProfile(type = DlnaProfileType.AUDIO)
jellyfinApi.mediaInfoApi.getPostedPlaybackInfo(
itemId, PlaybackInfoDto(
userId = jellyfinApi.userId!!,
deviceProfile = DeviceProfile(
name = "Direct play all",
maxStaticBitrate = 1_000_000_000,
maxStreamingBitrate = 1_000_000_000,
codecProfiles = emptyList(),
containerProfiles = emptyList(),
directPlayProfiles = listOf(
DirectPlayProfile(
type = DlnaProfileType.VIDEO
), DirectPlayProfile(type = DlnaProfileType.AUDIO)
),
transcodingProfiles = emptyList(),
responseProfiles = emptyList(),
enableAlbumArtInDidl = false,
enableMsMediaReceiverRegistrar = false,
enableSingleAlbumArtLimit = false,
enableSingleSubtitleLimit = false,
ignoreTranscodeByteRangeRequests = false,
maxAlbumArtHeight = 1_000_000_000,
maxAlbumArtWidth = 1_000_000_000,
requiresPlainFolders = false,
requiresPlainVideoItems = false,
timelineOffsetSeconds = 0
),
transcodingProfiles = emptyList(),
responseProfiles = emptyList(),
enableAlbumArtInDidl = false,
enableMsMediaReceiverRegistrar = false,
enableSingleAlbumArtLimit = false,
enableSingleSubtitleLimit = false,
ignoreTranscodeByteRangeRequests = false,
maxAlbumArtHeight = 1_000_000_000,
maxAlbumArtWidth = 1_000_000_000,
requiresPlainFolders = false,
requiresPlainVideoItems = false,
timelineOffsetSeconds = 0
),
startTimeTicks = null,
audioStreamIndex = null,
subtitleStreamIndex = null,
maxStreamingBitrate = 1_000_000_000,
)
)
mediaSourceInfoList = mediaInfo.mediaSources ?: emptyList()
return mediaSourceInfoList
}
startTimeTicks = null,
audioStreamIndex = null,
subtitleStreamIndex = null,
maxStreamingBitrate = 1_000_000_000,
)
).content.mediaSources.orEmpty()
}
override suspend fun getStreamUrl(itemId: UUID, mediaSourceId: String): String {
var streamUrl = ""
override suspend fun getStreamUrl(itemId: UUID, mediaSourceId: String): String =
withContext(Dispatchers.IO) {
try {
streamUrl = jellyfinApi.videosApi.getVideoStreamUrl(
jellyfinApi.videosApi.getVideoStreamUrl(
itemId,
static = true,
mediaSourceId = mediaSourceId
)
} catch (e: Exception) {
Timber.e(e)
""
}
}
return streamUrl
}
override suspend fun postCapabilities() {
Timber.d("Sending capabilities")
@ -291,13 +243,7 @@ class JellyfinRepositoryImpl(private val jellyfinApi: JellyfinApi) : JellyfinRep
}
}
override suspend fun getIntros(itemId: UUID): List<BaseItemDto> {
val intros: List<BaseItemDto>
withContext(Dispatchers.IO) {
intros =
jellyfinApi.userLibraryApi.getIntros(jellyfinApi.userId!!, itemId).content.items
?: emptyList()
}
return intros
override suspend fun getIntros(itemId: UUID): List<BaseItemDto> = withContext(Dispatchers.IO) {
jellyfinApi.userLibraryApi.getIntros(jellyfinApi.userId!!, itemId).content.items.orEmpty()
}
}

View file

@ -63,7 +63,7 @@ class HomeViewModel @Inject internal constructor(
}
}
private suspend fun loadDynamicItems() = withContext(Dispatchers.IO) {
private suspend fun loadDynamicItems(): List<Section> {
val resumeItems = repository.getResumeItems()
val nextUpItems = repository.getNextUp()
@ -88,18 +88,16 @@ class HomeViewModel @Inject internal constructor(
)
}
items.map { Section(it) }
return items.map { Section(it) }
}
private suspend fun loadViews() = withContext(Dispatchers.IO) {
repository
.getUserViews()
.filter { view -> unsupportedCollections().none { it.type == view.collectionType } }
.map { view -> view to repository.getLatestMedia(view.id) }
.filter { (_, latest) -> latest.isNotEmpty() }
.map { (view, latest) -> view.toView().apply { items = latest } }
.map { ViewItem(it) }
}
private suspend fun loadViews() = repository
.getUserViews()
.filter { view -> unsupportedCollections().none { it.type == view.collectionType } }
.map { view -> view to repository.getLatestMedia(view.id) }
.filter { (_, latest) -> latest.isNotEmpty() }
.map { (view, latest) -> view.toView().apply { items = latest } }
.map { ViewItem(it) }
}