Skip to content

Commit a3d3325

Browse files
authored
Chore - Code Cleanup (#33)
1 parent 5860a4b commit a3d3325

File tree

7 files changed

+95
-88
lines changed

7 files changed

+95
-88
lines changed

.idea/codeStyles/Project.xml

Lines changed: 23 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/java/com/chahinem/pageindicator/sample/MainActivity.kt

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,7 @@ import androidx.appcompat.app.AppCompatActivity
55
import androidx.recyclerview.widget.LinearSnapHelper
66
import com.chahinem.pageindicator.sample.MyAdapter.MyItem
77
import com.squareup.picasso.Picasso.Builder
8-
import kotlinx.android.synthetic.main.activity_main.leftBtn
9-
import kotlinx.android.synthetic.main.activity_main.list
10-
import kotlinx.android.synthetic.main.activity_main.manualPageIndicator
11-
import kotlinx.android.synthetic.main.activity_main.pageIndicator
12-
import kotlinx.android.synthetic.main.activity_main.pager
13-
import kotlinx.android.synthetic.main.activity_main.pagerPageIndicator
14-
import kotlinx.android.synthetic.main.activity_main.rightBtn
8+
import kotlinx.android.synthetic.main.activity_main.*
159

1610
class MainActivity : AppCompatActivity() {
1711

@@ -27,6 +21,7 @@ class MainActivity : AppCompatActivity() {
2721
LinearSnapHelper().attachToRecyclerView(list)
2822
adapter.swapData(LIST_ITEMS)
2923
pageIndicator attachTo list
24+
pageIndicator2 attachTo list
3025

3126
// ViewPager
3227
val myPagerAdapter = MyPagerAdapter(picasso, LIST_ITEMS)
@@ -42,33 +37,33 @@ class MainActivity : AppCompatActivity() {
4237
companion object {
4338
private val LIST_ITEMS = listOf(
4439
MyItem(
45-
"San Francisco, California",
46-
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod",
47-
"https://scontent-cdg2-1.cdninstagram.com/t51.2885-15/e35/25024416_182774512460963_6661054269581426688_n.jpg"),
40+
"Cormorant fishing at sunset",
41+
"Patryk Wojciechowicz",
42+
"https://cdn.dribbble.com/users/3178178/screenshots/6287074/cormorant_fishing_1600x1200_final_04_05_2019_4x.jpg"),
4843
MyItem(
49-
"Valensole",
50-
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod",
51-
"https://scontent-cdg2-1.cdninstagram.com/t51.2885-15/e35/25022757_375109192929045_3153371362454667264_n.jpg"),
44+
"Mountain House",
45+
"Alex Pasquarella",
46+
"https://cdn.dribbble.com/users/989466/screenshots/6100954/cabin-2-dribbble-alex-pasquarella_4x.png"),
5247
MyItem(
53-
"Étretat",
54-
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod",
55-
"https://scontent-cdg2-1.cdninstagram.com/t51.2885-15/e35/25005816_2020789231533395_4507938399037947904_n.jpg"),
48+
"journey",
49+
"Febin_Raj",
50+
"https://cdn.dribbble.com/users/1803663/screenshots/6163551/nature-4_4x.png"),
5651
MyItem(
57-
"Tokyo, Japan",
58-
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod",
59-
"https://scontent-cdg2-1.cdninstagram.com/t51.2885-15/e35/23421860_192904647945945_6319320906002857984_n.jpg"),
52+
"Explorer",
53+
"Uran",
54+
"https://cdn.dribbble.com/users/1355613/screenshots/6441984/landscape_4x.jpg"),
6055
MyItem(
61-
"Paris, France",
62-
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod",
63-
"https://scontent-cdg2-1.cdninstagram.com/t51.2885-15/e35/23347334_633654673691303_8149777143685971968_n.jpg"),
56+
"Fishers Peak Limited Edition Print",
57+
"Brian Edward Miller ",
58+
"https://cdn.dribbble.com/users/329207/screenshots/6128300/bemocs_fisherspeak_dribbble.jpg"),
6459
MyItem(
65-
"New York, New York",
66-
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod",
67-
"https://scontent-cdg2-1.cdninstagram.com/t51.2885-15/e35/23101824_1822687121355425_8930059065124454400_n.jpg"),
60+
"First Man",
61+
"Lana Marandina",
62+
"https://cdn.dribbble.com/users/1461762/screenshots/6280906/first_man_lana_marandina_4x.png"),
6863
MyItem(
69-
"New York, New York",
70-
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod",
71-
"https://scontent-cdg2-1.cdninstagram.com/t51.2885-15/e35/23161047_853121148199404_5154039615094521856_n.jpg")
64+
"On The Road Again",
65+
"Brian Edward Miller",
66+
"https://cdn.dribbble.com/users/329207/screenshots/6522800/2026_nationwide_02_train_landscape_v01.00.jpg")
7267
)
7368
}
7469
}

