Skip to content

Commit 9e6ebb4

Browse files
committed
NSPtrConfig API changes
1 parent fed8885 commit 9e6ebb4

File tree

11 files changed

+145
-45
lines changed

11 files changed

+145
-45
lines changed

buildSrc/src/main/java/wtf/s1rius/ptr/buildsrc/Configs.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@ object ClassPaths {
5757
const val kotlinPlugin = "org.jetbrains.kotlin:kotlin-gradle-plugin:${Versions.kotlin}"
5858
const val bytex = "com.bytedance.android.byteX:base-plugin:${Versions.bytex}"
5959
const val hugoByteX = "wtf.s1.pudge:hugo2-bytex:0.1.4"
60-
const val mavenPlugin = "com.vanniktech:gradle-maven-publish-plugin:0.14.0"
61-
const val dokaa = "org.jetbrains.dokka:dokka-gradle-plugin:1.4.10.2"
60+
const val mavenPlugin = "com.vanniktech:gradle-maven-publish-plugin:0.17.0"
61+
const val dokaa = "org.jetbrains.dokka:dokka-gradle-plugin:1.4.32"
6262
}

ptr-demo/src/main/java/wtf/s1/android/ptr/demo/damping/DampingView.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ class DampingView @JvmOverloads constructor(
4343
SwipeToRefreshLayout(context).apply {
4444
config = object : NSPtrConfig {
4545
override fun requireLayout(): NSPtrLayout = this@apply
46-
override fun overToRefreshPosition(): Boolean = false
47-
override fun refreshPosition(): Int = Int.MAX_VALUE
48-
override fun atStartPosition(): Boolean = requireLayout().contentTopPosition == 0
46+
override fun isContentOverRefreshPosition(): Boolean = false
47+
override fun contentRefreshPosition(): Int = Int.MAX_VALUE
48+
override fun isContentAtInitPosition(): Boolean = requireLayout().contentTopPosition == 0
4949
override fun pullFriction(type: Int): Float =
5050
if (type == ViewCompat.TYPE_TOUCH) 0.8f else 2f
5151
}

ptr-lib/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import wtf.s1rius.ptr.buildsrc.*
33
apply plugin: 'com.android.library'
44
apply plugin: 'kotlin-android'
55
apply plugin: "com.vanniktech.maven.publish"
6-
6+
apply plugin: "org.jetbrains.dokka"
77

88
android {
99
compileSdkVersion Versions.compileSdkVersion
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
11
package wtf.s1.android.ptr
22

3+
/**
4+
* if NSPtrLayout child want hold the measure and layout, will implement this interface
5+
*/
36
interface NSPtrComponent {
47

8+
/**
9+
* NSPtrLayout will call this function in it's onMeasure() function.
10+
* dispatch measure event to this method, we need call child measure() function
11+
*/
512
fun prtMeasure(ptrLayout: NSPtrLayout,
613
parentWidthMeasureSpec: Int,
714
parentHeightMeasureSpec: Int){}
815

16+
/**
17+
* NSPtrLayout will call this function in it's onLayout() function.
18+
* implement this function and layout this view
19+
*/
920
fun ptrLayout(ptrLayout: NSPtrLayout){}
1021
}
1122

23+
/**
24+
* Header View need implement this interface,
25+
* There is one-to-one correspondence between NSPtrLayout and NSPtrHeader
26+
* or implement NSPtrListener
27+
* @see {@link NSPtrListener}
28+
*/
1229
interface NSPtrHeader: NSPtrComponent
1330

1431
interface NSPtrFooter: NSPtrComponent

ptr-lib/src/main/java/wtf/s1/android/ptr/NSPtrConfig.kt

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,47 @@ package wtf.s1.android.ptr
22

33
interface NSPtrConfig {
44

5+
/**
6+
* Return the {@link NSPtrLayout} this NSPtrLayout is currently associated with.
7+
*/
58
fun requireLayout(): NSPtrLayout
69

7-
fun startPosition(): Int = 0
8-
9-
fun atStartPosition(): Boolean = false
10-
11-
fun overToRefreshPosition(): Boolean = false
12-
13-
fun refreshPosition(): Int = Int.MAX_VALUE
14-
10+
/**
11+
* The distance in pixels from the top edge of the NSPtrLayout
12+
* to the top edge of NSPtrLayout's content View.
13+
*/
14+
fun contentInitPosition(): Int = 0
15+
16+
/**
17+
* Is content View's top at the contentInitPosition()
18+
*/
19+
fun isContentAtInitPosition(): Boolean = false
20+
21+
/**
22+
* At trigger refreshing moment, the distance in pixels from the top edge
23+
* of the NSPtrLayout to the top edge of NSPtrLayout's content View.
24+
*/
25+
fun contentRefreshPosition(): Int = Int.MAX_VALUE
26+
27+
/**
28+
* Is content View's top at the contentRefreshPosition()
29+
*/
30+
fun isContentOverRefreshPosition(): Boolean = false
31+
32+
/**
33+
* Sets the friction for the drag offset. The greater the friction is, the sooner the
34+
* offset will grow up. When not set, the friction defaults to 0.56f.
35+
* @param type @see {@link ViewCompat.NestedScrollType}
36+
*/
1537
fun pullFriction(type: Int): Float = 0.56f
1638

39+
/**
40+
* generate the state-machine event to transition state when touch up,
41+
* cancel or stop nested-scroll
42+
* @return the NSPtrLayout.Event
43+
*/
1744
fun generateTouchReleaseEvent(): NSPtrLayout.Event? {
18-
return if (overToRefreshPosition()) {
45+
return if (isContentOverRefreshPosition()) {
1946
NSPtrLayout.Event.ReleaseToRefreshing
2047
} else {
2148
NSPtrLayout.Event.ReleaseToIdle

ptr-lib/src/main/java/wtf/s1/android/ptr/NSPtrEZHeader.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import android.util.AttributeSet
55
import android.view.Gravity
66
import android.widget.FrameLayout
77

8+
/**
9+
* implement NSPtrHeader in easy way
10+
*/
811
open class NSPtrEZHeader @JvmOverloads constructor(
912
context: Context, attrs: AttributeSet? = null
1013
) : FrameLayout(context, attrs), NSPtrHeader, NSPtrListener {
@@ -41,7 +44,7 @@ open class NSPtrEZHeader @JvmOverloads constructor(
4144
if (mIsOverToRefresh != frame.isOverToRefreshPosition) {
4245
mIsOverToRefresh = frame.isOverToRefreshPosition
4346
}
44-
progressBar.progress = (frame.contentTopPosition * 100f / frame.config.refreshPosition()).toInt()
47+
progressBar.progress = (frame.contentTopPosition * 100f / frame.config.contentRefreshPosition()).toInt()
4548
}
4649
}
4750

ptr-lib/src/main/java/wtf/s1/android/ptr/NSPtrEZLayout.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package wtf.s1.android.ptr
33
import android.content.Context
44
import android.util.AttributeSet
55

6+
/**
7+
* implement NSPtrEZLayout in easy way
8+
*/
69
open class NSPtrEZLayout @JvmOverloads constructor(
710
context: Context, attrs: AttributeSet? = null
811
) : NSPtrLayout(context, attrs) {

ptr-lib/src/main/java/wtf/s1/android/ptr/NSPtrLayout.kt

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,34 +37,46 @@ open class NSPtrLayout @JvmOverloads constructor(
3737
}
3838

3939
sealed class State {
40+
// init state
4041
// 初始化状态
4142
object IDLE : State()
43+
// refreshing state
4244
// 刷新状态
4345
object REFRESHING : State()
46+
// drag state
4447
// 拖动状态
4548
object DRAG : State()
4649
}
4750

4851
sealed class Event {
52+
// pull/drag the content
4953
// 下拉事件
5054
object Pull : Event()
55+
// touch release make content view to IDLE
5156
// 放手回到空闲
5257
object ReleaseToIdle : Event()
58+
// touch release make content view to REFRESHING
5359
// 放手开始刷新
5460
object ReleaseToRefreshing : Event()
61+
// refresh complete
5562
// 刷新完成
5663
object RefreshComplete : Event()
64+
// auto refresh
5765
// 自动刷新
5866
object AutoRefresh : Event()
5967
}
6068

6169
sealed class SideEffect {
70+
// detect touch release or other trigger transition to IDLE
6271
// 手势取消回到顶部
63-
object OnCancelToIdle : SideEffect()
72+
object OnToIdle : SideEffect()
73+
// detect refreshing action
6474
// 刷新动作
6575
object OnRefreshing : SideEffect()
76+
// detect drag action
6677
// 开始拖动控件
6778
object OnDragBegin : SideEffect()
79+
// detect refresh complete
6880
// 刷新完成的动作
6981
object OnComplete : SideEffect()
7082
}
@@ -93,7 +105,7 @@ open class NSPtrLayout @JvmOverloads constructor(
93105

94106
state<State.DRAG> {
95107
on<Event.ReleaseToIdle> {
96-
transitionTo(State.IDLE, SideEffect.OnCancelToIdle)
108+
transitionTo(State.IDLE, SideEffect.OnToIdle)
97109
}
98110
on<Event.ReleaseToRefreshing> {
99111
transitionTo(State.REFRESHING, SideEffect.OnRefreshing)
@@ -115,7 +127,7 @@ open class NSPtrLayout @JvmOverloads constructor(
115127
SideEffect.OnRefreshing -> {
116128
performRefresh()
117129
}
118-
SideEffect.OnCancelToIdle -> {
130+
SideEffect.OnToIdle -> {
119131
tryScrollBackToTop()
120132
}
121133
}
@@ -132,19 +144,19 @@ open class NSPtrLayout @JvmOverloads constructor(
132144
return this@NSPtrLayout
133145
}
134146

135-
override fun startPosition(): Int {
147+
override fun contentInitPosition(): Int {
136148
return 0
137149
}
138150

139-
override fun atStartPosition(): Boolean {
140-
return contentTopPosition == startPosition()
151+
override fun isContentAtInitPosition(): Boolean {
152+
return contentTopPosition == contentInitPosition()
141153
}
142154

143-
override fun overToRefreshPosition(): Boolean {
155+
override fun isContentOverRefreshPosition(): Boolean {
144156
return contentTopPosition > headerView?.height ?: measuredHeight
145157
}
146158

147-
override fun refreshPosition(): Int {
159+
override fun contentRefreshPosition(): Int {
148160
return headerView?.height ?: measuredHeight
149161
}
150162

@@ -384,7 +396,7 @@ open class NSPtrLayout @JvmOverloads constructor(
384396
// like ListView, we need enable nest scroll in intercept process
385397
val y = (ev.getY(pointerIndex) + 0.5f).toInt()
386398
val dy = (mLastTouch.y - y).toInt()
387-
if (config.atStartPosition() || dy > 0) {
399+
if (config.isContentAtInitPosition() || dy > 0) {
388400
if (dispatchNestedPreScroll(0, dy, mScrollConsumed, mScrollOffset)) {
389401
mLastTouch.y = (y - mScrollOffset[1]).toFloat()
390402
// handle touch when parent not accept nest scroll
@@ -432,7 +444,7 @@ open class NSPtrLayout @JvmOverloads constructor(
432444

433445
val y = (event.getY(pointerIndex) + 0.5f).toInt()
434446
var dy = (y - mLastTouch.y).toInt()
435-
if (dy > 0 || config.atStartPosition()) {
447+
if (dy > 0 || config.isContentAtInitPosition()) {
436448
if (dispatchNestedPreScroll(0, -dy, mScrollConsumed, mScrollOffset)) {
437449
mLastTouch.y = (y - mScrollOffset[1]).toFloat()
438450
// handle touch when parent not accept nest scroll
@@ -448,7 +460,7 @@ open class NSPtrLayout @JvmOverloads constructor(
448460
}
449461
val moveDown = dy > 0
450462
val moveUp = !moveDown
451-
val canMoveUp = config.atStartPosition()
463+
val canMoveUp = config.isContentAtInitPosition()
452464
if (contentTopPosition != 0) {
453465
movePos(dy.toFloat())
454466
return true
@@ -469,14 +481,14 @@ open class NSPtrLayout @JvmOverloads constructor(
469481
*/
470482
private fun movePos(deltaY: Float): Int {
471483
// has reached the top
472-
if (deltaY < 0 && config.atStartPosition()) {
484+
if (deltaY < 0 && config.isContentAtInitPosition()) {
473485
return 0
474486
}
475487
var to = contentTopPosition + deltaY.toInt()
476488

477489
// over top
478-
if (to < config.startPosition()) {
479-
to = config.startPosition()
490+
if (to < config.contentInitPosition()) {
491+
to = config.contentInitPosition()
480492
}
481493

482494
val change = to - contentTopPosition
@@ -516,7 +528,7 @@ open class NSPtrLayout @JvmOverloads constructor(
516528
if (isNestedScroll || event == null) {
517529
when (stateMachine.state) {
518530
is State.IDLE -> {
519-
if (contentTopPosition != config.startPosition()) {
531+
if (contentTopPosition != config.contentInitPosition()) {
520532
mScrollChecker.scrollToStart()
521533
}
522534
}
@@ -621,7 +633,7 @@ open class NSPtrLayout @JvmOverloads constructor(
621633
var isOverToRefreshPosition: Boolean = false
622634
private set
623635
get() {
624-
return config.overToRefreshPosition()
636+
return config.isContentOverRefreshPosition()
625637
}
626638

627639
var headerView: View?
@@ -833,7 +845,7 @@ open class NSPtrLayout @JvmOverloads constructor(
833845
override fun onNestedPreScroll(target: View, dx: Int, dy: Int, consumed: IntArray, type: Int) {
834846
// If we are in the middle of consuming, a scroll, then we want to move the ptr back up
835847
// before allowing the list to scroll
836-
if (dy > 0 && !config.atStartPosition()) {
848+
if (dy > 0 && !config.isContentAtInitPosition()) {
837849
consumed[1] = -movePos(-dy.toFloat())
838850
}
839851

@@ -1006,11 +1018,11 @@ open class NSPtrLayout @JvmOverloads constructor(
10061018
}
10071019

10081020
fun scrollToRefreshing() {
1009-
mScrollChecker.tryToScrollTo(config.refreshPosition())
1021+
mScrollChecker.tryToScrollTo(config.contentRefreshPosition())
10101022
}
10111023

10121024
fun scrollToStart() {
1013-
mScrollChecker.tryToScrollTo(config.startPosition())
1025+
mScrollChecker.tryToScrollTo(config.contentInitPosition())
10141026
}
10151027

10161028
private fun tryToScrollTo(to: Int, duration: Int = 200) {

ptr-lib/src/main/java/wtf/s1/android/ptr/NSPtrListener.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package wtf.s1.android.ptr
22

33
interface NSPtrListener {
44
/**
5-
* prepare for loading
5+
* perform dragging the content view
66
*
77
* @param ptrLayout
88
*/
@@ -38,7 +38,7 @@ interface NSPtrListener {
3838
}
3939

4040
/**
41-
* drag the content view and move it
41+
* drag or animate the content view and move it
4242
*/
43-
fun onPositionChange(frame: NSPtrLayout, offset: Int) {}
43+
fun onPositionChange(ptrLayout: NSPtrLayout, offset: Int) {}
4444
}

ptr-lib/src/main/java/wtf/s1/android/ptr/NSPtrListenerHolder.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import java.util.concurrent.CopyOnWriteArrayList
77
*/
88
internal class NSPtrListenerHolder: NSPtrListener {
99

10-
var listeners = CopyOnWriteArrayList<NSPtrListener>()
10+
private var listeners = CopyOnWriteArrayList<NSPtrListener>()
1111

1212
fun hasHandler(): Boolean {
1313
return listeners.size > 0
@@ -23,9 +23,9 @@ internal class NSPtrListenerHolder: NSPtrListener {
2323
listeners.remove(listener)
2424
}
2525

26-
override fun onComplete(frame: NSPtrLayout) {
26+
override fun onComplete(ptrLayout: NSPtrLayout) {
2727
for (listener in listeners) {
28-
listener.onComplete(frame)
28+
listener.onComplete(ptrLayout)
2929
}
3030
}
3131

0 commit comments

Comments
 (0)