|
@@ -7,13 +7,10 @@
|
|
|
|
|
|
package com.drake.net.scope
|
|
|
|
|
|
-import android.app.Activity
|
|
|
import android.view.View
|
|
|
-import androidx.fragment.app.Fragment
|
|
|
import com.drake.net.NetConfig
|
|
|
-import com.drake.net.R
|
|
|
import com.drake.statelayout.StateLayout
|
|
|
-import com.drake.statelayout.state
|
|
|
+import kotlinx.coroutines.CancellationException
|
|
|
import kotlinx.coroutines.cancel
|
|
|
|
|
|
/**
|
|
@@ -21,13 +18,6 @@ import kotlinx.coroutines.cancel
|
|
|
*/
|
|
|
class StateCoroutineScope(val state: StateLayout) : NetCoroutineScope() {
|
|
|
|
|
|
-
|
|
|
- constructor(view: View) : this(view.state())
|
|
|
-
|
|
|
- constructor(activity: Activity) : this(activity.state())
|
|
|
-
|
|
|
- constructor(fragment: Fragment) : this(fragment.state())
|
|
|
-
|
|
|
init {
|
|
|
state.addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener {
|
|
|
override fun onViewAttachedToWindow(v: View?) {
|
|
@@ -40,10 +30,7 @@ class StateCoroutineScope(val state: StateLayout) : NetCoroutineScope() {
|
|
|
}
|
|
|
|
|
|
override fun start() {
|
|
|
- (state.getTag(R.id.cache_succeed) as? Boolean)?.let {
|
|
|
- readCache = false
|
|
|
- cacheSucceed = it
|
|
|
- }
|
|
|
+ isReadCache = !state.loaded
|
|
|
state.trigger()
|
|
|
}
|
|
|
|
|
@@ -51,30 +38,23 @@ class StateCoroutineScope(val state: StateLayout) : NetCoroutineScope() {
|
|
|
if (succeed) {
|
|
|
state.showContent()
|
|
|
}
|
|
|
- state.setTag(R.id.cache_succeed, succeed)
|
|
|
}
|
|
|
|
|
|
override fun catch(e: Throwable) {
|
|
|
super.catch(e)
|
|
|
- if (!cacheSucceed) {
|
|
|
- state.showError(e)
|
|
|
- }
|
|
|
+ state.showError(e)
|
|
|
}
|
|
|
|
|
|
override fun handleError(e: Throwable) {
|
|
|
- if (cacheSucceed) {
|
|
|
- super.handleError(e)
|
|
|
- } else {
|
|
|
- NetConfig.onStateError(e, state)
|
|
|
- }
|
|
|
+ NetConfig.onStateError(e, state)
|
|
|
}
|
|
|
|
|
|
override fun finally(e: Throwable?) {
|
|
|
super.finally(e)
|
|
|
- if (e == null) {
|
|
|
+ if (e == null || e is CancellationException) {
|
|
|
state.showContent()
|
|
|
- state.trigger()
|
|
|
}
|
|
|
+ state.trigger()
|
|
|
}
|
|
|
|
|
|
}
|