app/src/main/res/layout/activity_main.xml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<androidx.core.widget.NestedScrollView
2+
<ScrollView
33
xmlns:android="http://schemas.android.com/apk/res/android"
44
xmlns:app="http://schemas.android.com/apk/res-auto"
55
xmlns:tools="http://schemas.android.com/tools"
@@ -37,6 +37,22 @@
3737
android:layout_marginTop="8dp"
3838
/>
3939

40+
<com.chahinem.pageindicator.PageIndicator
41+
android:id="@+id/pageIndicator2"
42+
android:layout_width="match_parent"
43+
android:layout_height="wrap_content"
44+
android:layout_gravity="start"
45+
android:layout_marginTop="8dp"
46+
app:piDotSpacing="8dp"
47+
app:piInitialPadding="16dp"
48+
app:piSize1="8dp"
49+
app:piSize2="8dp"
50+
app:piSize3="8dp"
51+
app:piSize4="8dp"
52+
app:piSize5="8dp"
53+
app:piSize6="8dp"
54+
/>
55+
4056
<TextView
4157
android:layout_width="match_parent"
4258
android:layout_height="wrap_content"
@@ -107,4 +123,4 @@
107123
style="@style/TextAppearance.AppCompat.Caption"
108124
/>
109125
</LinearLayout>
110-
</androidx.core.widget.NestedScrollView>
126+
</ScrollView>

pageindicator/src/main/java/com/chahinem/pageindicator/DotManager.kt

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ internal class DotManager(
7070
}
7171

