replace runBlocking{} in setCurrentEpisode with suspend
this fixes the player frezzing for a few 100ms when loading a new episode
This commit is contained in:
		| @ -167,14 +167,14 @@ class PlayerViewModel(application: Application) : AndroidViewModel(application) | ||||
|      */ | ||||
|     fun playNextEpisode() = currentEpisode.nextEpisodeId?.let { nextEpisodeId -> | ||||
|         updatePlayhead() // update playhead before switching to new episode | ||||
|         setCurrentEpisode(nextEpisodeId, startPlayback = true) | ||||
|         viewModelScope.launch { setCurrentEpisode(nextEpisodeId, startPlayback = true) } | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Set currentEpisodeCr to the episode of the given ID | ||||
|      * @param episodeId The ID of the episode you want to set currentEpisodeCr to | ||||
|      */ | ||||
|     fun setCurrentEpisode(episodeId: String, startPlayback: Boolean = false) { | ||||
|     suspend fun setCurrentEpisode(episodeId: String, startPlayback: Boolean = false) { | ||||
|         currentEpisode = episodes.items.find { episode -> | ||||
|             episode.id == episodeId | ||||
|         } ?: NoneEpisode | ||||
| @ -193,26 +193,24 @@ class PlayerViewModel(application: Application) : AndroidViewModel(application) | ||||
|         currentEpisodeChangedListener.forEach { it() } | ||||
|  | ||||
|         // needs to be blocking, currentPlayback must be present when calling playCurrentMedia() | ||||
|         runBlocking { | ||||
|             joinAll( | ||||
|                 viewModelScope.launch(Dispatchers.IO) { | ||||
|                     currentPlayback = Crunchyroll.playback(currentEpisode.playback) | ||||
|                 }, | ||||
|                 viewModelScope.launch(Dispatchers.IO) { | ||||
|                     Crunchyroll.playheads(listOf(currentEpisode.id))[currentEpisode.id]?.let { | ||||
|                         // if the episode was fully watched, start at the beginning | ||||
|                         currentPlayhead = if (it.fullyWatched) { | ||||
|                             0 | ||||
|                         } else { | ||||
|                             (it.playhead.times(1000)).toLong() | ||||
|                         } | ||||
|         joinAll( | ||||
|             viewModelScope.launch(Dispatchers.IO) { | ||||
|                 currentPlayback = Crunchyroll.playback(currentEpisode.playback) | ||||
|             }, | ||||
|             viewModelScope.launch(Dispatchers.IO) { | ||||
|                 Crunchyroll.playheads(listOf(currentEpisode.id))[currentEpisode.id]?.let { | ||||
|                     // if the episode was fully watched, start at the beginning | ||||
|                     currentPlayhead = if (it.fullyWatched) { | ||||
|                         0 | ||||
|                     } else { | ||||
|                         (it.playhead.times(1000)).toLong() | ||||
|                     } | ||||
|                 }, | ||||
|                 viewModelScope.launch(Dispatchers.IO) { | ||||
|                     currentIntroMetadata = Crunchyroll.datalabIntro(currentEpisode.id) | ||||
|                 } | ||||
|             ) | ||||
|         } | ||||
|             }, | ||||
|             viewModelScope.launch(Dispatchers.IO) { | ||||
|                 currentIntroMetadata = Crunchyroll.datalabIntro(currentEpisode.id) | ||||
|             } | ||||
|         ) | ||||
|         Log.d(classTag, "playback: ${currentEpisode.playback}") | ||||
|  | ||||
|         if (startPlayback) { | ||||
|  | ||||
| @ -7,6 +7,7 @@ import android.view.View | ||||
| import android.view.ViewGroup | ||||
| import androidx.fragment.app.DialogFragment | ||||
| import androidx.lifecycle.ViewModelProvider | ||||
| import kotlinx.coroutines.runBlocking | ||||
| import org.mosad.teapod.R | ||||
| import org.mosad.teapod.databinding.PlayerEpisodesListBinding | ||||
| import org.mosad.teapod.ui.activity.player.PlayerViewModel | ||||
| @ -46,7 +47,10 @@ class EpisodeListDialogFragment : DialogFragment()  { | ||||
|             model.currentPlayheads.toMap(), | ||||
|             EpisodeItemAdapter.OnClickListener { episode -> | ||||
|                 dismiss() | ||||
|                 model.setCurrentEpisode(episode.id, startPlayback = true) | ||||
|                 // TODO make this none blocking, if necessary? | ||||
|                 runBlocking { | ||||
|                     model.setCurrentEpisode(episode.id, startPlayback = true) | ||||
|                 } | ||||
|             }, | ||||
|             EpisodeItemAdapter.ViewType.PLAYER | ||||
|         ) | ||||
|  | ||||
		Reference in New Issue
	
	Block a user