63 lines
2.1 KiB
Kotlin
63 lines
2.1 KiB
Kotlin
package org.mosad.teapod.activity.main.fragments
|
|
|
|
import android.app.Application
|
|
import android.util.Log
|
|
import androidx.lifecycle.AndroidViewModel
|
|
import org.mosad.teapod.activity.main.MainActivity
|
|
import org.mosad.teapod.parser.AoDParser
|
|
import org.mosad.teapod.util.*
|
|
|
|
class MediaFragmentViewModel(application: Application) : AndroidViewModel(application) {
|
|
|
|
var media = Media(-1, "", DataTypes.MediaType.OTHER)
|
|
internal set
|
|
var nextEpisode = Episode()
|
|
internal set
|
|
var tmdb = TMDBResponse()
|
|
internal set
|
|
|
|
suspend fun load(mediaId: Int) {
|
|
media = AoDParser.getMediaById(mediaId)
|
|
tmdb = TMDBApiController().search(media.info.title, media.type)
|
|
|
|
if (media.type == DataTypes.MediaType.TVSHOW) {
|
|
nextEpisode = if (media.episodes.firstOrNull{ !it.watched } != null) {
|
|
media.episodes.first{ !it.watched }
|
|
} else {
|
|
media.episodes.first()
|
|
}
|
|
}
|
|
}
|
|
|
|
fun playEpisode(ep: Episode) {
|
|
playStream(ep)
|
|
|
|
// update nextEpisode
|
|
val currentEpNumber = nextEpisode.number
|
|
nextEpisode = if (media.episodes.firstOrNull{ !it.watched } != null) {
|
|
media.episodes.first{ !it.watched && it.number > currentEpNumber }
|
|
} else {
|
|
media.episodes.first()
|
|
}
|
|
}
|
|
|
|
fun playStream(ep: Episode) {
|
|
Log.d(javaClass.name, "Starting Player with mediaId: ${media.id}")
|
|
|
|
// TODO somehow start the player ...
|
|
MainActivity.instance.startPlayer(media.id, ep.id) // this is just a workaround!
|
|
|
|
// not working TODO once thsi is solved, use ViewModel instead of AndroidViewModel
|
|
// with(getApplication<Application>().baseContext) {
|
|
// val intent = Intent(this, PlayerActivity::class.java).apply {
|
|
// putExtra(getString(R.string.intent_media_id), media.id)
|
|
// putExtra(getString(R.string.intent_episode_id), ep.id)
|
|
// addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
|
// }
|
|
// startActivity(intent)
|
|
// }
|
|
|
|
//(activity as MainActivity).startPlayer(media.id, ep.id)
|
|
}
|
|
|
|
} |