Skip to content

Commit 60736f4

Browse files
committed
[1.7.0-feature] runCatching
1 parent 9f3d30e commit 60736f4

File tree

3 files changed

+72
-9
lines changed

3 files changed

+72
-9
lines changed

app/src/main/java/org/ninetripods/mq/study/jetpack/mvi/MviExampleActivity.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import org.ninetripods.mq.study.jetpack.mvi.widget.RankAdapter
1818
import org.ninetripods.mq.study.kotlin.ktx.flowWithLifecycle2
1919
import org.ninetripods.mq.study.kotlin.ktx.id
2020
import org.ninetripods.mq.study.kotlin.ktx.showToast
21+
import org.ninetripods.mq.study.kotlin.ktx.visible
2122
import org.ninetripods.mq.study.util.Constant
2223

2324
/**
@@ -73,7 +74,7 @@ class MviExampleActivity : BaseMviActivity() {
7374
when (state) {
7475
is BannerUiState.INIT -> {}
7576
is BannerUiState.SUCCESS -> {
76-
mViewPager2.visibility = View.VISIBLE
77+
mViewPager2.visible()
7778
mBtnQuest.visibility = View.GONE
7879
val imgs = mutableListOf<String>()
7980
for (model in state.models) {

app/src/main/java/org/ninetripods/mq/study/kotlin/ktx/CommonExt.kt

+38-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package org.ninetripods.mq.study.kotlin.ktx
22

3+
import android.app.Activity
34
import android.content.Context
45
import android.util.Log
6+
import android.view.View
57
import android.widget.Toast
8+
import androidx.annotation.StringRes
69
import androidx.fragment.app.Fragment
7-
import org.ninetripods.mq.study.kotlin.util.ScreenUtil
810
import org.ninetripods.mq.study.MyApplication
11+
import org.ninetripods.mq.study.kotlin.util.ScreenUtil
912

1013
/**
1114
* Toast扩展函数
@@ -26,13 +29,6 @@ fun log(message: String) {
2629
Log.e("TTT", message)
2730
}
2831

29-
/**
30-
* Fragment中Toast扩展函数
31-
*/
32-
fun Fragment.showToast(message: String, duration: Int = Toast.LENGTH_SHORT) {
33-
Toast.makeText(activity, message, duration).show()
34-
}
35-
3632
fun Number.dp2px(): Int {
3733
return ScreenUtil.dp2px(MyApplication.getApplication(), toFloat())
3834
}
@@ -49,5 +45,39 @@ fun Number.px2sp(): Int {
4945
return ScreenUtil.px2sp(MyApplication.getApplication(), toFloat())
5046
}
5147

48+
fun View?.visible() {
49+
if (this?.visibility != View.VISIBLE) {
50+
this?.visibility = View.VISIBLE
51+
}
52+
}
53+
54+
fun View?.invisible() {
55+
if (this?.visibility != View.INVISIBLE) {
56+
this?.visibility = View.INVISIBLE
57+
}
58+
}
59+
60+
fun View?.gone() {
61+
if (this?.visibility != View.GONE) {
62+
this?.visibility = View.GONE
63+
}
64+
}
65+
66+
fun Activity.showToast(msg: String, duration: Int = Toast.LENGTH_SHORT) {
67+
Toast.makeText(this, msg, duration).show()
68+
}
69+
70+
fun Activity.showToast(@StringRes msg: Int, duration: Int = Toast.LENGTH_SHORT) {
71+
Toast.makeText(this, msg, duration).show()
72+
}
73+
74+
fun Fragment.showToast(msg: String, duration: Int = Toast.LENGTH_SHORT) {
75+
Toast.makeText(requireContext(), msg, duration).show()
76+
}
77+
78+
fun Fragment.showToast(@StringRes message: Int, duration: Int = Toast.LENGTH_SHORT) {
79+
Toast.makeText(requireContext(), message, duration).show()
80+
}
81+
5282

5383

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.ninetripods.mq.study.util
2+
3+
import org.ninetripods.mq.study.kotlin.ktx.log
4+
5+
/**
6+
* runCatching代替try catch的各种用法
7+
* Created by mq on 2023/7/12
8+
*/
9+
class RunCatchException {
10+
11+
fun exceptionTest() {
12+
//不需要结果
13+
runCatching { 100 / 0 }
14+
.onFailure { ex -> ex.printStackTrace() }
15+
16+
//需要结果
17+
runCatching { 100 / 2 }
18+
.onSuccess { value -> log("onSuccess:$value") } //runCatching{}中执行成功,并传入执行结果
19+
.onFailure { exception -> log("onFailure:$exception") }//runCatching{}中执行失败,并传入exception
20+
//.getOrDefault(0) //获取runCatching{}中执行的结果,如果是Failure直接返回默认值
21+
.getOrElse { ex -> //获取runCatching{}中执行的结果,如果是Failure返回else内部的值。相比getOrDefault多了对exception的处理
22+
log("exception:$ex")
23+
100
24+
}
25+
//.getOrThrow()//获取runCatching{}中执行的结果,如果是Failure直接抛异常
26+
//.getOrNull() //获取runCatching{}中执行的结果,如果是Failure返回null
27+
//.exceptionOrNull() //如果有问题则返回exception;否则返回null
28+
.run {
29+
log("result:$this")
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)