Skip to content

Commit

Permalink
Chore - Code Cleanup (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
chahine authored Jun 21, 2019
1 parent 5860a4b commit a3d3325
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 88 deletions.
35 changes: 23 additions & 12 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 23 additions & 28 deletions app/src/main/java/com/chahinem/pageindicator/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearSnapHelper
import com.chahinem.pageindicator.sample.MyAdapter.MyItem
import com.squareup.picasso.Picasso.Builder
import kotlinx.android.synthetic.main.activity_main.leftBtn
import kotlinx.android.synthetic.main.activity_main.list
import kotlinx.android.synthetic.main.activity_main.manualPageIndicator
import kotlinx.android.synthetic.main.activity_main.pageIndicator
import kotlinx.android.synthetic.main.activity_main.pager
import kotlinx.android.synthetic.main.activity_main.pagerPageIndicator
import kotlinx.android.synthetic.main.activity_main.rightBtn
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

Expand All @@ -27,6 +21,7 @@ class MainActivity : AppCompatActivity() {
LinearSnapHelper().attachToRecyclerView(list)
adapter.swapData(LIST_ITEMS)
pageIndicator attachTo list
pageIndicator2 attachTo list

// ViewPager
val myPagerAdapter = MyPagerAdapter(picasso, LIST_ITEMS)
Expand All @@ -42,33 +37,33 @@ class MainActivity : AppCompatActivity() {
companion object {
private val LIST_ITEMS = listOf(
MyItem(
"San Francisco, California",
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod",
"https://scontent-cdg2-1.cdninstagram.com/t51.2885-15/e35/25024416_182774512460963_6661054269581426688_n.jpg"),
"Cormorant fishing at sunset",
"Patryk Wojciechowicz",
"https://cdn.dribbble.com/users/3178178/screenshots/6287074/cormorant_fishing_1600x1200_final_04_05_2019_4x.jpg"),
MyItem(
"Valensole",
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod",
"https://scontent-cdg2-1.cdninstagram.com/t51.2885-15/e35/25022757_375109192929045_3153371362454667264_n.jpg"),
"Mountain House",
"Alex Pasquarella",
"https://cdn.dribbble.com/users/989466/screenshots/6100954/cabin-2-dribbble-alex-pasquarella_4x.png"),
MyItem(
"Étretat",
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod",
"https://scontent-cdg2-1.cdninstagram.com/t51.2885-15/e35/25005816_2020789231533395_4507938399037947904_n.jpg"),
"journey",
"Febin_Raj",
"https://cdn.dribbble.com/users/1803663/screenshots/6163551/nature-4_4x.png"),
MyItem(
"Tokyo, Japan",
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod",
"https://scontent-cdg2-1.cdninstagram.com/t51.2885-15/e35/23421860_192904647945945_6319320906002857984_n.jpg"),
"Explorer",
"Uran",
"https://cdn.dribbble.com/users/1355613/screenshots/6441984/landscape_4x.jpg"),
MyItem(
"Paris, France",
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod",
"https://scontent-cdg2-1.cdninstagram.com/t51.2885-15/e35/23347334_633654673691303_8149777143685971968_n.jpg"),
"Fishers Peak Limited Edition Print",
"Brian Edward Miller ",
"https://cdn.dribbble.com/users/329207/screenshots/6128300/bemocs_fisherspeak_dribbble.jpg"),
MyItem(
"New York, New York",
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod",
"https://scontent-cdg2-1.cdninstagram.com/t51.2885-15/e35/23101824_1822687121355425_8930059065124454400_n.jpg"),
"First Man",
"Lana Marandina",
"https://cdn.dribbble.com/users/1461762/screenshots/6280906/first_man_lana_marandina_4x.png"),
MyItem(
"New York, New York",
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod",
"https://scontent-cdg2-1.cdninstagram.com/t51.2885-15/e35/23161047_853121148199404_5154039615094521856_n.jpg")
"On The Road Again",
"Brian Edward Miller",
"https://cdn.dribbble.com/users/329207/screenshots/6522800/2026_nationwide_02_train_landscape_v01.00.jpg")
)
}
}
20 changes: 18 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
Expand Down Expand Up @@ -37,6 +37,22 @@
android:layout_marginTop="8dp"
/>

