Skip to content

Commit 247e931

Browse files
committed
End of part 7
1 parent e9c14bd commit 247e931

3 files changed

Lines changed: 80 additions & 1 deletion

File tree

app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.codinginflow.imagesearchapp">
44

5+
<uses-permission android:name="android.permission.INTERNET" />
6+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
7+
58
<application
69
android:name=".ImageSearchApplication"
710
android:allowBackup="true"

app/src/main/java/com/codinginflow/imagesearchapp/ui/gallery/GalleryFragment.kt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,36 @@ import android.view.View
55
import androidx.fragment.app.Fragment
66
import androidx.fragment.app.viewModels
77
import com.codinginflow.imagesearchapp.R
8+
import com.codinginflow.imagesearchapp.databinding.FragmentGalleryBinding
89
import dagger.hilt.android.AndroidEntryPoint
910

1011
@AndroidEntryPoint
1112
class GalleryFragment : Fragment(R.layout.fragment_gallery) {
1213

1314
private val viewModel by viewModels<GalleryViewModel>()
1415

16+
private var _binding: FragmentGalleryBinding? = null
17+
private val binding get() = _binding!!
18+
1519
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
1620
super.onViewCreated(view, savedInstanceState)
1721

18-
viewModel.photos.observe(viewLifecycleOwner) {
22+
_binding = FragmentGalleryBinding.bind(view)
23+
24+
val adapter = UnsplashPhotoAdapter()
25+
26+
binding.apply {
27+
recyclerView.setHasFixedSize(true)
28+
recyclerView.adapter = adapter
29+
}
1930

31+
viewModel.photos.observe(viewLifecycleOwner) {
32+
adapter.submitData(viewLifecycleOwner.lifecycle, it)
2033
}
2134
}
35+
36+
override fun onDestroyView() {
37+
super.onDestroyView()
38+
_binding = null
39+
}
2240
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.codinginflow.imagesearchapp.ui.gallery
2+
3+
import android.view.LayoutInflater
4+
import android.view.ViewGroup
5+
import androidx.paging.PagingDataAdapter
6+
import androidx.recyclerview.widget.DiffUtil
7+
import androidx.recyclerview.widget.RecyclerView
8+
import com.bumptech.glide.Glide
9+
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
10+
import com.codinginflow.imagesearchapp.R
11+
import com.codinginflow.imagesearchapp.data.UnsplashPhoto
12+
import com.codinginflow.imagesearchapp.databinding.ItemUnsplashPhotoBinding
13+
14+
class UnsplashPhotoAdapter :
15+
PagingDataAdapter<UnsplashPhoto, UnsplashPhotoAdapter.PhotoViewHolder>(PHOTO_COMPARATOR) {
16+
17+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PhotoViewHolder {
18+
val binding =
19+
ItemUnsplashPhotoBinding.inflate(LayoutInflater.from(parent.context), parent, false)
20+
21+
return PhotoViewHolder(binding)
22+
}
23+
24+
override fun onBindViewHolder(holder: PhotoViewHolder, position: Int) {
25+
val currentItem = getItem(position)
26+
27+
if (currentItem != null) {
28+
holder.bind(currentItem)
29+
}
30+
}
31+
32+
class PhotoViewHolder(private val binding: ItemUnsplashPhotoBinding) :
33+
RecyclerView.ViewHolder(binding.root) {
34+
35+
fun bind(photo: UnsplashPhoto) {
36+
binding.apply {
37+
Glide.with(itemView)
38+
.load(photo.urls.regular)
39+
.centerCrop()
40+
.transition(DrawableTransitionOptions.withCrossFade())
41+
.error(R.drawable.ic_error)
42+
.into(imageView)
43+
44+
textViewUserName.text = photo.user.username
45+
}
46+
}
47+
}
48+
49+
companion object {
50+
private val PHOTO_COMPARATOR = object : DiffUtil.ItemCallback<UnsplashPhoto>() {
51+
override fun areItemsTheSame(oldItem: UnsplashPhoto, newItem: UnsplashPhoto) =
52+
oldItem.id == newItem.id
53+
54+
override fun areContentsTheSame(oldItem: UnsplashPhoto, newItem: UnsplashPhoto) =
55+
oldItem == newItem
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)