Skip to content

Commit

Permalink
Merge pull request #41 from Trendyol/feature/collapsedquantitypicker
Browse files Browse the repository at this point in the history
Add collapsed state to QuantityPickerView
  • Loading branch information
MertNYuksel authored May 6, 2020
2 parents 54d4f50 + 829ab68 commit 1db34f4
Show file tree
Hide file tree
Showing 7 changed files with 259 additions and 77 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.trendyol.uicomponents.quantitypickerview

import android.content.Context
import android.os.Build
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
Expand Down Expand Up @@ -41,19 +42,40 @@ class QuantityPickerView : ConstraintLayout {
this,
true
)
setUpView()
}
setUpView()
}

fun setQuantityPickerViewState(quantityPickerViewState: QuantityPickerViewState?) =
fun setQuantityPickerViewState(quantityPickerViewState: QuantityPickerViewState?) {
if (isInEditMode || quantityPickerViewState == null) return
bindRootViewProperties(quantityPickerViewState)
with(binding) {
viewState = quantityPickerViewState
executePendingBindings()
}
}

private fun bindRootViewProperties(quantityPickerViewState: QuantityPickerViewState) {
setBackground(quantityPickerViewState.backgroundDrawable)

val horizontalPadding = quantityPickerViewState.getRootViewHorizontalPadding(context)
setPadding(
horizontalPadding,
paddingTop,
horizontalPadding,
paddingBottom
)
}

fun setQuantity(quantity: Int) =
setQuantityPickerViewState(binding.viewState?.getWithQuantity(quantity))

fun incrementQuantityBy(quantity: Int) {
val viewState = binding.viewState ?: return
val updatedViewState = viewState.getWithIncrementQuantity(quantity)
setQuantityPickerViewState(updatedViewState)
}

fun stopLoading() = setQuantityPickerViewState(binding.viewState?.stopLoading())

fun reset() = setQuantityPickerViewState(binding.viewState?.reset())
Expand All @@ -65,18 +87,28 @@ class QuantityPickerView : ConstraintLayout {

setQuantityPickerViewState(
if (onAddClicked?.invoke(viewState?.currentQuantity ?: 0) == true) {
viewState?.getWithLoading(increment = true)
viewState?.getWithLoading()
} else {
viewState?.getWithAddValue()
}
)
}
quantityText.setOnClickListener {
val showLoading = onAddClicked?.invoke(viewState?.currentQuantity ?: 0) == true
val updatedViewState = if (showLoading) {
viewState?.getWithLoading()
} else {
viewState?.getWithAddValue()
}
setQuantityPickerViewState(updatedViewState)

}
imageSubtract.setOnClickListener {
if (viewState?.isLoading() == true) return@setOnClickListener

setQuantityPickerViewState(
if (onSubtractClicked?.invoke(viewState?.currentQuantity ?: 0) == true) {
viewState?.getWithLoading(increment = false)
viewState?.getWithLoading()
} else {
viewState?.getWithSubtractValue()
}
Expand All @@ -87,13 +119,17 @@ class QuantityPickerView : ConstraintLayout {

setQuantityPickerViewState(
if (onAddClicked?.invoke(viewState?.currentQuantity ?: 0) == true) {
viewState?.getWithLoading(true)
viewState?.getWithLoading()
} else {
viewState?.getWithAddValue()
}
)
}
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
clipToOutline = true
}
}

