Skip to content
This repository has been archived by the owner on Aug 10, 2020. It is now read-only.

Commit

Permalink
1.升级kotlin至1.3.61版本。
Browse files Browse the repository at this point in the history
2.为brick添加lazy系列方法。
3.调用brick部分api命名。
  • Loading branch information
xiazunyang committed May 7, 2020
1 parent 9934ea9 commit a114c71
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 112 deletions.
2 changes: 1 addition & 1 deletion common/src/main/java/com/numeron/common/State.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

public enum State {

Non, Empty, Loading, Failure, Success
Empty, Loading, Failure, Success

}
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>

}
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

}
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)
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)
}
}
}

}
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
}

}
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package com.numeron.status
package com.numeron.stateful.livedata

import androidx.lifecycle.LiveData
import com.numeron.common.State

class StatefulLiveData<T> @JvmOverloads constructor(
private val empty: String = "没有数据",
private val loading: String = "正在加载",
private val success: String = "加载成功",
private val failure: String = "加载失败"
) : LiveData<Stateful<T>>() {

private val impl: StatefulImpl<T>
get() = super.getValue() as? StatefulImpl<T> ?: StatefulImpl(State.Non)
get() = getValue() as? StatefulImpl ?: StatefulImpl(State.Empty)

val value: T?
@JvmName("value")
get() = (super.getValue() as? StatefulImpl)?.success
get() = (getValue() as? StatefulImpl)?.success

val requireValue: T
@JvmName("requireValue")
Expand All @@ -30,13 +28,8 @@ class StatefulLiveData<T> @JvmOverloads constructor(
postValue(impl.copy(state = State.Loading, progress = progress, message = message))
}

fun postSuccess(value: T, message: String? = this.success) {
postValue(impl.copy(state = State.Success, success = value, message = message))
}

@JvmOverloads
fun postEmpty(empty: String = this.empty) {
postValue(impl.copy(state = State.Empty, message = empty))
fun postSuccess(value: T) {
postValue(impl.copy(state = State.Success, success = value))
}

fun postFailure(cause: Throwable, message: String = this.failure) {
Expand All @@ -48,7 +41,18 @@ class StatefulLiveData<T> @JvmOverloads constructor(
}

fun postMessage(message: String) {
postValue(impl.copy(state = State.Non, message = message))
postValue(impl.copy(message = message, previous = impl.version, version = impl.version + 1))
}

companion object {

fun <T> LiveData<T>.toStateful(loading: String = "正在加载",
failure: String = "加载失败"): StatefulLiveData<T> {
val statefulLiveData = StatefulLiveData<T>(loading, failure)
observeForever(statefulLiveData::postSuccess)
return statefulLiveData
}

}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.numeron.status
package com.numeron.stateful.livedata

import androidx.lifecycle.Observer

class StatefulObserver<T>(private val callback: StatefulCallback<T>) : Observer<Stateful<T>> {

override fun onChanged(stateful: Stateful<T>?) {
stateful?.onEmpty(callback::onEmpty)
?.onFailure(callback::onFailure)
stateful?.onFailure(callback::onFailure)
?.onMessage(callback::onMessage)
?.onLoading(callback::onLoading)
?.onSuccess(callback::onSuccess)
Expand Down
19 changes: 0 additions & 19 deletions stateful-livedata/src/main/java/com/numeron/status/Stateful.kt

This file was deleted.

This file was deleted.

This file was deleted.

48 changes: 0 additions & 48 deletions stateful-livedata/src/main/java/com/numeron/status/StatefulImpl.kt

This file was deleted.

0 comments on commit a114c71

Please sign in to comment.