/
UnsplashPhotosItemFactory.kt
89 lines (71 loc) · 3.77 KB
/
UnsplashPhotosItemFactory.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package me.panpf.sketch.sample.adapter.itemfactory
import android.app.Activity
import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import me.panpf.sketch.sample.ImageOptions
import me.panpf.sketch.sample.R
import me.panpf.sketch.sample.bean.UnsplashImage
import me.panpf.sketch.sample.bindView
import me.panpf.sketch.sample.kotlinextends.isPortraitOrientation
import me.panpf.sketch.sample.util.DeviceUtils
import me.panpf.sketch.sample.widget.SampleImageView
import me.xiaopan.assemblyadapter.AssemblyRecyclerItem
import me.xiaopan.assemblyadapter.AssemblyRecyclerItemFactory
class UnsplashPhotosItemFactory(private val activity: Activity,
private val unsplashPhotosItemEventListener: UnsplashPhotosItemEventListener?)
: AssemblyRecyclerItemFactory<UnsplashPhotosItemFactory.UnsplashPhotosItem>() {
override fun isTarget(o: Any): Boolean {
return o is UnsplashImage
}
override fun createAssemblyItem(viewGroup: ViewGroup): UnsplashPhotosItem {
return UnsplashPhotosItem(R.layout.list_item_image_unsplash, viewGroup)
}
interface UnsplashPhotosItemEventListener {
fun onClickImage(position: Int, image: UnsplashImage, optionsKey: String)
fun onClickUser(position: Int, user: UnsplashImage.User)
}
inner class UnsplashPhotosItem(itemLayoutId: Int, parent: ViewGroup) : AssemblyRecyclerItem<UnsplashImage>(itemLayoutId, parent) {
val imageView: SampleImageView by bindView(R.id.image_unsplashImageItem)
val userProfileImageView: SampleImageView by bindView(R.id.image_unsplashImageItem_userProfile)
val userNameTextView: TextView by bindView(R.id.text_unsplashImageItem_userName)
val dateTextView: TextView by bindView(R.id.text_unsplashImageItem_date)
val rootViewGroup: ViewGroup by bindView(R.id.layout_unsplashImageItem_root)
override fun onConfigViews(context: Context) {
imageView.onClickListener = View.OnClickListener {
unsplashPhotosItemEventListener?.onClickImage(adapterPosition, data, imageView.optionsKey)
}
imageView.setOptions(ImageOptions.LIST_FULL)
imageView.page = SampleImageView.Page.UNSPLASH_LIST
userProfileImageView.setOptions(ImageOptions.CIRCULAR_STROKE)
userProfileImageView.onClickListener = View.OnClickListener {
unsplashPhotosItemEventListener?.onClickUser(adapterPosition, data.user!!)
}
userNameTextView.setOnClickListener { userProfileImageView.performClick() }
}
override fun onSetData(i: Int, image: UnsplashImage) {
val itemWidth = imageView.context.resources.displayMetrics.widthPixels
imageView.layoutParams?.let {
it.width = itemWidth
if (!imageView.context.isPortraitOrientation()) {
it.width += DeviceUtils.getWindowHeightSupplement(activity)
}
it.height = (it.width / (image.width / image.height.toFloat())).toInt()
imageView.layoutParams = it
}
rootViewGroup.layoutParams?.let {
it.width = itemWidth
if (!imageView.context.isPortraitOrientation()) {
it.width += DeviceUtils.getWindowHeightSupplement(activity)
}
it.height = (it.width / (image.width / image.height.toFloat())).toInt()
rootViewGroup.layoutParams = it
}
imageView.displayImage(image.urls!!.regular!!)
userProfileImageView.displayImage(image.user!!.profileImage!!.large!!)
userNameTextView.text = image.user!!.name
dateTextView.text = image.getFormattedUpdateDate()
}
}
}