Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate NextUp code to Kotlin flows #1583

Merged
merged 1 commit into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package org.jellyfin.androidtv.ui.playback.nextup
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import org.jellyfin.androidtv.data.service.BackgroundService
import org.jellyfin.androidtv.ui.playback.ExternalPlayerActivity
import org.jellyfin.androidtv.ui.playback.PlaybackOverlayActivity
Expand All @@ -24,20 +27,24 @@ class NextUpActivity : FragmentActivity() {
val useExternalPlayer = intent.getBooleanExtra(EXTRA_USE_EXTERNAL_PLAYER, false)

// Observe state
viewModel.state.observe(this) { state ->
when (state) {
// Open next item
NextUpState.PLAY_NEXT -> {
when (useExternalPlayer) {
true -> startActivity(Intent(this, ExternalPlayerActivity::class.java))
false -> startActivity(Intent(this, PlaybackOverlayActivity::class.java))
lifecycleScope.launchWhenCreated {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.state.collect { state ->
when (state) {
// Open next item
NextUpState.PLAY_NEXT -> {
when (useExternalPlayer) {
true -> startActivity(Intent(this@NextUpActivity, ExternalPlayerActivity::class.java))
false -> startActivity(Intent(this@NextUpActivity, PlaybackOverlayActivity::class.java))
}
finish()
}
// Close activity
NextUpState.CLOSE -> finish()
// Unknown state
else -> Unit
}
finish()
}
// Close activity
NextUpState.CLOSE -> finish()
// Unknown state
else -> Unit
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import kotlinx.coroutines.launch
import org.jellyfin.androidtv.data.service.BackgroundService
import org.jellyfin.androidtv.databinding.FragmentNextUpBinding
import org.jellyfin.androidtv.preference.UserPreferences
Expand All @@ -22,15 +26,19 @@ class NextUpFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
binding = FragmentNextUpBinding.inflate(inflater, container, false)

viewModel.item.observe(viewLifecycleOwner) { data ->
// No data, keep current
if (data == null) return@observe
viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.item.collect { data ->
// No data, keep current
if (data == null) return@collect

backgroundService.setBackground(data.baseItem)
backgroundService.setBackground(data.baseItem)

binding.logo.setImageBitmap(data.logo)
binding.image.setImageBitmap(data.thumbnail)
binding.title.text = data.title
binding.logo.setImageBitmap(data.logo)
binding.image.setImageBitmap(data.thumbnail)
binding.title.text = data.title
}
}
}

binding.fragmentNextUpButtons.apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package org.jellyfin.androidtv.ui.playback.nextup

import android.content.Context
import android.graphics.Bitmap
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.bumptech.glide.Glide
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.jellyfin.androidtv.preference.UserPreferences
Expand All @@ -24,27 +24,26 @@ class NextUpViewModel(
private val userPreferences: UserPreferences,
private val imageHelper: ImageHelper,
) : ViewModel() {
private val _item = MutableLiveData<NextUpItemData?>()
val item: LiveData<NextUpItemData?> = _item
private val _state = MutableLiveData(NextUpState.INITIALIZED)
val state: LiveData<NextUpState> = _state
private val _item = MutableStateFlow<NextUpItemData?>(null)
val item: StateFlow<NextUpItemData?> = _item
private val _state = MutableStateFlow(NextUpState.INITIALIZED)
val state: StateFlow<NextUpState> = _state

fun setItemId(id: UUID?) = viewModelScope.launch {
if (id == null) {
_item.postValue(null)
_state.postValue(NextUpState.NO_DATA)
_item.value = null
_state.value = NextUpState.NO_DATA
} else {
val item = loadItemData(id)
_item.postValue(item)
_item.value = loadItemData(id)
}
}

fun playNext() {
_state.postValue(NextUpState.PLAY_NEXT)
_state.value = NextUpState.PLAY_NEXT
}

fun close() {
_state.postValue(NextUpState.CLOSE)
_state.value = NextUpState.CLOSE
}

@Suppress("TooGenericExceptionCaught", "SwallowedException")
Expand Down