Skip to content

Commit 3310f61

Browse files
authored
Merge pull request #13 from vedraj360/master
Show file size
2 parents 64878d3 + f0e770f commit 3310f61

File tree

24 files changed

+657
-411
lines changed

24 files changed

+657
-411
lines changed

app/src/main/java/com/crazylegend/mediapicker/FragmentResult.kt

Lines changed: 115 additions & 167 deletions
Large diffs are not rendered by default.

app/src/main/java/com/crazylegend/mediapicker/MainActivity.kt

Lines changed: 141 additions & 104 deletions
Large diffs are not rendered by default.

core/src/main/java/com/crazylegend/core/Extensions.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import com.bumptech.glide.request.RequestListener
1919
import com.bumptech.glide.request.target.Target
2020
import com.crazylegend.core.modifiers.base.BaseMultiPickerModifier
2121
import com.crazylegend.core.modifiers.base.BaseSinglePickerModifier
22+
import kotlin.math.pow
2223

2324
/**
2425
* Created by crazy on 5/8/20 to long live and prosper !
@@ -141,4 +142,17 @@ internal var Float.dp: Float
141142
val metrics = Resources.getSystem().displayMetrics
142143
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, this, metrics)
143144
}
144-
set(_) {}
145+
set(_) {}
146+
147+
internal fun Int.bytesToFormattedString(): String {
148+
return when {
149+
this >= 1024.0.pow(3.0) -> {
150+
val gigabytes = this / 1024.0.pow(3.0)
151+
"%.2f GB".format(gigabytes)
152+
}
153+
else -> {
154+
val megabytes = this / 1024.0.pow(2.0)
155+
"%.2f MB".format(megabytes)
156+
}
157+
}
158+
}

core/src/main/java/com/crazylegend/core/adapters/multi/MultiSelectAdapter.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ import com.crazylegend.core.modifiers.base.BaseMultiPickerModifier
1212
/**
1313
* Created by crazy on 5/8/20 to long live and prosper !
1414
*/
15-
class MultiSelectAdapter(private val modifier: BaseMultiPickerModifier?) :
15+
class MultiSelectAdapter(private val modifier: BaseMultiPickerModifier?, private val showFileSize: Boolean) :
1616
ListAdapter<BaseCursorModel, MultiSelectViewHolder>(SingleDiffUtil()) {
1717

18-
1918
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MultiSelectViewHolder {
2019
val holder = MultiSelectViewHolder(ItemviewImageBinding.inflate(parent.inflater, parent, false), modifier)
2120
holder.itemView.setOnClickListener {
@@ -29,7 +28,7 @@ class MultiSelectAdapter(private val modifier: BaseMultiPickerModifier?) :
2928

3029
override fun onBindViewHolder(holder: MultiSelectViewHolder, position: Int) {
3130
val item = getItem(position)
32-
holder.bind(item)
31+
holder.bind(item, showFileSize)
3332
holder.itemView.tag = item
3433
}
3534

core/src/main/java/com/crazylegend/core/adapters/multi/MultiSelectViewHolder.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.crazylegend.core.adapters.multi
22

3+
import androidx.core.view.isVisible
34
import com.crazylegend.core.adapters.BaseViewHolder
5+
import com.crazylegend.core.bytesToFormattedString
46
import com.crazylegend.core.databinding.ItemviewImageBinding
57
import com.crazylegend.core.dto.BaseCursorModel
68
import com.crazylegend.core.modifiers.base.BaseMultiPickerModifier
@@ -24,8 +26,17 @@ class MultiSelectViewHolder(
2426
modifier?.applyGravity(binding.selection)
2527
}
2628

27-
fun bind(cursorModel: BaseCursorModel) {
29+
fun bind(cursorModel: BaseCursorModel, showFileSize: Boolean) {
2830
loadImage(binding.image, cursorModel.contentUri, viewHolderPlaceholderModifier)
31+
if (showFileSize) {
32+
modifier?.sizeTextModifier?.applyTextParams(binding.size)
33+
modifier?.sizeTextModifier?.applyTextParamsConstraint(binding.size)
34+
binding.size.isVisible = false
35+
cursorModel.size?.let { size ->
36+
binding.size.isVisible = size > 0
37+
binding.size.text = size.bytesToFormattedString()
38+
}
39+
}
2940
if (cursorModel.isSelected) {
3041
setupSelectedImage(binding.selection, selectIconModifier)
3142
} else {
@@ -34,4 +45,4 @@ class MultiSelectViewHolder(
3445
}
3546

3647

37-
}
48+
}

core/src/main/java/com/crazylegend/core/adapters/single/SingleAdapter.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,25 @@ import androidx.recyclerview.widget.ListAdapter
55
import com.crazylegend.core.databinding.ItemviewImageBinding
66
import com.crazylegend.core.dto.BaseCursorModel
77
import com.crazylegend.core.inflater
8+
import com.crazylegend.core.modifiers.SizeTextModifier
89
import com.crazylegend.core.modifiers.single.ImageModifier
910

1011

1112
/**
1213
* Created by crazy on 5/8/20 to long live and prosper !
1314
*/
1415

15-
open class SingleAdapter(private val viewHolderPlaceholderModifier: ImageModifier?,
16+
open class SingleAdapter(private val showFileSize: Boolean,
17+
private val viewHolderPlaceholderModifier: ImageModifier?,
18+
private val sizeTextModifier: SizeTextModifier?,
1619
private val onClick: (BaseCursorModel) -> Unit) : ListAdapter<BaseCursorModel, SingleViewHolder>(SingleDiffUtil()) {
20+
1721
override fun onCreateViewHolder(parent: ViewGroup, viewBaseCursorModelype: Int) =
18-
SingleViewHolder(ItemviewImageBinding.inflate(parent.inflater, parent, false), viewHolderPlaceholderModifier, onClick)
22+
SingleViewHolder(ItemviewImageBinding.inflate(parent.inflater, parent, false), viewHolderPlaceholderModifier, sizeTextModifier, onClick)
1923

2024
override fun onBindViewHolder(holder: SingleViewHolder, position: Int) {
2125
val item = getItem(position)
22-
holder.bind(item)
26+
holder.bind(item, showFileSize)
2327
holder.itemView.tag = item
2428
}
2529

core/src/main/java/com/crazylegend/core/adapters/single/SingleViewHolder.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.crazylegend.core.adapters.single
22

3+
import androidx.core.view.isVisible
34
import com.crazylegend.core.adapters.BaseViewHolder
5+
import com.crazylegend.core.bytesToFormattedString
46
import com.crazylegend.core.databinding.ItemviewImageBinding
57
import com.crazylegend.core.dto.BaseCursorModel
68
import com.crazylegend.core.gone
9+
import com.crazylegend.core.modifiers.SizeTextModifier
710
import com.crazylegend.core.modifiers.single.ImageModifier
811

912

@@ -12,11 +15,20 @@ import com.crazylegend.core.modifiers.single.ImageModifier
1215
*/
1316

1417
class SingleViewHolder(private val binding: ItemviewImageBinding,
15-
private val viewHolderPlaceholderModifier: ImageModifier?, onClick: (BaseCursorModel) -> Unit) :
18+
private val viewHolderPlaceholderModifier: ImageModifier?, private val sizeTextModifier: SizeTextModifier?, onClick: (BaseCursorModel) -> Unit) :
1619
BaseViewHolder(binding) {
1720

18-
fun bind(item: BaseCursorModel) {
21+
fun bind(item: BaseCursorModel, showFileSize: Boolean) {
1922
loadImage(binding.image, item.contentUri, viewHolderPlaceholderModifier)
23+
if (showFileSize) {
24+
sizeTextModifier?.applyTextParams(binding.size)
25+
sizeTextModifier?.applyTextParamsConstraint(binding.size)
26+
binding.size.isVisible = false
27+
item.size?.let { size ->
28+
binding.size.isVisible = size > 0
29+
binding.size.text = size.bytesToFormattedString()
30+
}
31+
}
2032
}
2133

2234

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.crazylegend.core.dto
2+
3+
data class PickerConfig(val showFileSize: Boolean = false)
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package com.crazylegend.core.modifiers
2+
3+
import android.content.res.ColorStateList
4+
import android.graphics.Color
5+
import android.graphics.Paint
6+
import android.graphics.Typeface
7+
import android.os.Parcelable
8+
import android.widget.TextView
9+
import androidx.appcompat.widget.AppCompatTextView
10+
import androidx.constraintlayout.widget.ConstraintLayout
11+
import androidx.core.content.ContextCompat
12+
import androidx.core.view.setMargins
13+
import androidx.core.view.setPadding
14+
import androidx.core.view.updateLayoutParams
15+
import kotlinx.parcelize.Parcelize
16+
17+
18+
/**
19+
* Created by crazy on 5/11/20 to long live and prosper !
20+
*/
21+
22+
23+
@Parcelize
24+
data class SizeTextModifier(
25+
var textColor: Int? = null,
26+
var textPadding: Int? = null,
27+
var textSize: Float? = null,
28+
var startMargin: Int? = null,
29+
var endMargin: Int? = null,
30+
var marginTop: Int? = null,
31+
var marginBottom: Int? = null,
32+
var margin: Int? = null,
33+
var backgroundColor: Int? = null,
34+
var backgroundDrawable: Int? = null,
35+
var textStyle: TextStyle = TextStyle.NORMAL,
36+
var textAlignment: Int = TextView.TEXT_ALIGNMENT_VIEW_START
37+
) : Parcelable {
38+
39+
40+
private val allSizeMarginCondition get() = margin != null
41+
42+
enum class TextStyle {
43+
BOLD, UNDERLINED, ITALIC, BOLD_ITALIC, NORMAL
44+
}
45+
46+
private fun updateMargins(textView: AppCompatTextView) {
47+
textView.updateLayoutParams<ConstraintLayout.LayoutParams> {
48+
startMargin?.let { marginStart = it }
49+
endMargin?.let { marginEnd = it }
50+
marginTop?.let { topMargin = it }
51+
marginBottom?.let { bottomMargin = it }
52+
}
53+
}
54+
55+
private fun updateMarginsConstraint(textView: AppCompatTextView) {
56+
textView.updateLayoutParams<ConstraintLayout.LayoutParams> {
57+
startMargin?.let { marginStart = it }
58+
endMargin?.let { marginEnd = it }
59+
marginTop?.let { topMargin = it }
60+
marginBottom?.let { bottomMargin = it }
61+
}
62+
}
63+
64+
private fun updateAllMargins(textView: AppCompatTextView) {
65+
textView.updateLayoutParams<ConstraintLayout.LayoutParams> {
66+
margin?.let { setMargins(it) }
67+
}
68+
}
69+
70+
private fun updateAllMarginsConstraint(textView: AppCompatTextView) {
71+
textView.updateLayoutParams<ConstraintLayout.LayoutParams> {
72+
margin?.let { setMargins(it) }
73+
}
74+
}
75+
76+
fun applyTextParams(text: AppCompatTextView) {
77+
textSize?.let { text.textSize = it }
78+
textPadding?.let { text.setPadding(it) }
79+
textColor?.let { text.setTextColor(it) }
80+
text.textAlignment = textAlignment
81+
backgroundColor?.let { text.setBackgroundColor(it) }
82+
applyTextStyle(text)
83+
if (allSizeMarginCondition) {
84+
updateAllMargins(text)
85+
} else {
86+
updateMargins(text)
87+
}
88+
}
89+
90+
fun applyTextParamsConstraint(text: AppCompatTextView) {
91+
textSize?.let { text.textSize = it }
92+
textPadding?.let { text.setPadding(it) }
93+
textColor?.let { text.setTextColor(it) }
94+
text.textAlignment = textAlignment
95+
backgroundDrawable?.let { text.background = ContextCompat.getDrawable(text.context, it) }
96+
?: run {
97+
text.backgroundTintList = ColorStateList.valueOf(Color.BLACK)
98+
}
99+
applyTextStyle(text)
100+
if (allSizeMarginCondition) {
101+
updateAllMarginsConstraint(text)
102+
} else {
103+
updateMarginsConstraint(text)
104+
}
105+
}
106+
107+
private fun applyTextStyle(text: AppCompatTextView) {
108+
when (textStyle) {
109+
TextStyle.BOLD -> {
110+
text.setTypeface(text.typeface, Typeface.BOLD)
111+
}
112+
113+
TextStyle.UNDERLINED -> {
114+
text.paintFlags = text.paintFlags or Paint.UNDERLINE_TEXT_FLAG
115+
}
116+
117+
TextStyle.ITALIC -> {
118+
text.setTypeface(text.typeface, Typeface.ITALIC)
119+
}
120+
121+
TextStyle.BOLD_ITALIC -> {
122+
text.setTypeface(text.typeface, Typeface.BOLD_ITALIC)
123+
}
124+
125+
else -> {
126+
127+
}
128+
}
129+
}
130+
}

core/src/main/java/com/crazylegend/core/modifiers/base/BaseMultiPickerModifier.kt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ import androidx.core.view.marginBottom
77
import androidx.core.view.marginLeft
88
import androidx.core.view.marginRight
99
import androidx.core.view.marginTop
10-
import com.crazylegend.core.*
10+
import com.crazylegend.core.bottom
11+
import com.crazylegend.core.constrainBottomToTopOf
12+
import com.crazylegend.core.left
13+
import com.crazylegend.core.modifiers.SizeTextModifier
1114
import com.crazylegend.core.modifiers.TitleTextModifier
1215
import com.crazylegend.core.modifiers.multi.DoneButtonModifier
1316
import com.crazylegend.core.modifiers.multi.SelectIconModifier
1417
import com.crazylegend.core.modifiers.single.ImageModifier
18+
import com.crazylegend.core.right
19+
import com.crazylegend.core.top
1520
import kotlinx.parcelize.Parcelize
1621

1722

@@ -28,7 +33,8 @@ open class BaseMultiPickerModifier(
2833
open var indicatorsGravity: Gravity = Gravity.BOTTOM_RIGHT,
2934
open val viewHolderPlaceholderModifier: ImageModifier = ImageModifier(),
3035
open val noContentTextModifier: TitleTextModifier = TitleTextModifier(),
31-
open var loadingIndicatorTint: Int? = null
36+
open var loadingIndicatorTint: Int? = null,
37+
open val sizeTextModifier: SizeTextModifier = SizeTextModifier(),
3238
) : Parcelable {
3339

3440
enum class Gravity {
@@ -42,7 +48,8 @@ open class BaseMultiPickerModifier(
4248
unSelectIconModifications: SelectIconModifier.() -> Unit = {},
4349
viewHolderPlaceholderModifications: ImageModifier.() -> Unit = {},
4450
gravityForSelectAndUnSelectIndicators: Gravity = Gravity.BOTTOM_RIGHT,
45-
tintForLoadingProgressBar: Int? = null
51+
tintForLoadingProgressBar: Int? = null,
52+
sizeTextModifications: SizeTextModifier.() -> Unit = {},
4653
) {
4754
doneButtonModifier.doneButtonModifications()
4855
titleTextModifier.titleModifications()
@@ -51,6 +58,7 @@ open class BaseMultiPickerModifier(
5158
viewHolderPlaceholderModifier.viewHolderPlaceholderModifications()
5259
indicatorsGravity = gravityForSelectAndUnSelectIndicators
5360
loadingIndicatorTint = tintForLoadingProgressBar
61+
sizeTextModifier.sizeTextModifications()
5462
}
5563

5664
fun applyGravity(imageView: AppCompatImageView) {
@@ -59,14 +67,17 @@ open class BaseMultiPickerModifier(
5967
imageView.top(imageView.marginTop)
6068
imageView.left(imageView.marginLeft)
6169
}
70+
6271
Gravity.TOP_RIGHT -> {
6372
imageView.top(imageView.marginTop)
6473
imageView.right(imageView.marginRight)
6574
}
75+
6676
Gravity.BOTTOM_LEFT -> {
6777
imageView.bottom(imageView.marginBottom)
6878
imageView.left(imageView.marginLeft)
6979
}
80+
7081
Gravity.BOTTOM_RIGHT -> {
7182
imageView.bottom(imageView.marginBottom)
7283
imageView.right(imageView.marginRight)
@@ -80,14 +91,17 @@ open class BaseMultiPickerModifier(
8091
imageView.top(imageView.marginTop)
8192
imageView.left(imageView.marginLeft)
8293
}
94+
8395
Gravity.TOP_RIGHT -> {
8496
imageView.top(imageView.marginTop)
8597
imageView.right(imageView.marginRight)
8698
}
99+
87100
Gravity.BOTTOM_LEFT -> {
88101
imageView.constrainBottomToTopOf(view, imageView.marginBottom)
89102
imageView.left(imageView.marginLeft)
90103
}
104+
91105
Gravity.BOTTOM_RIGHT -> {
92106
imageView.constrainBottomToTopOf(view, imageView.marginBottom)
93107
imageView.right(imageView.marginRight)

0 commit comments

Comments
 (0)