private fun setUpAttributes(attrs: AttributeSet?, defStyleAttr: Int) {
Expand All @@ -109,7 +145,10 @@ class QuantityPickerView : ConstraintLayout {
context.themeColor(R.attr.colorAccent)
)
val textSize =
it.getDimensionPixelSize(R.styleable.QuantityPickerView_qpv_textSize, context.asSP(12))
it.getDimensionPixelSize(
R.styleable.QuantityPickerView_qpv_textSize,
context.asSP(12)
)
val textStyle = it.getInt(R.styleable.QuantityPickerView_qpv_textStyle, 0)
val quantityTextColor =
it.getColor(
Expand Down Expand Up @@ -144,6 +183,11 @@ class QuantityPickerView : ConstraintLayout {
it.getDrawable(R.styleable.QuantityPickerView_qpv_quantityBackground)
?: AppCompatResources.getDrawable(context, android.R.color.transparent)!!

val collapsible = it.getBoolean(R.styleable.QuantityPickerView_qpv_collapsible, false)

val expansionState =
if (collapsible) ExpansionState.Collapsible(expanded = currentQuantity > 0) else ExpansionState.NonCollapsible

val quantityPickerViewState = QuantityPickerViewState(
text = text,
textColor = textColor,
Expand All @@ -159,9 +203,10 @@ class QuantityPickerView : ConstraintLayout {
addIconDrawable = addIcon,
subtractIconDrawable = subtractIcon,
showLoading = false,
quantityBackgroundDrawable = quantityBackground
quantityBackgroundDrawable = quantityBackground,
expansionState = expansionState
)
setQuantityPickerViewState(quantityPickerViewState)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.trendyol.uicomponents.quantitypickerview

import android.content.Context
import android.graphics.drawable.Drawable
import android.view.View
import androidx.annotation.ColorInt
Expand All @@ -19,7 +20,8 @@ data class QuantityPickerViewState(
private val subtractIconDrawable: Drawable,
private val removeIconDrawable: Drawable,
private val showLoading: Boolean = false,
private val quantityBackgroundDrawable: Drawable
private val quantityBackgroundDrawable: Drawable,
private val expansionState: ExpansionState = ExpansionState.NonCollapsible
) {

internal fun isInQuantityMode(): Boolean = currentQuantity > 0
Expand All @@ -29,45 +31,107 @@ data class QuantityPickerViewState(
internal fun isLoading(): Boolean = showLoading

fun getLeftIconDrawable(): Drawable =
if (isSingleQuantity()) removeIconDrawable else subtractIconDrawable
if (currentQuantity <= 1) removeIconDrawable else subtractIconDrawable

fun getAddSubtractButtonVisibility(): Int =
if (isInQuantityMode()) View.VISIBLE else View.GONE
fun getAddButtonVisibility(): Int =
if (isInQuantityMode() || expansionState is ExpansionState.Collapsible || showLoading) View.VISIBLE else View.GONE

fun getProgressBarVisibility(): Int = if (showLoading) View.VISIBLE else View.GONE
fun getSubtractButtonVisibility(): Int =
if (isInQuantityMode() && expansionState.isExpanded() || showLoading) View.VISIBLE else View.GONE

fun getTextVisibility(): Int = if (showLoading) View.GONE else View.VISIBLE
fun getProgressBarVisibility(): Int =
if (showLoading && expansionState.isExpanded()) View.VISIBLE else View.GONE

fun getText(): String = if (isInQuantityMode()) currentQuantity.toString() else text
fun getRootViewHorizontalPadding(context: Context): Int {
return if (expansionState.isExpanded()) context.resources.getDimensionPixelSize(R.dimen.qpv_default_padding)
else context.resources.getDimensionPixelSize(R.dimen.qpv_default_small_padding)
}

fun getTextAppearance(): QuantityPickerTextAppearance =
if (isInQuantityMode()) {
QuantityPickerTextAppearance(quantityTextColor, quantityTextSize, quantityTextStyle)
} else {
QuantityPickerTextAppearance(textColor, textSize, textStyle)
}
fun getQuantityPickerTextAppearance(): QuantityPickerTextAppearance = QuantityPickerTextAppearance(textColor, textSize, textStyle)

fun getQuantityTextAppearance() = QuantityPickerTextAppearance(quantityTextColor, quantityTextSize, quantityTextStyle)

fun getQuantity() = currentQuantity.toString()

fun getQuantityPickerText() = text

fun getQuantityBackgroundDrawable(): Drawable = quantityBackgroundDrawable

fun getQuantityBackgroundVisibility(): Int = if (isInQuantityMode()) View.VISIBLE else View.GONE
fun getQuantityPickerTextVisibility(): Int = if (showLoading.not() && expansionState is ExpansionState.NonCollapsible && expansionState.isExpanded() && currentQuantity == 0) View.VISIBLE else View.GONE

internal fun getWithLoading(increment: Boolean? = null): QuantityPickerViewState =
when (increment) {
true -> copy(showLoading = true, currentQuantity = currentQuantity + 1)
false -> copy(showLoading = true, currentQuantity = currentQuantity - 1)
else -> copy(showLoading = true)
fun getQuantityVisibility(): Int {
return if (showLoading.not() && expansionState.isExpanded() && currentQuantity > 0) {
View.VISIBLE
} else if (showLoading) {
View.INVISIBLE
} else {
View.GONE
}

internal fun getWithSubtractValue(): QuantityPickerViewState? =
if (currentQuantity >= 1) copy(currentQuantity = currentQuantity - 1) else null
}

fun getQuantityBackgroundVisibility(): Int =
if (showLoading.not() && isInQuantityMode() && expansionState.isExpanded()) View.VISIBLE else View.GONE

internal fun getWithLoading(): QuantityPickerViewState {
return copy(
showLoading = true,
expansionState = expansionState.expand()
)
}

internal fun getWithSubtractValue(): QuantityPickerViewState? {
val nextQuantity = currentQuantity - 1
val nextExpansionState =
if (nextQuantity == 0) expansionState.collapse() else expansionState
return if (nextQuantity < 0) null
else copy(currentQuantity = nextQuantity, expansionState = nextExpansionState)
}

internal fun getWithAddValue(): QuantityPickerViewState =
copy(currentQuantity = currentQuantity + 1)
copy(currentQuantity = currentQuantity + 1, expansionState = expansionState.expand())

internal fun getWithQuantity(quantity: Int): QuantityPickerViewState =
copy(currentQuantity = quantity, showLoading = false)

internal fun stopLoading(): QuantityPickerViewState = copy(showLoading = false)
internal fun getWithIncrementQuantity(quantity: Int): QuantityPickerViewState {
val updatedQuantity = currentQuantity + quantity
val nextExpansionState = if (updatedQuantity == 0) expansionState.collapse() else expansionState
return copy(currentQuantity = currentQuantity + quantity, showLoading = false, expansionState = nextExpansionState)
}

internal fun stopLoading(): QuantityPickerViewState =
copy(showLoading = false, expansionState = expansionState.expand())

internal fun reset(): QuantityPickerViewState = copy(currentQuantity = 0, showLoading = false)
fun isCollapsed(): Boolean {
return expansionState.isCollapsed()
}
}

sealed class ExpansionState(open val expanded: Boolean) {
abstract fun expand(): ExpansionState
abstract fun collapse(): ExpansionState

fun isExpanded() = expanded
fun isCollapsed() = expanded.not()

data class Collapsible(override val expanded: Boolean) : ExpansionState(expanded) {
override fun expand(): ExpansionState {
return copy(expanded = true)
}

override fun collapse(): ExpansionState {
return copy(expanded = false)
}
}

object NonCollapsible : ExpansionState(expanded = true) {
override fun expand(): ExpansionState {
return this
}

override fun collapse(): ExpansionState {
return this
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="4dp" />
<corners android:radius="4dp"/>
<stroke
android:width="1dp"
android:color="?attr/colorAccent" />
<solid android:color="#FFFFFF"/>
</shape>
Loading

0 comments on commit 1db34f4

Please sign in to comment.