Skip to content

Commit

Permalink
Library: Fixed attrs not loading for views
Browse files Browse the repository at this point in the history
  • Loading branch information
tamimattafi committed Jan 21, 2021
1 parent c01a4dd commit 0ad57e8
Show file tree
Hide file tree
Showing 7 changed files with 420 additions and 432 deletions.
154 changes: 72 additions & 82 deletions neumorphism/src/main/java/soup/neumorphism/NeumorphButton.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,81 @@ class NeumorphButton @JvmOverloads constructor(
private var isInitialized: Boolean = false
private lateinit var shapeDrawable: NeumorphShapeDrawable

private var insetStart = 0
private var insetEnd = 0
private var insetTop = 0
private var insetBottom = 0
private val backgroundDrawable: Drawable?
private val fillColor: ColorStateList?
private val strokeColor: ColorStateList?
private val strokeWidth: Float
private val shapeType: Int
private val inset: Int
private var insetStart: Int
private var insetEnd: Int
private var insetTop: Int
private var insetBottom: Int
private val shadowElevation: Float
private val shadowColorLight: Int
private val shadowColorDark: Int

init {
val a = context.obtainStyledAttributes(
attrs, R.styleable.NeumorphButton, defStyleAttr, defStyleRes
)

backgroundDrawable = a.getDrawable(R.styleable.NeumorphButton_neumorph_backgroundDrawable)
fillColor = a.getColorStateList(R.styleable.NeumorphButton_neumorph_backgroundColor)
strokeColor = a.getColorStateList(R.styleable.NeumorphButton_neumorph_strokeColor)
strokeWidth = a.getDimension(R.styleable.NeumorphButton_neumorph_strokeWidth, 0f)
shapeType = a.getInt(R.styleable.NeumorphButton_neumorph_shapeType, ShapeType.DEFAULT)
inset = a.getDimensionPixelSize(R.styleable.NeumorphButton_neumorph_inset, 0)
insetStart = a.getDimensionPixelSize(R.styleable.NeumorphButton_neumorph_insetStart, -1)
insetEnd = a.getDimensionPixelSize(R.styleable.NeumorphButton_neumorph_insetEnd, -1)
insetTop = a.getDimensionPixelSize(R.styleable.NeumorphButton_neumorph_insetTop, -1)
insetBottom = a.getDimensionPixelSize(R.styleable.NeumorphButton_neumorph_insetBottom, -1)
shadowElevation = a.getDimension(R.styleable.NeumorphButton_neumorph_shadowElevation, 0f)
shadowColorLight = NeumorphResources.getColor(context, a, R.styleable.NeumorphButton_neumorph_shadowColorLight, R.color.design_default_color_shadow_light)
shadowColorDark = NeumorphResources.getColor(context, a, R.styleable.NeumorphButton_neumorph_shadowColorDark, R.color.design_default_color_shadow_dark)
a.recycle()
}

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
this.initShapeDrawable()
initShapeDrawable()
}

private fun initShapeDrawable() {
if (isInitialized || measuredWidth * measuredHeight == 0) return

updateInsets(
if (insetStart >= 0) insetStart else inset,
if (insetTop >= 0) insetTop else inset,
if (insetEnd >= 0) insetEnd else inset,
if (insetBottom >= 0) insetBottom else inset
)

shapeDrawable = NeumorphDrawableFactory.createReusable(
context,
attrs,
defStyleAttr,
defStyleRes,
measuredWidth,
measuredHeight,
isInEditMode,
shapeType,
shadowElevation,
shadowColorLight,
shadowColorDark,
backgroundDrawable,
fillColor,
strokeWidth,
strokeColor,
translationZ,
this.insetStart,
this.insetTop,
this.insetEnd,
this.insetBottom
)

setBackgroundInternal(shapeDrawable)
isInitialized = true
}

override fun setBackground(drawable: Drawable?) {
Expand Down Expand Up @@ -149,83 +216,6 @@ class NeumorphButton @JvmOverloads constructor(
}
}

