Skip to content

Commit

Permalink
Fixes #5: Prevent invalidating mask before onLayout()
Browse files Browse the repository at this point in the history
  • Loading branch information
Faltenreich committed Mar 20, 2019
1 parent 8e56998 commit 31065a4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package com.faltenreich.skeletonlayout

import android.os.Bundle
import android.os.Handler
import androidx.viewpager.widget.ViewPager
import androidx.appcompat.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager.widget.ViewPager
import com.faltenreich.skeletonlayout.fragment.BaseSkeletonFragment
import com.faltenreich.skeletonlayout.fragment.BottomSheetFragment
import com.faltenreich.skeletonlayout.fragment.RecyclerViewFragment
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.faltenreich.skeletonlayout.fragment

import android.os.Bundle
import androidx.annotation.LayoutRes
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.fragment.app.Fragment

abstract class BaseFragment(@LayoutRes private val layoutResId: Int, val title: String) : Fragment() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

package com.faltenreich.skeletonlayout

import android.app.ActionBar
import android.view.View
import android.view.ViewGroup
import androidx.annotation.ColorInt
import androidx.annotation.LayoutRes
import androidx.core.content.ContextCompat
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ class SkeletonLayout @JvmOverloads constructor(
attrs: AttributeSet? = null,
defStyleAttr: Int = 0,
originView: View? = null,
@ColorInt maskColor: Int = ContextCompat.getColor(context, DEFAULT_MASK_COLOR),
cornerRadius: Float = DEFAULT_CORNER_RADIUS,
showShimmer: Boolean = DEFAULT_SHIMMER_SHOW,
@ColorInt shimmerColor: Int = ContextCompat.getColor(context, DEFAULT_SHIMMER_COLOR),
shimmerDurationInMillis: Long = DEFAULT_SHIMMER_DURATION_IN_MILLIS
@ColorInt private var maskColorInternal: Int = ContextCompat.getColor(context, DEFAULT_MASK_COLOR),
private var cornerRadiusInternal: Float = DEFAULT_CORNER_RADIUS,
private var showShimmerInternal: Boolean = DEFAULT_SHIMMER_SHOW,
@ColorInt private var shimmerColorInternal: Int = ContextCompat.getColor(context, DEFAULT_SHIMMER_COLOR),
private var shimmerDurationInMillisInternal: Long = DEFAULT_SHIMMER_DURATION_IN_MILLIS
) : FrameLayout(context, attrs, defStyleAttr), Skeleton {

internal constructor(
Expand All @@ -32,33 +32,38 @@ class SkeletonLayout @JvmOverloads constructor(
shimmerDuration: Long = 0
) : this(originView.context, null, 0, originView, maskColor, cornerRadius, showShimmer, shimmerColor, shimmerDuration)

override var maskColor: Int = maskColor
override var maskColor: Int
get() = maskColorInternal
set(value) {
field = value
maskColorInternal = value
invalidateMask()
}

override var maskCornerRadius: Float = cornerRadius
override var maskCornerRadius: Float
get() = cornerRadiusInternal
set(value) {
field = value
cornerRadiusInternal = value
invalidateMask()
}

override var showShimmer: Boolean = showShimmer
override var showShimmer: Boolean
get() = showShimmerInternal
set(value) {
field = value
showShimmerInternal = value
invalidateMask()
}

override var shimmerColor: Int = shimmerColor
override var shimmerColor: Int
get() = shimmerColorInternal
set(value) {
field = value
shimmerColorInternal = value
invalidateMask()
}

override var shimmerDurationInMillis: Long = shimmerDurationInMillis
override var shimmerDurationInMillis: Long
get() = shimmerDurationInMillisInternal
set(value) {
field = value
shimmerDurationInMillisInternal = value
invalidateMask()
}

Expand All @@ -69,14 +74,14 @@ class SkeletonLayout @JvmOverloads constructor(
init {
attrs?.let {
val typedArray = context.obtainStyledAttributes(it, R.styleable.SkeletonLayout, 0, 0)
this.maskColor = typedArray.getColor(R.styleable.SkeletonLayout_maskColor, maskColor)
this.maskCornerRadius = typedArray.getDimensionPixelSize(R.styleable.SkeletonLayout_maskCornerRadius, cornerRadius.toInt()).toFloat()
this.showShimmer = typedArray.getBoolean(R.styleable.SkeletonLayout_showShimmer, showShimmer)
this.shimmerColor = typedArray.getColor(R.styleable.SkeletonLayout_shimmerColor, shimmerColor)
this.shimmerDurationInMillis = typedArray.getInt(R.styleable.SkeletonLayout_shimmerDurationInMillis, shimmerDurationInMillis.toInt()).toLong()
this.maskColorInternal = typedArray.getColor(R.styleable.SkeletonLayout_maskColor, maskColorInternal)
this.cornerRadiusInternal = typedArray.getDimensionPixelSize(R.styleable.SkeletonLayout_maskCornerRadius, cornerRadiusInternal.toInt()).toFloat()
this.showShimmerInternal = typedArray.getBoolean(R.styleable.SkeletonLayout_showShimmer, showShimmerInternal)
this.shimmerColorInternal = typedArray.getColor(R.styleable.SkeletonLayout_shimmerColor, shimmerColorInternal)
this.shimmerDurationInMillisInternal = typedArray.getInt(R.styleable.SkeletonLayout_shimmerDurationInMillis, shimmerDurationInMillisInternal.toInt()).toLong()
typedArray.recycle()
}
originView?.let { addView(it) }
originView?.let { view -> addView(view) }
}

override fun showOriginal() {
Expand All @@ -100,7 +105,7 @@ class SkeletonLayout @JvmOverloads constructor(
mask?.invalidate()

} else {
Log.e(tag(), "Missing view to mask")
Log.i(tag(), "No views to mask")
}
}
}
Expand Down Expand Up @@ -131,8 +136,10 @@ class SkeletonLayout @JvmOverloads constructor(

override fun onAttachedToWindow() {
super.onAttachedToWindow()
invalidateMask()
mask?.start()
if (isRendered) {
invalidateMask()
mask?.start()
}
}

override fun onDetachedFromWindow() {
Expand All @@ -145,25 +152,14 @@ class SkeletonLayout @JvmOverloads constructor(
}

private fun invalidateMask() {
mask?.stop()

if (width == 0) {
Log.e(tag(), "Failed to mask due to width that is zero")
return
}

if (height == 0) {
Log.e(tag(), "Failed to mask due to height that is zero")
return
if (isRendered) {
mask?.stop()
mask = SkeletonMaskFactory
.createMask(this, maskColor, showShimmer, shimmerColor, shimmerDurationInMillis)
.also { mask -> mask.mask(this, maskCornerRadius) }
} else {
Log.e(tag(), "Skipping invalidation until view is rendered")
}

createMask()
}

private fun createMask() {
mask = SkeletonMaskFactory
.createMask(this, maskColor, showShimmer, shimmerColor, shimmerDurationInMillis)
.also { mask -> mask.mask(this, maskCornerRadius) }
}

companion object {
Expand Down

0 comments on commit 31065a4

Please sign in to comment.