@@ -5,12 +5,11 @@ import android.view.View.*
5
5
import androidx.recyclerview.widget.LinearLayoutManager
6
6
import androidx.recyclerview.widget.RecyclerView
7
7
import androidx.transition.TransitionManager
8
- import com.hannesdorfmann.adapterdelegates4.dsl.AdapterDelegateLayoutContainerViewHolder
9
- import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateLayoutContainer
8
+ import com.hannesdorfmann.adapterdelegates4.dsl.AdapterDelegateViewBindingViewHolder
9
+ import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding
10
10
import fr.free.nrw.commons.R
11
11
import fr.free.nrw.commons.bookmarks.locations.BookmarkLocationsDao
12
- import kotlinx.android.synthetic.main.item_place.*
13
- import kotlinx.android.synthetic.main.nearby_row_button.*
12
+ import fr.free.nrw.commons.databinding.ItemPlaceBinding
14
13
15
14
16
15
fun placeAdapterDelegate (
@@ -21,29 +20,33 @@ fun placeAdapterDelegate(
21
20
onBookmarkClicked : (Place , Boolean ) -> Unit ,
22
21
onOverflowIconClicked : (Place , View ) -> Unit ,
23
22
onDirectionsClicked : (Place ) -> Unit
24
- ) =
25
- adapterDelegateLayoutContainer<Place , Place >(R .layout.item_place) {
26
- containerView.setOnClickListener { _: View ? ->
23
+ ) = adapterDelegateViewBinding<Place , Place , ItemPlaceBinding >({ layoutInflater, parent ->
24
+ ItemPlaceBinding .inflate(layoutInflater, parent, false )
25
+ }) {
26
+ with (binding) {
27
+ root.setOnClickListener { _: View ? ->
27
28
showOrHideAndScrollToIfLast()
28
29
onItemClick?.invoke(item)
29
30
}
30
- containerView .setOnFocusChangeListener { view1: View ? , hasFocus: Boolean ->
31
- if (! hasFocus && buttonLayout.isShown) {
32
- buttonLayout.visibility = GONE
33
- } else if (hasFocus && ! buttonLayout.isShown) {
31
+ root .setOnFocusChangeListener { view1: View ? , hasFocus: Boolean ->
32
+ if (! hasFocus && nearbyButtonLayout. buttonLayout.isShown) {
33
+ nearbyButtonLayout. buttonLayout.visibility = GONE
34
+ } else if (hasFocus && ! nearbyButtonLayout. buttonLayout.isShown) {
34
35
showOrHideAndScrollToIfLast()
35
36
onItemClick?.invoke(item)
36
37
}
37
38
}
38
- cameraButton.setOnClickListener { onCameraClicked(item) }
39
- galleryButton.setOnClickListener { onGalleryClicked(item) }
39
+ nearbyButtonLayout. cameraButton.setOnClickListener { onCameraClicked(item) }
40
+ nearbyButtonLayout. galleryButton.setOnClickListener { onGalleryClicked(item) }
40
41
bookmarkButtonImage.setOnClickListener {
41
42
val isBookmarked = bookmarkLocationDao.updateBookmarkLocation(item)
42
- bookmarkButtonImage.setImageResource(if (isBookmarked) R .drawable.ic_round_star_filled_24px else R .drawable.ic_round_star_border_24px)
43
+ bookmarkButtonImage.setImageResource(
44
+ if (isBookmarked) R .drawable.ic_round_star_filled_24px else R .drawable.ic_round_star_border_24px
45
+ )
43
46
onBookmarkClicked(item, isBookmarked)
44
47
}
45
- iconOverflow.setOnClickListener { onOverflowIconClicked(item, it) }
46
- directionsButton.setOnClickListener { onDirectionsClicked(item) }
48
+ nearbyButtonLayout. iconOverflow.setOnClickListener { onOverflowIconClicked(item, it) }
49
+ nearbyButtonLayout. directionsButton.setOnClickListener { onDirectionsClicked(item) }
47
50
bind {
48
51
tvName.text = item.name
49
52
val descriptionText: String = item.longDescription
@@ -52,11 +55,13 @@ fun placeAdapterDelegate(
52
55
tvDesc.visibility = INVISIBLE
53
56
} else {
54
57
// Remove the label and display only texts inside pharentheses (description) since too long
55
- tvDesc.text = descriptionText.substringAfter(tvName.text.toString() + " (" ).substringBeforeLast(" )" );
58
+ tvDesc.text =
59
+ descriptionText.substringAfter(tvName.text.toString() + " (" )
60
+ .substringBeforeLast(" )" );
56
61
}
57
62
distance.text = item.distance
58
63
icon.setImageResource(item.label.icon)
59
- iconOverflow.visibility =
64
+ nearbyButtonLayout. iconOverflow.visibility =
60
65
if (item.hasCommonsLink() || item.hasWikidataLink()) VISIBLE
61
66
else GONE
62
67
@@ -68,18 +73,24 @@ fun placeAdapterDelegate(
68
73
)
69
74
}
70
75
}
76
+ }
71
77
72
- private fun AdapterDelegateLayoutContainerViewHolder<Place>.showOrHideAndScrollToIfLast () {
73
- TransitionManager .beginDelayedTransition(buttonLayout)
74
- if (buttonLayout.isShown) {
75
- buttonLayout.visibility = GONE
76
- } else {
77
- buttonLayout.visibility = VISIBLE
78
- val recyclerView = containerView.parent as RecyclerView
79
- val lastPosition = recyclerView.adapter!! .itemCount - 1
80
- if (recyclerView.getChildLayoutPosition(containerView) == lastPosition) {
81
- (recyclerView.layoutManager as LinearLayoutManager ? )
82
- ?.scrollToPositionWithOffset(lastPosition, buttonLayout.height)
78
+ private fun AdapterDelegateViewBindingViewHolder <Place , ItemPlaceBinding >.showOrHideAndScrollToIfLast () {
79
+ with (binding) {
80
+ TransitionManager .beginDelayedTransition(nearbyButtonLayout.buttonLayout)
81
+ if (nearbyButtonLayout.buttonLayout.isShown) {
82
+ nearbyButtonLayout.buttonLayout.visibility = GONE
83
+ } else {
84
+ nearbyButtonLayout.buttonLayout.visibility = VISIBLE
85
+ val recyclerView = root.parent as RecyclerView
86
+ val lastPosition = recyclerView.adapter!! .itemCount - 1
87
+ if (recyclerView.getChildLayoutPosition(root) == lastPosition) {
88
+ (recyclerView.layoutManager as LinearLayoutManager ? )
89
+ ?.scrollToPositionWithOffset(
90
+ lastPosition,
91
+ nearbyButtonLayout.buttonLayout.height
92
+ )
93
+ }
83
94
}
84
95
}
85
96
}
0 commit comments