private fun initShapeDrawable() {
if (isInitialized || measuredHeight * measuredWidth == 0) return

val a = context.obtainStyledAttributes(
attrs, R.styleable.NeumorphButton, defStyleAttr, defStyleRes
)

val backgroundDrawable = a.getDrawable(R.styleable.NeumorphImageButton_neumorph_backgroundDrawable)
val fillColor = a.getColorStateList(R.styleable.NeumorphFloatingActionButton_neumorph_backgroundColor)
val strokeColor = a.getColorStateList(R.styleable.NeumorphFloatingActionButton_neumorph_strokeColor)
val strokeWidth = a.getDimension(R.styleable.NeumorphFloatingActionButton_neumorph_strokeWidth, 0f)
val shapeType =
a.getInt(R.styleable.NeumorphFloatingActionButton_neumorph_shapeType, ShapeType.DEFAULT)
val inset = a.getDimensionPixelSize(
R.styleable.NeumorphFloatingActionButton_neumorph_inset, 0
)
val insetStart = a.getDimensionPixelSize(
R.styleable.NeumorphFloatingActionButton_neumorph_insetStart, -1
)
val insetEnd = a.getDimensionPixelSize(
R.styleable.NeumorphFloatingActionButton_neumorph_insetEnd, -1
)
val insetTop = a.getDimensionPixelSize(
R.styleable.NeumorphFloatingActionButton_neumorph_insetTop, -1
)
val insetBottom = a.getDimensionPixelSize(
R.styleable.NeumorphFloatingActionButton_neumorph_insetBottom, -1
)
val shadowElevation = a.getDimension(
R.styleable.NeumorphFloatingActionButton_neumorph_shadowElevation, 0f
)
val shadowColorLight = NeumorphResources.getColor(
context, a,
R.styleable.NeumorphFloatingActionButton_neumorph_shadowColorLight,
R.color.design_default_color_shadow_light
)
val shadowColorDark = NeumorphResources.getColor(
context, a,
R.styleable.NeumorphFloatingActionButton_neumorph_shadowColorDark,
R.color.design_default_color_shadow_dark
)
a.recycle()

updateInsets(
if (insetStart >= 0) insetStart else inset,
if (insetTop >= 0) insetTop else inset,
if (insetEnd >= 0) insetEnd else inset,
if (insetBottom >= 0) insetBottom else inset
)

shapeDrawable = NeumorphDrawableFactory.createReusable(
context,
attrs,
defStyleAttr,
defStyleRes,
measuredWidth,
measuredHeight,
isInEditMode,
shapeType,
shadowElevation,
shadowColorLight,
shadowColorDark,
backgroundDrawable,
fillColor,
strokeWidth,
strokeColor,
translationZ,
this.insetStart,
this.insetTop,
this.insetEnd,
this.insetBottom
)

setBackgroundInternal(shapeDrawable)
isInitialized = true
}

companion object {
private const val LOG_TAG = "NeumorphButton"
}
Expand Down
154 changes: 72 additions & 82 deletions neumorphism/src/main/java/soup/neumorphism/NeumorphCardView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,81 @@ class NeumorphCardView @JvmOverloads constructor(
private var isInitialized: Boolean = false
private lateinit var shapeDrawable: NeumorphShapeDrawable

private var insetStart = 0
private var insetEnd = 0
private var insetTop = 0
private var insetBottom = 0
private val backgroundDrawable: Drawable?
private val fillColor: ColorStateList?
private val strokeColor: ColorStateList?
private val strokeWidth: Float
private val shapeType: Int
private val inset: Int
private var insetStart: Int
private var insetEnd: Int
private var insetTop: Int
private var insetBottom: Int
private val shadowElevation: Float
private val shadowColorLight: Int
private val shadowColorDark: Int

init {
val a = context.obtainStyledAttributes(
attrs, R.styleable.NeumorphCardView, defStyleAttr, defStyleRes
)

backgroundDrawable = a.getDrawable(R.styleable.NeumorphCardView_neumorph_backgroundDrawable)
fillColor = a.getColorStateList(R.styleable.NeumorphCardView_neumorph_backgroundColor)
strokeColor = a.getColorStateList(R.styleable.NeumorphCardView_neumorph_strokeColor)
strokeWidth = a.getDimension(R.styleable.NeumorphCardView_neumorph_strokeWidth, 0f)
shapeType = a.getInt(R.styleable.NeumorphCardView_neumorph_shapeType, ShapeType.DEFAULT)
inset = a.getDimensionPixelSize(R.styleable.NeumorphCardView_neumorph_inset, 0)
insetStart = a.getDimensionPixelSize(R.styleable.NeumorphCardView_neumorph_insetStart, -1)
insetEnd = a.getDimensionPixelSize(R.styleable.NeumorphCardView_neumorph_insetEnd, -1)
insetTop = a.getDimensionPixelSize(R.styleable.NeumorphCardView_neumorph_insetTop, -1)
insetBottom = a.getDimensionPixelSize(R.styleable.NeumorphCardView_neumorph_insetBottom, -1)
shadowElevation = a.getDimension(R.styleable.NeumorphCardView_neumorph_shadowElevation, 0f)
shadowColorLight = NeumorphResources.getColor(context, a, R.styleable.NeumorphCardView_neumorph_shadowColorLight, R.color.design_default_color_shadow_light)
shadowColorDark = NeumorphResources.getColor(context, a, R.styleable.NeumorphCardView_neumorph_shadowColorDark, R.color.design_default_color_shadow_dark)
a.recycle()
}

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
this.initShapeDrawable()
initShapeDrawable()
}

