@@ -14,27 +14,129 @@ import pers.pslilysm.sdk_library.EventHandler
1414object 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