/
FastScrollerBuilder.kt
160 lines (137 loc) · 6.21 KB
/
FastScrollerBuilder.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* Copyright 2023-present StellarSand
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.stellarsand.android.fastscroll
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.view.ViewGroup
import android.webkit.WebView
import android.widget.ScrollView
import android.widget.TextView
import androidx.core.util.Consumer
import androidx.core.widget.NestedScrollView
import androidx.recyclerview.widget.RecyclerView
import me.stellarsand.android.fastscroll.FastScroller.AnimationHelper
class FastScrollerBuilder(private val mView: ViewGroup) {
private var mViewHelper: FastScroller.ViewHelper? = null
private var mPopupTextProvider: PopupTextProvider? = null
private var mPadding: Rect? = null
private var mTrackDrawable: Drawable? = null
private var mThumbDrawable: Drawable? = null
private var mPopupStyle: Consumer<TextView?>? = null
private var mAnimationHelper: AnimationHelper? = null
init {
useDefaultStyle()
}
fun setViewHelper(viewHelper: FastScroller.ViewHelper?): FastScrollerBuilder {
mViewHelper = viewHelper
return this
}
fun setPopupTextProvider(popupTextProvider: PopupTextProvider?): FastScrollerBuilder {
mPopupTextProvider = popupTextProvider
return this
}
fun setPadding(left: Int, top: Int, right: Int, bottom: Int): FastScrollerBuilder {
if (mPadding == null) {
mPadding = Rect()
}
mPadding !![left, top, right] = bottom
return this
}
fun setPadding(padding: Rect?): FastScrollerBuilder {
padding?.let {
if (mPadding == null) {
mPadding = Rect()
}
mPadding!!.set(it)
} ?: run {
mPadding = null
}
return this
}
fun setTrackDrawable(trackDrawable: Drawable): FastScrollerBuilder {
mTrackDrawable = trackDrawable
return this
}
fun setThumbDrawable(thumbDrawable: Drawable): FastScrollerBuilder {
mThumbDrawable = thumbDrawable
return this
}
fun setPopupStyle(popupStyle: Consumer<TextView?>): FastScrollerBuilder {
mPopupStyle = popupStyle
return this
}
fun useMd1Style(): FastScrollerBuilder {
val context = mView.context
mTrackDrawable = Utils.getGradientDrawableWithTintAttr(R.drawable.afs_md1_track,
android.R.attr.colorControlNormal,
context)!!
mThumbDrawable = Utils.getGradientDrawableWithTintAttr(R.drawable.afs_md1_thumb,
android.R.attr.colorControlActivated,
context)!!
mPopupStyle = PopupStyles.MD1
return this
}
fun useDefaultStyle(): FastScrollerBuilder {
val context = mView.context
mTrackDrawable = Utils.getGradientDrawableWithTintAttr(R.drawable.afs_default_track,
android.R.attr.colorControlNormal,
context)!!
mThumbDrawable = Utils.getGradientDrawableWithTintAttr(R.drawable.afs_default_thumb,
android.R.attr.colorControlActivated,
context)!!
mPopupStyle = PopupStyles.DEFAULT
return this
}
fun setAnimationHelper(animationHelper: AnimationHelper?) {
mAnimationHelper = animationHelper
}
fun disableScrollbarAutoHide() {
val animationHelper = DefaultAnimationHelper(mView)
animationHelper.setScrollbarAutoHideEnabled(false)
mAnimationHelper = animationHelper
}
fun build(): FastScroller {
return FastScroller(mView, getOrCreateViewHelper(), mPadding, mTrackDrawable!!,
mThumbDrawable!!, mPopupStyle!!, getOrCreateAnimationHelper())
}
private fun getOrCreateViewHelper(): FastScroller.ViewHelper {
return mViewHelper ?: when (mView) {
is ViewHelperProvider -> (mView as ViewHelperProvider).viewHelper
is RecyclerView -> RecyclerViewHelper(mView, mPopupTextProvider)
is NestedScrollView -> throwUnsupportedException(NestedScrollView::class.java)
is ScrollView -> throwUnsupportedException(ScrollView::class.java)
is WebView -> throwUnsupportedException(WebView::class.java)
else -> throwUnsupportedException(mView.javaClass)
}
}
private fun throwUnsupportedException(currentClass: Class<*>): Nothing {
val replacementClassName =
when (currentClass) {
NestedScrollView::class.java -> FastScrollNestedScrollView::class.java.simpleName
ScrollView::class.java -> FastScrollScrollView::class.java.simpleName
WebView::class.java -> FastScrollWebView::class.java.simpleName
else -> throw UnsupportedOperationException("${currentClass.simpleName} is not supported for fast scroll")
}
throw UnsupportedOperationException("Please use " +
"$replacementClassName " +
"instead of " +
"${currentClass.simpleName} " +
"for fast scroll")
}
private fun getOrCreateAnimationHelper(): AnimationHelper {
return mAnimationHelper ?: DefaultAnimationHelper(mView)
}
}