mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 04:43:54 +01:00
Swapped over to the ViewBinding version of adapterdelegates4 (#4687)
This commit is contained in:
parent
8168f454ed
commit
3dcd271980
6 changed files with 72 additions and 58 deletions
|
|
@ -48,7 +48,7 @@ dependencies {
|
||||||
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
|
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
|
||||||
|
|
||||||
kapt "com.jakewharton:butterknife-compiler:$BUTTERKNIFE_VERSION"
|
kapt "com.jakewharton:butterknife-compiler:$BUTTERKNIFE_VERSION"
|
||||||
implementation "com.hannesdorfmann:adapterdelegates4-kotlin-dsl-layoutcontainer:$ADAPTER_DELEGATES_VERSION"
|
implementation "com.hannesdorfmann:adapterdelegates4-kotlin-dsl-viewbinding:$ADAPTER_DELEGATES_VERSION"
|
||||||
implementation "com.hannesdorfmann:adapterdelegates4-pagination:$ADAPTER_DELEGATES_VERSION"
|
implementation "com.hannesdorfmann:adapterdelegates4-pagination:$ADAPTER_DELEGATES_VERSION"
|
||||||
implementation "androidx.paging:paging-runtime-ktx:$PAGING_VERSION"
|
implementation "androidx.paging:paging-runtime-ktx:$PAGING_VERSION"
|
||||||
testImplementation "androidx.paging:paging-common-ktx:$PAGING_VERSION"
|
testImplementation "androidx.paging:paging-common-ktx:$PAGING_VERSION"
|
||||||
|
|
|
||||||
|
|
@ -5,12 +5,11 @@ import android.view.View.*
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
import androidx.transition.TransitionManager
|
import androidx.transition.TransitionManager
|
||||||
import com.hannesdorfmann.adapterdelegates4.dsl.AdapterDelegateLayoutContainerViewHolder
|
import com.hannesdorfmann.adapterdelegates4.dsl.AdapterDelegateViewBindingViewHolder
|
||||||
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateLayoutContainer
|
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding
|
||||||
import fr.free.nrw.commons.R
|
import fr.free.nrw.commons.R
|
||||||
import fr.free.nrw.commons.bookmarks.locations.BookmarkLocationsDao
|
import fr.free.nrw.commons.bookmarks.locations.BookmarkLocationsDao
|
||||||
import kotlinx.android.synthetic.main.item_place.*
|
import fr.free.nrw.commons.databinding.ItemPlaceBinding
|
||||||
import kotlinx.android.synthetic.main.nearby_row_button.*
|
|
||||||
|
|
||||||
|
|
||||||
fun placeAdapterDelegate(
|
fun placeAdapterDelegate(
|
||||||
|
|
@ -21,29 +20,33 @@ fun placeAdapterDelegate(
|
||||||
onBookmarkClicked: (Place, Boolean) -> Unit,
|
onBookmarkClicked: (Place, Boolean) -> Unit,
|
||||||
onOverflowIconClicked: (Place, View) -> Unit,
|
onOverflowIconClicked: (Place, View) -> Unit,
|
||||||
onDirectionsClicked: (Place) -> Unit
|
onDirectionsClicked: (Place) -> Unit
|
||||||
) =
|
) = adapterDelegateViewBinding<Place, Place, ItemPlaceBinding>({ layoutInflater, parent ->
|
||||||
adapterDelegateLayoutContainer<Place, Place>(R.layout.item_place) {
|
ItemPlaceBinding.inflate(layoutInflater, parent, false)
|
||||||
containerView.setOnClickListener { _: View? ->
|
}) {
|
||||||
|
with(binding) {
|
||||||
|
root.setOnClickListener { _: View? ->
|
||||||
showOrHideAndScrollToIfLast()
|
showOrHideAndScrollToIfLast()
|
||||||
onItemClick?.invoke(item)
|
onItemClick?.invoke(item)
|
||||||
}
|
}
|
||||||
containerView.setOnFocusChangeListener { view1: View?, hasFocus: Boolean ->
|
root.setOnFocusChangeListener { view1: View?, hasFocus: Boolean ->
|
||||||
if (!hasFocus && buttonLayout.isShown) {
|
if (!hasFocus && nearbyButtonLayout.buttonLayout.isShown) {
|
||||||
buttonLayout.visibility = GONE
|
nearbyButtonLayout.buttonLayout.visibility = GONE
|
||||||
} else if (hasFocus && !buttonLayout.isShown) {
|
} else if (hasFocus && !nearbyButtonLayout.buttonLayout.isShown) {
|
||||||
showOrHideAndScrollToIfLast()
|
showOrHideAndScrollToIfLast()
|
||||||
onItemClick?.invoke(item)
|
onItemClick?.invoke(item)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cameraButton.setOnClickListener { onCameraClicked(item) }
|
nearbyButtonLayout.cameraButton.setOnClickListener { onCameraClicked(item) }
|
||||||
galleryButton.setOnClickListener { onGalleryClicked(item) }
|
nearbyButtonLayout.galleryButton.setOnClickListener { onGalleryClicked(item) }
|
||||||
bookmarkButtonImage.setOnClickListener {
|
bookmarkButtonImage.setOnClickListener {
|
||||||
val isBookmarked = bookmarkLocationDao.updateBookmarkLocation(item)
|
val isBookmarked = bookmarkLocationDao.updateBookmarkLocation(item)
|
||||||
bookmarkButtonImage.setImageResource(if (isBookmarked) R.drawable.ic_round_star_filled_24px else R.drawable.ic_round_star_border_24px)
|
bookmarkButtonImage.setImageResource(
|
||||||
|
if (isBookmarked) R.drawable.ic_round_star_filled_24px else R.drawable.ic_round_star_border_24px
|
||||||
|
)
|
||||||
onBookmarkClicked(item, isBookmarked)
|
onBookmarkClicked(item, isBookmarked)
|
||||||
}
|
}
|
||||||
iconOverflow.setOnClickListener { onOverflowIconClicked(item, it) }
|
nearbyButtonLayout.iconOverflow.setOnClickListener { onOverflowIconClicked(item, it) }
|
||||||
directionsButton.setOnClickListener { onDirectionsClicked(item) }
|
nearbyButtonLayout.directionsButton.setOnClickListener { onDirectionsClicked(item) }
|
||||||
bind {
|
bind {
|
||||||
tvName.text = item.name
|
tvName.text = item.name
|
||||||
val descriptionText: String = item.longDescription
|
val descriptionText: String = item.longDescription
|
||||||
|
|
@ -52,11 +55,13 @@ fun placeAdapterDelegate(
|
||||||
tvDesc.visibility = INVISIBLE
|
tvDesc.visibility = INVISIBLE
|
||||||
} else {
|
} else {
|
||||||
// Remove the label and display only texts inside pharentheses (description) since too long
|
// Remove the label and display only texts inside pharentheses (description) since too long
|
||||||
tvDesc.text = descriptionText.substringAfter(tvName.text.toString() + " (").substringBeforeLast(")");
|
tvDesc.text =
|
||||||
|
descriptionText.substringAfter(tvName.text.toString() + " (")
|
||||||
|
.substringBeforeLast(")");
|
||||||
}
|
}
|
||||||
distance.text = item.distance
|
distance.text = item.distance
|
||||||
icon.setImageResource(item.label.icon)
|
icon.setImageResource(item.label.icon)
|
||||||
iconOverflow.visibility =
|
nearbyButtonLayout.iconOverflow.visibility =
|
||||||
if (item.hasCommonsLink() || item.hasWikidataLink()) VISIBLE
|
if (item.hasCommonsLink() || item.hasWikidataLink()) VISIBLE
|
||||||
else GONE
|
else GONE
|
||||||
|
|
||||||
|
|
@ -68,18 +73,24 @@ fun placeAdapterDelegate(
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun AdapterDelegateLayoutContainerViewHolder<Place>.showOrHideAndScrollToIfLast() {
|
private fun AdapterDelegateViewBindingViewHolder<Place, ItemPlaceBinding>.showOrHideAndScrollToIfLast() {
|
||||||
TransitionManager.beginDelayedTransition(buttonLayout)
|
with(binding) {
|
||||||
if (buttonLayout.isShown) {
|
TransitionManager.beginDelayedTransition(nearbyButtonLayout.buttonLayout)
|
||||||
buttonLayout.visibility = GONE
|
if (nearbyButtonLayout.buttonLayout.isShown) {
|
||||||
} else {
|
nearbyButtonLayout.buttonLayout.visibility = GONE
|
||||||
buttonLayout.visibility = VISIBLE
|
} else {
|
||||||
val recyclerView = containerView.parent as RecyclerView
|
nearbyButtonLayout.buttonLayout.visibility = VISIBLE
|
||||||
val lastPosition = recyclerView.adapter!!.itemCount - 1
|
val recyclerView = root.parent as RecyclerView
|
||||||
if (recyclerView.getChildLayoutPosition(containerView) == lastPosition) {
|
val lastPosition = recyclerView.adapter!!.itemCount - 1
|
||||||
(recyclerView.layoutManager as LinearLayoutManager?)
|
if (recyclerView.getChildLayoutPosition(root) == lastPosition) {
|
||||||
?.scrollToPositionWithOffset(lastPosition, buttonLayout.height)
|
(recyclerView.layoutManager as LinearLayoutManager?)
|
||||||
|
?.scrollToPositionWithOffset(
|
||||||
|
lastPosition,
|
||||||
|
nearbyButtonLayout.buttonLayout.height
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,18 @@
|
||||||
package fr.free.nrw.commons.notification
|
package fr.free.nrw.commons.notification
|
||||||
|
|
||||||
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateLayoutContainer
|
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding
|
||||||
import fr.free.nrw.commons.R
|
import fr.free.nrw.commons.databinding.ItemNotificationBinding
|
||||||
import kotlinx.android.synthetic.main.activity_login.title
|
|
||||||
import kotlinx.android.synthetic.main.item_notification.*
|
|
||||||
import org.wikipedia.util.StringUtil
|
import org.wikipedia.util.StringUtil
|
||||||
|
|
||||||
|
|
||||||
fun notificationDelegate(onNotificationClicked: (Notification) -> Unit) =
|
fun notificationDelegate(onNotificationClicked: (Notification) -> Unit) =
|
||||||
adapterDelegateLayoutContainer<Notification, Notification>(R.layout.item_notification) {
|
adapterDelegateViewBinding<Notification, Notification, ItemNotificationBinding>({ layoutInflater, parent ->
|
||||||
containerView.setOnClickListener { onNotificationClicked(item) }
|
ItemNotificationBinding.inflate(layoutInflater, parent, false)
|
||||||
|
}) {
|
||||||
|
binding.root.setOnClickListener { onNotificationClicked(item) }
|
||||||
bind {
|
bind {
|
||||||
title.text = item.processedNotificationText
|
binding.title.text = item.processedNotificationText
|
||||||
time.text = item.date
|
binding.time.text = item.date
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,20 @@
|
||||||
package fr.free.nrw.commons.upload.categories
|
package fr.free.nrw.commons.upload.categories
|
||||||
|
|
||||||
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateLayoutContainer
|
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding
|
||||||
import fr.free.nrw.commons.R
|
|
||||||
import fr.free.nrw.commons.category.CategoryItem
|
import fr.free.nrw.commons.category.CategoryItem
|
||||||
import kotlinx.android.synthetic.main.layout_upload_categories_item.*
|
import fr.free.nrw.commons.databinding.LayoutUploadCategoriesItemBinding
|
||||||
|
|
||||||
fun uploadCategoryDelegate(onCategoryClicked: (CategoryItem) -> Unit) =
|
fun uploadCategoryDelegate(onCategoryClicked: (CategoryItem) -> Unit) =
|
||||||
adapterDelegateLayoutContainer<CategoryItem, CategoryItem>(R.layout.layout_upload_categories_item) {
|
adapterDelegateViewBinding<CategoryItem, CategoryItem, LayoutUploadCategoriesItemBinding>({ layoutInflater, root ->
|
||||||
containerView.setOnClickListener {
|
LayoutUploadCategoriesItemBinding.inflate(layoutInflater, root, false)
|
||||||
|
}) {
|
||||||
|
binding.root.setOnClickListener {
|
||||||
item.isSelected = !item.isSelected
|
item.isSelected = !item.isSelected
|
||||||
uploadCategoryCheckbox.isChecked = item.isSelected
|
binding.uploadCategoryCheckbox.isChecked = item.isSelected
|
||||||
onCategoryClicked(item)
|
onCategoryClicked(item)
|
||||||
}
|
}
|
||||||
bind {
|
bind {
|
||||||
uploadCategoryCheckbox.isChecked = item.isSelected
|
binding.uploadCategoryCheckbox.isChecked = item.isSelected
|
||||||
uploadCategoryCheckbox.text = item.name
|
binding.uploadCategoryCheckbox.text = item.name
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,30 +3,32 @@ package fr.free.nrw.commons.upload.depicts
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.text.TextUtils
|
import android.text.TextUtils
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateLayoutContainer
|
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding
|
||||||
import fr.free.nrw.commons.R
|
import fr.free.nrw.commons.R
|
||||||
|
import fr.free.nrw.commons.databinding.LayoutUploadDepictsItemBinding
|
||||||
import fr.free.nrw.commons.upload.structure.depictions.DepictedItem
|
import fr.free.nrw.commons.upload.structure.depictions.DepictedItem
|
||||||
import kotlinx.android.synthetic.main.layout_upload_depicts_item.*
|
|
||||||
|
|
||||||
|
|
||||||
fun uploadDepictsDelegate(onDepictClicked: (DepictedItem) -> Unit) =
|
fun uploadDepictsDelegate(onDepictClicked: (DepictedItem) -> Unit) =
|
||||||
adapterDelegateLayoutContainer<DepictedItem, DepictedItem>(R.layout.layout_upload_depicts_item) {
|
adapterDelegateViewBinding<DepictedItem, DepictedItem, LayoutUploadDepictsItemBinding>({ layoutInflater, parent ->
|
||||||
|
LayoutUploadDepictsItemBinding.inflate(layoutInflater, parent, false)
|
||||||
|
}) {
|
||||||
val onClickListener = { _: View? ->
|
val onClickListener = { _: View? ->
|
||||||
item.isSelected = !item.isSelected
|
item.isSelected = !item.isSelected
|
||||||
depict_checkbox.isChecked = item.isSelected
|
binding.depictCheckbox.isChecked = item.isSelected
|
||||||
onDepictClicked(item)
|
onDepictClicked(item)
|
||||||
}
|
}
|
||||||
containerView.setOnClickListener(onClickListener)
|
binding.root.setOnClickListener(onClickListener)
|
||||||
depict_checkbox.setOnClickListener(onClickListener)
|
binding.depictCheckbox.setOnClickListener(onClickListener)
|
||||||
bind {
|
bind {
|
||||||
depict_checkbox.isChecked = item.isSelected
|
binding.depictCheckbox.isChecked = item.isSelected
|
||||||
depicts_label.text = item.name
|
binding.depictsLabel.text = item.name
|
||||||
description.text = item.description
|
binding.description.text = item.description
|
||||||
val imageUrl = item.imageUrl
|
val imageUrl = item.imageUrl
|
||||||
if (TextUtils.isEmpty(imageUrl)) {
|
if (TextUtils.isEmpty(imageUrl)) {
|
||||||
depicted_image.setActualImageResource(R.drawable.ic_wikidata_logo_24dp)
|
binding.depictedImage.setActualImageResource(R.drawable.ic_wikidata_logo_24dp)
|
||||||
} else {
|
} else {
|
||||||
depicted_image.setImageURI(Uri.parse(imageUrl))
|
binding.depictedImage.setImageURI(Uri.parse(imageUrl))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -82,5 +82,5 @@
|
||||||
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
|
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<include layout="@layout/nearby_row_button" />
|
<include android:id="@+id/nearby_button_layout" layout="@layout/nearby_row_button" />
|
||||||
</RelativeLayout>
|
</RelativeLayout>
|
||||||
Loading…
Add table
Add a link
Reference in a new issue