-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathMainActivity.kt
More file actions
170 lines (154 loc) · 6.05 KB
/
Copy pathMainActivity.kt
File metadata and controls
170 lines (154 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package com.ssimagepicker.app.ui
import android.net.Uri
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.app.imagepickerlibrary.ImagePicker
import com.app.imagepickerlibrary.ImagePicker.Companion.registerImagePicker
import com.app.imagepickerlibrary.listener.ImagePickerResultListener
import com.app.imagepickerlibrary.model.ImageProvider
import com.app.imagepickerlibrary.model.PickerType
import com.app.imagepickerlibrary.ui.bottomsheet.SSPickerOptionsBottomSheet
import com.ssimagepicker.app.PickerOptions
import com.ssimagepicker.app.R
import com.ssimagepicker.app.databinding.ActivityMainBinding
import com.ssimagepicker.app.enableEdgeToEdge
import com.ssimagepicker.app.isAtLeast11
/**
* MainActivity which displays all the functionality of the ImagePicker library. All the attributes are modified with the ui.
*/
class MainActivity : AppCompatActivity(), View.OnClickListener,
SSPickerOptionsBottomSheet.ImagePickerClickListener,
ImagePickerResultListener, PickerOptionsBottomSheet.PickerOptionsListener {
companion object {
private const val IMAGE_LIST = "IMAGE_LIST"
}
private lateinit var binding: ActivityMainBinding
private val imagePicker: ImagePicker by lazy {
registerImagePicker(this@MainActivity)
}
private val imageList = mutableListOf<Uri>()
private val imageDataAdapter = ImageDataAdapter(imageList)
private var pickerOptions = PickerOptions.default()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
title = getString(R.string.activity_demo)
binding.clickHandler = this
setUpToolbar()
setUI(savedInstanceState)
enableEdgeToEdge(binding.toolbar.root)
}
private fun setUI(savedInstanceState: Bundle?) {
binding.imageRecyclerView.adapter = imageDataAdapter
if (savedInstanceState != null && savedInstanceState.containsKey(IMAGE_LIST)) {
val uriList: List<Uri> =
savedInstanceState.getParcelableArrayList(IMAGE_LIST) ?: listOf()
updateImageList(uriList)
}
}
override fun onClick(v: View) {
when (v.id) {
R.id.options_button -> {
openPickerOptions()
}
R.id.open_picker_button -> {
pickerOptions = pickerOptions.copy(compressImage = true)
openImagePicker()
}
R.id.open_sheet_button -> {
val fragment =
SSPickerOptionsBottomSheet.newInstance(R.style.CustomPickerBottomSheet)
fragment.show(supportFragmentManager, SSPickerOptionsBottomSheet.BOTTOM_SHEET_TAG)
}
}
}
/**
* This method receives the selected picker type option from the bottom sheet.
*/
override fun onImageProvider(provider: ImageProvider) {
when (provider) {
ImageProvider.GALLERY -> {
pickerOptions = pickerOptions.copy(pickerType = PickerType.GALLERY)
openImagePicker()
}
ImageProvider.CAMERA -> {
pickerOptions = pickerOptions.copy(pickerType = PickerType.CAMERA)
openImagePicker()
}
ImageProvider.NONE -> {
//User has pressed cancel show anything or just leave it blank.
}
}
}
/**
* Opens the options for picker. The picker option is bottom sheet with many input parameters.
*/
private fun openPickerOptions() {
val fragment = PickerOptionsBottomSheet.newInstance(pickerOptions)
fragment.setClickListener(this)
fragment.show(supportFragmentManager, PickerOptionsBottomSheet.BOTTOM_SHEET_TAG)
}
/**
* Once the picker options are selected in bottom sheet
* we will receive the latest picker options in this method
*/
override fun onPickerOptions(pickerOptions: PickerOptions) {
this.pickerOptions = pickerOptions
openImagePicker()
}
/**
* Open the image picker according to picker type and the ui options.
* The new system picker is only available for Android 13+.
*/
private fun openImagePicker() {
imagePicker
.title("My Picker")
.multipleSelection(pickerOptions.allowMultipleSelection, pickerOptions.maxPickCount)
.showCountInToolBar(pickerOptions.showCountInToolBar)
.showFolder(pickerOptions.showFolders)
.cameraIcon(pickerOptions.showCameraIconInGallery)
.doneIcon(pickerOptions.isDoneIcon)
.compressImage(true)
.allowCropping(false)
.maxImageSize(pickerOptions.maxPickSizeMB)
.extension(pickerOptions.pickExtension)
.aspectRatio(pickerOptions.aspectRatio)
if (isAtLeast11()) {
imagePicker.systemPicker(pickerOptions.openSystemPicker)
}
imagePicker.open(pickerOptions.pickerType)
}
/**
* Single Selection and the image captured from camera will be received in this method.
*/
override fun onImagePick(uri: Uri?) {
uri?.let { updateImageList(listOf(it)) }
}
/**
* Multiple Selection uris will be received in this method
*/
override fun onMultiImagePick(uris: List<Uri>?) {
if (!uris.isNullOrEmpty()) {
updateImageList(uris)
}
}
private fun updateImageList(list: List<Uri>) {
imageList.clear()
imageList.addAll(list)
imageDataAdapter.notifyDataSetChanged()
}
override fun onSaveInstanceState(outState: Bundle) {
outState.putParcelableArrayList(IMAGE_LIST, ArrayList(imageList))
super.onSaveInstanceState(outState)
}
private fun setUpToolbar() {
binding.toolbar.apply {
title = this@MainActivity.title.toString()
clickListener = View.OnClickListener {
onBackPressedDispatcher.onBackPressed()
}
}
}
}