This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun saveState(){ | |
if (player != null) { | |
playbackPosition = player?.currentPosition ?: 0L | |
currentWindowIndex = player?.currentWindowIndex ?: 0 | |
playWhenReady = player?.playWhenReady ?: true | |
} | |
} | |
private fun loadState(){ | |
player?.apply { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Build MediaSource for one video and prepare player | |
private fun preparePlayer(uri: Uri){ | |
val mediaSource = MediaSourceBuilder().build(uri) | |
player?.prepare(mediaSource, true, false) | |
} | |
//Overloaded function to build MediaSource for whole playlist and prepare player | |
private fun preparePlayer(uriList: Array<Uri>){ | |
val mediaSource = MediaSourceBuilder().build(uriList) | |
player?.prepare(mediaSource, true, false) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.navendra.player | |
class MediaSourceBuilder { | |
//Build various MediaSource depending upon the type of Media for a given video/audio uri | |
fun build(uri: Uri): MediaSource { | |
val userAgent = PlayerConstants.USER_AGENT | |
val lastPath = uri.lastPathSegment?:"" | |
val defaultHttpDataSourceFactory = DefaultHttpDataSourceFactory(userAgent) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open class BaseRepository{ | |
suspend fun <T : Any> safeApiCall(call: suspend () -> Response<T>, errorMessage: String): T? { | |
val result : Result<T> = safeApiResult(call,errorMessage) | |
var data : T? = null | |
when(result) { | |
is Result.Success -> | |
data = result.data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed class Result<out T: Any> { | |
data class Success<out T : Any>(val data: T) : Result<T>() | |
data class Error(val exception: Exception) : Result<Nothing>() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object BetterLog{ | |
private fun tag() : String{ | |
val stackTrace = Throwable().stackTrace | |
val fileName = stackTrace[2].fileName | |
val methodName = stackTrace[2].methodName | |
val lineNumber = stackTrace[2].lineNumber | |
return "$fileName::$methodName, Line - $lineNumber" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MovieActivity : AppCompatActivity(){ | |
private lateinit var tmdbViewModel: TmdbViewModel | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_movie) | |
tmdbViewModel = ViewModelProviders.of(this).get(TmdbViewModel::class.java) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MovieRepository(private val api : TmdbApi) : BaseRepository() { | |
fun getPopularMovies() : MutableList<TmdbMovie>?{ | |
//safeApiCall is defined in BaseRepository.kt (https://gist.github.com/navi25/67176730f5595b3f1fb5095062a92f15) | |
val movieResponse = safeApiCall( | |
call = {api.getPopularMovie().await()}, | |
errorMessage = "Error Fetching Popular Movies" | |
) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TmdbViewModel : ViewModel(){ | |
private val parentJob = Job() | |
private val coroutineContext: CoroutineContext | |
get() = parentJob + Dispatchers.Default | |
private val scope = CoroutineScope(coroutineContext) | |
private val repository : MovieRepository = MovieRepository(ApiFactory.tmdbApi) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"page": 1, | |
"total_results": 19848, | |
"total_pages": 993, | |
"results": [ | |
{ | |
"vote_count": 2109, | |
"id": 297802, | |
"video": false, | |
"vote_average": 6.9, |
NewerOlder