Skip to content

Commit 63802a4

Browse files
authored
Custom multi-selection gallery stability fixes (#3)
1 parent 9e57ceb commit 63802a4

File tree

10 files changed

+136
-115
lines changed

10 files changed

+136
-115
lines changed

app/src/main/java/com/shz/imagepicker/imagepickerapp/MainActivity.kt

Lines changed: 43 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@ package com.shz.imagepicker.imagepickerapp
22

33
import android.os.Bundle
44
import android.util.Log
5-
import android.widget.ImageView
5+
import androidx.appcompat.app.AlertDialog
66
import androidx.appcompat.app.AppCompatActivity
77
import androidx.databinding.DataBindingUtil
88
import androidx.recyclerview.widget.GridLayoutManager
99
import com.bumptech.glide.Glide
1010
import com.shz.imagepicker.imagepicker.ImagePicker
1111
import com.shz.imagepicker.imagepicker.ImagePickerCallback
12-
import com.shz.imagepicker.imagepicker.ImagePickerLoadDelegate
1312
import com.shz.imagepicker.imagepicker.model.GalleryPicker
1413
import com.shz.imagepicker.imagepicker.model.PickedResult
1514
import com.shz.imagepicker.imagepickerapp.databinding.ActivityMainBinding
16-
import java.io.File
1715

1816
class MainActivity : AppCompatActivity(), ImagePickerCallback {
1917

@@ -26,81 +24,78 @@ class MainActivity : AppCompatActivity(), ImagePickerCallback {
2624
override fun onCreate(savedInstanceState: Bundle?) {
2725
super.onCreate(savedInstanceState)
2826
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
27+
initializeButtons()
28+
initializeImagesList()
29+
}
2930

30-
adapter = ImagesDemoAdapter()
31-
32-
binding.rv.adapter = adapter
33-
binding.rv.layoutManager = GridLayoutManager(this, 2)
31+
override fun onImagePickerResult(result: PickedResult) {
32+
Log.d("MainActivity", "result: $result")
33+
when (result) {
34+
PickedResult.Empty -> {
35+
adapter.submitList(emptyList())
36+
}
37+
is PickedResult.Error -> {
38+
result.throwable.printStackTrace()
39+
AlertDialog.Builder(this)
40+
.setTitle(R.string.action_error)
41+
.setMessage(result.throwable.message.toString())
42+
.setPositiveButton(R.string.action_ok) { d, _ -> d.dismiss() }
43+
.show()
44+
}
45+
is PickedResult.Multiple -> {
46+
adapter.submitList(result.images)
47+
}
48+
is PickedResult.Single -> {
49+
adapter.submitList(listOf(result.image))
50+
}
51+
}
52+
}
3453

35-
binding.btnCamera.setOnClickListener {
54+
private fun initializeButtons() = with(binding) {
55+
btnCamera.setOnClickListener {
3656
imagePicker
3757
.autoRotate(binding.switchAutoRotate.isChecked)
3858
.useCamera()
3959
.build()
40-
.launch(this)
60+
.launch(this@MainActivity)
4161
}
42-
binding.btnGallery.setOnClickListener {
62+
btnGallery.setOnClickListener {
4363
imagePicker
4464
.autoRotate(binding.switchAutoRotate.isChecked)
4565
.useGallery()
46-
// .multipleSelection(true)
66+
.multipleSelection(binding.switchMultipleSelection.isChecked)
4767
.build()
48-
.launch(this)
68+
.launch(this@MainActivity)
4969
}
50-
binding.btnGalCustomSingle.setOnClickListener {
70+
btnGalCustomSingle.setOnClickListener {
5171
imagePicker
5272
.autoRotate(binding.switchAutoRotate.isChecked)
5373
.useGallery()
74+
.multipleSelection(binding.switchMultipleSelection.isChecked)
5475
.galleryPicker(GalleryPicker.CUSTOM)
5576
.loadDelegate { imageView, file ->
56-
Glide.with(this)
77+
Glide.with(imageView)
5778
.load(file)
5879
.into(imageView)
5980
}
6081
.build()
61-
.launch(this)
82+
.launch(this@MainActivity)
6283
}
63-
binding.btnGalCustomMultiple.setOnClickListener {
64-
imagePicker
65-
.autoRotate(binding.switchAutoRotate.isChecked)
66-
.useGallery()
67-
.multipleSelection()
68-
.galleryPicker(GalleryPicker.CUSTOM)
69-
.loadDelegate { imageView, file ->
70-
Glide.with(this)
71-
.load(file)
72-
.into(imageView)
73-
}
74-
.build()
75-
.launch(this)
76-
}
77-
binding.btnDialog.setOnClickListener {
84+
btnDialog.setOnClickListener {
7885
imagePicker
7986
.autoRotate(binding.switchAutoRotate.isChecked)
8087
.useGallery()
8188
.useCamera()
82-
.multipleSelection()
89+
.multipleSelection(binding.switchMultipleSelection.isChecked)
8390
.galleryPicker(GalleryPicker.NATIVE)
8491
.build()
85-
.launch(this)
92+
.launch(this@MainActivity)
8693
}
8794
}
8895

89-
override fun onImagePickerResult(result: PickedResult) {
90-
Log.d("MainActivity", "result: $result")
91-
when (result) {
92-
PickedResult.Empty -> {
93-
adapter.submitList(emptyList())
94-
}
95-
is PickedResult.Error -> {
96-
result.throwable.printStackTrace()
97-
}
98-
is PickedResult.Multiple -> {
99-
adapter.submitList(result.images)
100-
}
101-
is PickedResult.Single -> {
102-
adapter.submitList(listOf(result.image))
103-
}
104-
}
96+
private fun initializeImagesList() = with(binding) {
97+
adapter = ImagesDemoAdapter()
98+
recyclerImages.adapter = adapter
99+
recyclerImages.layoutManager = GridLayoutManager(this@MainActivity, 2)
105100
}
106101
}

app/src/main/res/layout/activity_main.xml

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99
android:layout_height="match_parent">
1010

1111
<androidx.recyclerview.widget.RecyclerView
12-
android:id="@+id/rv"
12+
android:id="@+id/recycler_images"
1313
android:layout_width="match_parent"
1414
android:layout_height="0dp"
15-
app:layout_constraintBottom_toTopOf="@+id/linear"
15+
app:layout_constraintBottom_toTopOf="@+id/container_actions"
1616
app:layout_constraintEnd_toEndOf="parent"
1717
app:layout_constraintStart_toStartOf="parent"
1818
app:layout_constraintTop_toTopOf="parent" />
1919

2020
<LinearLayout
21-
android:id="@+id/linear"
21+
android:id="@+id/container_actions"
2222
android:layout_width="match_parent"
2323
android:layout_height="wrap_content"
2424
android:orientation="vertical"
@@ -27,6 +27,48 @@
2727
app:layout_constraintEnd_toEndOf="parent"
2828
app:layout_constraintStart_toStartOf="parent">
2929

30+
<LinearLayout
31+
android:id="@+id/section_multiple_selection"
32+
android:layout_width="match_parent"
33+
android:layout_height="wrap_content"
34+
android:orientation="horizontal">
35+
36+
<androidx.appcompat.widget.SwitchCompat
37+
android:id="@+id/switch_multiple_selection"
38+
android:layout_width="wrap_content"
39+
android:layout_height="wrap_content"
40+
app:switchPadding="8dp"
41+
/>
42+
43+
<TextView
44+
android:layout_width="wrap_content"
45+
android:layout_height="wrap_content"
46+
android:layout_marginStart="8dp"
47+
android:layout_marginLeft="8dp"
48+
android:text="@string/setting_multiple_selection" />
49+
</LinearLayout>
50+
51+
<LinearLayout
52+
android:id="@+id/section_auto_rotate"
53+
android:layout_width="match_parent"
54+
android:layout_height="wrap_content"
55+
android:orientation="horizontal">
56+
57+
<androidx.appcompat.widget.SwitchCompat
58+
android:id="@+id/switch_auto_rotate"
59+
android:layout_width="wrap_content"
60+
android:layout_height="wrap_content"
61+
app:switchPadding="8dp"
62+
/>
63+
64+
<TextView
65+
android:layout_width="wrap_content"
66+
android:layout_height="wrap_content"
67+
android:layout_marginStart="8dp"
68+
android:layout_marginLeft="8dp"
69+
android:text="@string/setting_auto_rotate" />
70+
</LinearLayout>
71+
3072
<LinearLayout
3173
android:layout_width="match_parent"
3274
android:layout_height="wrap_content"
@@ -45,7 +87,7 @@
4587
android:focusable="true"
4688
android:gravity="center"
4789
android:padding="8dp"
48-
android:text="CAMERA"
90+
android:text="@string/action_native_camera"
4991
android:textColor="#FFFFFF"
5092
android:textSize="16sp" />
5193

@@ -62,7 +104,7 @@
62104
android:focusable="true"
63105
android:gravity="center"
64106
android:padding="8dp"
65-
android:text="GALLERY"
107+
android:text="@string/action_native_gallery"
66108
android:textColor="#FFFFFF"
67109
android:textSize="16sp" />
68110

@@ -86,54 +128,28 @@
86128
android:focusable="true"
87129
android:gravity="center"
88130
android:padding="8dp"
89-
android:text="Custom single gallery"
131+
android:text="@string/action_custom_gallery"
90132
android:textColor="#FFFFFF"
91133
android:textSize="16sp" />
92134

93135
<TextView
94-
android:id="@+id/btn_gal_custom_multiple"
136+
android:id="@+id/btn_dialog"
95137
android:layout_width="match_parent"
96138
android:layout_height="wrap_content"
97139
android:layout_marginHorizontal="2dp"
98140
android:layout_marginVertical="2dp"
99141
android:layout_weight="1"
100142
android:background="#E16666"
101143
android:clickable="true"
102-
android:drawableTop="@drawable/ic_custom"
144+
android:drawableTop="@drawable/ic_list"
103145
android:focusable="true"
104146
android:gravity="center"
105147
android:padding="8dp"
106-
android:text="Custom multi gallery"
148+
android:text="@string/action_launch_interactive_dialog"
107149
android:textColor="#FFFFFF"
108150
android:textSize="16sp" />
109151

110152
</LinearLayout>
111-
112-
<TextView
113-
android:id="@+id/btn_dialog"
114-
android:layout_width="match_parent"
115-
android:layout_height="wrap_content"
116-
android:layout_marginHorizontal="2dp"
117-
android:layout_marginVertical="2dp"
118-
android:layout_weight="1"
119-
android:background="#E16666"
120-
android:clickable="true"
121-
android:drawableStart="@drawable/ic_list"
122-
android:drawableLeft="@drawable/ic_list"
123-
android:focusable="true"
124-
android:gravity="center"
125-
android:padding="8dp"
126-
android:text="Launch interactive dialog"
127-
android:textColor="#FFFFFF"
128-
android:textSize="16sp" />
129-
130-
<androidx.appcompat.widget.SwitchCompat
131-
android:id="@+id/switch_auto_rotate"
132-
android:layout_width="match_parent"
133-
android:layout_height="wrap_content"
134-
android:layoutDirection="rtl"
135-
app:switchPadding="8dp"
136-
android:text="Auto rotate images" />
137153
</LinearLayout>
138154
</androidx.constraintlayout.widget.ConstraintLayout>
139155
</layout>

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
<resources>
22
<string name="app_name">ImagePicker Android Library</string>
3+
<string name="setting_multiple_selection">Multiple selection</string>
4+
<string name="setting_auto_rotate">Auto rotate images</string>
5+
<string name="action_native_camera">CAMERA</string>
6+
<string name="action_native_gallery">GALLERY</string>
7+
<string name="action_custom_gallery">Custom Gallery</string>
8+
<string name="action_launch_interactive_dialog">Interactive dialog</string>
9+
<string name="action_error">Error</string>
10+
<string name="action_ok">OK</string>
311
</resources>

imagepicker/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66

77
def libraryGroupId = 'com.shz.imagepicker'
88
def libraryArtifactId = 'imagepicker'
9-
def libraryVersion = '2.0'
9+
def libraryVersion = '2.0.1'
1010

1111

1212
android {

imagepicker/src/main/java/com/shz/imagepicker/imagepicker/ImagePicker.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ class ImagePicker private constructor(
3030
private val galleryPicker: GalleryPicker,
3131
private val loadDelegate: ImagePickerLoadDelegate,
3232
) {
33-
3433
/**
3534
* Checks for access Camera or Media permissions, then
3635
* launches **ImagePicker** according to defined configuration.
@@ -175,7 +174,6 @@ class ImagePicker private constructor(
175174
*
176175
* @return [ImagePicker.Builder]
177176
*/
178-
@Deprecated("GalleryPicker.CUSTOM is deprecated from Android 13 (SDK 33)")
179177
fun minimumSelectionCount(minimumSelectionCount: Int) = apply {
180178
this.minimumSelectionCount = minimumSelectionCount
181179
}
@@ -189,7 +187,6 @@ class ImagePicker private constructor(
189187
*
190188
* @return [ImagePicker.Builder]
191189
*/
192-
@Deprecated("GalleryPicker.CUSTOM is deprecated from Android 13 (SDK 33)")
193190
fun maximumSelectionCount(maximumSelectionCount: Int) = apply {
194191
this.maximumSelectionCount = maximumSelectionCount
195192
}
@@ -211,12 +208,10 @@ class ImagePicker private constructor(
211208
this.galleryPicker = galleryPicker
212209
}
213210

214-
@Deprecated("GalleryPicker.CUSTOM is deprecated from Android 13 (SDK 33)")
215211
fun loadDelegate(loadDelegate: ImagePickerLoadDelegate) = apply {
216212
this.loadDelegate = loadDelegate
217213
}
218214

219-
@Deprecated("GalleryPicker.CUSTOM is deprecated from Android 13 (SDK 33)")
220215
fun loadDelegate(loadDelegate: (ImageView, File) -> Unit) = apply {
221216
this.loadDelegate = ImagePickerLoadDelegate { iv, file -> loadDelegate(iv, file) }
222217
}

imagepicker/src/main/java/com/shz/imagepicker/imagepicker/ImagePickerLauncher.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ internal class ImagePickerLauncher(private val context: Context) {
101101
})
102102
}
103103

104-
@Deprecated("GalleryPicker.CUSTOM is deprecated from Android 13 (SDK 33)")
105104
private fun launchMultipleSelectionGalleryCustom(
106105
callback: ImagePickerCallback,
107106
delegate: ImagePickerLoadDelegate,

imagepicker/src/main/java/com/shz/imagepicker/imagepicker/activity/customgallery/GalleryPickerCustomActivity.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,10 @@ internal class GalleryPickerCustomActivity : ImagePickerActivity() {
8888
submitList(files)
8989
}
9090
} else {
91-
GalleryImagesMultipleAdapter(ArrayList(files), max) { selectedFiles ->
91+
GalleryImagesMultipleAdapter(loadDelegate, max) { selectedFiles ->
9292
onSelectionChanged(selectedFiles.size)
93+
}.apply {
94+
submit(files)
9395
}
9496
}
9597
}

0 commit comments

Comments
 (0)