From 3b5b8138d7c67ba91b4c0bb7a7e98da0712cb08a Mon Sep 17 00:00:00 2001 From: Tamim Date: Fri, 22 Jan 2021 00:34:11 +0700 Subject: [PATCH] Library: Fixed app crashing when setting background programatically --- .../java/soup/neumorphism/NeumorphButton.kt | 41 +++++++++--------- .../java/soup/neumorphism/NeumorphCardView.kt | 42 ++++++++++--------- .../NeumorphFloatingActionButton.kt | 41 +++++++++--------- .../soup/neumorphism/NeumorphImageButton.kt | 41 +++++++++--------- .../soup/neumorphism/NeumorphImageView.kt | 42 ++++++++++--------- 5 files changed, 112 insertions(+), 95 deletions(-) diff --git a/neumorphism/src/main/java/soup/neumorphism/NeumorphButton.kt b/neumorphism/src/main/java/soup/neumorphism/NeumorphButton.kt index b381f4e..c26eb36 100644 --- a/neumorphism/src/main/java/soup/neumorphism/NeumorphButton.kt +++ b/neumorphism/src/main/java/soup/neumorphism/NeumorphButton.kt @@ -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( @@ -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?) { @@ -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) } diff --git a/neumorphism/src/main/java/soup/neumorphism/NeumorphCardView.kt b/neumorphism/src/main/java/soup/neumorphism/NeumorphCardView.kt index f3bbdc0..c787729 100644 --- a/neumorphism/src/main/java/soup/neumorphism/NeumorphCardView.kt +++ b/neumorphism/src/main/java/soup/neumorphism/NeumorphCardView.kt @@ -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( @@ -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) } @@ -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) } diff --git a/neumorphism/src/main/java/soup/neumorphism/NeumorphFloatingActionButton.kt b/neumorphism/src/main/java/soup/neumorphism/NeumorphFloatingActionButton.kt index ae5f5af..65a228c 100644 --- a/neumorphism/src/main/java/soup/neumorphism/NeumorphFloatingActionButton.kt +++ b/neumorphism/src/main/java/soup/neumorphism/NeumorphFloatingActionButton.kt @@ -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( @@ -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?) { @@ -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) } diff --git a/neumorphism/src/main/java/soup/neumorphism/NeumorphImageButton.kt b/neumorphism/src/main/java/soup/neumorphism/NeumorphImageButton.kt index f4a5cd2..f625343 100644 --- a/neumorphism/src/main/java/soup/neumorphism/NeumorphImageButton.kt +++ b/neumorphism/src/main/java/soup/neumorphism/NeumorphImageButton.kt @@ -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( @@ -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?) { @@ -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) } diff --git a/neumorphism/src/main/java/soup/neumorphism/NeumorphImageView.kt b/neumorphism/src/main/java/soup/neumorphism/NeumorphImageView.kt index 4d2ebd3..46cc6be 100644 --- a/neumorphism/src/main/java/soup/neumorphism/NeumorphImageView.kt +++ b/neumorphism/src/main/java/soup/neumorphism/NeumorphImageView.kt @@ -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( @@ -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) } @@ -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) }