7272
private fun goToNextLarge() {
73-
var needScroll = false
74-
7573
// swap 6 and 5
7674
dots[selectedIndex] = 6
7775
dots[selectedIndex - 1] = 5
@@ -83,7 +81,6 @@ internal class DotManager(
8381
&& dots[selectedIndex - 3] == 5.toByte()
8482
&& dots[selectedIndex - 4] == 5.toByte()) {
8583
dots[selectedIndex - 4] = 4
86-
needScroll = true
8784
if (selectedIndex - 5 >= 0) {
8885
dots[selectedIndex - 5] = 2
8986
(selectedIndex - 6 downTo 0)
@@ -92,23 +89,20 @@ internal class DotManager(
9289
}
9390
}
9491

95-
// 6 must around around 3 or higher
92+
// 6 must be around 3 or higher
9693
if (selectedIndex + 1 < dots.size && dots[selectedIndex + 1] < 3) {
9794
dots[selectedIndex + 1] = 3
98-
needScroll = true
9995
// set the next one to 1 if any
10096
if (selectedIndex + 2 < dots.size && dots[selectedIndex + 2] < 1) {
10197
dots[selectedIndex + 2] = 1
10298
}
10399
}
104100

105101
// Scroll to keep the selected dot within bound
106-
if (needScroll) {
107-
val endBound = selectedIndex * (dotSize + dotSpacing) + dotSize
108-
if (endBound > dotBound) {
109-
scrollAmount = endBound - dotBound
110-
targetScrollListener?.scrollToTarget(scrollAmount)
111-
}
102+
val endBound = selectedIndex * (dotSize + dotSpacing) + dotSize
103+
if (endBound > dotBound) {
104+
scrollAmount = endBound - dotBound
105+
targetScrollListener?.scrollToTarget(scrollAmount)
112106
}
113107
}
114108

@@ -118,7 +112,6 @@ internal class DotManager(
118112
}
119113

120114
private fun goToPreviousLarge() {
121-
var needScroll = false
122115
// swap 6 and 5
123116
dots[selectedIndex] = 6
124117
dots[selectedIndex + 1] = 5
@@ -130,7 +123,6 @@ internal class DotManager(
130123
&& dots[selectedIndex + 3] == 5.toByte()
131124
&& dots[selectedIndex + 4] == 5.toByte()) {
132125
dots[selectedIndex + 4] = 4
133-
needScroll = true
134126
if (selectedIndex + 5 < dots.size) {
135127
dots[selectedIndex + 5] = 2
136128
(selectedIndex + 6 until dots.size)
@@ -139,9 +131,8 @@ internal class DotManager(
139131
}
140132
}
141133

142-
// 6 must around around 3 or higher
134+
// 6 must be around 3 or higher
143135
if (selectedIndex - 1 >= 0 && dots[selectedIndex - 1] < 3) {
144-
needScroll = true
145136
dots[selectedIndex - 1] = 3
146137
// set the next one to 1 if any
147138
if (selectedIndex - 2 >= 0 && dots[selectedIndex - 2] < 1) {
@@ -150,12 +141,10 @@ internal class DotManager(
150141
}
151142

152143
// Scroll to keep the selected dot within bound
153-
if (needScroll) {
154-
val startBound = selectedIndex * (dotSize + dotSpacing)
155-
if (startBound < scrollAmount) {
156-
scrollAmount = selectedIndex * (dotSize + dotSpacing)
157-
targetScrollListener?.scrollToTarget(scrollAmount)
158-
}
144+
val startBound = selectedIndex * (dotSize + dotSpacing)
145+
if (startBound < scrollAmount) {
146+
scrollAmount = selectedIndex * (dotSize + dotSpacing)
147+
targetScrollListener?.scrollToTarget(scrollAmount)
159148
}
160149
}
161150

pageindicator/src/main/java/com/chahinem/pageindicator/PageIndicator.kt

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ import androidx.recyclerview.widget.RecyclerView
1515
import androidx.viewpager.widget.PagerAdapter
1616
import androidx.viewpager.widget.ViewPager
1717
import com.chahinem.pageindicator.DotManager.TargetScrollListener
18+
import kotlin.math.max
19+
import kotlin.math.min
1820

19-
class PageIndicator @JvmOverloads constructor(
21+
open class PageIndicator @JvmOverloads constructor(
2022
context: Context,
2123
attrs: AttributeSet? = null,
2224
defStyleAttr: Int = 0
@@ -34,6 +36,8 @@ class PageIndicator @JvmOverloads constructor(
3436
private val dotSpacing: Int
3537
private val animDuration: Long
3638
private val animInterpolator: Interpolator
39+
private var centered: Boolean = true
40+
private val customInitalPadding: Int
3741

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

60-
initialPadding = when (value) {
61-
in 0..4 -> (dotBound + (4 - value) * (dotSize + dotSpacing) + dotSpacing) / 2
62-
else -> 2 * (dotSize + dotSpacing)
64+
initialPadding = when {
65+
!centered -> 0
66+
customInitalPadding != -1 -> customInitalPadding
67+
else -> when (value) {
68+
in 0..4 -> (dotBound + (4 - value) * (dotSize + dotSpacing) + dotSpacing) / 2
69+
else -> 2 * (dotSize + dotSpacing)
70+
}
6371
}
72+
73+
field = value
6474
invalidate()
6575
}
6676

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

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

101113
override fun onDraw(canvas: Canvas?) {
@@ -207,9 +219,8 @@ class PageIndicator @JvmOverloads constructor(
207219
}
208220

209221
private fun getDrawingRange(): Pair<Int, Int> {
210-
val start = Math.max(0, (dotManager?.selectedIndex ?: 0) - MOST_VISIBLE_COUNT)
211-
val end = Math.min(
212-
dotManager?.dots?.size ?: 0,
222+
val start = max(0, (dotManager?.selectedIndex ?: 0) - MOST_VISIBLE_COUNT)
223+
val end = min(dotManager?.dots?.size ?: 0,
213224
(dotManager?.selectedIndex ?: 0) + MOST_VISIBLE_COUNT)
214225
return Pair(start, end)
215226
}

pageindicator/src/main/res/values/attrs.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
<declare-styleable name="PageIndicator">
44
<attr format="dimension" name="piDotSpacing"/>
55
<attr format="dimension" name="piDotBound"/>
6+
<attr format="dimension" name="piInitialPadding"/>
7+
<attr format="boolean" name="piCentered"/>
68

79
<attr format="dimension" name="piSize1"/>
810
<attr format="dimension" name="piSize2"/>

pageindicator/src/test/java/com/chahinem/pageindicator/ExampleUnitTest.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)