BaseViewModel 添加异常处理
This commit is contained in:
parent
164d68d56e
commit
e43329e535
|
|
@ -1,6 +1,9 @@
|
|||
package luyao.util.ktx.ui
|
||||
|
||||
import android.Manifest
|
||||
import android.content.Context
|
||||
import android.os.PowerManager
|
||||
import android.view.WindowManager
|
||||
import com.afollestad.materialdialogs.MaterialDialog
|
||||
import com.afollestad.materialdialogs.files.FileFilter
|
||||
import com.afollestad.materialdialogs.files.fileChooser
|
||||
|
|
@ -45,6 +48,7 @@ class IntentExtActivity : CommonListActivity() {
|
|||
|
||||
private fun installApk() {
|
||||
|
||||
powerManager
|
||||
request(Manifest.permission.READ_EXTERNAL_STORAGE) {
|
||||
onGranted {
|
||||
chooseFile()
|
||||
|
|
|
|||
|
|
@ -2,7 +2,9 @@ package luyao.util.ktx.base
|
|||
|
||||
import android.os.Bundle
|
||||
import androidx.lifecycle.LifecycleObserver
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.lifecycle.ViewModelProviders
|
||||
import java.lang.Exception
|
||||
|
||||
/**
|
||||
* Created by luyao
|
||||
|
|
@ -28,7 +30,11 @@ abstract class BaseVMActivity<VM : BaseViewModel> : BaseActivity(),LifecycleObse
|
|||
open fun providerVMClass(): Class<VM>? = null
|
||||
|
||||
|
||||
open fun startObserve() {}
|
||||
open fun startObserve() {
|
||||
mViewModel.mException.observe(this, Observer { it?.let { onError(it) } })
|
||||
}
|
||||
|
||||
open fun onError(e: Exception){}
|
||||
|
||||
override fun onDestroy() {
|
||||
mViewModel.let {
|
||||
|
|
|
|||
|
|
@ -4,7 +4,9 @@ import android.os.Bundle
|
|||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.lifecycle.ViewModelProviders
|
||||
import java.lang.Exception
|
||||
|
||||
/**
|
||||
* Created by luyao
|
||||
|
|
@ -26,7 +28,12 @@ abstract class BaseVMFragment<VM : BaseViewModel> : androidx.fragment.app.Fragme
|
|||
super.onViewCreated(view, savedInstanceState)
|
||||
}
|
||||
|
||||
open fun startObserve() {}
|
||||
open fun startObserve() {
|
||||
mViewModel.mException.observe(this, Observer { it?.let { onError(it) } })
|
||||
}
|
||||
|
||||
open fun onError(e:Exception){}
|
||||
|
||||
abstract fun getLayoutResId(): Int
|
||||
|
||||
abstract fun initView()
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ import kotlinx.coroutines.*
|
|||
*/
|
||||
open class BaseViewModel : ViewModel(), LifecycleObserver {
|
||||
|
||||
private val mException: MutableLiveData<Exception> = MutableLiveData()
|
||||
val mException: MutableLiveData<Exception> = MutableLiveData()
|
||||
|
||||
|
||||
private fun launchOnUI(block: suspend CoroutineScope.() -> Unit) {
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ import androidx.core.content.ContextCompat
|
|||
* Created by luyao
|
||||
* on 2019/7/9 9:17
|
||||
*/
|
||||
inline fun <reified T : Any> Context.getSystemService(): T? =
|
||||
inline fun <reified T> Context.getSystemService(): T? =
|
||||
ContextCompat.getSystemService(this, T::class.java)
|
||||
|
||||
val Context.windowManager get() = getSystemService<WindowManager>()
|
||||
|
|
|
|||
Loading…
Reference in New Issue