Skip to content

Commit 3dcd271

Browse files
authored
Swapped over to the ViewBinding version of adapterdelegates4 (commons-app#4687)
1 parent 8168f45 commit 3dcd271

File tree

6 files changed

+72
-58
lines changed

6 files changed

+72
-58
lines changed

app/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ dependencies {
4848
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
4949

5050
kapt "com.jakewharton:butterknife-compiler:$BUTTERKNIFE_VERSION"
51-
implementation "com.hannesdorfmann:adapterdelegates4-kotlin-dsl-layoutcontainer:$ADAPTER_DELEGATES_VERSION"
51+
implementation "com.hannesdorfmann:adapterdelegates4-kotlin-dsl-viewbinding:$ADAPTER_DELEGATES_VERSION"
5252
implementation "com.hannesdorfmann:adapterdelegates4-pagination:$ADAPTER_DELEGATES_VERSION"
5353
implementation "androidx.paging:paging-runtime-ktx:$PAGING_VERSION"
5454
testImplementation "androidx.paging:paging-common-ktx:$PAGING_VERSION"

app/src/main/java/fr/free/nrw/commons/nearby/PlaceAdapterDelegate.kt

+40-29
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ import android.view.View.*
55
import androidx.recyclerview.widget.LinearLayoutManager
66
import androidx.recyclerview.widget.RecyclerView
77
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
1010
import fr.free.nrw.commons.R
1111
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
1413

1514

1615
fun placeAdapterDelegate(
@@ -21,29 +20,33 @@ fun placeAdapterDelegate(
2120
onBookmarkClicked: (Place, Boolean) -> Unit,
2221
onOverflowIconClicked: (Place, View) -> Unit,
2322
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? ->
2728
showOrHideAndScrollToIfLast()
2829
onItemClick?.invoke(item)
2930
}
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) {
3435
showOrHideAndScrollToIfLast()
3536
onItemClick?.invoke(item)
3637
}
3738
}
38-
cameraButton.setOnClickListener { onCameraClicked(item) }
39-
galleryButton.setOnClickListener { onGalleryClicked(item) }
39+
nearbyButtonLayout.cameraButton.setOnClickListener { onCameraClicked(item) }
40+
nearbyButtonLayout.galleryButton.setOnClickListener { onGalleryClicked(item) }
4041
bookmarkButtonImage.setOnClickListener {
4142
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+
)
4346
onBookmarkClicked(item, isBookmarked)
4447
}
45-
iconOverflow.setOnClickListener { onOverflowIconClicked(item, it) }
46-
directionsButton.setOnClickListener { onDirectionsClicked(item) }
48+
nearbyButtonLayout.iconOverflow.setOnClickListener { onOverflowIconClicked(item, it) }
49+
nearbyButtonLayout.directionsButton.setOnClickListener { onDirectionsClicked(item) }
4750
bind {
4851
tvName.text = item.name
4952
val descriptionText: String = item.longDescription
@@ -52,11 +55,13 @@ fun placeAdapterDelegate(
5255
tvDesc.visibility = INVISIBLE
5356
} else {
5457
// 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(")");
5661
}
5762
distance.text = item.distance
5863
icon.setImageResource(item.label.icon)
59-
iconOverflow.visibility =
64+
nearbyButtonLayout.iconOverflow.visibility =
6065
if (item.hasCommonsLink() || item.hasWikidataLink()) VISIBLE
6166
else GONE
6267

@@ -68,18 +73,24 @@ fun placeAdapterDelegate(
6873
)
6974
}
7075
}
76+
}
7177

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+
}
8394
}
8495
}
8596
}

app/src/main/java/fr/free/nrw/commons/notification/NotificationAdapterDelegates.kt

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package fr.free.nrw.commons.notification
22

3-
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateLayoutContainer
4-
import fr.free.nrw.commons.R
5-
import kotlinx.android.synthetic.main.activity_login.title
6-
import kotlinx.android.synthetic.main.item_notification.*
3+
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding
4+
import fr.free.nrw.commons.databinding.ItemNotificationBinding
75
import org.wikipedia.util.StringUtil
86

97

