Skip to content

Commit

Permalink
feat: Display error screen when Movies aren't retrieved
Browse files Browse the repository at this point in the history
  • Loading branch information
HadySalhab committed Feb 8, 2020
1 parent 4ba271d commit 83bf21d
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,18 @@ public class RemoteDataSource(
val detailsResponse:LiveData<MovieDetailsResponse>
get() = _detailsResponse

private val _retrieveMoviesTimeOut = MutableLiveData<Boolean>()
val retrieveMoviesTimeOut:LiveData<Boolean>
get() = _retrieveMoviesTimeOut

fun getMovies(pageNumber: Int, sortBy: Categories) {
reset()
retrieveMoviesRunnable = RetrieveMoviesRunnable(pageNumber = pageNumber, sortBy = sortBy)

val handler = appExecutors.networkIO.submit(retrieveMoviesRunnable)

appExecutors.networkIO.schedule(Runnable {
_retrieveMoviesTimeOut.postValue(true)
handler.cancel(true)
}, NETWORK_TIMEOUT, TimeUnit.MILLISECONDS)
}
Expand All @@ -51,6 +56,7 @@ public class RemoteDataSource(
val handler = appExecutors.networkIO.submit(searchMovieRunnable)

appExecutors.networkIO.schedule(Runnable {
_retrieveMoviesTimeOut.postValue(true)
handler.cancel(true)
}, NETWORK_TIMEOUT, TimeUnit.MILLISECONDS)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class MoviesRepository(private val remoteDataSource: RemoteDataSource) {
private var query: String? = null
val movieList: LiveData<List<Movie>> = remoteDataSource.movieList
val detailMovieResponse: LiveData<MovieDetailsResponse> = remoteDataSource.detailsResponse
val retrieveMoviesTimeOut = remoteDataSource.retrieveMoviesTimeOut
fun getMovies(pageNumber: Int, sortBy: Categories) {
categories = sortBy
remoteDataSource.getMovies(pageNumber, sortBy)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class DetailFragmentViewModel(app: Application,
}

val movieDetail = Transformations.map(detailMovieResponse) { detailMovieResponse ->
Log.d(TAG, "${remoteToLocal.getMovieDetails(detailMovieResponse)} ")
remoteToLocal.getMovieDetails(detailMovieResponse)
}
val castDetails = Transformations.map(detailMovieResponse){detailMovieResponse->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class MovieListFragment : Fragment() {
private lateinit var recyclerView: RecyclerView
private lateinit var loadingView: View
private lateinit var paginationLoadingView: View
private lateinit var errorScreen:View

private var callbacks: Callbacks? = null

Expand All @@ -56,6 +57,7 @@ class MovieListFragment : Fragment() {
recyclerView = rootView.findViewById(R.id.recyclerview)
loadingView = rootView.findViewById(R.id.loading_view)
paginationLoadingView = rootView.findViewById(R.id.pagination_loading_view)
errorScreen = rootView.findViewById(R.id.error_screen)
initRecyclerView()
setHasOptionsMenu(true)
return rootView
Expand All @@ -68,15 +70,41 @@ class MovieListFragment : Fragment() {
movies?.let {
hideDisplayLoading()
adapter.submitList(movies)
movieListViewModel.areMoviesRetrieved = true
hideErrorScreen()
}
})
movieListViewModel.retrieveMoviesTimeOut.observe(viewLifecycleOwner, Observer {isTimedOut->
if(isTimedOut && !movieListViewModel.areMoviesRetrieved){
showErrorScreen()
}
})
}
fun showErrorScreen(){
errorScreen.visibility = View.VISIBLE
loadingView.visibility = View.GONE
paginationLoadingView.visibility = View.GONE
recyclerView.visibility = View.INVISIBLE
}
fun hideErrorScreen(){
errorScreen.visibility = View.GONE
loadingView.visibility = View.GONE
paginationLoadingView.visibility = View.GONE
recyclerView.visibility = View.VISIBLE
}

private fun hideDisplayLoading() {
loadingView.visibility = View.GONE
paginationLoadingView.visibility = View.GONE
}


private fun showDisplayLoading() {
movieListViewModel.areMoviesRetrieved =false
loadingView.visibility = View.VISIBLE
errorScreen.visibility = View.GONE
}

private fun initRecyclerView() {
recyclerView.apply {
this@MovieListFragment.adapter =
Expand Down Expand Up @@ -104,7 +132,7 @@ class MovieListFragment : Fragment() {
private fun fireRequest() {
val query = PreferencesStorage.getStoredQuery(this.requireContext())
query?.let {
//priority to queries
showDisplayLoading()
movieListViewModel.query = it
movieListViewModel.searchMovies(1, it)
return
Expand All @@ -119,6 +147,7 @@ class MovieListFragment : Fragment() {


fun getMovies(pageNumber: Int, sortBy: Categories) {
showDisplayLoading()
movieListViewModel.query = null
movieListViewModel.categories = sortBy
movieListViewModel.getMovies(pageNumber, sortBy)
Expand All @@ -137,7 +166,7 @@ class MovieListFragment : Fragment() {
queryHint = context.getString(R.string.query_hint)
setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
displayLoading()
showDisplayLoading()

query?.let {
movieListViewModel.query = it
Expand All @@ -159,25 +188,18 @@ class MovieListFragment : Fragment() {

}

private fun displayLoading() {
loadingView.visibility = View.VISIBLE
// recyclerView.visibility = View.INVISIBLE
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.menu_item_up_coming -> {
displayLoading()
getMovies(pageNumber = 1, sortBy = Categories.UPCOMING)
true
}
R.id.menu_item_popular_movies -> {
displayLoading()
getMovies(pageNumber = 1, sortBy = Categories.POPULAR)
true
}
R.id.menu_item_top_rated -> {
displayLoading()
getMovies(pageNumber = 1, sortBy = Categories.TOP_RATED)
true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ class MovieListViewModel(private val repository: MoviesRepository) : ViewModel()

var query:String?=null
var categories:Categories = Categories.POPULAR

var areMoviesRetrieved = false
val movieList: LiveData<List<Movie>> = repository.movieList
val retrieveMoviesTimeOut = repository.retrieveMoviesTimeOut

fun getMovies(pageNumber: Int, sortBy: Categories) {
repository.getMovies(pageNumber, sortBy)
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/res/layout/error_screen.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="No network connection"
android:textColor="@color/colorAccent"
android:textSize="25sp" />

</FrameLayout>
11 changes: 10 additions & 1 deletion app/src/main/res/layout/fragment_movie_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,14 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />


<include
android:id="@+id/error_screen"
layout="@layout/error_screen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/recyclerview" />
</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 83bf21d

Please sign in to comment.