This repository has been archived by the owner on Aug 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
12 changed files
with
128 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,6 @@ | |
|
||
public enum State { | ||
|
||
Non, Empty, Loading, Failure, Success | ||
Empty, Loading, Failure, Success | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
stateful-livedata/src/main/java/com/numeron/stateful/livedata/Stateful.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.numeron.stateful.livedata | ||
|
||
import com.numeron.common.State | ||
|
||
interface Stateful<T> { | ||
|
||
val state: State | ||
|
||
fun onSuccess(c: (T) -> Unit): Stateful<T> | ||
|
||
fun onFailure(c: (String, Throwable) -> Unit): Stateful<T> | ||
|
||
fun onLoading(c: (String, Float) -> Unit): Stateful<T> | ||
|
||
fun onMessage(c: (String) -> Unit): Stateful<T> | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
stateful-livedata/src/main/java/com/numeron/stateful/livedata/StatefulCallback.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.numeron.stateful.livedata | ||
|
||
interface StatefulCallback<T> { | ||
|
||
fun onSuccess(value: T) | ||
fun onLoading(message: String, progress: Float) | ||
fun onFailure(message: String, cause: Throwable) | ||
fun onMessage(message: String): Unit = Unit | ||
|
||
} |
3 changes: 3 additions & 0 deletions
3
stateful-livedata/src/main/java/com/numeron/stateful/livedata/StatefulException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.numeron.stateful.livedata | ||
|
||
class StatefulException(override val message: String, cause: Throwable? = null) : RuntimeException(message, cause) |
35 changes: 35 additions & 0 deletions
35
stateful-livedata/src/main/java/com/numeron/stateful/livedata/StatefulExceptionHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.numeron.stateful.livedata | ||
|
||
import kotlinx.coroutines.CoroutineExceptionHandler | ||
import java.net.ConnectException | ||
import java.net.SocketTimeoutException | ||
import java.net.UnknownHostException | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
class StatefulExceptionHandler<T>(private val statefulLiveData: StatefulLiveData<T>) : CoroutineExceptionHandler { | ||
|
||
override val key: CoroutineContext.Key<*> | ||
get() = CoroutineExceptionHandler | ||
|
||
override fun handleException(context: CoroutineContext, exception: Throwable) { | ||
exception.printStackTrace() | ||
when (exception) { | ||
is StatefulException -> { | ||
statefulLiveData.postFailure(exception, exception.message) | ||
} | ||
is ConnectException, is UnknownHostException -> { | ||
statefulLiveData.postFailure(exception, "无法连接到服务器,请检查网络后重试。") | ||
} | ||
is SocketTimeoutException -> { | ||
statefulLiveData.postFailure(exception, "网络连接超时,请检查网络后重试。") | ||
} | ||
is NullPointerException -> { | ||
statefulLiveData.postFailure(exception, "没有获取到数据,请稍候重试!") | ||
} | ||
else -> { | ||
statefulLiveData.postFailure(exception) | ||
} | ||
} | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
stateful-livedata/src/main/java/com/numeron/stateful/livedata/StatefulImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.numeron.stateful.livedata | ||
|
||
import com.numeron.common.State | ||
|
||
internal data class StatefulImpl<T>( | ||
override val state: State, | ||
internal val success: T? = null, | ||
internal val failure: Throwable? = null, | ||
internal val progress: Float = -1f, | ||
internal val message: String? = null, | ||
internal val version: Int = 0, | ||
internal val previous: Int = 0 | ||
) : Stateful<T> { | ||
|
||
override fun onSuccess(c: (T) -> Unit): Stateful<T> { | ||
if (state == State.Success && success != null) { | ||
c(success) | ||
} | ||
return this | ||
} | ||
|
||
override fun onFailure(c: (String, Throwable) -> Unit): Stateful<T> { | ||
if (state == State.Failure && failure != null) { | ||
c(message!!, failure) | ||
} | ||
return this | ||
} | ||
|
||
override fun onLoading(c: (String, Float) -> Unit): Stateful<T> { | ||
if (state == State.Loading) { | ||
c(message!!, progress) | ||
} | ||
return this | ||
} | ||
|
||
override fun onMessage(c: (String) -> Unit): Stateful<T> { | ||
if (version > previous && message != null) { | ||
c(message) | ||
} | ||
return this | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 2 additions & 3 deletions
5
...va/com/numeron/status/StatefulObserver.kt → ...ron/stateful/livedata/StatefulObserver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 0 additions & 19 deletions
19
stateful-livedata/src/main/java/com/numeron/status/Stateful.kt
This file was deleted.
Oops, something went wrong.
12 changes: 0 additions & 12 deletions
12
stateful-livedata/src/main/java/com/numeron/status/StatefulCallback.kt
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
stateful-livedata/src/main/java/com/numeron/status/StatefulExceptionHandler.kt
This file was deleted.
Oops, something went wrong.
48 changes: 0 additions & 48 deletions
48
stateful-livedata/src/main/java/com/numeron/status/StatefulImpl.kt
This file was deleted.
Oops, something went wrong.