<com.chahinem.pageindicator.PageIndicator
android:id="@+id/pageIndicator2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_marginTop="8dp"
app:piDotSpacing="8dp"
app:piInitialPadding="16dp"
app:piSize1="8dp"
app:piSize2="8dp"
app:piSize3="8dp"
app:piSize4="8dp"
app:piSize5="8dp"
app:piSize6="8dp"
/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down Expand Up @@ -107,4 +123,4 @@
style="@style/TextAppearance.AppCompat.Caption"
/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</ScrollView>
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ internal class DotManager(
}

private fun goToNextLarge() {
var needScroll = false

// swap 6 and 5
dots[selectedIndex] = 6
dots[selectedIndex - 1] = 5
Expand All @@ -83,7 +81,6 @@ internal class DotManager(
&& dots[selectedIndex - 3] == 5.toByte()
&& dots[selectedIndex - 4] == 5.toByte()) {
dots[selectedIndex - 4] = 4
needScroll = true
if (selectedIndex - 5 >= 0) {
dots[selectedIndex - 5] = 2
(selectedIndex - 6 downTo 0)
Expand All @@ -92,23 +89,20 @@ internal class DotManager(
}
}

// 6 must around around 3 or higher
// 6 must be around 3 or higher
if (selectedIndex + 1 < dots.size && dots[selectedIndex + 1] < 3) {
dots[selectedIndex + 1] = 3
needScroll = true
// set the next one to 1 if any
if (selectedIndex + 2 < dots.size && dots[selectedIndex + 2] < 1) {
dots[selectedIndex + 2] = 1
}
}

// Scroll to keep the selected dot within bound
if (needScroll) {
val endBound = selectedIndex * (dotSize + dotSpacing) + dotSize
if (endBound > dotBound) {
scrollAmount = endBound - dotBound
targetScrollListener?.scrollToTarget(scrollAmount)
}
val endBound = selectedIndex * (dotSize + dotSpacing) + dotSize
if (endBound > dotBound) {
scrollAmount = endBound - dotBound
targetScrollListener?.scrollToTarget(scrollAmount)
}
}

Expand All @@ -118,7 +112,6 @@ internal class DotManager(
}

private fun goToPreviousLarge() {
var needScroll = false
// swap 6 and 5
dots[selectedIndex] = 6
dots[selectedIndex + 1] = 5
Expand All @@ -130,7 +123,6 @@ internal class DotManager(
&& dots[selectedIndex + 3] == 5.toByte()
&& dots[selectedIndex + 4] == 5.toByte()) {
dots[selectedIndex + 4] = 4
needScroll = true
if (selectedIndex + 5 < dots.size) {
dots[selectedIndex + 5] = 2
(selectedIndex + 6 until dots.size)
Expand All @@ -139,9 +131,8 @@ internal class DotManager(
}
}

// 6 must around around 3 or higher
// 6 must be around 3 or higher
if (selectedIndex - 1 >= 0 && dots[selectedIndex - 1] < 3) {
needScroll = true
dots[selectedIndex - 1] = 3
// set the next one to 1 if any
if (selectedIndex - 2 >= 0 && dots[selectedIndex - 2] < 1) {
Expand All @@ -150,12 +141,10 @@ internal class DotManager(
}

// Scroll to keep the selected dot within bound
if (needScroll) {
val startBound = selectedIndex * (dotSize + dotSpacing)
if (startBound < scrollAmount) {
scrollAmount = selectedIndex * (dotSize + dotSpacing)
targetScrollListener?.scrollToTarget(scrollAmount)
}
val startBound = selectedIndex * (dotSize + dotSpacing)
if (startBound < scrollAmount) {
scrollAmount = selectedIndex * (dotSize + dotSpacing)
targetScrollListener?.scrollToTarget(scrollAmount)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import com.chahinem.pageindicator.DotManager.TargetScrollListener
import kotlin.math.max
import kotlin.math.min

class PageIndicator @JvmOverloads constructor(
open class PageIndicator @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
Expand All @@ -34,6 +36,8 @@ class PageIndicator @JvmOverloads constructor(
private val dotSpacing: Int
private val animDuration: Long
private val animInterpolator: Interpolator
private var centered: Boolean = true
private val customInitalPadding: Int

private var dotManager: DotManager? = null
private var scrollAmount: Int = 0
Expand All @@ -57,10 +61,16 @@ class PageIndicator @JvmOverloads constructor(
dotManager?.let { it.dots.forEachIndexed { index, dot -> dotSizes[index] = it.dotSizeFor(dot) } }
dotAnimators = Array(value) { ValueAnimator() }

initialPadding = when (value) {
in 0..4 -> (dotBound + (4 - value) * (dotSize + dotSpacing) + dotSpacing) / 2
else -> 2 * (dotSize + dotSpacing)
initialPadding = when {
!centered -> 0
customInitalPadding != -1 -> customInitalPadding
else -> when (value) {
in 0..4 -> (dotBound + (4 - value) * (dotSize + dotSpacing) + dotSpacing) / 2
else -> 2 * (dotSize + dotSpacing)
}
}

field = value
invalidate()
}

Expand All @@ -76,7 +86,9 @@ class PageIndicator @JvmOverloads constructor(
)
dotSize = dotSizeMap.values.max() ?: 0
dotSpacing = ta.getDimensionPixelSize(R.styleable.PageIndicator_piDotSpacing, 3.dp)
centered = ta.getBoolean(R.styleable.PageIndicator_piCentered, true)
dotBound = ta.getDimensionPixelSize(R.styleable.PageIndicator_piDotBound, 40.dp)
customInitalPadding = ta.getDimensionPixelSize(R.styleable.PageIndicator_piInitialPadding, -1)

animDuration = ta.getInteger(
R.styleable.PageIndicator_piAnimDuration, DEFAULT_ANIM_DURATION).toLong()
Expand All @@ -95,7 +107,7 @@ class PageIndicator @JvmOverloads constructor(
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
// FIXME: add support for `match_parent`
setMeasuredDimension(4 * (dotSize + dotSpacing) + dotBound, dotSize)
setMeasuredDimension(4 * (dotSize + dotSpacing) + dotBound + initialPadding, dotSize)
}

override fun onDraw(canvas: Canvas?) {
Expand Down Expand Up @@ -207,9 +219,8 @@ class PageIndicator @JvmOverloads constructor(
}

private fun getDrawingRange(): Pair<Int, Int> {
val start = Math.max(0, (dotManager?.selectedIndex ?: 0) - MOST_VISIBLE_COUNT)
val end = Math.min(
dotManager?.dots?.size ?: 0,
val start = max(0, (dotManager?.selectedIndex ?: 0) - MOST_VISIBLE_COUNT)
val end = min(dotManager?.dots?.size ?: 0,
(dotManager?.selectedIndex ?: 0) + MOST_VISIBLE_COUNT)
return Pair(start, end)
}
Expand Down
2 changes: 2 additions & 0 deletions pageindicator/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<declare-styleable name="PageIndicator">
<attr format="dimension" name="piDotSpacing"/>
<attr format="dimension" name="piDotBound"/>
<attr format="dimension" name="piInitialPadding"/>
<attr format="boolean" name="piCentered"/>

<attr format="dimension" name="piSize1"/>
<attr format="dimension" name="piSize2"/>
Expand Down

This file was deleted.

0 comments on commit a3d3325

Please sign in to comment.