Skip to content

Commit

Permalink
#6 - Add error handling in RecyclerView
Browse files Browse the repository at this point in the history
  • Loading branch information
jhg3410 committed Jul 8, 2022
1 parent 0e5f7b3 commit 53ea86d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@

package com.example.android.marsrealestate

import android.view.View
import android.widget.ImageView
import androidx.core.net.toUri
import androidx.databinding.BindingAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.example.android.marsrealestate.network.MarsProperty
import com.example.android.marsrealestate.overview.MarsApiStatus
import com.example.android.marsrealestate.overview.PhotoGridAdapter

@BindingAdapter("imageUrl")
Expand All @@ -43,4 +45,21 @@ fun bindImage(imgView: ImageView, imgUrl:String?){
fun bindRecyclerView(recyclerView: RecyclerView, data: List<MarsProperty>?){
val adapter = recyclerView.adapter as PhotoGridAdapter
adapter.submitList(data)
}

@BindingAdapter("marsApiStatus") // viewModel 의 init 에서 한 번 호출되는 getMarsRealEstateProperties 에 따른 변화
fun bindStatus(statusImageView: ImageView, status: MarsApiStatus?){
when(status) {
MarsApiStatus.LOADING -> {
statusImageView.visibility = View.VISIBLE
statusImageView.setImageResource(R.drawable.loading_animation)
}
MarsApiStatus.ERROR -> {
statusImageView.visibility = View.VISIBLE
statusImageView.setImageResource(R.drawable.ic_connection_error)
}
MarsApiStatus.DONE -> {
statusImageView.visibility = View.GONE
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,21 @@ import retrofit2.Call
import retrofit2.Response
import javax.security.auth.callback.Callback


enum class MarsApiStatus { LOADING, ERROR, DONE }


/**
* The [ViewModel] that is attached to the [OverviewFragment].
*/
class OverviewViewModel : ViewModel() {

// The internal MutableLiveData String that stores the most recent response
private val _response = MutableLiveData<String>()
private val _status = MutableLiveData<MarsApiStatus>()

// The external immutable LiveData for the response String
val response: LiveData<String>
get() = _response
val status: LiveData<MarsApiStatus>
get() = _status

private val _properties = MutableLiveData<List<MarsProperty>>()
val properties : LiveData<List<MarsProperty>>
Expand All @@ -54,13 +58,15 @@ class OverviewViewModel : ViewModel() {
/**
* Sets the value of the status LiveData to the Mars API status.
*/
private fun getMarsRealEstateProperties() {
private fun getMarsRealEstateProperties() { // 시작할 때만 호출
viewModelScope.launch {
_status.value = MarsApiStatus.LOADING
try {
_status.value = MarsApiStatus.DONE
_properties.value = MarsApi.retrofitService.getProperties()
_response.value = "Success: Mars properties retrieved"
} catch (e: Exception){
_response.value = "Failure: ${e.message}"
_status.value = MarsApiStatus.ERROR
_properties.value = ArrayList()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,15 @@
tools:itemCount="16"
tools:listitem="@layout/grid_view_item" />

<ImageView
android:id="@+id/status_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:marsApiStatus="@{viewModel.status}" />

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

0 comments on commit 53ea86d

Please sign in to comment.