Skip to content

Commit aaaadd3

Browse files
committed
add displaying a grid of images
1 parent 7dd980f commit aaaadd3

13 files changed

+95
-81
lines changed

10-mars-photos/.idea/libraries/Gradle__androidx_databinding_databinding_adapters_7_1_2_aar.xml

Lines changed: 0 additions & 13 deletions
This file was deleted.

10-mars-photos/.idea/libraries/Gradle__androidx_databinding_databinding_common_7_1_2.xml

Lines changed: 0 additions & 13 deletions
This file was deleted.

10-mars-photos/.idea/libraries/Gradle__androidx_databinding_databinding_ktx_7_1_2_aar.xml

Lines changed: 0 additions & 13 deletions
This file was deleted.

10-mars-photos/.idea/libraries/Gradle__androidx_databinding_databinding_runtime_7_1_2_aar.xml

Lines changed: 0 additions & 13 deletions
This file was deleted.

10-mars-photos/.idea/libraries/Gradle__androidx_databinding_viewbinding_7_1_2_aar.xml

Lines changed: 0 additions & 13 deletions
This file was deleted.

10-mars-photos/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@ An app to learn how to get data from an API, using Retrofit to make REST request
1717
- displaying a grid of images with a RecyclerView.
1818
- handling potential errors.
1919

20-
Based on [Get data from the internet](https://developer.android.com/codelabs/basic-android-kotlin-training-getting-data-internet) by Google Codelabs (2022).
20+
Based on 2 tutorials by Google Codelabs (2022):
21+
22+
- [Get data from the internet](https://developer.android.com/codelabs/basic-android-kotlin-training-getting-data-internet)
23+
- [Loading and displaying images from the internet](https://codelabs.developers.google.com/codelabs/kotlin-android-training-internet-images/index.html?index=..%2F..android-kotlin-fundamentals#0)

10-mars-photos/app/src/main/java/com/example/android/marsphotos/BindingAdapters.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ package com.example.android.marsphotos
33
import android.widget.ImageView
44
import androidx.core.net.toUri
55
import androidx.databinding.BindingAdapter
6+
import androidx.recyclerview.widget.RecyclerView
67
import com.bumptech.glide.Glide
78
import com.bumptech.glide.request.RequestOptions
9+
import com.example.android.marsphotos.network.MarsPhoto
10+
import com.example.android.marsphotos.overview.PhotoGridAdapter
811

912
@BindingAdapter("imageUrl")
1013
fun bindImage(imgView: ImageView, imgUrl: String?) {
@@ -17,4 +20,10 @@ fun bindImage(imgView: ImageView, imgUrl: String?) {
1720
.error(R.drawable.ic_broken_image))
1821
.into(imgView)
1922
}
23+
}
24+
25+
@BindingAdapter("listData")
26+
fun bindRecyclerView(recyclerView: RecyclerView, data: List<MarsPhoto>?) {
27+
val adapter = recyclerView.adapter as PhotoGridAdapter
28+
adapter.submitList(data)
2029
}

10-mars-photos/app/src/main/java/com/example/android/marsphotos/overview/OverviewFragment.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,20 @@ class OverviewFragment : Fragment() {
4040
inflater: LayoutInflater, container: ViewGroup?,
4141
savedInstanceState: Bundle?
4242
): View? {
43-
//val binding = FragmentOverviewBinding.inflate(inflater)
44-
val binding = GridViewItemBinding.inflate(inflater)
43+
val binding = FragmentOverviewBinding.inflate(inflater)
44+
//val binding = GridViewItemBinding.inflate(inflater)
4545

4646
// Allows Data Binding to Observe LiveData with the lifecycle of this Fragment
4747
binding.lifecycleOwner = this
4848

4949
// Giving the binding access to the OverviewViewModel
5050
binding.viewModel = viewModel
5151

52+
// Display a grid of images
53+
binding.photosGrid.adapter = PhotoGridAdapter()
54+
55+
setHasOptionsMenu(true)
56+
5257
return binding.root
5358
}
5459
}

10-mars-photos/app/src/main/java/com/example/android/marsphotos/overview/OverviewViewModel.kt

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,14 @@ class OverviewViewModel : ViewModel() {
3636
val status: LiveData<String> = _status
3737

3838
// LiveData for a single MarsProperty object
39-
private val _property = MutableLiveData<MarsPhoto>()
39+
/*private val _property = MutableLiveData<MarsPhoto>()
4040
val property: LiveData<MarsPhoto>
41-
get() = _property
41+
get() = _property*/
42+
43+
// LiveData for a list of images
44+
private val _properties = MutableLiveData<List<MarsPhoto>>()
45+
val properties: LiveData<List<MarsPhoto>>
46+
get() = _properties
4247

4348
/**
4449
* Call getMarsPhotos() on init so we can display status immediately.
@@ -54,11 +59,13 @@ class OverviewViewModel : ViewModel() {
5459
private fun getMarsPhotos() {
5560
viewModelScope.launch {
5661
try {
57-
val listResult = MarsApi.retrofitService.getPhotos()
58-
_status.value = "Success: ${listResult.size} Mars photos retrieved"
59-
if (listResult.isNotEmpty()) {
60-
_property.value = listResult[0]
61-
}
62+
// val listResult = MarsApi.retrofitService.getPhotos()
63+
// _status.value = "Success: ${listResult.size} Mars photos retrieved"
64+
/*if (listResult.isNotEmpty()) {
65+
//_property.value = listResult[0]
66+
}*/
67+
_properties.value = MarsApi.retrofitService.getPhotos()
68+
_status.value = "Success: Mars properties retrived"
6269
} catch (e: Exception) {
6370
_status.value = "Failure: ${e.message}"
6471
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.example.android.marsphotos.overview
2+
3+
import android.view.LayoutInflater
4+
import android.view.ViewGroup
5+
import androidx.recyclerview.widget.DiffUtil
6+
import androidx.recyclerview.widget.ListAdapter
7+
import androidx.recyclerview.widget.RecyclerView
8+
import com.example.android.marsphotos.databinding.GridViewItemBinding
9+
import com.example.android.marsphotos.network.MarsPhoto
10+
11+
class PhotoGridAdapter : ListAdapter<MarsPhoto,
12+
PhotoGridAdapter.MarsPhotoViewHolder>(DiffCallback) {
13+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PhotoGridAdapter.MarsPhotoViewHolder {
14+
return MarsPhotoViewHolder(GridViewItemBinding.inflate(
15+
LayoutInflater.from(parent.context)
16+
))
17+
}
18+
19+
override fun onBindViewHolder(holder: PhotoGridAdapter.MarsPhotoViewHolder, position: Int) {
20+
val marsPhoto = getItem(position)
21+
holder.bind(marsPhoto)
22+
}
23+
24+
companion object DiffCallback : DiffUtil.ItemCallback<MarsPhoto>() {
25+
override fun areItemsTheSame(oldItem: MarsPhoto, newItem: MarsPhoto): Boolean {
26+
return oldItem === newItem
27+
}
28+
29+
override fun areContentsTheSame(oldItem: MarsPhoto, newItem: MarsPhoto): Boolean {
30+
return oldItem.id == newItem.id
31+
}
32+
}
33+
34+
class MarsPhotoViewHolder(private var binding: GridViewItemBinding): RecyclerView.ViewHolder(binding.root) {
35+
fun bind(marsPhoto: MarsPhoto) {
36+
binding.property = marsPhoto
37+
binding.executePendingBindings()
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)