Skip to content

Commit

Permalink
status of pics using BindingAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
nijuyonkadesu committed Oct 5, 2022
1 parent 03be245 commit d514e9e
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 4 deletions.
Binary file modified .gradle/7.4/executionHistory/executionHistory.lock
Binary file not shown.
Binary file modified .gradle/7.4/fileHashes/fileHashes.bin
Binary file not shown.
Binary file modified .gradle/7.4/fileHashes/fileHashes.lock
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/outputFiles.bin
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.android.marsphotos.overview

import android.view.View
import android.widget.ImageView
import androidx.core.net.toUri
import androidx.databinding.BindingAdapter
Expand All @@ -22,4 +23,20 @@ fun bindImage(imgView: ImageView, imgUrl: String?) {
fun bindRecyclerView(recyclerView: RecyclerView, data: List<MarsPhoto>?) {
val adapter = recyclerView.adapter as PhotoGridAdapter
adapter.submitList(data) // tells recycler view when new list is available
}
@BindingAdapter("marsApiStatus")
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 @@ -24,14 +24,16 @@ import com.example.android.marsphotos.network.MarsApi
import com.example.android.marsphotos.network.MarsPhoto
import kotlinx.coroutines.launch

enum class MarsApiStatus { LOADING, ERROR, DONE }

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

// The internal MutableLiveData that stores the status of the most recent request
private val _status = MutableLiveData<String>()
val status: LiveData<String> = _status
private val _status = MutableLiveData<MarsApiStatus>()
val status: LiveData<MarsApiStatus> = _status

private val _photos = MutableLiveData<List<MarsPhoto>>()
val photos: LiveData<List<MarsPhoto>> = _photos
Expand All @@ -49,12 +51,14 @@ class OverviewViewModel : ViewModel() {
*/
private fun getMarsPhotos() {
viewModelScope.launch {
_status.value = MarsApiStatus.LOADING
try {
_photos.value = MarsApi.retrofitService.getPhotos()
_status.value = "Success: Mars properties retrieved"
_status.value = MarsApiStatus.DONE
}
catch (e: Exception){
_status.value = "Failure: ${e.message}"
_status.value = MarsApiStatus.ERROR
_photos.value = listOf() // clears RecyclerView
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/layout/fragment_overview.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,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 d514e9e

Please sign in to comment.