-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathMainActivity.kt
367 lines (323 loc) · 16.2 KB
/
MainActivity.kt
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package com.crazylegend.mediapicker
import android.Manifest
import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.TextView
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.bumptech.glide.Glide
import com.crazylegend.audiopicker.audios.AudioModel
import com.crazylegend.audiopicker.pickers.MultiAudioPicker
import com.crazylegend.audiopicker.pickers.SingleAudioPicker
import com.crazylegend.core.dto.PickerConfig
import com.crazylegend.core.modifiers.SizeTextModifier
import com.crazylegend.core.modifiers.TitleTextModifier
import com.crazylegend.core.modifiers.base.BaseMultiPickerModifier
import com.crazylegend.imagepicker.images.ImageModel
import com.crazylegend.imagepicker.pickers.MultiImagePicker
import com.crazylegend.imagepicker.pickers.SingleImagePicker
import com.crazylegend.mediapicker.databinding.ActivityMainBinding
import com.crazylegend.videopicker.pickers.MultiVideoPicker
import com.crazylegend.videopicker.pickers.SingleVideoPicker
import com.crazylegend.videopicker.videos.VideoModel
@SuppressLint("MissingPermission")
class MainActivity : AppCompatActivity(), View.OnClickListener {
private lateinit var binding: ActivityMainBinding
private var clickedID = R.id.singleImageBottomSheetPick
private val askForStoragePermission =
registerForActivityResult(ActivityResultContracts.RequestPermission()) {
if (it) {
when (clickedID) {
//images
R.id.singleImageBottomSheetPick -> {
showSingleImageBottomSheetPicker()
}
R.id.imageBottomSheetMultiPick -> {
showImageMultiBottomSheetPicker()
}
//videos
R.id.singleVideoBottomSheetPick -> {
showSingleVideoBottomSheetPicker()
}
R.id.videoBottomSheetMultiPick -> {
showVideoMultiBottomSheetPicker()
}
//audios
R.id.singleAudioBottomSheetPick -> {
showSingleAudioBottomSheetPicker()
}
R.id.audioBottomSheetMultiPick -> {
showAudioMultiBottomSheetPicker()
}
//fragment result
R.id.launchFragmentResult -> {
val dialog = FragmentResult()
dialog.show(supportFragmentManager, FragmentResult::class.java.name)
}
}
} else {
Log.e("PERMISSION", "NOT ALLOWED!")
}
}
//audios
private fun showSingleAudioBottomSheetPicker() {
SingleAudioPicker.showPicker(this, {
setupViewHolderTitleText {
textColor = Color.BLACK
textPadding = 10 // use dp or sp this is only for demonstration purposes
}
setupBaseModifier(loadingIndicatorColor = R.color.minusColor, titleTextModifications = {
textAlignment = TextView.TEXT_ALIGNMENT_VIEW_START
textStyle = TitleTextModifier.TextStyle.ITALIC
textColor = Color.BLACK
marginBottom = 30 // use dp or sp this is only for demonstration purposes
textPadding = 5 // use dp or sp this is only for demonstration purposes
textSize = 30f // use sp this is only for demonstration purposes
textString = "Pick an audio"
}, placeHolderModifications = {
resID = R.drawable.ic_image
})
}, ::loadAudio)
}
private fun showAudioMultiBottomSheetPicker() {
MultiAudioPicker.showPicker(this, {
setupViewHolderTitleText {
textColor = ContextCompat.getColor(this@MainActivity, R.color.colorPrimaryDark)
textStyle = TitleTextModifier.TextStyle.BOLD
textPadding = 10 // use dp or sp this is only for demonstration purposes
}
setupBaseMultiPicker(tintForLoadingProgressBar = ContextCompat.getColor(
this@MainActivity, R.color.colorPrimaryDark
),
gravityForSelectAndUnSelectIndicators = BaseMultiPickerModifier.Gravity.BOTTOM_LEFT,
titleModifications = {
textAlignment = TextView.TEXT_ALIGNMENT_VIEW_START
textStyle = TitleTextModifier.TextStyle.ITALIC
textColor = Color.BLACK
marginBottom = 30 // use dp or sp this is only for demonstration purposes
textPadding = 5 // use dp or sp this is only for demonstration purposes
textSize = 30f // use sp this is only for demonstration purposes
textString = "Pick multiple songs"
},
selectIconModifications = {
resID = R.drawable.ic_checked
tint = Color.BLACK
},
unSelectIconModifications = {
resID = R.drawable.ic_unchecked
tint = Color.BLACK
},
viewHolderPlaceholderModifications = {
resID = R.drawable.ic_album_second
})
}, ::doSomethingWithAudioList)
}
private fun loadAudio(audioModel: AudioModel) {
Glide.with(this).load(audioModel.loadThumbnail(contentResolver)).error(R.drawable.ic_album)
.into(binding.audio)
binding.audioTitle.text = audioModel.displayName
Log.d("AUDIO_PICKED ${audioModel.thumbnail?.isRecycled}", audioModel.toString())
}
private fun doSomethingWithAudioList(list: List<AudioModel>) {
list.forEach {
Log.d("AUDIO LIST size ${list.size}", it.toString())
}
}
//videos
private fun showVideoMultiBottomSheetPicker() {
MultiVideoPicker.showPicker(this, extensions = arrayOf(), pickerConfig = PickerConfig(showFileSize = true), {
setupBaseMultiPicker(tintForLoadingProgressBar = ContextCompat.getColor(
this@MainActivity, R.color.colorPrimaryDark
),
gravityForSelectAndUnSelectIndicators = BaseMultiPickerModifier.Gravity.TOP_RIGHT,
titleModifications = {
textAlignment = TextView.TEXT_ALIGNMENT_CENTER
textStyle = TitleTextModifier.TextStyle.ITALIC
textColor = Color.BLACK
marginBottom = 30 // use dp or sp this is only for demonstration purposes
textPadding = 5 // use dp or sp this is only for demonstration purposes
textSize = 30f // use sp this is only for demonstration purposes
textString = "Pick multiple videos"
},
selectIconModifications = {
resID = R.drawable.ic_checked
tint = Color.BLACK
},
unSelectIconModifications = {
resID = R.drawable.ic_unchecked
tint = Color.BLACK
},
viewHolderPlaceholderModifications = {
resID = R.drawable.ic_close
},
sizeTextModifications = {
textAlignment = TextView.TEXT_ALIGNMENT_CENTER
textStyle = SizeTextModifier.TextStyle.BOLD
margin = 22 // use dp or sp this is only for demonstration purposes
textPadding = 8 // use dp or sp this is only for demonstration purposes
textSize = 10f // use sp this is only for demonstration purposes
}
)
}, ::doSomethingWithVideoList)
}
private fun showSingleVideoBottomSheetPicker() {
//SingleVideoPicker.showPicker(context = this, onPickedVideo = ::loadVideo)
SingleVideoPicker.showPicker(this, extensions = arrayOf(), pickerConfig = PickerConfig(showFileSize = true), {
setupBaseModifier(loadingIndicatorColor = R.color.minusColor, titleTextModifications = {
textAlignment = TextView.TEXT_ALIGNMENT_VIEW_START
textStyle = TitleTextModifier.TextStyle.ITALIC
textColor = Color.BLACK
marginBottom = 30 // use dp or sp this is only for demonstration purposes
textPadding = 5 // use dp or sp this is only for demonstration purposes
textSize = 30f // use sp this is only for demonstration purposes
textString = "Pick a video"
}, placeHolderModifications = {
resID = R.drawable.ic_image
}, sizeTextModifications = {
textAlignment = TextView.TEXT_ALIGNMENT_VIEW_START
textStyle = SizeTextModifier.TextStyle.NORMAL
margin = 22 // use dp or sp this is only for demonstration purposes
textColor = Color.BLACK
textPadding = 5 // use dp or sp this is only for demonstration purposes
textSize = 12f // use sp this is only for demonstration purposes
backgroundDrawable = R.drawable.rounded_bg_abstract_dialog
}
)
}, ::loadVideo)
}
private fun loadVideo(videoModel: VideoModel) {
Glide.with(this).load(videoModel.contentUri).into(binding.video)
Log.d("VIDEO_PICKED", videoModel.toString())
}
private fun doSomethingWithVideoList(list: List<VideoModel>) {
list.forEach {
Log.d("VIDEO LIST size ${list.size}", it.toString())
}
}
//images
private fun showImageMultiBottomSheetPicker() {
MultiImagePicker.showPicker(this, extensions = arrayOf(), pickerConfig = PickerConfig(showFileSize = true), {
setupBaseMultiPicker(tintForLoadingProgressBar = ContextCompat.getColor(
this@MainActivity, R.color.colorPrimaryDark
),
gravityForSelectAndUnSelectIndicators = BaseMultiPickerModifier.Gravity.TOP_LEFT,
titleModifications = {
textAlignment = TextView.TEXT_ALIGNMENT_VIEW_START
textStyle = TitleTextModifier.TextStyle.BOLD_ITALIC
textColor = Color.BLACK
marginBottom = 30 // use dp or sp this is only for demonstration purposes
textPadding = 5 // use dp or sp this is only for demonstration purposes
textSize = 30f // use sp this is only for demonstration purposes
textString = "Pick multiple images"
},
selectIconModifications = {
resID = R.drawable.ic_checked
tint = Color.BLACK
},
unSelectIconModifications = {
resID = R.drawable.ic_unchecked
tint = Color.BLACK
},
viewHolderPlaceholderModifications = {
resID = R.drawable.ic_image
},
sizeTextModifications = {
textAlignment = TextView.TEXT_ALIGNMENT_VIEW_START
textStyle = SizeTextModifier.TextStyle.NORMAL
margin = 22 // use dp or sp this is only for demonstration purposes
textColor = Color.GREEN
textPadding = 6 // use dp or sp this is only for demonstration purposes
textSize = 12f // use sp this is only for demonstration purposes
}
)
}) { list ->
doSomethingWithImageList(list)
}
}
private fun showSingleImageBottomSheetPicker() {
SingleImagePicker.showPicker(this, extensions = arrayOf(), pickerConfig = PickerConfig(showFileSize = true), {
loadingIndicatorTint =
ContextCompat.getColor(this@MainActivity, R.color.colorPrimaryDark)
titleTextModifier.apply {
textAlignment = TextView.TEXT_ALIGNMENT_VIEW_START
textStyle = TitleTextModifier.TextStyle.BOLD_ITALIC
margin = 22 // use dp or sp this is only for demonstration purposes
textColor = Color.BLACK
textPadding = 5 // use dp or sp this is only for demonstration purposes
textSize = 20f // use sp this is only for demonstration purposes
textString = "Select an image"
}
noContentTextModifier.apply {
textAlignment = TextView.TEXT_ALIGNMENT_CENTER
textStyle = TitleTextModifier.TextStyle.BOLD_ITALIC
margin = 22 // use dp or sp this is only for demonstration purposes
textColor = Color.RED
textPadding = 5 // use dp or sp this is only for demonstration purposes
textSize = 20f // use sp this is only for demonstration purposes
}
viewHolderPlaceholderModifier.apply {
resID = R.drawable.ic_image
}
sizeTextModifier.apply {
textAlignment = TextView.TEXT_ALIGNMENT_VIEW_START
textStyle = SizeTextModifier.TextStyle.NORMAL
margin = 22 // use dp or sp this is only for demonstration purposes
textColor = Color.BLACK
textPadding = 5 // use dp or sp this is only for demonstration purposes
textSize = 12f // use sp this is only for demonstration purposes
backgroundDrawable = R.drawable.rounded_bg_abstract_dialog
}
}, ::loadImage)
}
private fun loadImage(imageModel: ImageModel) {
Glide.with(this).load(imageModel.contentUri).into(binding.image)
Log.d("IMAGE_PICKED", imageModel.toString())
}
private fun doSomethingWithImageList(list: List<ImageModel>) {
list.forEach {
Log.d("IMAGE LIST size ${list.size}", it.toString())
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
//images
binding.singleImageBottomSheetPick.setOnClickListener(this)
binding.imageBottomSheetMultiPick.setOnClickListener(this)
//videos
binding.singleVideoBottomSheetPick.setOnClickListener(this)
binding.videoBottomSheetMultiPick.setOnClickListener(this)
//audios
binding.singleAudioBottomSheetPick.setOnClickListener(this)
binding.audioBottomSheetMultiPick.setOnClickListener(this)
//fragment result
binding.launchFragmentResult.setOnClickListener(this)
}
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState)
//images
MultiImagePicker.restoreListener(this, ::doSomethingWithImageList)
SingleImagePicker.restoreListener(this, ::loadImage)
//videos
MultiVideoPicker.restoreListener(this, ::doSomethingWithVideoList)
SingleVideoPicker.restoreListener(this, ::loadVideo)
//audios
MultiAudioPicker.restoreListener(this, ::doSomethingWithAudioList)
SingleAudioPicker.restoreListener(this, ::loadAudio)
}
override fun onClick(clickedview: View?) {
clickedview ?: return
clickedID = clickedview.id
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
askForStoragePermission.launch(Manifest.permission.READ_EXTERNAL_STORAGE)
} else {
askForStoragePermission.launch(Manifest.permission.READ_MEDIA_IMAGES)
}
}
}