108
fun notificationDelegate(onNotificationClicked: (Notification) -> Unit) =
11-
adapterDelegateLayoutContainer<Notification, Notification>(R.layout.item_notification) {
12-
containerView.setOnClickListener { onNotificationClicked(item) }
9+
adapterDelegateViewBinding<Notification, Notification, ItemNotificationBinding>({ layoutInflater, parent ->
10+
ItemNotificationBinding.inflate(layoutInflater, parent, false)
11+
}) {
12+
binding.root.setOnClickListener { onNotificationClicked(item) }
1313
bind {
14-
title.text = item.processedNotificationText
15-
time.text = item.date
14+
binding.title.text = item.processedNotificationText
15+
binding.time.text = item.date
1616
}
1717

1818
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
package fr.free.nrw.commons.upload.categories
22

3-
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateLayoutContainer
4-
import fr.free.nrw.commons.R
3+
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding
54
import fr.free.nrw.commons.category.CategoryItem
6-
import kotlinx.android.synthetic.main.layout_upload_categories_item.*
5+
import fr.free.nrw.commons.databinding.LayoutUploadCategoriesItemBinding
76

87
fun uploadCategoryDelegate(onCategoryClicked: (CategoryItem) -> Unit) =
9-
adapterDelegateLayoutContainer<CategoryItem, CategoryItem>(R.layout.layout_upload_categories_item) {
10-
containerView.setOnClickListener {
8+
adapterDelegateViewBinding<CategoryItem, CategoryItem, LayoutUploadCategoriesItemBinding>({ layoutInflater, root ->
9+
LayoutUploadCategoriesItemBinding.inflate(layoutInflater, root, false)
10+
}) {
11+
binding.root.setOnClickListener {
1112
item.isSelected = !item.isSelected
12-
uploadCategoryCheckbox.isChecked = item.isSelected
13+
binding.uploadCategoryCheckbox.isChecked = item.isSelected
1314
onCategoryClicked(item)
1415
}
1516
bind {
16-
uploadCategoryCheckbox.isChecked = item.isSelected
17-
uploadCategoryCheckbox.text = item.name
17+
binding.uploadCategoryCheckbox.isChecked = item.isSelected
18+
binding.uploadCategoryCheckbox.text = item.name
1819
}
1920
}

app/src/main/java/fr/free/nrw/commons/upload/depicts/UploadDepictsAdapterDelegates.kt

+13-11
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,32 @@ package fr.free.nrw.commons.upload.depicts
33
import android.net.Uri
44
import android.text.TextUtils
55
import android.view.View
6-
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateLayoutContainer
6+
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding
77
import fr.free.nrw.commons.R
8+
import fr.free.nrw.commons.databinding.LayoutUploadDepictsItemBinding
89
import fr.free.nrw.commons.upload.structure.depictions.DepictedItem
9-
import kotlinx.android.synthetic.main.layout_upload_depicts_item.*
1010

1111

1212
fun uploadDepictsDelegate(onDepictClicked: (DepictedItem) -> Unit) =
13-
adapterDelegateLayoutContainer<DepictedItem, DepictedItem>(R.layout.layout_upload_depicts_item) {
13+
adapterDelegateViewBinding<DepictedItem, DepictedItem, LayoutUploadDepictsItemBinding>({ layoutInflater, parent ->
14+
LayoutUploadDepictsItemBinding.inflate(layoutInflater, parent, false)
15+
}) {
1416
val onClickListener = { _: View? ->
1517
item.isSelected = !item.isSelected
16-
depict_checkbox.isChecked = item.isSelected
18+
binding.depictCheckbox.isChecked = item.isSelected
1719
onDepictClicked(item)
1820
}
19-
containerView.setOnClickListener(onClickListener)
20-
depict_checkbox.setOnClickListener(onClickListener)
21+
binding.root.setOnClickListener(onClickListener)
22+
binding.depictCheckbox.setOnClickListener(onClickListener)
2123
bind {
22-
depict_checkbox.isChecked = item.isSelected
23-
depicts_label.text = item.name
24-
description.text = item.description
24+
binding.depictCheckbox.isChecked = item.isSelected
25+
binding.depictsLabel.text = item.name
26+
binding.description.text = item.description
2527
val imageUrl = item.imageUrl
2628
if (TextUtils.isEmpty(imageUrl)) {
27-
depicted_image.setActualImageResource(R.drawable.ic_wikidata_logo_24dp)
29+
binding.depictedImage.setActualImageResource(R.drawable.ic_wikidata_logo_24dp)
2830
} else {
29-
depicted_image.setImageURI(Uri.parse(imageUrl))
31+
binding.depictedImage.setImageURI(Uri.parse(imageUrl))
3032
}
3133
}
3234
}

app/src/main/res/layout/item_place.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,5 @@
8282
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
8383
/>
8484

85-
<include layout="@layout/nearby_row_button" />
85+
<include android:id="@+id/nearby_button_layout" layout="@layout/nearby_row_button" />
8686
</RelativeLayout>

0 commit comments

Comments
 (0)