Skip to content

Commit 79d3fdf

Browse files
committed
pref: ToastUtil.kt opt
1 parent be59f6a commit 79d3fdf

File tree

3 files changed

+123
-8
lines changed

3 files changed

+123
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ dependencyResolutionManagement {
2727
```groovy
2828
dependencies {
2929
// add sdk-ktx to your dependencies
30-
implementation 'com.github.pslilysm:sdk-ktx:2.0.3'
30+
implementation 'com.github.pslilysm:sdk-ktx:2.0.4'
3131
}
3232
```
3333

app/src/main/java/perx/pslilysm/sdk_ktx/SdkTest.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package perx.pslilysm.sdk_ktx
22

3+
import android.os.SystemClock
34
import android.util.Log
45
import com.tencent.mmkv.MMKV
56
import pers.pslilysm.sdk_library.AppHolder
@@ -64,7 +65,19 @@ object SdkTest {
6465
Log.i(TAG, "test: run")
6566
}
6667
ThreadUtil.checkIsMainThread()
67-
ToastUtil.showShort("sdk test")
68+
GlobalExecutors.io().execute {
69+
ToastUtil.showShort("sdk test")
70+
SystemClock.sleep(1000)
71+
ToastUtil.showShort("sdk test1")
72+
SystemClock.sleep(1000)
73+
ToastUtil.showShort("sdk test2")
74+
SystemClock.sleep(1000)
75+
ToastUtil.showShort("sdk test3")
76+
SystemClock.sleep(1000)
77+
ToastUtil.showLong("sdk test4")
78+
SystemClock.sleep(1000)
79+
ToastUtil.showLong("sdk test5")
80+
}
6881
}
6982

7083
}

sdk-library/src/main/java/pers/pslilysm/sdk_library/util/ToastUtil.kt

Lines changed: 108 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,129 @@ import pers.pslilysm.sdk_library.EventHandler
1414
object ToastUtil {
1515
@kotlin.jvm.JvmStatic
1616
fun showShort(text: CharSequence) {
17-
showToast(text, Toast.LENGTH_SHORT)
17+
showToast(
18+
ToastProp.newProp()
19+
.lengthShort(text)
20+
)
1821
}
1922

2023
@kotlin.jvm.JvmStatic
2124
fun showLong(text: CharSequence) {
22-
showToast(text, Toast.LENGTH_LONG)
25+
showToast(
26+
ToastProp.newProp()
27+
.lengthLong(text)
28+
)
2329
}
2430

25-
private fun showToast(text: CharSequence, duration: Int) {
26-
val runnable = ShowToastRunnable(text, duration)
31+
@kotlin.jvm.JvmStatic
32+
fun showToast(toastProp: ToastProp) {
33+
val runnable = ShowToastRunnable(toastProp)
2734
if (Looper.myLooper() == null) {
2835
EventHandler.default.post(runnable)
2936
} else {
3037
runnable.run()
3138
}
3239
}
3340

34-
private class ShowToastRunnable(private val text: CharSequence, private val length: Int) :
41+
42+
class ToastProp {
43+
44+
companion object {
45+
46+
@kotlin.jvm.JvmStatic
47+
fun newProp(): ToastProp {
48+
return ToastProp()
49+
}
50+
51+
}
52+
53+
var text: CharSequence? = null
54+
var duration: Int = Toast.LENGTH_SHORT
55+
var gravity: Int = 0
56+
var xOffset: Int = 0
57+
var yOffset: Int = 0
58+
var horizontalMargin: Float = 0f
59+
var verticalMargin: Float = 0f
60+
61+
fun duration(duration: Int): ToastProp {
62+
this.duration = duration
63+
return this
64+
}
65+
66+
@kotlin.Deprecated("Starting from Android [Build.VERSION_CODES.R], apps\n" +
67+
" targeting API level [Build.VERSION_CODES#R] or higher, this method is a no-op when\n" +
68+
" called on text toasts.")
69+
fun gravity(gravity: Int): ToastProp {
70+
this.gravity = gravity
71+
return this
72+
}
73+
74+
@kotlin.Deprecated("Starting from Android [Build.VERSION_CODES.R], apps\n" +
75+
" targeting API level [Build.VERSION_CODES#R] or higher, this method is a no-op when\n" +
76+
" called on text toasts.")
77+
fun xOffset(xOffset: Int): ToastProp {
78+
this.xOffset = xOffset
79+
return this
80+
}
81+
82+
@kotlin.Deprecated("Starting from Android [Build.VERSION_CODES.R], apps\n" +
83+
" targeting API level [Build.VERSION_CODES#R] or higher, this method is a no-op when\n" +
84+
" called on text toasts.")
85+
fun yOffset(yOffset: Int): ToastProp {
86+
this.yOffset = yOffset
87+
return this
88+
}
89+
90+
@kotlin.Deprecated("Starting from Android [Build.VERSION_CODES.R], apps\n" +
91+
" targeting API level [Build.VERSION_CODES#R] or higher, this method is a no-op when\n" +
92+
" called on text toasts.")
93+
fun horizontalMargin(horizontalMargin: Float): ToastProp {
94+
this.horizontalMargin = horizontalMargin
95+
return this
96+
}
97+
98+
@kotlin.Deprecated("Starting from Android [Build.VERSION_CODES.R], apps\n" +
99+
" targeting API level [Build.VERSION_CODES#R] or higher, this method is a no-op when\n" +
100+
" called on text toasts.")
101+
fun verticalMargin(verticalMargin: Float): ToastProp {
102+
this.verticalMargin = verticalMargin
103+
return this
104+
}
105+
106+
fun text(text: CharSequence?): ToastProp {
107+
this.text = text
108+
return this
109+
}
110+
111+
fun lengthShort(text: CharSequence?): ToastProp {
112+
return text(text)
113+
.duration(Toast.LENGTH_SHORT)
114+
}
115+
116+
fun lengthLong(text: CharSequence?): ToastProp {
117+
return text(text)
118+
.duration(Toast.LENGTH_LONG)
119+
}
120+
121+
}
122+
123+
private class ShowToastRunnable(private val toastProp: ToastProp) :
35124
Runnable {
125+
126+
companion object {
127+
val sToastTLS = ThreadLocal<Toast>()
128+
}
129+
36130
override fun run() {
37-
Toast.makeText(AppHolder.get(), text, length).show()
131+
var toast = sToastTLS.get()
132+
toast?.cancel()
133+
toast = Toast(AppHolder.get())
134+
toast.duration = toastProp.duration
135+
toast.setGravity(toastProp.gravity, toastProp.xOffset, toastProp.yOffset)
136+
toast.setMargin(toastProp.horizontalMargin, toast.verticalMargin)
137+
toast.setText(toastProp.text)
138+
toast.show()
139+
sToastTLS.set(toast)
38140
}
39141
}
40142
}

0 commit comments

Comments
 (0)