Skip to content

Commit

Permalink
Merge pull request #13 from vedraj360/master
Browse files Browse the repository at this point in the history
Show file size
  • Loading branch information
FunkyMuse authored Mar 14, 2024
2 parents 64878d3 + f0e770f commit 3310f61
Show file tree
Hide file tree
Showing 24 changed files with 657 additions and 411 deletions.
282 changes: 115 additions & 167 deletions app/src/main/java/com/crazylegend/mediapicker/FragmentResult.kt

Large diffs are not rendered by default.

245 changes: 141 additions & 104 deletions app/src/main/java/com/crazylegend/mediapicker/MainActivity.kt

Large diffs are not rendered by default.

16 changes: 15 additions & 1 deletion core/src/main/java/com/crazylegend/core/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.crazylegend.core.modifiers.base.BaseMultiPickerModifier
import com.crazylegend.core.modifiers.base.BaseSinglePickerModifier
import kotlin.math.pow

/**
* Created by crazy on 5/8/20 to long live and prosper !
Expand Down Expand Up @@ -141,4 +142,17 @@ internal var Float.dp: Float
val metrics = Resources.getSystem().displayMetrics
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, this, metrics)
}
set(_) {}
set(_) {}

internal fun Int.bytesToFormattedString(): String {
return when {
this >= 1024.0.pow(3.0) -> {
val gigabytes = this / 1024.0.pow(3.0)
"%.2f GB".format(gigabytes)
}
else -> {
val megabytes = this / 1024.0.pow(2.0)
"%.2f MB".format(megabytes)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ import com.crazylegend.core.modifiers.base.BaseMultiPickerModifier
/**
* Created by crazy on 5/8/20 to long live and prosper !
*/
class MultiSelectAdapter(private val modifier: BaseMultiPickerModifier?) :
class MultiSelectAdapter(private val modifier: BaseMultiPickerModifier?, private val showFileSize: Boolean) :
ListAdapter<BaseCursorModel, MultiSelectViewHolder>(SingleDiffUtil()) {


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

override fun onBindViewHolder(holder: MultiSelectViewHolder, position: Int) {
val item = getItem(position)
holder.bind(item)
holder.bind(item, showFileSize)
holder.itemView.tag = item
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.crazylegend.core.adapters.multi

import androidx.core.view.isVisible
import com.crazylegend.core.adapters.BaseViewHolder
import com.crazylegend.core.bytesToFormattedString
import com.crazylegend.core.databinding.ItemviewImageBinding
import com.crazylegend.core.dto.BaseCursorModel
import com.crazylegend.core.modifiers.base.BaseMultiPickerModifier
Expand All @@ -24,8 +26,17 @@ class MultiSelectViewHolder(
modifier?.applyGravity(binding.selection)
}

fun bind(cursorModel: BaseCursorModel) {
fun bind(cursorModel: BaseCursorModel, showFileSize: Boolean) {
loadImage(binding.image, cursorModel.contentUri, viewHolderPlaceholderModifier)
if (showFileSize) {
modifier?.sizeTextModifier?.applyTextParams(binding.size)
modifier?.sizeTextModifier?.applyTextParamsConstraint(binding.size)
binding.size.isVisible = false
cursorModel.size?.let { size ->
binding.size.isVisible = size > 0
binding.size.text = size.bytesToFormattedString()
}
}
if (cursorModel.isSelected) {
setupSelectedImage(binding.selection, selectIconModifier)
} else {
Expand All @@ -34,4 +45,4 @@ class MultiSelectViewHolder(
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,25 @@ import androidx.recyclerview.widget.ListAdapter
import com.crazylegend.core.databinding.ItemviewImageBinding
import com.crazylegend.core.dto.BaseCursorModel
import com.crazylegend.core.inflater
import com.crazylegend.core.modifiers.SizeTextModifier
import com.crazylegend.core.modifiers.single.ImageModifier


/**
* Created by crazy on 5/8/20 to long live and prosper !
*/

open class SingleAdapter(private val viewHolderPlaceholderModifier: ImageModifier?,
open class SingleAdapter(private val showFileSize: Boolean,
private val viewHolderPlaceholderModifier: ImageModifier?,
private val sizeTextModifier: SizeTextModifier?,
private val onClick: (BaseCursorModel) -> Unit) : ListAdapter<BaseCursorModel, SingleViewHolder>(SingleDiffUtil()) {

override fun onCreateViewHolder(parent: ViewGroup, viewBaseCursorModelype: Int) =
SingleViewHolder(ItemviewImageBinding.inflate(parent.inflater, parent, false), viewHolderPlaceholderModifier, onClick)
SingleViewHolder(ItemviewImageBinding.inflate(parent.inflater, parent, false), viewHolderPlaceholderModifier, sizeTextModifier, onClick)

override fun onBindViewHolder(holder: SingleViewHolder, position: Int) {
val item = getItem(position)
holder.bind(item)
holder.bind(item, showFileSize)
holder.itemView.tag = item
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.crazylegend.core.adapters.single

import androidx.core.view.isVisible
import com.crazylegend.core.adapters.BaseViewHolder
import com.crazylegend.core.bytesToFormattedString
import com.crazylegend.core.databinding.ItemviewImageBinding
import com.crazylegend.core.dto.BaseCursorModel
import com.crazylegend.core.gone
import com.crazylegend.core.modifiers.SizeTextModifier
import com.crazylegend.core.modifiers.single.ImageModifier


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

class SingleViewHolder(private val binding: ItemviewImageBinding,
private val viewHolderPlaceholderModifier: ImageModifier?, onClick: (BaseCursorModel) -> Unit) :
private val viewHolderPlaceholderModifier: ImageModifier?, private val sizeTextModifier: SizeTextModifier?, onClick: (BaseCursorModel) -> Unit) :
BaseViewHolder(binding) {

fun bind(item: BaseCursorModel) {
fun bind(item: BaseCursorModel, showFileSize: Boolean) {
loadImage(binding.image, item.contentUri, viewHolderPlaceholderModifier)
if (showFileSize) {
sizeTextModifier?.applyTextParams(binding.size)
sizeTextModifier?.applyTextParamsConstraint(binding.size)
binding.size.isVisible = false
item.size?.let { size ->
binding.size.isVisible = size > 0
binding.size.text = size.bytesToFormattedString()
}
}
}


Expand Down
3 changes: 3 additions & 0 deletions core/src/main/java/com/crazylegend/core/dto/PickerConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.crazylegend.core.dto

data class PickerConfig(val showFileSize: Boolean = false)
130 changes: 130 additions & 0 deletions core/src/main/java/com/crazylegend/core/modifiers/SizeTextModifier.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package com.crazylegend.core.modifiers

import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Typeface
import android.os.Parcelable
import android.widget.TextView
import androidx.appcompat.widget.AppCompatTextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import androidx.core.view.setMargins
import androidx.core.view.setPadding
import androidx.core.view.updateLayoutParams
import kotlinx.parcelize.Parcelize


/**
* Created by crazy on 5/11/20 to long live and prosper !
*/


@Parcelize
data class SizeTextModifier(
var textColor: Int? = null,
var textPadding: Int? = null,
var textSize: Float? = null,
var startMargin: Int? = null,
var endMargin: Int? = null,
var marginTop: Int? = null,
var marginBottom: Int? = null,
var margin: Int? = null,
var backgroundColor: Int? = null,
var backgroundDrawable: Int? = null,
var textStyle: TextStyle = TextStyle.NORMAL,
var textAlignment: Int = TextView.TEXT_ALIGNMENT_VIEW_START
) : Parcelable {


private val allSizeMarginCondition get() = margin != null

enum class TextStyle {
BOLD, UNDERLINED, ITALIC, BOLD_ITALIC, NORMAL
}

private fun updateMargins(textView: AppCompatTextView) {
textView.updateLayoutParams<ConstraintLayout.LayoutParams> {
startMargin?.let { marginStart = it }
endMargin?.let { marginEnd = it }
marginTop?.let { topMargin = it }
marginBottom?.let { bottomMargin = it }
}
}

private fun updateMarginsConstraint(textView: AppCompatTextView) {
textView.updateLayoutParams<ConstraintLayout.LayoutParams> {
startMargin?.let { marginStart = it }
endMargin?.let { marginEnd = it }
marginTop?.let { topMargin = it }
marginBottom?.let { bottomMargin = it }
}
}

private fun updateAllMargins(textView: AppCompatTextView) {
textView.updateLayoutParams<ConstraintLayout.LayoutParams> {
margin?.let { setMargins(it) }
}
}

private fun updateAllMarginsConstraint(textView: AppCompatTextView) {
textView.updateLayoutParams<ConstraintLayout.LayoutParams> {
margin?.let { setMargins(it) }
}
}

fun applyTextParams(text: AppCompatTextView) {
textSize?.let { text.textSize = it }
textPadding?.let { text.setPadding(it) }
textColor?.let { text.setTextColor(it) }
text.textAlignment = textAlignment
backgroundColor?.let { text.setBackgroundColor(it) }
applyTextStyle(text)
if (allSizeMarginCondition) {
updateAllMargins(text)
} else {
updateMargins(text)
}
}

fun applyTextParamsConstraint(text: AppCompatTextView) {
textSize?.let { text.textSize = it }
textPadding?.let { text.setPadding(it) }
textColor?.let { text.setTextColor(it) }
text.textAlignment = textAlignment
backgroundDrawable?.let { text.background = ContextCompat.getDrawable(text.context, it) }
?: run {
text.backgroundTintList = ColorStateList.valueOf(Color.BLACK)
}
applyTextStyle(text)
if (allSizeMarginCondition) {
updateAllMarginsConstraint(text)
} else {
updateMarginsConstraint(text)
}
}

private fun applyTextStyle(text: AppCompatTextView) {
when (textStyle) {
TextStyle.BOLD -> {
text.setTypeface(text.typeface, Typeface.BOLD)
}

TextStyle.UNDERLINED -> {
text.paintFlags = text.paintFlags or Paint.UNDERLINE_TEXT_FLAG
}

TextStyle.ITALIC -> {
text.setTypeface(text.typeface, Typeface.ITALIC)
}

TextStyle.BOLD_ITALIC -> {
text.setTypeface(text.typeface, Typeface.BOLD_ITALIC)
}

else -> {

}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ import androidx.core.view.marginBottom
import androidx.core.view.marginLeft
import androidx.core.view.marginRight
import androidx.core.view.marginTop
import com.crazylegend.core.*
import com.crazylegend.core.bottom
import com.crazylegend.core.constrainBottomToTopOf
import com.crazylegend.core.left
import com.crazylegend.core.modifiers.SizeTextModifier
import com.crazylegend.core.modifiers.TitleTextModifier
import com.crazylegend.core.modifiers.multi.DoneButtonModifier
import com.crazylegend.core.modifiers.multi.SelectIconModifier
import com.crazylegend.core.modifiers.single.ImageModifier
import com.crazylegend.core.right
import com.crazylegend.core.top
import kotlinx.parcelize.Parcelize


Expand All @@ -28,7 +33,8 @@ open class BaseMultiPickerModifier(
open var indicatorsGravity: Gravity = Gravity.BOTTOM_RIGHT,
open val viewHolderPlaceholderModifier: ImageModifier = ImageModifier(),
open val noContentTextModifier: TitleTextModifier = TitleTextModifier(),
open var loadingIndicatorTint: Int? = null
open var loadingIndicatorTint: Int? = null,
open val sizeTextModifier: SizeTextModifier = SizeTextModifier(),
) : Parcelable {

enum class Gravity {
Expand All @@ -42,7 +48,8 @@ open class BaseMultiPickerModifier(
unSelectIconModifications: SelectIconModifier.() -> Unit = {},
viewHolderPlaceholderModifications: ImageModifier.() -> Unit = {},
gravityForSelectAndUnSelectIndicators: Gravity = Gravity.BOTTOM_RIGHT,
tintForLoadingProgressBar: Int? = null
tintForLoadingProgressBar: Int? = null,
sizeTextModifications: SizeTextModifier.() -> Unit = {},
) {
doneButtonModifier.doneButtonModifications()
titleTextModifier.titleModifications()
Expand All @@ -51,6 +58,7 @@ open class BaseMultiPickerModifier(
viewHolderPlaceholderModifier.viewHolderPlaceholderModifications()
indicatorsGravity = gravityForSelectAndUnSelectIndicators
loadingIndicatorTint = tintForLoadingProgressBar
sizeTextModifier.sizeTextModifications()
}

fun applyGravity(imageView: AppCompatImageView) {
Expand All @@ -59,14 +67,17 @@ open class BaseMultiPickerModifier(
imageView.top(imageView.marginTop)
imageView.left(imageView.marginLeft)
}

Gravity.TOP_RIGHT -> {
imageView.top(imageView.marginTop)
imageView.right(imageView.marginRight)
}

Gravity.BOTTOM_LEFT -> {
imageView.bottom(imageView.marginBottom)
imageView.left(imageView.marginLeft)
}

Gravity.BOTTOM_RIGHT -> {
imageView.bottom(imageView.marginBottom)
imageView.right(imageView.marginRight)
Expand All @@ -80,14 +91,17 @@ open class BaseMultiPickerModifier(
imageView.top(imageView.marginTop)
imageView.left(imageView.marginLeft)
}

Gravity.TOP_RIGHT -> {
imageView.top(imageView.marginTop)
imageView.right(imageView.marginRight)
}

Gravity.BOTTOM_LEFT -> {
imageView.constrainBottomToTopOf(view, imageView.marginBottom)
imageView.left(imageView.marginLeft)
}

Gravity.BOTTOM_RIGHT -> {
imageView.constrainBottomToTopOf(view, imageView.marginBottom)
imageView.right(imageView.marginRight)
Expand Down
Loading

0 comments on commit 3310f61

Please sign in to comment.