#3101: Add image upload limit of 20 to custom selector (#5369)

* Add basic image limit warning to custom selector

* Block upload button when image limit is exceeded

* Complete basic functionality for upload limit: Disabled button, warning sign & toast

* Consolidate functionality between upload limit and not for upload marking

* Upload Limit: write unit tests and optimize control flow

* Upload Limit Tests: improve logic coverage and alter to stay valid if limit is changed

* Upload Limit: polish javadocs and add explanation to warning toast.

* Upload Limit: refactor variable names

* Upload Limit: change warning toast to dialog box, repurposing welcome dialog code & layout

* Upload Limit: remove error icon when the mark as not for upload button is pressed
This commit is contained in:
u7469570 2023-11-20 20:13:05 +11:00 committed by GitHub
parent 3118a8368b
commit 60764f6f73
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 216 additions and 51 deletions

View file

@ -28,9 +28,9 @@ import fr.free.nrw.commons.media.ZoomableActivity
import fr.free.nrw.commons.theme.BaseActivity
import fr.free.nrw.commons.upload.FileUtilsWrapper
import fr.free.nrw.commons.utils.CustomSelectorUtils
import kotlinx.android.synthetic.main.activity_login.view.title
import kotlinx.coroutines.*
import java.io.File
import java.lang.Integer.max
import javax.inject.Inject
@ -67,6 +67,22 @@ class CustomSelectorActivity : BaseActivity(), FolderClickListener, ImageSelectL
*/
private lateinit var prefs: SharedPreferences
/**
* Maximum number of images that can be selected.
*/
private val uploadLimit: Int = 20
/**
* Flag that is marked true when the amount
* of selected images is greater than the upload limit.
*/
private var uploadLimitExceeded: Boolean = false
/**
* Tracks the amount by which the upload limit has been exceeded.
*/
private var uploadLimitExceededBy: Int = 0
/**
* View Model Factory.
*/
@ -201,6 +217,7 @@ class CustomSelectorActivity : BaseActivity(), FolderClickListener, ImageSelectL
i++
}
markAsNotForUpload(selectedImages)
toolbarBinding.imageLimitError.visibility = View.INVISIBLE
}
/**
@ -314,6 +331,10 @@ class CustomSelectorActivity : BaseActivity(), FolderClickListener, ImageSelectL
private fun setUpToolbar() {
val back: ImageButton = findViewById(R.id.back)
back.setOnClickListener { onBackPressed() }
val limitError: ImageButton = findViewById(R.id.image_limit_error)
limitError.visibility = View.INVISIBLE
limitError.setOnClickListener { displayUploadLimitWarning() }
}
/**
@ -342,7 +363,19 @@ class CustomSelectorActivity : BaseActivity(), FolderClickListener, ImageSelectL
viewModel.selectedImages.value = selectedImages
changeTitle(bucketName, selectedImages.size)
if (selectedNotForUploadImages > 0) {
uploadLimitExceeded = selectedImages.size > uploadLimit
uploadLimitExceededBy = max(selectedImages.size - uploadLimit,0)
if (uploadLimitExceeded && selectedNotForUploadImages == 0) {
toolbarBinding.imageLimitError.visibility = View.VISIBLE
bottomSheetBinding.upload.text = resources.getString(
R.string.custom_selector_button_limit_text, uploadLimit)
} else {
toolbarBinding.imageLimitError.visibility = View.INVISIBLE
bottomSheetBinding.upload.text = resources.getString(R.string.upload)
}
if (uploadLimitExceeded || selectedNotForUploadImages > 0) {
bottomSheetBinding.upload.isEnabled = false
bottomSheetBinding.upload.alpha = 0.5f
} else {
@ -390,22 +423,22 @@ class CustomSelectorActivity : BaseActivity(), FolderClickListener, ImageSelectL
* Get the selected images. Remove any non existent file, forward the data to finish selector.
*/
fun onDone() {
val selectedImages = viewModel.selectedImages.value
if (selectedImages.isNullOrEmpty()) {
finishPickImages(arrayListOf())
return
}
var i = 0
while (i < selectedImages.size) {
val path = selectedImages[i].path
val file = File(path)
if (!file.exists()) {
selectedImages.removeAt(i)
i--
val selectedImages = viewModel.selectedImages.value
if (selectedImages.isNullOrEmpty()) {
finishPickImages(arrayListOf())
return
}
i++
}
finishPickImages(selectedImages)
var i = 0
while (i < selectedImages.size) {
val path = selectedImages[i].path
val file = File(path)
if (!file.exists()) {
selectedImages.removeAt(i)
i--
}
i++
}
finishPickImages(selectedImages)
}
/**
@ -432,6 +465,20 @@ class CustomSelectorActivity : BaseActivity(), FolderClickListener, ImageSelectL
}
}
/**
* Displays a dialog explaining the upload limit warning.
*/
private fun displayUploadLimitWarning() {
val dialog = Dialog(this)
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialog.setContentView(R.layout.custom_selector_limit_dialog)
(dialog.findViewById(R.id.btn_dismiss_limit_warning) as Button).setOnClickListener()
{ dialog.dismiss() }
(dialog.findViewById(R.id.upload_limit_warning) as TextView).text = resources.getString(
R.string.custom_selector_over_limit_warning, uploadLimit, uploadLimitExceededBy)
dialog.show()
}
/**
* On activity destroy
* If image fragment is open, overwrite its attributes otherwise discard the values.