From 39171b617cf195f71dcf1dc4bc8f0b18c5e41a44 Mon Sep 17 00:00:00 2001 From: dendi6 Date: Sun, 2 May 2021 14:29:09 +0700 Subject: [PATCH] request data using retrofit --- .../data/source/remote/RemoteDataSource.kt | 120 +++++++++++++----- .../com/dendi/filmscatalogs/di/Injection.kt | 6 +- .../filmscatalogs/ui/detail/DetailActivity.kt | 2 +- .../filmscatalogs/ui/movies/MoviesFragment.kt | 2 +- .../filmscatalogs/ui/tvshow/TvShowFragment.kt | 2 +- .../viewmodel/ViewModelFactory.kt | 5 +- 6 files changed, 93 insertions(+), 44 deletions(-) diff --git a/app/src/main/java/com/dendi/filmscatalogs/data/source/remote/RemoteDataSource.kt b/app/src/main/java/com/dendi/filmscatalogs/data/source/remote/RemoteDataSource.kt index 554c353..a6bbd18 100644 --- a/app/src/main/java/com/dendi/filmscatalogs/data/source/remote/RemoteDataSource.kt +++ b/app/src/main/java/com/dendi/filmscatalogs/data/source/remote/RemoteDataSource.kt @@ -1,69 +1,121 @@ package com.dendi.filmscatalogs.data.source.remote -import android.os.Handler -import android.os.Looper +import android.content.ContentValues +import android.util.Log +import com.dendi.filmscatalogs.data.source.remote.network.ApiConfig import com.dendi.filmscatalogs.data.source.remote.response.DetailResponse import com.dendi.filmscatalogs.data.source.remote.response.ListResponse +import com.dendi.filmscatalogs.data.source.remote.response.ResponseItem import com.dendi.filmscatalogs.utils.EspressoIdlingResource -import com.dendi.filmscatalogs.utils.JsonHelper - -class RemoteDataSource private constructor(private val jsonHelper: JsonHelper) { - private val handler = Handler(Looper.getMainLooper()) +import retrofit2.Call +import retrofit2.Callback +import retrofit2.Response +class RemoteDataSource { companion object { - private const val SERVICE_LATENCY_IN_MILLIS: Long = 2000 - @Volatile private var instance: RemoteDataSource? = null - fun getInstance(helper: JsonHelper): RemoteDataSource = + fun getInstance(): RemoteDataSource = instance ?: synchronized(this) { - instance ?: RemoteDataSource(helper).apply { instance = this } + instance ?: RemoteDataSource().apply { instance = this } } } fun getAllMovies(callback: LoadAllMoviesCallback) { EspressoIdlingResource.increment() - handler.postDelayed( - { - callback.onAllMoviesReceived(jsonHelper.getMovies()) + val client = ApiConfig.getApiService().getMovies() + client.enqueue(object : Callback { + override fun onResponse( + call: Call, + response: Response + ) { + if (response.isSuccessful) { + response.body()?.let { callback.onAllMoviesReceived(it.results) } + EspressoIdlingResource.decrement() + } else { + Log.e(ContentValues.TAG, "onFailure: ${response.message()}") + EspressoIdlingResource.decrement() + } + } + + override fun onFailure(call: Call, t: Throwable) { + Log.e(ContentValues.TAG, "onFailure: ${t.message.toString()}") EspressoIdlingResource.decrement() - }, - SERVICE_LATENCY_IN_MILLIS - ) + } + }) } fun getAllTvShow(callback: LoadAllTvCallback) { EspressoIdlingResource.increment() - handler.postDelayed( - { - callback.onAllTvShowReceived(jsonHelper.getTvShow()) + val client = ApiConfig.getApiService().getTv() + client.enqueue(object : Callback { + override fun onResponse( + call: Call, + response: Response + ) { + if (response.isSuccessful) { + response.body()?.let { callback.onAllTvShowReceived(it.results) } + EspressoIdlingResource.decrement() + } else { + Log.e(ContentValues.TAG, "onFailure: ${response.message()}") + EspressoIdlingResource.decrement() + } + } + + override fun onFailure(call: Call, t: Throwable) { + Log.e(ContentValues.TAG, "onFailure: ${t.message.toString()}") EspressoIdlingResource.decrement() - }, - SERVICE_LATENCY_IN_MILLIS - ) + } + }) } fun getDetailMovies(id: Int, callback: LoadDetailMovieCallback) { EspressoIdlingResource.increment() - handler.postDelayed( - { - callback.onDetailMovieReceived(jsonHelper.getDetailMovies(id)) + val client = ApiConfig.getApiService().detailMovies(id) + client.enqueue(object : Callback { + override fun onResponse( + call: Call, + response: Response + ) { + if (response.isSuccessful) { + response.body()?.let { callback.onDetailMovieReceived(it) } + EspressoIdlingResource.decrement() + } else { + Log.e(ContentValues.TAG, "onFailure: ${response.message()}") + EspressoIdlingResource.decrement() + } + } + + override fun onFailure(call: Call, t: Throwable) { + Log.e(ContentValues.TAG, "onFailure: ${t.message.toString()}") EspressoIdlingResource.decrement() - }, - SERVICE_LATENCY_IN_MILLIS - ) + } + }) } fun getDetailTvShow(id: Int, callback: LoadDetailTvShowCallback) { EspressoIdlingResource.increment() - handler.postDelayed( - { - callback.onDetailTvShowReceived(jsonHelper.getDetailTvShow(id)) + val client = ApiConfig.getApiService().detailTv(id) + client.enqueue(object : Callback { + override fun onResponse( + call: Call, + response: Response + ) { + if (response.isSuccessful) { + response.body()?.let { callback.onDetailTvShowReceived(it) } + EspressoIdlingResource.decrement() + } else { + Log.e(ContentValues.TAG, "onFailure: ${response.message()}") + EspressoIdlingResource.decrement() + } + } + + override fun onFailure(call: Call, t: Throwable) { + Log.e(ContentValues.TAG, "onFailure: ${t.message.toString()}") EspressoIdlingResource.decrement() - }, - SERVICE_LATENCY_IN_MILLIS - ) + } + }) } interface LoadAllMoviesCallback { diff --git a/app/src/main/java/com/dendi/filmscatalogs/di/Injection.kt b/app/src/main/java/com/dendi/filmscatalogs/di/Injection.kt index 1636b8b..bcb8e0a 100644 --- a/app/src/main/java/com/dendi/filmscatalogs/di/Injection.kt +++ b/app/src/main/java/com/dendi/filmscatalogs/di/Injection.kt @@ -1,14 +1,12 @@ package com.dendi.filmscatalogs.di -import android.content.Context import com.dendi.filmscatalogs.data.FilmRepository import com.dendi.filmscatalogs.data.source.remote.RemoteDataSource -import com.dendi.filmscatalogs.utils.JsonHelper object Injection { - fun provideRepository(context: Context): FilmRepository { + fun provideRepository(): FilmRepository { - val remoteDataSource = RemoteDataSource.getInstance(JsonHelper(context)) + val remoteDataSource = RemoteDataSource.getInstance() return FilmRepository.getInstance(remoteDataSource) } diff --git a/app/src/main/java/com/dendi/filmscatalogs/ui/detail/DetailActivity.kt b/app/src/main/java/com/dendi/filmscatalogs/ui/detail/DetailActivity.kt index 19ecd73..e470a97 100644 --- a/app/src/main/java/com/dendi/filmscatalogs/ui/detail/DetailActivity.kt +++ b/app/src/main/java/com/dendi/filmscatalogs/ui/detail/DetailActivity.kt @@ -40,7 +40,7 @@ class DetailActivity : AppCompatActivity() { } val type = intent.getStringExtra(EXTRA_TYPE) - val factory = ViewModelFactory.getInstance(this) + val factory = ViewModelFactory.getInstance() val film = intent.getParcelableExtra(EXTRA_DATA) as ListEntity showLoading(true) diff --git a/app/src/main/java/com/dendi/filmscatalogs/ui/movies/MoviesFragment.kt b/app/src/main/java/com/dendi/filmscatalogs/ui/movies/MoviesFragment.kt index 6da9548..dd17302 100644 --- a/app/src/main/java/com/dendi/filmscatalogs/ui/movies/MoviesFragment.kt +++ b/app/src/main/java/com/dendi/filmscatalogs/ui/movies/MoviesFragment.kt @@ -37,7 +37,7 @@ class MoviesFragment : Fragment() { progressBar.visibility = View.VISIBLE - val factory = ViewModelFactory.getInstance(requireActivity()) + val factory = ViewModelFactory.getInstance() moviesViewModel = ViewModelProvider(this, factory)[MoviesViewModel::class.java] moviesViewModel.getMovies().observe(this, { listMovie -> diff --git a/app/src/main/java/com/dendi/filmscatalogs/ui/tvshow/TvShowFragment.kt b/app/src/main/java/com/dendi/filmscatalogs/ui/tvshow/TvShowFragment.kt index 523fb3c..f61b255 100644 --- a/app/src/main/java/com/dendi/filmscatalogs/ui/tvshow/TvShowFragment.kt +++ b/app/src/main/java/com/dendi/filmscatalogs/ui/tvshow/TvShowFragment.kt @@ -35,7 +35,7 @@ class TvShowFragment : Fragment() { progressBar.visibility = View.VISIBLE - val factory = ViewModelFactory.getInstance(requireActivity()) + val factory = ViewModelFactory.getInstance() tvShowViewModel = ViewModelProvider(this, factory)[TvShowViewModel::class.java] tvShowViewModel.getTvShow().observe(this, { listTvShow -> diff --git a/app/src/main/java/com/dendi/filmscatalogs/viewmodel/ViewModelFactory.kt b/app/src/main/java/com/dendi/filmscatalogs/viewmodel/ViewModelFactory.kt index cda8e70..c22d33b 100644 --- a/app/src/main/java/com/dendi/filmscatalogs/viewmodel/ViewModelFactory.kt +++ b/app/src/main/java/com/dendi/filmscatalogs/viewmodel/ViewModelFactory.kt @@ -1,6 +1,5 @@ package com.dendi.filmscatalogs.viewmodel -import android.content.Context import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider import com.dendi.filmscatalogs.data.FilmRepository @@ -16,9 +15,9 @@ class ViewModelFactory private constructor(private val mFilmsRepository: FilmRep @Volatile private var instance: ViewModelFactory? = null - fun getInstance(context: Context): ViewModelFactory = + fun getInstance(): ViewModelFactory = instance ?: synchronized(this) { - instance ?: ViewModelFactory(Injection.provideRepository(context)).apply { + instance ?: ViewModelFactory(Injection.provideRepository()).apply { instance = this } }