private fun initShapeDrawable() {
if (isInitialized || measuredWidth * measuredHeight == 0) return

updateInsets(
if (insetStart >= 0) insetStart else inset,
if (insetTop >= 0) insetTop else inset,
if (insetEnd >= 0) insetEnd else inset,
if (insetBottom >= 0) insetBottom else inset
)

shapeDrawable = NeumorphDrawableFactory.createReusable(
context,
attrs,
defStyleAttr,
defStyleRes,
measuredWidth,
measuredHeight,
isInEditMode,
shapeType,
shadowElevation,
shadowColorLight,
shadowColorDark,
backgroundDrawable,
fillColor,
strokeWidth,
strokeColor,
translationZ,
this.insetStart,
this.insetTop,
this.insetEnd,
this.insetBottom
)

setBackgroundInternal(shapeDrawable)
isInitialized = true
}

override fun drawChild(canvas: Canvas, child: View, drawingTime: Long): Boolean {
Expand Down Expand Up @@ -160,83 +227,6 @@ class NeumorphCardView @JvmOverloads constructor(
}
}

private fun initShapeDrawable() {
if (isInitialized || measuredHeight * measuredWidth == 0) return

val a = context.obtainStyledAttributes(
attrs, R.styleable.NeumorphCardView, defStyleAttr, defStyleRes
)

val backgroundDrawable = a.getDrawable(R.styleable.NeumorphImageButton_neumorph_backgroundDrawable)
val fillColor = a.getColorStateList(R.styleable.NeumorphFloatingActionButton_neumorph_backgroundColor)
val strokeColor = a.getColorStateList(R.styleable.NeumorphFloatingActionButton_neumorph_strokeColor)
val strokeWidth = a.getDimension(R.styleable.NeumorphFloatingActionButton_neumorph_strokeWidth, 0f)
val shapeType =
a.getInt(R.styleable.NeumorphFloatingActionButton_neumorph_shapeType, ShapeType.DEFAULT)
val inset = a.getDimensionPixelSize(
R.styleable.NeumorphFloatingActionButton_neumorph_inset, 0
)
val insetStart = a.getDimensionPixelSize(
R.styleable.NeumorphFloatingActionButton_neumorph_insetStart, -1
)
val insetEnd = a.getDimensionPixelSize(
R.styleable.NeumorphFloatingActionButton_neumorph_insetEnd, -1
)
val insetTop = a.getDimensionPixelSize(
R.styleable.NeumorphFloatingActionButton_neumorph_insetTop, -1
)
val insetBottom = a.getDimensionPixelSize(
R.styleable.NeumorphFloatingActionButton_neumorph_insetBottom, -1
)
val shadowElevation = a.getDimension(
R.styleable.NeumorphFloatingActionButton_neumorph_shadowElevation, 0f
)
val shadowColorLight = NeumorphResources.getColor(
context, a,
R.styleable.NeumorphFloatingActionButton_neumorph_shadowColorLight,
R.color.design_default_color_shadow_light
)
val shadowColorDark = NeumorphResources.getColor(
context, a,
R.styleable.NeumorphFloatingActionButton_neumorph_shadowColorDark,
R.color.design_default_color_shadow_dark
)
a.recycle()

updateInsets(
if (insetStart >= 0) insetStart else inset,
if (insetTop >= 0) insetTop else inset,
if (insetEnd >= 0) insetEnd else inset,
if (insetBottom >= 0) insetBottom else inset
)

shapeDrawable = NeumorphDrawableFactory.createReusable(
context,
attrs,
defStyleAttr,
defStyleRes,
measuredWidth,
measuredHeight,
isInEditMode,
shapeType,
shadowElevation,
shadowColorLight,
shadowColorDark,
backgroundDrawable,
fillColor,
strokeWidth,
strokeColor,
translationZ,
this.insetStart,
this.insetTop,
this.insetEnd,
this.insetBottom
)

setBackgroundInternal(shapeDrawable)
isInitialized = true
}

companion object {
private const val LOG_TAG = "NeumorphCardView"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ object NeumorphDrawableFactory {
hashCode = 31 * hashCode + insetRight
hashCode = 31 * hashCode + insetBottom

return reusable_drawables[hashCode] ?: NeumorphShapeDrawable(context, attrs, defStyleAttr, defStyleRes).apply {
val drawable = reusable_drawables[hashCode] ?: NeumorphShapeDrawable(context, attrs, defStyleAttr, defStyleRes).apply {
setInEditMode(isInEditMode)
setShapeType(shapeType)
setShadowElevation(shadowElevation)
Expand All @@ -66,6 +66,8 @@ object NeumorphDrawableFactory {
setInset(insetLeft, insetTop, insetRight, insetBottom)
reusable_drawables[hashCode] = this
}

return drawable.clone()
}

}
Loading

0 comments on commit 0ad57e8

Please sign in to comment.