-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from ericktijerou/feature/textview-length
Add TextView length support
- Loading branch information
Showing
18 changed files
with
339 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
koleton-base/src/main/kotlin/koleton/custom/TextKoletonView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package koleton.custom | ||
|
||
import android.content.Context | ||
import android.graphics.Canvas | ||
import android.util.AttributeSet | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import koleton.mask.KoletonMask | ||
import koleton.util.* | ||
|
||
internal class TextKoletonView @JvmOverloads constructor( | ||
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 | ||
) : KoletonView(context, attrs, defStyleAttr) { | ||
|
||
private var koletonMask: KoletonMask? = null | ||
override var isSkeletonShown: Boolean = false | ||
private var isMeasured: Boolean = false | ||
private val viewList = arrayListOf<View>() | ||
|
||
var attributes: TextViewAttributes? = null | ||
set(value) { | ||
field = value | ||
originalText = value?.view?.text.toString() | ||
value?.let { applyAttributes() } | ||
} | ||
|
||
private var originalText: String = EMPTY_STRING | ||
|
||
private fun hideVisibleChildren(view: View) { | ||
when (view) { | ||
is ViewGroup -> view.children().forEach { hideVisibleChildren(it) } | ||
else -> hideVisibleChild(view) | ||
} | ||
} | ||
|
||
private fun hideVisibleChild(view: View) { | ||
if (view.isVisible()) { | ||
view.invisible() | ||
viewList.add(view) | ||
} | ||
} | ||
|
||
override fun showSkeleton() { | ||
isSkeletonShown = true | ||
setFakeText() | ||
if (isMeasured && childCount > 0) { | ||
hideVisibleChildren(this) | ||
applyAttributes() | ||
} | ||
} | ||
|
||
override fun hideSkeleton() { | ||
isSkeletonShown = false | ||
restoreOriginalText() | ||
if (childCount > 0) { | ||
viewList.forEach { it.visible() } | ||
hideShimmer() | ||
koletonMask = null | ||
} | ||
} | ||
|
||
private fun setFakeText() { | ||
attributes?.apply { | ||
view.text = getRandomAlphaNumericString(length) | ||
} | ||
} | ||
|
||
private fun restoreOriginalText() { | ||
attributes?.apply { | ||
view.text = originalText | ||
} | ||
} | ||
|
||
override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) { | ||
super.onLayout(changed, left, top, right, bottom) | ||
isMeasured = width > NUMBER_ZERO && height > NUMBER_ZERO | ||
if (isSkeletonShown) { | ||
showSkeleton() | ||
} | ||
} | ||
|
||
override fun onDraw(canvas: Canvas?) { | ||
super.onDraw(canvas) | ||
canvas?.let { koletonMask?.draw(it) } | ||
} | ||
|
||
override fun applyAttributes() { | ||
if (isMeasured) { | ||
attributes?.let { attrs -> | ||
if (!attrs.isShimmerEnabled) { | ||
hideShimmer() | ||
} else { | ||
setShimmer(attrs.shimmer) | ||
} | ||
koletonMask = KoletonMask(this, attrs.color, attrs.cornerRadius, attrs.lineSpacing) | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
koleton-base/src/main/kotlin/koleton/target/TextViewTarget.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package koleton.target | ||
|
||
import android.widget.TextView | ||
import androidx.lifecycle.DefaultLifecycleObserver | ||
import androidx.lifecycle.LifecycleOwner | ||
import koleton.custom.KoletonView | ||
|
||
/** A [Target] that handles setting skeleton on a [TextView]. */ | ||
open class TextViewTarget(override val view: TextView) : ViewTarget<TextView>, DefaultLifecycleObserver { | ||
|
||
override fun onStart() = Unit | ||
|
||
/** Show the [skeleton] view */ | ||
override fun onSuccess(skeleton: KoletonView) = skeleton.showSkeleton() | ||
|
||
override fun onError() = Unit | ||
|
||
override fun onStart(owner: LifecycleOwner) {} | ||
|
||
override fun onStop(owner: LifecycleOwner) {} | ||
} |
File renamed without changes.
Oops, something went wrong.