Skip to content

Commit 21b0e0c

Browse files
Enhance customselector upload limit (#6557)
* Fix: enfore a maximum selection limit of 20 images * Fix: enfore a maximum selection limit of 20 images
1 parent c8faac7 commit 21b0e0c

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

app/src/main/java/fr/free/nrw/commons/customselector/helper/CustomSelectorConstants.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ object CustomSelectorConstants {
1010
const val NEW_SELECTED_IMAGES = "new_selected_images"
1111
const val SHOULD_REFRESH = "should_refresh"
1212
const val FULL_SCREEN_MODE_FIRST_LUNCH = "full_screen_mode_first_launch"
13+
const val MAX_IMAGE_COUNT = 20
1314
}

app/src/main/java/fr/free/nrw/commons/customselector/ui/adapter/ImageAdapter.kt

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package fr.free.nrw.commons.customselector.ui.adapter
22

33
import android.content.Context
44
import android.content.SharedPreferences
5+
import android.view.LayoutInflater
56
import android.view.View
67
import android.view.ViewGroup
78
import android.widget.ImageView
@@ -13,6 +14,7 @@ import com.bumptech.glide.Glide
1314
import com.simplecityapps.recyclerview_fastscroll.views.FastScrollRecyclerView
1415
import fr.free.nrw.commons.R
1516
import fr.free.nrw.commons.contributions.Contribution
17+
import fr.free.nrw.commons.customselector.helper.CustomSelectorConstants.MAX_IMAGE_COUNT
1618
import fr.free.nrw.commons.customselector.helper.ImageHelper
1719
import fr.free.nrw.commons.customselector.helper.ImageHelper.CUSTOM_SELECTOR_PREFERENCE_KEY
1820
import 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)) {

app/src/main/java/fr/free/nrw/commons/customselector/ui/selector/CustomSelectorActivity.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import fr.free.nrw.commons.R
4646
import fr.free.nrw.commons.customselector.database.NotForUploadStatus
4747
import fr.free.nrw.commons.customselector.database.NotForUploadStatusDao
4848
import fr.free.nrw.commons.customselector.helper.CustomSelectorConstants
49+
import fr.free.nrw.commons.customselector.helper.CustomSelectorConstants.MAX_IMAGE_COUNT
4950
import fr.free.nrw.commons.customselector.helper.FolderDeletionHelper
5051
import fr.free.nrw.commons.customselector.listeners.FolderClickListener
5152
import fr.free.nrw.commons.customselector.listeners.ImageSelectListener
@@ -107,7 +108,7 @@ class CustomSelectorActivity :
107108
/**
108109
* Maximum number of images that can be selected.
109110
*/
110-
private var uploadLimit: Int = 20
111+
private var uploadLimit: Int = MAX_IMAGE_COUNT
111112

112113
/**
113114
* Flag that is marked true when the amount
@@ -162,7 +163,7 @@ class CustomSelectorActivity :
162163
* Waits for confirmation of delete folder
163164
*/
164165
private val startForFolderDeletionResult = registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()){
165-
result -> onDeleteFolderResultReceived(result)
166+
result -> onDeleteFolderResultReceived(result)
166167
}
167168

168169
private val startForResult = registerForActivityResult(StartActivityForResult()){ result ->
@@ -214,7 +215,7 @@ class CustomSelectorActivity :
214215
)
215216

216217
// Check for single selection extra
217-
uploadLimit = if (intent.getBooleanExtra(EXTRA_SINGLE_SELECTION, false)) 1 else 20
218+
uploadLimit = if (intent.getBooleanExtra(EXTRA_SINGLE_SELECTION, false)) 1 else MAX_IMAGE_COUNT
218219

219220
setupViews()
220221

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,4 +887,6 @@ Upload your first media by tapping on the add button.</string>
887887
<string name="image_tag_line_created_and_uploaded_by">Created and uploaded by: %1$s</string>
888888
<string name="image_tag_line_created_by_and_uploaded_by">Created by %1$s and uploaded by %2$s</string>
889889
<string name="nominated_for_deletion_btn">Nominated for Deletion</string>
890+
<string name="custom_selector_max_image_limit_reached">You can only select a maximum of %d images.</string>
891+
890892
</resources>

0 commit comments

Comments
 (0)