Skip to content

Commit ed70d9d

Browse files
committed
Refactor EasyToast
1 parent 3f60324 commit ed70d9d

File tree

3 files changed

+58
-66
lines changed

3 files changed

+58
-66
lines changed

app/src/main/java/com/haoge/sample/easyandroid/activities/EasyToastActivity.kt

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.haoge.sample.easyandroid.activities
22

33
import android.view.Gravity
4-
import android.widget.Toast
54
import butterknife.OnClick
65
import com.haoge.easyandroid.easy.EasyToast
76
import com.haoge.sample.easyandroid.BaseActivity
@@ -15,7 +14,11 @@ import java.util.concurrent.Executors
1514
class EasyToastActivity : BaseActivity(){
1615

1716
val default by lazy { EasyToast.DEFAULT }
18-
val creator by lazy { EasyToast.create(R.layout.toast_style, R.id.toast_tv, Toast.LENGTH_SHORT) }
17+
val creator by lazy {
18+
EasyToast.newBuilder(R.layout.toast_style, R.id.toast_tv)
19+
.setGravity(Gravity.CENTER, 0, 0)
20+
.build()
21+
}
1922

2023
override fun getLayoutId(): Int {
2124
return R.layout.activity_easy_toast
@@ -41,16 +44,6 @@ class EasyToastActivity : BaseActivity(){
4144
Instance.pool.execute { creator.show("使用自定义样式在子线程中进行展示, Thread: ${Thread.currentThread()}") }
4245
}
4346

44-
@OnClick(R.id.showDefaultCenterWithGravity)
45-
fun showDefaultCenterWithGravity() {
46-
default.setGravity(Gravity.CENTER, 0, 0).show("使用默认样式在中心展示")
47-
}
48-
49-
@OnClick(R.id.showCustomTopWithGravity)
50-
fun showCustomTopWithGravity() {
51-
creator.setGravity(Gravity.TOP, 0, 20).show("使用自定义样式在顶部展示")
52-
}
53-
5447
@OnClick(R.id.showMultiTimeToast)
5548
fun showMultiTimeToast() {
5649
Instance.pool.execute {

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,6 @@
2727
android:layout_width="match_parent"
2828
android:layout_height="wrap_content" />
2929

30-
<Button
31-
android:id="@+id/showDefaultCenterWithGravity"
32-
android:text="使用Gravity配置默认Toast在中心展示"
33-
android:layout_width="match_parent"
34-
android:layout_height="wrap_content" />
35-
36-
<Button
37-
android:id="@+id/showCustomTopWithGravity"
38-
android:text="使用Gravity配置新建Toast在顶部展示"
39-
android:layout_width="match_parent"
40-
android:layout_height="wrap_content" />
41-
4230
<Button
4331
android:id="@+id/showMultiTimeToast"
4432
android:text="短期连续多次展示toast文本"

utils/src/main/java/com/haoge/easyandroid/easy/EasyToast.kt

Lines changed: 53 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,7 @@ import com.haoge.easyandroid.EasyAndroid
1616
*
1717
* AUTHOR: haoge
1818
*/
19-
class EasyToast private constructor(private val layoutId: Int = -1,
20-
private val tvId: Int = -1,
21-
private val duration: Int = Toast.LENGTH_SHORT,
22-
private val isDefault: Boolean = true) {
23-
24-
private var toast:Toast? = null
25-
private var tv:TextView? = null
26-
private var gravity:Gravity? = null
27-
28-
fun reset() {
29-
this.toast = null
30-
this.tv = null
31-
this.gravity = null
32-
}
33-
34-
fun setGravity(gravity: Int, offsetX: Int, offsetY: Int):EasyToast {
35-
this.gravity = EasyToast.Gravity(gravity, offsetX, offsetY)
36-
return this
37-
}
19+
class EasyToast private constructor(private val builder:Builder) {
3820

3921
fun show(resId:Int) {
4022
show(EasyAndroid.getApplicationContext().getString(resId))
@@ -60,31 +42,30 @@ class EasyToast private constructor(private val layoutId: Int = -1,
6042
private fun showInternal(message: String) {
6143
createToastIfNeeded()
6244

63-
val gravity = this.gravity
64-
if (gravity != null) {
65-
toast?.setGravity(gravity.gravity, gravity.offsetX, gravity.offsetY)
66-
this.gravity = null
67-
}
68-
if (isDefault) {
69-
toast?.setText(message)
70-
toast?.show()
45+
if (builder.isDefault) {
46+
builder.toast?.setText(message)
47+
builder.toast?.show()
7148
} else {
72-
tv?.text = message
73-
toast?.show()
49+
builder.tv?.text = message
50+
builder.toast?.show()
7451
}
7552
}
7653

7754
@SuppressLint("ShowToast")
7855
private fun createToastIfNeeded() {
79-
if (toast == null) {
80-
if (isDefault) {
81-
toast = Toast.makeText(EasyAndroid.getApplicationContext(), "", Toast.LENGTH_SHORT)
56+
if (builder.toast == null) {
57+
if (builder.isDefault) {
58+
builder.toast = Toast.makeText(EasyAndroid.getApplicationContext(), "", Toast.LENGTH_SHORT)
8259
} else {
83-
val container = LayoutInflater.from(EasyAndroid.getApplicationContext()).inflate(layoutId, null)
84-
tv = container.findViewById(tvId)
85-
toast = Toast(EasyAndroid.getApplicationContext())
86-
toast?.view = container
87-
toast?.duration = duration
60+
val container = LayoutInflater.from(EasyAndroid.getApplicationContext()).inflate(builder.layoutId, null)
61+
builder.tv = container.findViewById(builder.tvId)
62+
builder.toast = Toast(EasyAndroid.getApplicationContext())
63+
builder.toast?.view = container
64+
builder.toast?.duration = builder.duration
65+
}
66+
67+
if (builder.gravity != 0) {
68+
builder.toast?.setGravity(builder.gravity, builder.offsetX, builder.offsetY)
8869
}
8970
}
9071
}
@@ -95,16 +76,46 @@ class EasyToast private constructor(private val layoutId: Int = -1,
9576
/**
9677
* 默认提供的Toast实例,在首次使用时进行加载。
9778
*/
98-
val DEFAULT: EasyToast by lazy { default() }
79+
val DEFAULT: EasyToast by lazy { return@lazy newBuilder().build() }
80+
81+
fun newBuilder():Builder {
82+
return Builder(true, 0, 0)
83+
}
9984

100-
private fun default(): EasyToast {
101-
return EasyToast(isDefault = true)
85+
fun newBuilder(layoutId: Int, tvId: Int):Builder {
86+
return Builder(false, layoutId, tvId)
10287
}
10388

10489
fun create(layoutId: Int, tvId: Int, duration: Int): EasyToast {
105-
return EasyToast(layoutId, tvId, duration, isDefault = false)
90+
return newBuilder(layoutId, tvId).setDuration(duration).build()
10691
}
10792
}
10893

109-
private class Gravity(val gravity:Int, val offsetX:Int, val offsetY:Int)
94+
class Builder(internal var isDefault: Boolean,
95+
internal var layoutId: Int,
96+
internal var tvId: Int) {
97+
internal var toast:Toast? = null
98+
internal var tv:TextView? = null
99+
100+
internal var duration:Int = Toast.LENGTH_SHORT
101+
internal var gravity:Int = 0
102+
internal var offsetX:Int = 0
103+
internal var offsetY:Int = 0
104+
105+
fun setGravity(gravity: Int, offsetX: Int, offsetY: Int): Builder {
106+
this.gravity = gravity
107+
this.offsetX = offsetX
108+
this.offsetY = offsetY
109+
return this
110+
}
111+
112+
fun setDuration(duration:Int): Builder {
113+
this.duration = duration
114+
return this
115+
}
116+
117+
fun build():EasyToast {
118+
return EasyToast(this)
119+
}
120+
}
110121
}

0 commit comments

Comments
 (0)