Skip to content

Commit f78dc43

Browse files
committed
Added ViewModel extension func: ioWithState
1 parent bb3c042 commit f78dc43

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

common/src/main/java/me/li2/android/common/arch/ViewModelExt.kt

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package me.li2.android.common.arch
22

3+
import androidx.lifecycle.MutableLiveData
34
import androidx.lifecycle.ViewModel
45
import androidx.lifecycle.viewModelScope
56
import kotlinx.coroutines.CoroutineScope
@@ -19,4 +20,16 @@ fun ViewModel.io(
1920
}
2021
}
2122
}
22-
}
23+
}
24+
25+
fun <T> ViewModel.ioWithState(
26+
state: MutableLiveData<Resource<T>>,
27+
ioHandler: suspend () -> T
28+
) {
29+
state.postLoading()
30+
io({
31+
state.postError(this)
32+
}) {
33+
state.postSuccess(ioHandler())
34+
}
35+
}

0 commit comments

Comments
 (0)