From e536573c281b92ccd5b0056dba2b15c11173ace8 Mon Sep 17 00:00:00 2001 From: Eudy Contreras Date: Sun, 7 Mar 2021 15:43:50 +0100 Subject: [PATCH] introduces new apis and extensions for more convenience and ease of use --- .../boneslibrary/extensions/ViewExtensions.kt | 74 +++++++++++++++++-- .../framework/bones/BoneBuilder.kt | 11 +++ .../framework/bones/BoneDrawable.kt | 19 ++++- .../framework/shimmer/ShimmerRayBuilder.kt | 3 + .../framework/skeletons/SkeletonBuilder.kt | 15 +++- .../framework/skeletons/SkeletonDrawable.kt | 16 ++++ 6 files changed, 125 insertions(+), 13 deletions(-) diff --git a/boneslibrary/src/main/java/com/eudycontreras/boneslibrary/extensions/ViewExtensions.kt b/boneslibrary/src/main/java/com/eudycontreras/boneslibrary/extensions/ViewExtensions.kt index f8de6f2..dbfc130 100644 --- a/boneslibrary/src/main/java/com/eudycontreras/boneslibrary/extensions/ViewExtensions.kt +++ b/boneslibrary/src/main/java/com/eudycontreras/boneslibrary/extensions/ViewExtensions.kt @@ -6,8 +6,9 @@ import android.os.Build import android.view.View import android.view.View.NO_ID import android.view.ViewGroup -import android.widget.ImageView import androidx.core.view.* +import com.eudycontreras.boneslibrary.bindings.addBoneLoader +import com.eudycontreras.boneslibrary.bindings.addSkeletonLoader import com.eudycontreras.boneslibrary.bindings.getParentSkeletonDrawable import com.eudycontreras.boneslibrary.doWith import com.eudycontreras.boneslibrary.framework.bones.BoneDrawable @@ -229,7 +230,7 @@ private fun findViews( * @author Eudy Contreras * @since Feburary 2021 * - * Disables skeleton loading for this view and its descendants + * Enables skeleton loading for this view and its descendants */ fun View.enableSkeletonLoading() = this.toggleSkeletonLoading(true) @@ -238,18 +239,38 @@ fun View.enableSkeletonLoading() = this.toggleSkeletonLoading(true) * @author Eudy Contreras * @since Feburary 2021 * - * Enables skeleton loading for this view and its descendants + * Disables skeleton loading for this view and its descendants */ fun View.disableSkeletonLoading() = this.toggleSkeletonLoading(false) +/** + * @Project Project Bones + * @author Eudy Contreras + * @since Feburary 2021 + * + * Enables skeleton loading for this view and its descendants + */ +fun ViewGroup.enableSkeletonLoading() = this.toggleSkeletonLoading(true) + +/** + * @Project Project Bones + * @author Eudy Contreras + * @since Feburary 2021 + * + * Disables skeleton loading for this view and its descendants + */ +fun ViewGroup.disableSkeletonLoading() = this.toggleSkeletonLoading(false) + /** * @Project Project Bones * @author Eudy Contreras * @since Feburary 2021 * * Toggles skeleton loading for this view and its descendants + * + * @return The bone loader associated with this View or null if known is found */ -fun View.toggleSkeletonLoading(enabled: Boolean) { +fun View.toggleSkeletonLoading(enabled: Boolean): BoneDrawable? { val id = generateId() val parent = getParentSkeletonDrawable() if (parent != null) { @@ -257,10 +278,49 @@ fun View.toggleSkeletonLoading(enabled: Boolean) { parent.getProps().getBoneProps(id).apply { this.enabled = enabled } + return null + } else { + var loaderDrawable: BoneDrawable? = null + doWith(foreground) { + if (it is BoneDrawable) { + it.enabled = enabled + loaderDrawable = it + } else { + loaderDrawable = addBoneLoader(enabled = enabled) + } + } + return loaderDrawable } - doWith(foreground) { - if (it is BoneDrawable) { - it.getProps().enabled = enabled +} + +/** + * @Project Project Bones + * @author Eudy Contreras + * @since Feburary 2021 + * + * Toggles skeleton loading for this view and its descendants + * + * @return The skeleton associated with this ViewGroup or null if known is found + */ +fun ViewGroup.toggleSkeletonLoading(enabled: Boolean): SkeletonDrawable? { + val id = generateId() + val parent = getParentSkeletonDrawable() + if (parent != null) { + parent.getProps().setStateOwner(id, false) + parent.getProps().getBoneProps(id).apply { + this.enabled = enabled + } + return null + } else { + var loaderDrawable: SkeletonDrawable? = null + doWith(foreground) { + if (it is SkeletonDrawable) { + it.enabled = enabled + loaderDrawable = it + } else { + loaderDrawable = addSkeletonLoader(enabled = enabled) + } } + return loaderDrawable } } \ No newline at end of file diff --git a/boneslibrary/src/main/java/com/eudycontreras/boneslibrary/framework/bones/BoneBuilder.kt b/boneslibrary/src/main/java/com/eudycontreras/boneslibrary/framework/bones/BoneBuilder.kt index 6d53f3d..c26d703 100644 --- a/boneslibrary/src/main/java/com/eudycontreras/boneslibrary/framework/bones/BoneBuilder.kt +++ b/boneslibrary/src/main/java/com/eudycontreras/boneslibrary/framework/bones/BoneBuilder.kt @@ -31,6 +31,9 @@ data class BoneBuilder( internal var boneProperties: BoneProperties = BoneProperties() ) { + val properties: BoneProperties + get() = boneProperties + /** * Allows building shimmer ray properties. * @@ -378,4 +381,12 @@ data class BoneBuilder( this.boneProperties.enabled = enabled return this } + + /** + * Creates a returns a Bone Drawable built with this + * builder + */ + fun get(): BoneDrawable { + return BoneDrawable.from(this) + } } diff --git a/boneslibrary/src/main/java/com/eudycontreras/boneslibrary/framework/bones/BoneDrawable.kt b/boneslibrary/src/main/java/com/eudycontreras/boneslibrary/framework/bones/BoneDrawable.kt index f218a0f..ac2065d 100644 --- a/boneslibrary/src/main/java/com/eudycontreras/boneslibrary/framework/bones/BoneDrawable.kt +++ b/boneslibrary/src/main/java/com/eudycontreras/boneslibrary/framework/bones/BoneDrawable.kt @@ -21,9 +21,6 @@ import com.eudycontreras.boneslibrary.extensions.clearProps import com.eudycontreras.boneslibrary.extensions.saveProps import com.eudycontreras.boneslibrary.framework.bones.BoneDrawable.Companion.create import com.eudycontreras.boneslibrary.framework.shimmer.ShimmerRay -import com.eudycontreras.boneslibrary.framework.skeletons.SkeletonDrawable -import com.eudycontreras.boneslibrary.framework.skeletons.SkeletonManager -import com.eudycontreras.boneslibrary.framework.skeletons.SkeletonProperties import com.eudycontreras.boneslibrary.properties.CornerRadii import com.eudycontreras.boneslibrary.properties.ShapeType import com.eudycontreras.boneslibrary.tryGet @@ -254,6 +251,22 @@ class BoneDrawable internal constructor( */ fun builder(): BoneBuilder = boneManager.getBuilder() + /** + * Disables this bone drawable if it is enabled for for some + * View + */ + fun disable() { + this.enabled = false + } + + /** + * Enables the bone drawable for the given View + * @param view The view this drawable will be attached to. + */ + fun enable(view: View) { + view.addBoneLoader(true, this) + } + override fun resetForReuse() { owner = null enabled = false diff --git a/boneslibrary/src/main/java/com/eudycontreras/boneslibrary/framework/shimmer/ShimmerRayBuilder.kt b/boneslibrary/src/main/java/com/eudycontreras/boneslibrary/framework/shimmer/ShimmerRayBuilder.kt index 4b43ca9..0c0251a 100644 --- a/boneslibrary/src/main/java/com/eudycontreras/boneslibrary/framework/shimmer/ShimmerRayBuilder.kt +++ b/boneslibrary/src/main/java/com/eudycontreras/boneslibrary/framework/shimmer/ShimmerRayBuilder.kt @@ -29,6 +29,9 @@ data class ShimmerRayBuilder( private val shimmerRayProperties: ShimmerRayProperties = ShimmerRayProperties() ) { + val properties: ShimmerRayProperties + get() = shimmerRayProperties + /** * @Project Project Bones * @author Eudy Contreras diff --git a/boneslibrary/src/main/java/com/eudycontreras/boneslibrary/framework/skeletons/SkeletonBuilder.kt b/boneslibrary/src/main/java/com/eudycontreras/boneslibrary/framework/skeletons/SkeletonBuilder.kt index 77e3b83..9ac960b 100644 --- a/boneslibrary/src/main/java/com/eudycontreras/boneslibrary/framework/skeletons/SkeletonBuilder.kt +++ b/boneslibrary/src/main/java/com/eudycontreras/boneslibrary/framework/skeletons/SkeletonBuilder.kt @@ -32,6 +32,9 @@ class SkeletonBuilder( internal var skeletonProperties: SkeletonProperties = SkeletonProperties() ) { + val properties: SkeletonProperties + get() = skeletonProperties + private val builderQueue: Queue<() -> Unit> = LinkedList() /** @@ -325,9 +328,15 @@ class SkeletonBuilder( @Synchronized internal fun applyBuilders(): SkeletonBuilder { - while (builderQueue.peek() != null) { - builderQueue.poll()?.invoke() - } + builderQueue.forEach { it.invoke() } return this } + + /** + * Creates a returns a Skeleton Drawable built with this + * builder + */ + fun get(): SkeletonDrawable { + return SkeletonDrawable.from(this) + } } diff --git a/boneslibrary/src/main/java/com/eudycontreras/boneslibrary/framework/skeletons/SkeletonDrawable.kt b/boneslibrary/src/main/java/com/eudycontreras/boneslibrary/framework/skeletons/SkeletonDrawable.kt index 0bac797..9efd921 100644 --- a/boneslibrary/src/main/java/com/eudycontreras/boneslibrary/framework/skeletons/SkeletonDrawable.kt +++ b/boneslibrary/src/main/java/com/eudycontreras/boneslibrary/framework/skeletons/SkeletonDrawable.kt @@ -222,6 +222,22 @@ class SkeletonDrawable internal constructor( @Synchronized fun getProps(): SkeletonProperties = skeletonManager.properties + /** + * Disables this skeleton drawable if it is enabled for for some + * ViewGroup + */ + fun disable() { + this.enabled = false + } + + /** + * Enables the skeleton drawable for the given ViewGroup + * @param viewGroup The view this drawable will be attached to. + */ + fun enable(viewGroup: ViewGroup) { + viewGroup.addSkeletonLoader(true, this) + } + /** * Sets the properties use by this **SkeletonDrawable** * @param properties The properties to be set on this drawable