mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 12:53:55 +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
|
|
@ -5,12 +5,11 @@ import android.view.View.*
|
|||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import androidx.transition.TransitionManager
|
||||
import com.hannesdorfmann.adapterdelegates4.dsl.AdapterDelegateLayoutContainerViewHolder
|
||||
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateLayoutContainer
|
||||
import com.hannesdorfmann.adapterdelegates4.dsl.AdapterDelegateViewBindingViewHolder
|
||||
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding
|
||||
import fr.free.nrw.commons.R
|
||||
import fr.free.nrw.commons.bookmarks.locations.BookmarkLocationsDao
|
||||
import kotlinx.android.synthetic.main.item_place.*
|
||||
import kotlinx.android.synthetic.main.nearby_row_button.*
|
||||
import fr.free.nrw.commons.databinding.ItemPlaceBinding
|
||||
|
||||
|
||||
fun placeAdapterDelegate(
|
||||
|
|
@ -21,29 +20,33 @@ fun placeAdapterDelegate(
|
|||
onBookmarkClicked: (Place, Boolean) -> Unit,
|
||||
onOverflowIconClicked: (Place, View) -> Unit,
|
||||
onDirectionsClicked: (Place) -> Unit
|
||||
) =
|
||||
adapterDelegateLayoutContainer<Place, Place>(R.layout.item_place) {
|
||||
containerView.setOnClickListener { _: View? ->
|
||||
) = adapterDelegateViewBinding<Place, Place, ItemPlaceBinding>({ layoutInflater, parent ->
|
||||
ItemPlaceBinding.inflate(layoutInflater, parent, false)
|
||||
}) {
|
||||
with(binding) {
|
||||
root.setOnClickListener { _: View? ->
|
||||
showOrHideAndScrollToIfLast()
|
||||
onItemClick?.invoke(item)
|
||||
}
|
||||
containerView.setOnFocusChangeListener { view1: View?, hasFocus: Boolean ->
|
||||
if (!hasFocus && buttonLayout.isShown) {
|
||||
buttonLayout.visibility = GONE
|
||||
} else if (hasFocus && !buttonLayout.isShown) {
|
||||
root.setOnFocusChangeListener { view1: View?, hasFocus: Boolean ->
|
||||
if (!hasFocus && nearbyButtonLayout.buttonLayout.isShown) {
|
||||
nearbyButtonLayout.buttonLayout.visibility = GONE
|
||||
} else if (hasFocus && !nearbyButtonLayout.buttonLayout.isShown) {
|
||||
showOrHideAndScrollToIfLast()
|
||||
onItemClick?.invoke(item)
|
||||
}
|
||||
}
|
||||
cameraButton.setOnClickListener { onCameraClicked(item) }
|
||||
galleryButton.setOnClickListener { onGalleryClicked(item) }
|
||||
nearbyButtonLayout.cameraButton.setOnClickListener { onCameraClicked(item) }
|
||||
nearbyButtonLayout.galleryButton.setOnClickListener { onGalleryClicked(item) }
|
||||
bookmarkButtonImage.setOnClickListener {
|
||||
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)
|
||||
}
|
||||
iconOverflow.setOnClickListener { onOverflowIconClicked(item, it) }
|
||||
directionsButton.setOnClickListener { onDirectionsClicked(item) }
|
||||
nearbyButtonLayout.iconOverflow.setOnClickListener { onOverflowIconClicked(item, it) }
|
||||
nearbyButtonLayout.directionsButton.setOnClickListener { onDirectionsClicked(item) }
|
||||
bind {
|
||||
tvName.text = item.name
|
||||
val descriptionText: String = item.longDescription
|
||||
|
|
@ -52,11 +55,13 @@ fun placeAdapterDelegate(
|
|||
tvDesc.visibility = INVISIBLE
|
||||
} else {
|
||||
// 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
|
||||
icon.setImageResource(item.label.icon)
|
||||
iconOverflow.visibility =
|
||||
nearbyButtonLayout.iconOverflow.visibility =
|
||||
if (item.hasCommonsLink() || item.hasWikidataLink()) VISIBLE
|
||||
else GONE
|
||||
|
||||
|
|
@ -68,18 +73,24 @@ fun placeAdapterDelegate(
|
|||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun AdapterDelegateLayoutContainerViewHolder<Place>.showOrHideAndScrollToIfLast() {
|
||||
TransitionManager.beginDelayedTransition(buttonLayout)
|
||||
if (buttonLayout.isShown) {
|
||||
buttonLayout.visibility = GONE
|
||||
} else {
|
||||
buttonLayout.visibility = VISIBLE
|
||||
val recyclerView = containerView.parent as RecyclerView
|
||||
val lastPosition = recyclerView.adapter!!.itemCount - 1
|
||||
if (recyclerView.getChildLayoutPosition(containerView) == lastPosition) {
|
||||
(recyclerView.layoutManager as LinearLayoutManager?)
|
||||
?.scrollToPositionWithOffset(lastPosition, buttonLayout.height)
|
||||
private fun AdapterDelegateViewBindingViewHolder<Place, ItemPlaceBinding>.showOrHideAndScrollToIfLast() {
|
||||
with(binding) {
|
||||
TransitionManager.beginDelayedTransition(nearbyButtonLayout.buttonLayout)
|
||||
if (nearbyButtonLayout.buttonLayout.isShown) {
|
||||
nearbyButtonLayout.buttonLayout.visibility = GONE
|
||||
} else {
|
||||
nearbyButtonLayout.buttonLayout.visibility = VISIBLE
|
||||
val recyclerView = root.parent as RecyclerView
|
||||
val lastPosition = recyclerView.adapter!!.itemCount - 1
|
||||
if (recyclerView.getChildLayoutPosition(root) == lastPosition) {
|
||||
(recyclerView.layoutManager as LinearLayoutManager?)
|
||||
?.scrollToPositionWithOffset(
|
||||
lastPosition,
|
||||
nearbyButtonLayout.buttonLayout.height
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,18 +1,18 @@
|
|||
package fr.free.nrw.commons.notification
|
||||
|
||||
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateLayoutContainer
|
||||
import fr.free.nrw.commons.R
|
||||
import kotlinx.android.synthetic.main.activity_login.title
|
||||
import kotlinx.android.synthetic.main.item_notification.*
|
||||
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding
|
||||
import fr.free.nrw.commons.databinding.ItemNotificationBinding
|
||||
import org.wikipedia.util.StringUtil
|
||||
|
||||
|
||||
fun notificationDelegate(onNotificationClicked: (Notification) -> Unit) =
|
||||
adapterDelegateLayoutContainer<Notification, Notification>(R.layout.item_notification) {
|
||||
containerView.setOnClickListener { onNotificationClicked(item) }
|
||||
adapterDelegateViewBinding<Notification, Notification, ItemNotificationBinding>({ layoutInflater, parent ->
|
||||
ItemNotificationBinding.inflate(layoutInflater, parent, false)
|
||||
}) {
|
||||
binding.root.setOnClickListener { onNotificationClicked(item) }
|
||||
bind {
|
||||
title.text = item.processedNotificationText
|
||||
time.text = item.date
|
||||
binding.title.text = item.processedNotificationText
|
||||
binding.time.text = item.date
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,19 +1,20 @@
|
|||
package fr.free.nrw.commons.upload.categories
|
||||
|
||||
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateLayoutContainer
|
||||
import fr.free.nrw.commons.R
|
||||
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding
|
||||
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) =
|
||||
adapterDelegateLayoutContainer<CategoryItem, CategoryItem>(R.layout.layout_upload_categories_item) {
|
||||
containerView.setOnClickListener {
|
||||
adapterDelegateViewBinding<CategoryItem, CategoryItem, LayoutUploadCategoriesItemBinding>({ layoutInflater, root ->
|
||||
LayoutUploadCategoriesItemBinding.inflate(layoutInflater, root, false)
|
||||
}) {
|
||||
binding.root.setOnClickListener {
|
||||
item.isSelected = !item.isSelected
|
||||
uploadCategoryCheckbox.isChecked = item.isSelected
|
||||
binding.uploadCategoryCheckbox.isChecked = item.isSelected
|
||||
onCategoryClicked(item)
|
||||
}
|
||||
bind {
|
||||
uploadCategoryCheckbox.isChecked = item.isSelected
|
||||
uploadCategoryCheckbox.text = item.name
|
||||
binding.uploadCategoryCheckbox.isChecked = item.isSelected
|
||||
binding.uploadCategoryCheckbox.text = item.name
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,30 +3,32 @@ package fr.free.nrw.commons.upload.depicts
|
|||
import android.net.Uri
|
||||
import android.text.TextUtils
|
||||
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.databinding.LayoutUploadDepictsItemBinding
|
||||
import fr.free.nrw.commons.upload.structure.depictions.DepictedItem
|
||||
import kotlinx.android.synthetic.main.layout_upload_depicts_item.*
|
||||
|
||||
|
||||
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? ->
|
||||
item.isSelected = !item.isSelected
|
||||
depict_checkbox.isChecked = item.isSelected
|
||||
binding.depictCheckbox.isChecked = item.isSelected
|
||||
onDepictClicked(item)
|
||||
}
|
||||
containerView.setOnClickListener(onClickListener)
|
||||
depict_checkbox.setOnClickListener(onClickListener)
|
||||
binding.root.setOnClickListener(onClickListener)
|
||||
binding.depictCheckbox.setOnClickListener(onClickListener)
|
||||
bind {
|
||||
depict_checkbox.isChecked = item.isSelected
|
||||
depicts_label.text = item.name
|
||||
description.text = item.description
|
||||
binding.depictCheckbox.isChecked = item.isSelected
|
||||
binding.depictsLabel.text = item.name
|
||||
binding.description.text = item.description
|
||||
val imageUrl = item.imageUrl
|
||||
if (TextUtils.isEmpty(imageUrl)) {
|
||||
depicted_image.setActualImageResource(R.drawable.ic_wikidata_logo_24dp)
|
||||
binding.depictedImage.setActualImageResource(R.drawable.ic_wikidata_logo_24dp)
|
||||
} else {
|
||||
depicted_image.setImageURI(Uri.parse(imageUrl))
|
||||
binding.depictedImage.setImageURI(Uri.parse(imageUrl))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -82,5 +82,5 @@
|
|||
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>
|
||||
Loading…
Add table
Add a link
Reference in a new issue