@@ -2,6 +2,7 @@ package fr.free.nrw.commons.customselector.ui.adapter
22
33import android.content.Context
44import android.content.SharedPreferences
5+ import android.view.LayoutInflater
56import android.view.View
67import android.view.ViewGroup
78import android.widget.ImageView
@@ -13,6 +14,7 @@ import com.bumptech.glide.Glide
1314import com.simplecityapps.recyclerview_fastscroll.views.FastScrollRecyclerView
1415import fr.free.nrw.commons.R
1516import fr.free.nrw.commons.contributions.Contribution
17+ import fr.free.nrw.commons.customselector.helper.CustomSelectorConstants.MAX_IMAGE_COUNT
1618import fr.free.nrw.commons.customselector.helper.ImageHelper
1719import fr.free.nrw.commons.customselector.helper.ImageHelper.CUSTOM_SELECTOR_PREFERENCE_KEY
1820import fr.free.nrw.commons.customselector.helper.ImageHelper.SHOW_ALREADY_ACTIONED_IMAGES_PREFERENCE_KEY
@@ -37,7 +39,7 @@ class ImageAdapter(
3739 /* *
3840 * Application Context.
3941 */
40- context : Context ,
42+ private val context : Context ,
4143 /* *
4244 * Image select listener for click events on image.
4345 */
@@ -46,8 +48,11 @@ class ImageAdapter(
4648 * ImageLoader queries images.
4749 */
4850 private var imageLoader : ImageLoader ,
49- ) : RecyclerViewAdapter <ImageAdapter.ImageViewHolder>(context ),
51+ ) : RecyclerView.Adapter <ImageAdapter.ImageViewHolder>(),
5052 FastScrollRecyclerView .SectionedAdapter {
53+
54+ private val inflater: LayoutInflater = LayoutInflater .from(context)
55+
5156 /* *
5257 * ImageSelectedOrUpdated payload class.
5358 */
@@ -124,6 +129,15 @@ class ImageAdapter(
124129 private var ioDispatcher: CoroutineDispatcher = Dispatchers .IO
125130 private val scope: CoroutineScope = MainScope ()
126131
132+ // maximum number of images that can be selected.
133+ private var maxUploadLimit: Int = MAX_IMAGE_COUNT
134+
135+
136+ // set maximum number of images allowed for upload.
137+ fun setMaxUploadLimit (limit : Int ) {
138+ maxUploadLimit = limit
139+ }
140+
127141 /* *
128142 * Create View holder.
129143 */
@@ -350,6 +364,20 @@ class ImageAdapter(
350364 // Notify listener of deselection to update UI
351365 imageSelectListener.onSelectedImagesChanged(selectedImages, numberOfSelectedImagesMarkedAsNotForUpload)
352366 } else {
367+ // check the maximum limit before allowing the selection
368+ if (! singleSelection && selectedImages.size >= maxUploadLimit) {
369+ // limit reached, show a toast and prevent selection
370+ Toast .makeText(
371+ context,
372+ context.getString(
373+ R .string.custom_selector_max_image_limit_reached,
374+ maxUploadLimit
375+ ),
376+ Toast .LENGTH_SHORT
377+ ).show()
378+ return // exit the function, preventing selection
379+ }
380+
353381 // Prevent adding the same image multiple times
354382 val image = if (showAlreadyActionedImages) images[position] else ArrayList (actionableImagesMap.values)[position]
355383 if (selectedImages.contains(image)) {
0 commit comments