@@ -14,6 +14,7 @@ import android.os.Bundle
14
14
import android.provider.Settings
15
15
import android.view.View
16
16
import android.widget.CheckBox
17
+ import androidx.activity.OnBackPressedCallback
17
18
import androidx.appcompat.app.AlertDialog
18
19
import androidx.fragment.app.Fragment
19
20
import androidx.fragment.app.FragmentManager
@@ -122,7 +123,7 @@ class UploadActivity : BaseActivity(), UploadContract.View, UploadBaseFragment.C
122
123
/* *
123
124
* Set the value of the showPermissionDialog variable.
124
125
*
125
- * @param showPermissionsDialog `true` to indicate to show
126
+ * @property isShowPermissionsDialog `true` to indicate to show
126
127
* Permissions Dialog if permissions are missing, `false` otherwise.
127
128
*/
128
129
/* *
@@ -166,13 +167,32 @@ class UploadActivity : BaseActivity(), UploadContract.View, UploadBaseFragment.C
166
167
private var _binding : ActivityUploadBinding ? = null
167
168
private val binding: ActivityUploadBinding get() = _binding !!
168
169
170
+ private lateinit var onBackPressedCallback: OnBackPressedCallback
171
+
169
172
@SuppressLint(" CheckResult" )
170
173
override fun onCreate (savedInstanceState : Bundle ? ) {
171
174
super .onCreate(savedInstanceState)
172
175
173
176
_binding = ActivityUploadBinding .inflate(layoutInflater)
174
177
setContentView(binding.root)
175
178
179
+ // Overrides the back button to make sure the user is prepared to lose their progress
180
+ onBackPressedCallback = object : OnBackPressedCallback (true ) {
181
+ override fun handleOnBackPressed () {
182
+ showAlertDialog(
183
+ this @UploadActivity,
184
+ getString(R .string.back_button_warning),
185
+ getString(R .string.back_button_warning_desc),
186
+ getString(R .string.back_button_continue),
187
+ getString(R .string.back_button_warning),
188
+ null
189
+ ) {
190
+ finish()
191
+ }
192
+ }
193
+ }
194
+ onBackPressedDispatcher.addCallback(this , onBackPressedCallback)
195
+
176
196
/*
177
197
If Configuration of device is changed then get the new fragments
178
198
created by the system and populate the fragments ArrayList
@@ -187,7 +207,7 @@ class UploadActivity : BaseActivity(), UploadContract.View, UploadBaseFragment.C
187
207
}
188
208
189
209
init ()
190
- binding.rlContainerTitle.setOnClickListener { v : View ? -> onRlContainerTitleClicked() }
210
+ binding.rlContainerTitle.setOnClickListener { _ : View ? -> onRlContainerTitleClicked() }
191
211
nearbyPopupAnswers = mutableMapOf ()
192
212
// getting the current dpi of the device and if it is less than 320dp i.e. overlapping
193
213
// threshold, thumbnails automatically minimizes
@@ -201,7 +221,7 @@ class UploadActivity : BaseActivity(), UploadContract.View, UploadBaseFragment.C
201
221
}
202
222
locationManager!! .requestLocationUpdatesFromProvider(LocationManager .GPS_PROVIDER )
203
223
locationManager!! .requestLocationUpdatesFromProvider(LocationManager .NETWORK_PROVIDER )
204
- store = BasicKvStore (this , storeNameForCurrentUploadImagesSize ).apply {
224
+ store = BasicKvStore (this , STORE_NAME_FOR_CURRENT_UPLOAD_IMAGE_SIZE ).apply {
205
225
clearAll()
206
226
}
207
227
checkStoragePermissions()
@@ -241,7 +261,7 @@ class UploadActivity : BaseActivity(), UploadContract.View, UploadBaseFragment.C
241
261
242
262
override fun onPageSelected (position : Int ) {
243
263
currentSelectedPosition = position
244
- if (position >= uploadableFiles!! .size) {
264
+ if (position >= uploadableFiles.size) {
245
265
binding.cvContainerTopCard.visibility = View .GONE
246
266
} else {
247
267
thumbnailsAdapter!! .notifyDataSetChanged()
@@ -274,7 +294,7 @@ class UploadActivity : BaseActivity(), UploadContract.View, UploadBaseFragment.C
274
294
.subscribeOn(Schedulers .io())
275
295
.observeOn(AndroidSchedulers .mainThread())
276
296
.filter { result: Boolean? -> result!! }
277
- .subscribe { result : Boolean? ->
297
+ .subscribe { _ : Boolean? ->
278
298
showAlertDialog(
279
299
this ,
280
300
getString(R .string.block_notification_title),
@@ -284,7 +304,7 @@ class UploadActivity : BaseActivity(), UploadContract.View, UploadBaseFragment.C
284
304
})
285
305
}
286
306
287
- fun checkStoragePermissions () {
307
+ private fun checkStoragePermissions () {
288
308
// Check if all required permissions are granted
289
309
val hasAllPermissions = hasPermission(this , PERMISSIONS_STORAGE )
290
310
val hasPartialAccess = hasPartialAccess(this )
@@ -355,7 +375,7 @@ class UploadActivity : BaseActivity(), UploadContract.View, UploadBaseFragment.C
355
375
showLongToast(this , messageResourceId)
356
376
}
357
377
358
- override fun getUploadableFiles (): List <UploadableFile >? {
378
+ override fun getUploadableFiles (): List <UploadableFile > {
359
379
return uploadableFiles
360
380
}
361
381
@@ -367,6 +387,14 @@ class UploadActivity : BaseActivity(), UploadContract.View, UploadBaseFragment.C
367
387
override fun onUploadMediaDeleted (index : Int ) {
368
388
fragments!! .removeAt(index) // Remove the corresponding fragment
369
389
uploadableFiles.removeAt(index) // Remove the files from the list
390
+
391
+ val isMediaDetailFragment = fragments!! .getOrNull(currentSelectedPosition)?.let {
392
+ it is UploadMediaDetailFragment
393
+ } ? : false
394
+ if (! isMediaDetailFragment) {
395
+ // Should hide the top card current fragment is not the media detail fragment
396
+ showHideTopCard(false )
397
+ }
370
398
thumbnailsAdapter!! .notifyItemRemoved(index) // Notify the thumbnails adapter
371
399
uploadImagesAdapter!! .notifyDataSetChanged() // Notify the ViewPager
372
400
}
@@ -375,8 +403,8 @@ class UploadActivity : BaseActivity(), UploadContract.View, UploadBaseFragment.C
375
403
binding.tvTopCardTitle.text = resources
376
404
.getQuantityString(
377
405
R .plurals.upload_count_title,
378
- uploadableFiles!! .size,
379
- uploadableFiles!! .size
406
+ uploadableFiles.size,
407
+ uploadableFiles.size
380
408
)
381
409
}
382
410
@@ -444,15 +472,16 @@ class UploadActivity : BaseActivity(), UploadContract.View, UploadBaseFragment.C
444
472
receiveInternalSharedItems()
445
473
}
446
474
447
- if (uploadableFiles == null || uploadableFiles !! .isEmpty()) {
475
+ if (uploadableFiles.isEmpty()) {
448
476
handleNullMedia()
449
477
} else {
450
478
// Show thumbnails
451
- if (uploadableFiles!! .size > 1 ) {
452
- if (! defaultKvStore.getBoolean(" hasAlreadyLaunchedCategoriesDialog" )) { // If there is only file, no need to show the image thumbnails
479
+ if (uploadableFiles.size > 1 ) {
480
+ if (! defaultKvStore.getBoolean(" hasAlreadyLaunchedCategoriesDialog" )) {
481
+ // If there is only file, no need to show the image thumbnails
453
482
showAlertDialogForCategories()
454
483
}
455
- if (uploadableFiles!! .size > 3 &&
484
+ if (uploadableFiles.size > 3 &&
456
485
! defaultKvStore.getBoolean(" hasAlreadyLaunchedBigMultiupload" )
457
486
) {
458
487
showAlertForBattery()
@@ -464,8 +493,8 @@ class UploadActivity : BaseActivity(), UploadContract.View, UploadBaseFragment.C
464
493
binding.tvTopCardTitle.text = resources
465
494
.getQuantityString(
466
495
R .plurals.upload_count_title,
467
- uploadableFiles!! .size,
468
- uploadableFiles!! .size
496
+ uploadableFiles.size,
497
+ uploadableFiles.size
469
498
)
470
499
471
500
@@ -474,7 +503,7 @@ class UploadActivity : BaseActivity(), UploadContract.View, UploadBaseFragment.C
474
503
}
475
504
476
505
477
- for (uploadableFile in uploadableFiles!! ) {
506
+ for (uploadableFile in uploadableFiles) {
478
507
val uploadMediaDetailFragment = UploadMediaDetailFragment ()
479
508
480
509
if (! uploadIsOfAPlace) {
@@ -497,8 +526,8 @@ class UploadActivity : BaseActivity(), UploadContract.View, UploadBaseFragment.C
497
526
object : UploadMediaDetailFragmentCallback {
498
527
override fun deletePictureAtIndex (index : Int ) {
499
528
store!! .putInt(
500
- keyForCurrentUploadImagesSize ,
501
- (store!! .getInt(keyForCurrentUploadImagesSize ) - 1 )
529
+ KEY_FOR_CURRENT_UPLOAD_IMAGE_SIZE ,
530
+ (store!! .getInt(KEY_FOR_CURRENT_UPLOAD_IMAGE_SIZE ) - 1 )
502
531
)
503
532
presenter!! .deletePictureAtIndex(index)
504
533
}
@@ -576,11 +605,11 @@ class UploadActivity : BaseActivity(), UploadContract.View, UploadBaseFragment.C
576
605
fragments!! .add(mediaLicenseFragment!! )
577
606
} else {
578
607
for (i in 1 until fragments!! .size) {
579
- fragments!! [i]!! .callback = object : UploadBaseFragment .Callback {
608
+ fragments!! [i].callback = object : UploadBaseFragment .Callback {
580
609
override fun onNextButtonClicked (index : Int ) {
581
610
if (index < fragments!! .size - 1 ) {
582
611
binding.vpUpload.setCurrentItem(index + 1 , false )
583
- fragments!! [index + 1 ]!! .onBecameVisible()
612
+ fragments!! [index + 1 ].onBecameVisible()
584
613
(binding.rvThumbnails.layoutManager as LinearLayoutManager )
585
614
.scrollToPositionWithOffset(
586
615
if ((index > 0 )) index - 1 else 0 ,
@@ -594,7 +623,7 @@ class UploadActivity : BaseActivity(), UploadContract.View, UploadBaseFragment.C
594
623
override fun onPreviousButtonClicked (index : Int ) {
595
624
if (index != 0 ) {
596
625
binding.vpUpload.setCurrentItem(index - 1 , true )
597
- fragments!! [index - 1 ]!! .onBecameVisible()
626
+ fragments!! [index - 1 ].onBecameVisible()
598
627
(binding.rvThumbnails.layoutManager as LinearLayoutManager )
599
628
.scrollToPositionWithOffset(
600
629
if ((index > 3 )) index - 2 else 0 ,
@@ -632,11 +661,12 @@ class UploadActivity : BaseActivity(), UploadContract.View, UploadBaseFragment.C
632
661
binding.vpUpload.offscreenPageLimit = fragments!! .size
633
662
}
634
663
// Saving size of uploadableFiles
635
- store!! .putInt(keyForCurrentUploadImagesSize , uploadableFiles!! .size)
664
+ store!! .putInt(KEY_FOR_CURRENT_UPLOAD_IMAGE_SIZE , uploadableFiles.size)
636
665
}
637
666
638
667
/* *
639
- * Changes current image when one image upload is cancelled, to highlight next image in the top thumbnail.
668
+ * Changes current image when one image upload is cancelled, to highlight next image in the top
669
+ * thumbnail.
640
670
* Fixes: [Issue](https://github.com/commons-app/apps-android-commons/issues/5511)
641
671
*
642
672
* @param index Index of image to be removed
@@ -771,7 +801,7 @@ class UploadActivity : BaseActivity(), UploadContract.View, UploadBaseFragment.C
771
801
override fun onNextButtonClicked (index : Int ) {
772
802
if (index < fragments!! .size - 1 ) {
773
803
binding.vpUpload.setCurrentItem(index + 1 , false )
774
- fragments!! [index + 1 ]!! .onBecameVisible()
804
+ fragments!! [index + 1 ].onBecameVisible()
775
805
(binding.rvThumbnails.layoutManager as LinearLayoutManager )
776
806
.scrollToPositionWithOffset(if ((index > 0 )) index - 1 else 0 , 0 )
777
807
if (index < fragments!! .size - 4 ) {
@@ -786,18 +816,21 @@ class UploadActivity : BaseActivity(), UploadContract.View, UploadBaseFragment.C
786
816
override fun onPreviousButtonClicked (index : Int ) {
787
817
if (index != 0 ) {
788
818
binding.vpUpload.setCurrentItem(index - 1 , true )
789
- fragments!! [index - 1 ]!! .onBecameVisible()
819
+ fragments!! [index - 1 ].onBecameVisible()
790
820
(binding.rvThumbnails.layoutManager as LinearLayoutManager )
791
821
.scrollToPositionWithOffset(if ((index > 3 )) index - 2 else 0 , 0 )
792
- if ((index != 1 ) && ((index - 1 ) < uploadableFiles!! .size)) {
822
+ if ((index != 1 ) && ((index - 1 ) < uploadableFiles.size)) {
793
823
// Shows the top card if it was hidden because of the last image being deleted and
794
824
// now the user has hit previous button to go back to the media details
795
825
showHideTopCard(true )
796
826
}
797
827
}
798
828
}
799
829
800
- override fun onThumbnailDeleted (position : Int ) = presenter!! .deletePictureAtIndex(position)
830
+ override fun onThumbnailDeleted (position : Int ) {
831
+ presenter!! .deletePictureAtIndex(position)
832
+ thumbnailsAdapter?.notifyDataSetChanged()
833
+ }
801
834
802
835
/* *
803
836
* The adapter used to show image upload intermediate fragments
@@ -824,11 +857,11 @@ class UploadActivity : BaseActivity(), UploadContract.View, UploadBaseFragment.C
824
857
}
825
858
826
859
827
- fun onRlContainerTitleClicked () {
860
+ private fun onRlContainerTitleClicked () {
828
861
binding.rvThumbnails.visibility =
829
862
if (isTitleExpanded) View .GONE else View .VISIBLE
830
863
isTitleExpanded = ! isTitleExpanded
831
- binding.ibToggleTopCard.rotation = binding.ibToggleTopCard.rotation + 180
864
+ binding.ibToggleTopCard.rotation + = 180
832
865
}
833
866
834
867
override fun onDestroy () {
@@ -845,21 +878,7 @@ class UploadActivity : BaseActivity(), UploadContract.View, UploadBaseFragment.C
845
878
if (uploadCategoriesFragment != null ) {
846
879
uploadCategoriesFragment!! .callback = null
847
880
}
848
- }
849
-
850
- /* *
851
- * Overrides the back button to make sure the user is prepared to lose their progress
852
- */
853
- @SuppressLint(" MissingSuperCall" )
854
- override fun onBackPressed () {
855
- showAlertDialog(
856
- this ,
857
- getString(R .string.back_button_warning),
858
- getString(R .string.back_button_warning_desc),
859
- getString(R .string.back_button_continue),
860
- getString(R .string.back_button_warning),
861
- null
862
- ) { finish() }
881
+ onBackPressedCallback.remove()
863
882
}
864
883
865
884
/* *
@@ -879,7 +898,7 @@ class UploadActivity : BaseActivity(), UploadContract.View, UploadBaseFragment.C
879
898
.setView(view)
880
899
.setTitle(getString(R .string.multiple_files_depiction_header))
881
900
.setMessage(getString(R .string.multiple_files_depiction))
882
- .setPositiveButton(" OK" ) { dialog : DialogInterface ? , which : Int ->
901
+ .setPositiveButton(" OK" ) { _ : DialogInterface ? , _ : Int ->
883
902
if (checkBox.isChecked) {
884
903
// Save the user's choice to not show the dialog again
885
904
defaultKvStore.putBoolean(" hasAlreadyLaunchedCategoriesDialog" , true )
@@ -913,14 +932,14 @@ class UploadActivity : BaseActivity(), UploadContract.View, UploadBaseFragment.C
913
932
getString(R .string.cancel),
914
933
{
915
934
/* Since opening the right settings page might be device dependent, using
916
- https://github.com/WaseemSabir/BatteryPermissionHelper
917
- directly appeared like a promising idea.
918
- However, this simply closed the popup and did not make
919
- the settings page appear on a Pixel as well as a Xiaomi device.
920
- Used the standard intent instead of using this library as
921
- it shows a list of all the apps on the device and allows users to
922
- turn battery optimisation off.
923
- */
935
+ https://github.com/WaseemSabir/BatteryPermissionHelper
936
+ directly appeared like a promising idea.
937
+ However, this simply closed the popup and did not make
938
+ the settings page appear on a Pixel as well as a Xiaomi device.
939
+ Used the standard intent instead of using this library as
940
+ it shows a list of all the apps on the device and allows users to
941
+ turn battery optimisation off.
942
+ */
924
943
val batteryOptimisationSettingsIntent = Intent (
925
944
Settings .ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS
926
945
)
@@ -958,7 +977,8 @@ class UploadActivity : BaseActivity(), UploadContract.View, UploadBaseFragment.C
958
977
Also, location information is discarded if the difference between
959
978
current location and location recorded just before capturing the image
960
979
is greater than 100 meters */
961
- if (isLocationTagUnchecked || locationDifference > 100 || ! defaultKvStore.getBoolean(" inAppCameraLocationPref" )
980
+ if (isLocationTagUnchecked || locationDifference > 100
981
+ || ! defaultKvStore.getBoolean(" inAppCameraLocationPref" )
962
982
|| ! isInAppCameraUpload
963
983
) {
964
984
currLocation = null
@@ -979,8 +999,8 @@ class UploadActivity : BaseActivity(), UploadContract.View, UploadBaseFragment.C
979
999
@JvmField
980
1000
var nearbyPopupAnswers: MutableMap <Place , Boolean >? = null
981
1001
982
- const val keyForCurrentUploadImagesSize : String = " CurrentUploadImagesSize"
983
- const val storeNameForCurrentUploadImagesSize : String = " CurrentUploadImageQualities"
1002
+ const val KEY_FOR_CURRENT_UPLOAD_IMAGE_SIZE : String = " CurrentUploadImagesSize"
1003
+ const val STORE_NAME_FOR_CURRENT_UPLOAD_IMAGE_SIZE : String = " CurrentUploadImageQualities"
984
1004
985
1005
/* *
986
1006
* Sets the flag indicating whether the upload is of a specific place.
0 commit comments