Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ object CustomSelectorConstants {
const val NEW_SELECTED_IMAGES = "new_selected_images"
const val SHOULD_REFRESH = "should_refresh"
const val FULL_SCREEN_MODE_FIRST_LUNCH = "full_screen_mode_first_launch"
const val MAX_IMAGE_COUNT = 20
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fr.free.nrw.commons.customselector.ui.adapter

import android.content.Context
import android.content.SharedPreferences
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
Expand All @@ -13,6 +14,7 @@ import com.bumptech.glide.Glide
import com.simplecityapps.recyclerview_fastscroll.views.FastScrollRecyclerView
import fr.free.nrw.commons.R
import fr.free.nrw.commons.contributions.Contribution
import fr.free.nrw.commons.customselector.helper.CustomSelectorConstants.MAX_IMAGE_COUNT
import fr.free.nrw.commons.customselector.helper.ImageHelper
import fr.free.nrw.commons.customselector.helper.ImageHelper.CUSTOM_SELECTOR_PREFERENCE_KEY
import fr.free.nrw.commons.customselector.helper.ImageHelper.SHOW_ALREADY_ACTIONED_IMAGES_PREFERENCE_KEY
Expand All @@ -37,7 +39,7 @@ class ImageAdapter(
/**
* Application Context.
*/
context: Context,
private val context: Context,
/**
* Image select listener for click events on image.
*/
Expand All @@ -46,8 +48,11 @@ class ImageAdapter(
* ImageLoader queries images.
*/
private var imageLoader: ImageLoader,
) : RecyclerViewAdapter<ImageAdapter.ImageViewHolder>(context),
) : RecyclerView.Adapter<ImageAdapter.ImageViewHolder>(),
FastScrollRecyclerView.SectionedAdapter {

private val inflater: LayoutInflater = LayoutInflater.from(context)

/**
* ImageSelectedOrUpdated payload class.
*/
Expand Down Expand Up @@ -124,6 +129,15 @@ class ImageAdapter(
private var ioDispatcher: CoroutineDispatcher = Dispatchers.IO
private val scope: CoroutineScope = MainScope()

//maximum number of images that can be selected.
private var maxUploadLimit: Int = MAX_IMAGE_COUNT


//set maximum number of images allowed for upload.
fun setMaxUploadLimit(limit: Int) {
maxUploadLimit = limit
}

/**
* Create View holder.
*/
Expand Down Expand Up @@ -350,6 +364,20 @@ class ImageAdapter(
// Notify listener of deselection to update UI
imageSelectListener.onSelectedImagesChanged(selectedImages, numberOfSelectedImagesMarkedAsNotForUpload)
} else {
//check the maximum limit before allowing the selection
if (!singleSelection && selectedImages.size >= maxUploadLimit) {
// limit reached, show a toast and prevent selection
Toast.makeText(
context,
context.getString(
R.string.custom_selector_max_image_limit_reached,
maxUploadLimit
),
Toast.LENGTH_SHORT
).show()
return //exit the function, preventing selection
}

// Prevent adding the same image multiple times
val image = if (showAlreadyActionedImages) images[position] else ArrayList(actionableImagesMap.values)[position]
if (selectedImages.contains(image)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import fr.free.nrw.commons.R
import fr.free.nrw.commons.customselector.database.NotForUploadStatus
import fr.free.nrw.commons.customselector.database.NotForUploadStatusDao
import fr.free.nrw.commons.customselector.helper.CustomSelectorConstants
import fr.free.nrw.commons.customselector.helper.CustomSelectorConstants.MAX_IMAGE_COUNT
import fr.free.nrw.commons.customselector.helper.FolderDeletionHelper
import fr.free.nrw.commons.customselector.listeners.FolderClickListener
import fr.free.nrw.commons.customselector.listeners.ImageSelectListener
Expand Down Expand Up @@ -107,7 +108,7 @@ class CustomSelectorActivity :
/**
* Maximum number of images that can be selected.
*/
private var uploadLimit: Int = 20
private var uploadLimit: Int = MAX_IMAGE_COUNT

/**
* Flag that is marked true when the amount
Expand Down Expand Up @@ -162,7 +163,7 @@ class CustomSelectorActivity :
* Waits for confirmation of delete folder
*/
private val startForFolderDeletionResult = registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()){
result -> onDeleteFolderResultReceived(result)
result -> onDeleteFolderResultReceived(result)
}

private val startForResult = registerForActivityResult(StartActivityForResult()){ result ->
Expand Down Expand Up @@ -214,7 +215,7 @@ class CustomSelectorActivity :
)

// Check for single selection extra
uploadLimit = if (intent.getBooleanExtra(EXTRA_SINGLE_SELECTION, false)) 1 else 20
uploadLimit = if (intent.getBooleanExtra(EXTRA_SINGLE_SELECTION, false)) 1 else MAX_IMAGE_COUNT

setupViews()

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -887,4 +887,6 @@ Upload your first media by tapping on the add button.</string>
<string name="image_tag_line_created_and_uploaded_by">Created and uploaded by: %1$s</string>
<string name="image_tag_line_created_by_and_uploaded_by">Created by %1$s and uploaded by %2$s</string>
<string name="nominated_for_deletion_btn">Nominated for Deletion</string>
<string name="custom_selector_max_image_limit_reached">You can only select a maximum of %d images.</string>

</resources>