Skip to content

Instantly share code, notes, and snippets.

View BambangHeriSetiawan's full-sized avatar

Bambang Heri Setiawan BambangHeriSetiawan

View GitHub Profile
/**
* Created by simx on 14,August,2019
*/
class MainVM:BaseObservable() {
var movies : LiveData<PagedList<ResponseMovies.ResultsItem>>
var dataSource: MovieDataSourceFactory
init {
val config = PagedList.Config.Builder()
.setPageSize(10)
object ApiRequests {
suspend fun discover(page:Int, year:Int) = API.Factory.create(BuildConfig.BASE_URL).discoverAsync(BuildConfig.API_KEY,page,year).await()
suspend fun search(page:Int, year:Int,query:String) = API.Factory.create(BuildConfig.BASE_URL).searchAsync(BuildConfig.API_KEY,page,year,query).await()
}
interface API {
@Headers("Accept: application/json", "Content-type: application/json")
@GET("discover/movie")
fun discoverAsync(@Query("api_key")apiKey:String,
@Query("page")page:Int,
@Query("year")year:Int
): Deferred<ResponseMovies>
@Headers("Accept: application/json", "Content-type: application/json")
@BambangHeriSetiawan
BambangHeriSetiawan / MovieDataSourceFactory.kt
Last active August 14, 2019 17:41
MovieDataSourceFactory
package com.simx.paggingsample.data.paging
import androidx.paging.DataSource
import com.simx.paggingsample.data.discover.ResponseMovies
/**
* Created by simx on 14,August,2019
*/
class MovieDataSourceFactory(): DataSource.Factory<Int, ResponseMovies.ResultsItem>() {
/**
@BambangHeriSetiawan
BambangHeriSetiawan / MovieDataSource.kt
Last active August 14, 2019 17:40
MovieDataSource
package com.simx.paggingsample.data.paging
import androidx.paging.PageKeyedDataSource
import com.simx.paggingsample.data.ApiRequests
import com.simx.paggingsample.data.discover.ResponseMovies
import kotlinx.coroutines.*
import kotlin.coroutines.CoroutineContext
/**
@BambangHeriSetiawan
BambangHeriSetiawan / activity_main.xml
Last active August 14, 2019 17:40
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable name="mainVm" type="com.simx.paggingsample.MainVM"/>
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
@BambangHeriSetiawan
BambangHeriSetiawan / build.gradle
Last active August 14, 2019 17:39
build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 29
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
if (savedInstanceState == null) {
supportFragmentManager.beginTransaction()
.replace(R.id.container, MainFragment.newInstance())
.commitNow()
}