Skip to content

Commit

Permalink
Library: Fixed app crashing when setting background programatically
Browse files Browse the repository at this point in the history
  • Loading branch information
tamimattafi committed Jan 21, 2021
1 parent 0ad57e8 commit 3b5b813
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 95 deletions.
41 changes: 22 additions & 19 deletions neumorphism/src/main/java/soup/neumorphism/NeumorphButton.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ class NeumorphButton @JvmOverloads constructor(
private var isInitialized: Boolean = false
private lateinit var shapeDrawable: NeumorphShapeDrawable

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 backgroundDrawable: Drawable?
private var fillColor: ColorStateList?
private var strokeColor: ColorStateList?
private var strokeWidth: Float
private var shapeType: Int
private var 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
private var shadowElevation: Float
private var shadowColorLight: Int
private var shadowColorDark: Int

init {
val a = context.obtainStyledAttributes(
Expand Down Expand Up @@ -106,7 +106,19 @@ class NeumorphButton @JvmOverloads constructor(
}

fun setNeumorphBackgroundDrawable(drawable: Drawable?) {
shapeDrawable.setBackgroundDrawable(drawable)
if (isInitialized) shapeDrawable.setBackgroundDrawable(drawable)
else backgroundDrawable = drawable
}

fun setShapeType(@ShapeType shapeType: Int) {
if (isInitialized) shapeDrawable.setShapeType(shapeType)
else this.shapeType = shapeType
}

@ShapeType
fun getShapeType(): Int {
return if (isInitialized) shapeDrawable.getShapeType()
else shapeType
}

private fun setBackgroundInternal(drawable: Drawable?) {
Expand Down Expand Up @@ -149,15 +161,6 @@ class NeumorphButton @JvmOverloads constructor(
return shapeDrawable.getStrokeWidth()
}

fun setShapeType(@ShapeType shapeType: Int) {
shapeDrawable.setShapeType(shapeType)
}

@ShapeType
fun getShapeType(): Int {
return shapeDrawable.getShapeType()
}

fun setInset(left: Int, top: Int, right: Int, bottom: Int) {
internalSetInset(left, top, right, bottom)
}
Expand Down
42 changes: 23 additions & 19 deletions neumorphism/src/main/java/soup/neumorphism/NeumorphCardView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ class NeumorphCardView @JvmOverloads constructor(
private var isInitialized: Boolean = false
private lateinit var shapeDrawable: NeumorphShapeDrawable

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 backgroundDrawable: Drawable?
private var fillColor: ColorStateList?
private var strokeColor: ColorStateList?
private var strokeWidth: Float
private var shapeType: Int
private var 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
private var shadowElevation: Float
private var shadowColorLight: Int
private var shadowColorDark: Int

init {
val a = context.obtainStyledAttributes(
Expand Down Expand Up @@ -118,8 +118,21 @@ class NeumorphCardView @JvmOverloads constructor(
}

fun setNeumorphBackgroundDrawable(drawable: Drawable?) {
shapeDrawable.setBackgroundDrawable(drawable)
if (isInitialized) shapeDrawable.setBackgroundDrawable(drawable)
else backgroundDrawable = drawable
}

fun setShapeType(@ShapeType shapeType: Int) {
if (isInitialized) shapeDrawable.setShapeType(shapeType)
else this.shapeType = shapeType
}

@ShapeType
fun getShapeType(): Int {
return if (isInitialized) shapeDrawable.getShapeType()
else shapeType
}

private fun setBackgroundInternal(drawable: Drawable?) {
super.setBackgroundDrawable(drawable)
}
Expand Down Expand Up @@ -160,15 +173,6 @@ class NeumorphCardView @JvmOverloads constructor(
return shapeDrawable.getStrokeWidth()
}

fun setShapeType(@ShapeType shapeType: Int) {
shapeDrawable.setShapeType(shapeType)
}

@ShapeType
fun getShapeType(): Int {
return shapeDrawable.getShapeType()
}

fun setInset(left: Int, top: Int, right: Int, bottom: Int) {
internalSetInset(left, top, right, bottom)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ class NeumorphFloatingActionButton @JvmOverloads constructor(
private var isInitialized: Boolean = false
private lateinit var shapeDrawable: NeumorphShapeDrawable

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 backgroundDrawable: Drawable?
private var fillColor: ColorStateList?
private var strokeColor: ColorStateList?
private var strokeWidth: Float
private var shapeType: Int
private var 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
private var shadowElevation: Float
private var shadowColorLight: Int
private var shadowColorDark: Int

init {
val a = context.obtainStyledAttributes(
Expand Down Expand Up @@ -105,7 +105,19 @@ class NeumorphFloatingActionButton @JvmOverloads constructor(
}

fun setNeumorphBackgroundDrawable(drawable: Drawable?) {
shapeDrawable.setBackgroundDrawable(drawable)
if (isInitialized) shapeDrawable.setBackgroundDrawable(drawable)
else backgroundDrawable = drawable
}

fun setShapeType(@ShapeType shapeType: Int) {
if (isInitialized) shapeDrawable.setShapeType(shapeType)
else this.shapeType = shapeType
}

@ShapeType
fun getShapeType(): Int {
return if (isInitialized) shapeDrawable.getShapeType()
else shapeType
}

private fun setBackgroundInternal(drawable: Drawable?) {
Expand Down Expand Up @@ -148,15 +160,6 @@ class NeumorphFloatingActionButton @JvmOverloads constructor(
return shapeDrawable.getStrokeWidth()
}

fun setShapeType(@ShapeType shapeType: Int) {
shapeDrawable.setShapeType(shapeType)
}

@ShapeType
fun getShapeType(): Int {
return shapeDrawable.getShapeType()
}

fun setInset(left: Int, top: Int, right: Int, bottom: Int) {
internalSetInset(left, top, right, bottom)
}
Expand Down
41 changes: 22 additions & 19 deletions neumorphism/src/main/java/soup/neumorphism/NeumorphImageButton.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ class NeumorphImageButton @JvmOverloads constructor(
private var isInitialized: Boolean = false
private lateinit var shapeDrawable: NeumorphShapeDrawable

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 backgroundDrawable: Drawable?
private var fillColor: ColorStateList?
private var strokeColor: ColorStateList?
private var strokeWidth: Float
private var shapeType: Int
private var 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
private var shadowElevation: Float
private var shadowColorLight: Int
private var shadowColorDark: Int

init {
val a = context.obtainStyledAttributes(
Expand Down Expand Up @@ -105,7 +105,19 @@ class NeumorphImageButton @JvmOverloads constructor(
}

fun setNeumorphBackgroundDrawable(drawable: Drawable?) {
shapeDrawable.setBackgroundDrawable(drawable)
if (isInitialized) shapeDrawable.setBackgroundDrawable(drawable)
else backgroundDrawable = drawable
}

fun setShapeType(@ShapeType shapeType: Int) {
if (isInitialized) shapeDrawable.setShapeType(shapeType)
else this.shapeType = shapeType
}

@ShapeType
fun getShapeType(): Int {
return if (isInitialized) shapeDrawable.getShapeType()
else shapeType
}

private fun setBackgroundInternal(drawable: Drawable?) {
Expand Down Expand Up @@ -148,15 +160,6 @@ class NeumorphImageButton @JvmOverloads constructor(
return shapeDrawable.getStrokeWidth()
}

fun setShapeType(@ShapeType shapeType: Int) {
shapeDrawable.setShapeType(shapeType)
}

@ShapeType
fun getShapeType(): Int {
return shapeDrawable.getShapeType()
}

fun setInset(left: Int, top: Int, right: Int, bottom: Int) {
internalSetInset(left, top, right, bottom)
}
Expand Down
42 changes: 23 additions & 19 deletions neumorphism/src/main/java/soup/neumorphism/NeumorphImageView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ class NeumorphImageView @JvmOverloads constructor(
private var isInitialized: Boolean = false
private lateinit var shapeDrawable: NeumorphShapeDrawable

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 backgroundDrawable: Drawable?
private var fillColor: ColorStateList?
private var strokeColor: ColorStateList?
private var strokeWidth: Float
private var shapeType: Int
private var 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
private var shadowElevation: Float
private var shadowColorLight: Int
private var shadowColorDark: Int

init {
val a = context.obtainStyledAttributes(
Expand Down Expand Up @@ -105,8 +105,21 @@ class NeumorphImageView @JvmOverloads constructor(
}

fun setNeumorphBackgroundDrawable(drawable: Drawable?) {
shapeDrawable.setBackgroundDrawable(drawable)
if (isInitialized) shapeDrawable.setBackgroundDrawable(drawable)
else backgroundDrawable = drawable
}

fun setShapeType(@ShapeType shapeType: Int) {
if (isInitialized) shapeDrawable.setShapeType(shapeType)
else this.shapeType = shapeType
}

@ShapeType
fun getShapeType(): Int {
return if (isInitialized) shapeDrawable.getShapeType()
else shapeType
}

private fun setBackgroundInternal(drawable: Drawable?) {
super.setBackgroundDrawable(drawable)
}
Expand Down Expand Up @@ -147,15 +160,6 @@ class NeumorphImageView @JvmOverloads constructor(
return shapeDrawable.getStrokeWidth()
}

fun setShapeType(@ShapeType shapeType: Int) {
shapeDrawable.setShapeType(shapeType)
}

@ShapeType
fun getShapeType(): Int {
return shapeDrawable.getShapeType()
}

fun setInset(left: Int, top: Int, right: Int, bottom: Int) {
internalSetInset(left, top, right, bottom)
}
Expand Down

0 comments on commit 3b5b813

Please sign in to comment.