Skip to content

Commit

Permalink
request data using retrofit
Browse files Browse the repository at this point in the history
  • Loading branch information
Dendi6 committed May 2, 2021
1 parent 7c42693 commit 39171b6
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -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<ResponseItem> {
override fun onResponse(
call: Call<ResponseItem>,
response: Response<ResponseItem>
) {
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<ResponseItem>, 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<ResponseItem> {
override fun onResponse(
call: Call<ResponseItem>,
response: Response<ResponseItem>
) {
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<ResponseItem>, 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<DetailResponse> {
override fun onResponse(
call: Call<DetailResponse>,
response: Response<DetailResponse>
) {
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<DetailResponse>, 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<DetailResponse> {
override fun onResponse(
call: Call<DetailResponse>,
response: Response<DetailResponse>
) {
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<DetailResponse>, t: Throwable) {
Log.e(ContentValues.TAG, "onFailure: ${t.message.toString()}")
EspressoIdlingResource.decrement()
},
SERVICE_LATENCY_IN_MILLIS
)
}
})
}

interface LoadAllMoviesCallback {
Expand Down
6 changes: 2 additions & 4 deletions app/src/main/java/com/dendi/filmscatalogs/di/Injection.kt
Original file line number Diff line number Diff line change
@@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ListEntity>(EXTRA_DATA) as ListEntity

showLoading(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}
}
Expand Down

0 comments on commit 39171b6

Please sign in to comment.