Skip to content

Commit

Permalink
Null CHecks
Browse files Browse the repository at this point in the history
  • Loading branch information
deepan5901 committed Sep 18, 2018
1 parent 5bdfb85 commit f56d564
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import android.util.AttributeSet
import android.view.View

class DotsIndicator : LinearLayout {
private var mViewpager: ViewPager? = null
lateinit var mViewpager: ViewPager
private var mIndicatorMargin = -1
private var mIndicatorWidth = -1
private var mIndicatorHeight = -1
Expand All @@ -25,7 +25,7 @@ class DotsIndicator : LinearLayout {
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}

override fun onPageSelected(position: Int) {
if (mViewpager!!.adapter == null || mViewpager!!.adapter!!.count <= 0) return
if (mViewpager.adapter == null || mViewpager.adapter?.count ?: 0 <= 0) return
if (mLastPosition >= 0) getChildAt(mLastPosition)?.setBackgroundResource(mIndicatorUnselectedBackgroundResId)
getChildAt(position)?.setBackgroundResource(mIndicatorBackgroundResId)
mLastPosition = position
Expand All @@ -37,12 +37,11 @@ class DotsIndicator : LinearLayout {
val dataSetObserver: DataSetObserver = object : DataSetObserver() {
override fun onChanged() {
super.onChanged()
if (mViewpager == null) return
val newCount = mViewpager!!.adapter!!.count
val newCount = mViewpager.adapter?.count ?: 0
val currentCount = childCount
mLastPosition = when {
newCount == currentCount -> return
mLastPosition < newCount -> mViewpager!!.currentItem
mLastPosition < newCount -> mViewpager.currentItem
else -> -1
}
createIndicators()
Expand Down Expand Up @@ -87,29 +86,29 @@ class DotsIndicator : LinearLayout {
}

private fun checkIndicatorConfig() {
mIndicatorWidth = if (mIndicatorWidth < 0) dip2px(DEFAULT_INDICATOR_WIDTH.toFloat()) else mIndicatorWidth
mIndicatorHeight = if (mIndicatorHeight < 0) dip2px(DEFAULT_INDICATOR_WIDTH.toFloat()) else mIndicatorHeight
mIndicatorMargin = if (mIndicatorMargin < 0) dip2px(DEFAULT_INDICATOR_WIDTH.toFloat()) else mIndicatorMargin
mIndicatorWidth = if (mIndicatorWidth < 0) dpTopx(DEFAULT_INDICATOR_WIDTH.toFloat()) else mIndicatorWidth
mIndicatorHeight = if (mIndicatorHeight < 0) dpTopx(DEFAULT_INDICATOR_WIDTH.toFloat()) else mIndicatorHeight
mIndicatorMargin = if (mIndicatorMargin < 0) dpTopx(DEFAULT_INDICATOR_WIDTH.toFloat()) else mIndicatorMargin
mIndicatorBackgroundResId = if (mIndicatorBackgroundResId == 0) R.drawable.ic_dot_lightgrey else mIndicatorBackgroundResId
mIndicatorUnselectedBackgroundResId = if (mIndicatorUnselectedBackgroundResId == 0) mIndicatorBackgroundResId else mIndicatorUnselectedBackgroundResId
}

fun setViewPager(viewPager: ViewPager) {
mViewpager = viewPager
if (mViewpager != null && mViewpager!!.adapter != null) {
if (mViewpager.adapter != null) {
mLastPosition = -1
createIndicators()
mViewpager!!.removeOnPageChangeListener(mInternalPageChangeListener)
mViewpager!!.addOnPageChangeListener(mInternalPageChangeListener)
mInternalPageChangeListener.onPageSelected(mViewpager!!.currentItem)
mViewpager.removeOnPageChangeListener(mInternalPageChangeListener)
mViewpager.addOnPageChangeListener(mInternalPageChangeListener)
mInternalPageChangeListener.onPageSelected(mViewpager.currentItem)
}
}

private fun createIndicators() {
removeAllViews()
val count = mViewpager!!.adapter!!.count
val count = mViewpager.adapter?.count ?: 0
if (count <= 0) return
val currentItem = mViewpager!!.currentItem
val currentItem = mViewpager.currentItem
for (i in 0 until count) {
if (currentItem == i) addIndicator(mIndicatorBackgroundResId)
else addIndicator(mIndicatorUnselectedBackgroundResId)
Expand All @@ -128,12 +127,9 @@ class DotsIndicator : LinearLayout {
indicator.layoutParams = lp
}

fun dip2px(dpValue: Float): Int {
val scale = resources.displayMetrics.density
return (dpValue * scale + 0.5f).toInt()
}
fun dpTopx(dpValue: Float): Int = (dpValue * resources.displayMetrics.density + 0.5f).toInt()

companion object {
private val DEFAULT_INDICATOR_WIDTH = 5
private const val DEFAULT_INDICATOR_WIDTH = 5
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">

<com.make.dots.DotsIndicator
<com.make.dots.dotsindicator.DotsIndicator
android:id="@+id/dotsIndicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down

0 comments on commit f56d564

Please sign in to comment.