Move HomeViewModel to use JellyfinRepository
This commit is contained in:
parent
565336cbf6
commit
df73db0ece
3 changed files with 64 additions and 49 deletions
|
@ -6,13 +6,19 @@ import org.jellyfin.sdk.model.api.MediaSourceInfo
|
|||
import java.util.*
|
||||
|
||||
interface JellyfinRepository {
|
||||
suspend fun getUserViews(): List<BaseItemDto>
|
||||
|
||||
suspend fun getItem(itemId: UUID): BaseItemDto
|
||||
|
||||
suspend fun getItems(parentId: UUID? = null): List<BaseItemDto>
|
||||
|
||||
suspend fun getResumeItems(): List<BaseItemDto>
|
||||
|
||||
suspend fun getLatestMedia(parentId: UUID): List<BaseItemDto>
|
||||
|
||||
suspend fun getSeasons(seriesId: UUID): List<BaseItemDto>
|
||||
|
||||
suspend fun getNextUp(seriesId: UUID): List<BaseItemDto>
|
||||
suspend fun getNextUp(seriesId: UUID? = null): List<BaseItemDto>
|
||||
|
||||
suspend fun getEpisodes(seriesId: UUID, seasonId: UUID, fields: List<ItemFields>? = null): List<BaseItemDto>
|
||||
|
||||
|
|
|
@ -8,6 +8,15 @@ import org.jellyfin.sdk.model.api.*
|
|||
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 ?: listOf()
|
||||
}
|
||||
return views
|
||||
}
|
||||
|
||||
override suspend fun getItem(itemId: UUID): BaseItemDto {
|
||||
val item: BaseItemDto
|
||||
withContext(Dispatchers.IO) {
|
||||
|
@ -27,6 +36,26 @@ class JellyfinRepositoryImpl(private val jellyfinApi: JellyfinApi) : JellyfinRep
|
|||
return items
|
||||
}
|
||||
|
||||
override suspend fun getResumeItems(): List<BaseItemDto> {
|
||||
val items: List<BaseItemDto>
|
||||
withContext(Dispatchers.IO) {
|
||||
items =
|
||||
jellyfinApi.itemsApi.getResumeItems(jellyfinApi.userId!!).content.items ?: listOf()
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
override suspend fun getLatestMedia(parentId: UUID): List<BaseItemDto> {
|
||||
val items: List<BaseItemDto>
|
||||
withContext(Dispatchers.IO) {
|
||||
items = jellyfinApi.userLibraryApi.getLatestMedia(
|
||||
jellyfinApi.userId!!,
|
||||
parentId = parentId
|
||||
).content
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
override suspend fun getSeasons(seriesId: UUID): List<BaseItemDto> {
|
||||
val seasons: List<BaseItemDto>
|
||||
withContext(Dispatchers.IO) {
|
||||
|
@ -36,12 +65,12 @@ class JellyfinRepositoryImpl(private val jellyfinApi: JellyfinApi) : JellyfinRep
|
|||
return seasons
|
||||
}
|
||||
|
||||
override suspend fun getNextUp(seriesId: UUID): List<BaseItemDto> {
|
||||
override suspend fun getNextUp(seriesId: UUID?): List<BaseItemDto> {
|
||||
val nextUpItems: List<BaseItemDto>
|
||||
withContext(Dispatchers.IO) {
|
||||
nextUpItems = jellyfinApi.showsApi.getNextUp(
|
||||
jellyfinApi.userId!!,
|
||||
seriesId = seriesId.toString()
|
||||
seriesId = seriesId?.toString(),
|
||||
).content.items ?: listOf()
|
||||
}
|
||||
return nextUpItems
|
||||
|
@ -126,13 +155,26 @@ class JellyfinRepositoryImpl(private val jellyfinApi: JellyfinApi) : JellyfinRep
|
|||
override suspend fun postPlaybackStop(itemId: UUID, positionTicks: Long) {
|
||||
Log.d("PlayerActivity", "Sending stop $itemId")
|
||||
withContext(Dispatchers.IO) {
|
||||
jellyfinApi.playStateApi.onPlaybackStopped(jellyfinApi.userId!!, itemId, positionTicks = positionTicks)
|
||||
jellyfinApi.playStateApi.onPlaybackStopped(
|
||||
jellyfinApi.userId!!,
|
||||
itemId,
|
||||
positionTicks = positionTicks
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun postPlaybackProgress(itemId: UUID, positionTicks: Long, isPaused: Boolean) {
|
||||
override suspend fun postPlaybackProgress(
|
||||
itemId: UUID,
|
||||
positionTicks: Long,
|
||||
isPaused: Boolean
|
||||
) {
|
||||
withContext(Dispatchers.IO) {
|
||||
jellyfinApi.playStateApi.onPlaybackProgress(jellyfinApi.userId!!, itemId, positionTicks = positionTicks, isPaused = isPaused)
|
||||
jellyfinApi.playStateApi.onPlaybackProgress(
|
||||
jellyfinApi.userId!!,
|
||||
itemId,
|
||||
positionTicks = positionTicks,
|
||||
isPaused = isPaused
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package dev.jdtech.jellyfin.viewmodels
|
||||
|
||||
import android.app.Application
|
||||
import android.util.Log
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
|
@ -8,14 +9,11 @@ import androidx.lifecycle.viewModelScope
|
|||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import dev.jdtech.jellyfin.R
|
||||
import dev.jdtech.jellyfin.adapters.HomeItem
|
||||
import dev.jdtech.jellyfin.api.JellyfinApi
|
||||
import dev.jdtech.jellyfin.models.HomeSection
|
||||
import dev.jdtech.jellyfin.models.View
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import dev.jdtech.jellyfin.repository.JellyfinRepository
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.jellyfin.sdk.model.api.BaseItemDto
|
||||
import org.jellyfin.sdk.model.api.BaseItemDtoQueryResult
|
||||
import java.util.*
|
||||
import javax.inject.Inject
|
||||
|
||||
|
@ -23,9 +21,9 @@ import javax.inject.Inject
|
|||
class HomeViewModel
|
||||
@Inject
|
||||
constructor(
|
||||
application: Application
|
||||
application: Application,
|
||||
private val jellyfinRepository: JellyfinRepository
|
||||
) : ViewModel() {
|
||||
private val jellyfinApi = JellyfinApi.getInstance(application, "")
|
||||
|
||||
private val continueWatchingString = application.resources.getString(R.string.continue_watching)
|
||||
private val nextUpString = application.resources.getString(R.string.next_up)
|
||||
|
@ -52,9 +50,9 @@ constructor(
|
|||
viewModelScope.launch {
|
||||
try {
|
||||
val views: MutableList<View> = mutableListOf()
|
||||
val viewsResult = getViews(jellyfinApi.userId!!)
|
||||
for (view in viewsResult.items!!) {
|
||||
val latestItems = getLatestMedia(jellyfinApi.userId!!, view.id)
|
||||
val userViews = jellyfinRepository.getUserViews()
|
||||
for (view in userViews) {
|
||||
val latestItems = jellyfinRepository.getLatestMedia(view.id)
|
||||
if (latestItems.isEmpty()) continue
|
||||
val v = view.toView()
|
||||
v.items = latestItems
|
||||
|
@ -63,7 +61,7 @@ constructor(
|
|||
|
||||
val items = mutableListOf<HomeItem>()
|
||||
|
||||
val resumeItems = getResumeItems()
|
||||
val resumeItems = jellyfinRepository.getResumeItems()
|
||||
val resumeSection =
|
||||
HomeSection(UUID.randomUUID(), continueWatchingString, resumeItems)
|
||||
|
||||
|
@ -71,7 +69,7 @@ constructor(
|
|||
items.add(HomeItem.Section(resumeSection))
|
||||
}
|
||||
|
||||
val nextUpItems = getNextUp()
|
||||
val nextUpItems = jellyfinRepository.getNextUp()
|
||||
val nextUpSection = HomeSection(UUID.randomUUID(), nextUpString, nextUpItems)
|
||||
|
||||
if (!nextUpItems.isNullOrEmpty()) {
|
||||
|
@ -82,43 +80,12 @@ constructor(
|
|||
|
||||
_finishedLoading.value = true
|
||||
} catch (e: Exception) {
|
||||
Log.e("HomeViewModel", e.message.toString())
|
||||
_finishedLoading.value = true
|
||||
_error.value = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun getViews(userId: UUID): BaseItemDtoQueryResult {
|
||||
val views: BaseItemDtoQueryResult
|
||||
withContext(Dispatchers.IO) {
|
||||
views = jellyfinApi.viewsApi.getUserViews(userId).content
|
||||
}
|
||||
return views
|
||||
}
|
||||
|
||||
private suspend fun getLatestMedia(userId: UUID, parentId: UUID): List<BaseItemDto> {
|
||||
val items: List<BaseItemDto>
|
||||
withContext(Dispatchers.IO) {
|
||||
items = jellyfinApi.userLibraryApi.getLatestMedia(userId, parentId = parentId).content
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
private suspend fun getNextUp(): List<BaseItemDto>? {
|
||||
val items: List<BaseItemDto>?
|
||||
withContext(Dispatchers.IO) {
|
||||
items = jellyfinApi.showsApi.getNextUp(jellyfinApi.userId!!).content.items
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
private suspend fun getResumeItems(): List<BaseItemDto>? {
|
||||
val items: List<BaseItemDto>?
|
||||
withContext(Dispatchers.IO) {
|
||||
items = jellyfinApi.itemsApi.getResumeItems(jellyfinApi.userId!!).content.items
|
||||
}
|
||||
return items
|
||||
}
|
||||
}
|
||||
|
||||
private fun BaseItemDto.toView(): View {
|
||||
|
|
Loading…
Reference in a new issue