Skip to content

Commit f45f26e

Browse files
authored
Implement single selection logic in custom image picker (#6341)
* build failure cause * Fix image selector logic in custom picker
1 parent 06a613e commit f45f26e

File tree

7 files changed

+45
-22
lines changed

7 files changed

+45
-22
lines changed

app/src/main/java/fr/free/nrw/commons/contributions/ContributionController.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,13 +253,14 @@ class ContributionController @Inject constructor(@param:Named("default_preferenc
253253
*/
254254
fun initiateCustomGalleryPickWithPermission(
255255
activity: Activity,
256-
resultLauncher: ActivityResultLauncher<Intent>
256+
resultLauncher: ActivityResultLauncher<Intent>,
257+
singleSelection: Boolean = false
257258
) {
258259
setPickerConfiguration(activity, true)
259260

260261
checkPermissionsAndPerformAction(
261262
activity,
262-
{ openCustomSelector(activity, resultLauncher, 0) },
263+
{ FilePicker.openCustomSelector(activity, resultLauncher, 0, singleSelection) },
263264
R.string.storage_permission_title,
264265
R.string.write_storage_permission_rationale,
265266
*PERMISSIONS_STORAGE

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,12 +327,17 @@ class ImageAdapter(
327327

328328
// Getting clicked index from all images index when show_already_actioned_images
329329
// switch is on
330+
if (singleSelection) {
331+
// If single selection mode, clear previous selection and select only the new one
332+
if (selectedImages.isNotEmpty() && (selectedImages[0] != images[position])) {
333+
val prevIndex = images.indexOf(selectedImages[0])
334+
selectedImages.clear()
335+
notifyItemChanged(prevIndex, ImageUnselected())
336+
}
337+
}
330338
val clickedIndex: Int =
331339
if (showAlreadyActionedImages) {
332340
ImageHelper.getIndex(selectedImages, images[position])
333-
334-
// Getting clicked index from actionable images when show_already_actioned_images
335-
// switch is off
336341
} else {
337342
ImageHelper.getIndex(selectedImages, ArrayList(actionableImagesMap.values)[position])
338343
}
@@ -618,4 +623,13 @@ class ImageAdapter(
618623
* Returns the text for showing inside the bubble during bubble scroll.
619624
*/
620625
override fun getSectionName(position: Int): String = images[position].date
626+
627+
private var singleSelection: Boolean = false
628+
629+
/**
630+
* Set single selection mode
631+
*/
632+
fun setSingleSelection(single: Boolean) {
633+
singleSelection = single
634+
}
621635
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class CustomSelectorActivity :
104104
/**
105105
* Maximum number of images that can be selected.
106106
*/
107-
private val uploadLimit: Int = 20
107+
private var uploadLimit: Int = 20
108108

109109
/**
110110
* Flag that is marked true when the amount
@@ -207,6 +207,9 @@ class CustomSelectorActivity :
207207
CustomSelectorViewModel::class.java,
208208
)
209209

210+
// Check for single selection extra
211+
uploadLimit = if (intent.getBooleanExtra(EXTRA_SINGLE_SELECTION, false)) 1 else 20
212+
210213
setupViews()
211214

212215
if (prefs.getBoolean("customSelectorFirstLaunch", true)) {
@@ -728,6 +731,7 @@ class CustomSelectorActivity :
728731
const val FOLDER_ID: String = "FolderId"
729732
const val FOLDER_NAME: String = "FolderName"
730733
const val ITEM_ID: String = "ItemId"
734+
const val EXTRA_SINGLE_SELECTION: String = "EXTRA_SINGLE_SELECTION"
731735
}
732736
}
733737

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ class ImageFragment :
212212
_binding = FragmentCustomSelectorBinding.inflate(inflater, container, false)
213213
imageAdapter =
214214
ImageAdapter(requireActivity(), activity as ImageSelectListener, imageLoader!!)
215+
// Set single selection mode if needed
216+
val singleSelection = (activity as? CustomSelectorActivity)?.intent?.getBooleanExtra(CustomSelectorActivity.EXTRA_SINGLE_SELECTION, false) == true
217+
imageAdapter.setSingleSelection(singleSelection)
215218
gridLayoutManager = GridLayoutManager(context, getSpanCount())
216219
with(binding?.selectorRv) {
217220
this?.layoutManager = gridLayoutManager

app/src/main/java/fr/free/nrw/commons/filepicker/FilePicker.kt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ object FilePicker : Constants {
2525
private const val KEY_LAST_CAMERA_VIDEO = "last_video"
2626
private const val KEY_TYPE = "type"
2727

28+
// Add extra for single selection
29+
private const val EXTRA_SINGLE_SELECTION = "EXTRA_SINGLE_SELECTION"
30+
2831
/**
2932
* Returns the uri of the clicked image so that it can be put in MediaStore
3033
*/
@@ -73,12 +76,17 @@ object FilePicker : Constants {
7376
* CreateCustomSectorIntent, creates intent for custom selector activity.
7477
* @param context
7578
* @param type
79+
* @param singleSelection If true, restricts to single image selection
7680
* @return Custom selector intent
7781
*/
7882
@JvmStatic
79-
private fun createCustomSelectorIntent(context: Context, type: Int): Intent {
83+
private fun createCustomSelectorIntent(context: Context, type: Int, singleSelection: Boolean = false): Intent {
8084
storeType(context, type)
81-
return Intent(context, CustomSelectorActivity::class.java)
85+
val intent = Intent(context, CustomSelectorActivity::class.java)
86+
if (singleSelection) {
87+
intent.putExtra(EXTRA_SINGLE_SELECTION, true)
88+
}
89+
return intent
8290
}
8391

8492
@JvmStatic
@@ -153,9 +161,10 @@ object FilePicker : Constants {
153161
fun openCustomSelector(
154162
activity: Activity,
155163
resultLauncher: ActivityResultLauncher<Intent>,
156-
type: Int
164+
type: Int,
165+
singleSelection: Boolean = false
157166
) {
158-
val intent = createCustomSelectorIntent(activity, type)
167+
val intent = createCustomSelectorIntent(activity, type, singleSelection)
159168
resultLauncher.launch(intent)
160169
}
161170

app/src/main/java/fr/free/nrw/commons/nearby/fragments/NearbyParentFragment.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,7 @@ class NearbyParentFragment : CommonsDaggerSupportFragment(),
974974
} else if (bottomSheetDetailsBehavior!!.state
975975
== BottomSheetBehavior.STATE_EXPANDED
976976
) {
977-
bottomSheetDetailsBehavior!!.state = BottomSheetBehavior.STATE_COLLAPSED
977+
bottomSheetDetailsBehavior!!.setState(BottomSheetBehavior.STATE_COLLAPSED)
978978
}
979979
}
980980

@@ -2457,9 +2457,11 @@ class NearbyParentFragment : CommonsDaggerSupportFragment(),
24572457
Timber.d("Gallery button tapped. Place: %s", selectedPlace.toString())
24582458
storeSharedPrefs(selectedPlace!!)
24592459
activity?.let {
2460+
// Pass singleSelection = true for Nearby flow
24602461
controller!!.initiateCustomGalleryPickWithPermission(
24612462
it,
2462-
customSelectorLauncherForResult
2463+
customSelectorLauncherForResult,
2464+
singleSelection = true
24632465
)
24642466
}
24652467
}

app/src/main/res/values-x-invalidLanguageCode/error.xml

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)