Add dependency injection to MediaViewModel

This commit is contained in:
Jarne Demeulemeester 2021-07-06 15:29:35 +02:00
parent 2c8ddaad24
commit 7908661b82
No known key found for this signature in database
GPG key ID: 60884A0C1EBA43E5
4 changed files with 7 additions and 20 deletions

View file

@ -30,7 +30,7 @@ class MediaFragment : Fragment() {
binding.viewModel = viewModel
binding.viewsRecyclerView.adapter =
CollectionListAdapter(CollectionListAdapter.OnClickListener { library ->
nagivateToLibraryFragment(library)
navigateToLibraryFragment(library)
})
viewModel.finishedLoading.observe(viewLifecycleOwner, {
@ -42,7 +42,7 @@ class MediaFragment : Fragment() {
return binding.root
}
private fun nagivateToLibraryFragment(library: BaseItemDto) {
private fun navigateToLibraryFragment(library: BaseItemDto) {
findNavController().navigate(
MediaFragmentDirections.actionNavigationMediaToLibraryFragment(
library.id,

View file

@ -6,7 +6,7 @@ import java.util.*
interface JellyfinRepository {
suspend fun getItem(itemId: UUID): BaseItemDto
suspend fun getItems(parentId: UUID): List<BaseItemDto>
suspend fun getItems(parentId: UUID? = null): List<BaseItemDto>
suspend fun getSeasons(seriesId: UUID): List<BaseItemDto>

View file

@ -15,7 +15,7 @@ class JellyfinRepositoryImpl(private val jellyfinApi: JellyfinApi) : JellyfinRep
return item
}
override suspend fun getItems(parentId: UUID): List<BaseItemDto> {
override suspend fun getItems(parentId: UUID?): List<BaseItemDto> {
val items: List<BaseItemDto>
withContext(Dispatchers.IO) {
items = jellyfinApi.itemsApi.getItems(

View file

@ -1,23 +1,18 @@
package dev.jdtech.jellyfin.viewmodels
import android.app.Application
import androidx.lifecycle.*
import dagger.hilt.android.lifecycle.HiltViewModel
import dev.jdtech.jellyfin.api.JellyfinApi
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 java.util.*
import javax.inject.Inject
@HiltViewModel
class MediaViewModel
@Inject
constructor(
application: Application
private val jellyfinRepository: JellyfinRepository
) : ViewModel() {
private val jellyfinApi = JellyfinApi.getInstance(application, "")
private val _collections = MutableLiveData<List<BaseItemDto>>()
val collections: LiveData<List<BaseItemDto>> = _collections
@ -27,17 +22,9 @@ constructor(
init {
viewModelScope.launch {
val items = getItems(jellyfinApi.userId!!)
val items = jellyfinRepository.getItems()
_collections.value = items
_finishedLoading.value = true
}
}
private suspend fun getItems(userId: UUID): List<BaseItemDto>? {
var items: List<BaseItemDto>?
withContext(Dispatchers.IO) {
items = jellyfinApi.itemsApi.getItems(userId).content.items
}
return items
}
}