Commit ebfeb811 authored by 王雷's avatar 王雷

新增请求方法

parent 0f83da82
...@@ -37,7 +37,7 @@ repositories { ...@@ -37,7 +37,7 @@ repositories {
} }
group 'com.qmai.android.widget' group 'com.qmai.android.widget'
version '1.1.25-SNAPSHOT' version '1.1.30-SNAPSHOT'
gradlePublish { gradlePublish {
......
This diff is collapsed.
...@@ -55,7 +55,7 @@ repositories { ...@@ -55,7 +55,7 @@ repositories {
mavenCentral() mavenCentral()
} }
group 'com.qmai.android.fetch' group 'com.qmai.android.fetch'
version '1.2.15-SNAPSHOT' version '1.2.18-SNAPSHOT'
gradlePublish { gradlePublish {
......
36 38
0 2
\ No newline at end of file \ No newline at end of file
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.qmai.android.fetch</groupId> <groupId>com.qmai.android.fetch</groupId>
<artifactId>fetch</artifactId> <artifactId>fetch</artifactId>
<version>1.2.15-SNAPSHOT</version> <version>1.2.18-SNAPSHOT</version>
<packaging>aar</packaging> <packaging>aar</packaging>
<dependencies> <dependencies>
<dependency> <dependency>
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
<groupId>com.qmai.android.fetch</groupId> <groupId>com.qmai.android.fetch</groupId>
<artifactId>fetch</artifactId> <artifactId>fetch</artifactId>
<versioning> <versioning>
<latest>1.2.15-SNAPSHOT</latest> <latest>1.2.18-SNAPSHOT</latest>
<versions> <versions>
<version>1.2.3-SNAPSHOT</version> <version>1.2.3-SNAPSHOT</version>
<version>1.2.4-SNAPSHOT</version> <version>1.2.4-SNAPSHOT</version>
...@@ -19,7 +19,10 @@ ...@@ -19,7 +19,10 @@
<version>1.2.14-SNAPSHOT</version> <version>1.2.14-SNAPSHOT</version>
<version>1.2.2-SNAPSHOT</version> <version>1.2.2-SNAPSHOT</version>
<version>1.2.15-SNAPSHOT</version> <version>1.2.15-SNAPSHOT</version>
<version>1.2.16-SNAPSHOT</version>
<version>1.2.17-SNAPSHOT</version>
<version>1.2.18-SNAPSHOT</version>
</versions> </versions>
<lastUpdated>20201207063657</lastUpdated> <lastUpdated>20201214104412</lastUpdated>
</versioning> </versioning>
</metadata> </metadata>
...@@ -2,29 +2,29 @@ ...@@ -2,29 +2,29 @@
<metadata modelVersion="1.1.0"> <metadata modelVersion="1.1.0">
<groupId>com.qmai.android.fetch</groupId> <groupId>com.qmai.android.fetch</groupId>
<artifactId>fetch</artifactId> <artifactId>fetch</artifactId>
<version>1.2.15-SNAPSHOT</version> <version>1.2.18-SNAPSHOT</version>
<versioning> <versioning>
<snapshot> <snapshot>
<timestamp>20201207.063649</timestamp> <timestamp>20201214.104406</timestamp>
<buildNumber>1</buildNumber> <buildNumber>1</buildNumber>
</snapshot> </snapshot>
<lastUpdated>20201207063649</lastUpdated> <lastUpdated>20201214104406</lastUpdated>
<snapshotVersions> <snapshotVersions>
<snapshotVersion> <snapshotVersion>
<classifier>sources</classifier> <classifier>sources</classifier>
<extension>jar</extension> <extension>jar</extension>
<value>1.2.15-20201207.063649-1</value> <value>1.2.18-20201214.104406-1</value>
<updated>20201207063649</updated> <updated>20201214104406</updated>
</snapshotVersion> </snapshotVersion>
<snapshotVersion> <snapshotVersion>
<extension>pom</extension> <extension>aar</extension>
<value>1.2.15-20201207.063649-1</value> <value>1.2.18-20201214.104406-1</value>
<updated>20201207063649</updated> <updated>20201214104406</updated>
</snapshotVersion> </snapshotVersion>
<snapshotVersion> <snapshotVersion>
<extension>aar</extension> <extension>pom</extension>
<value>1.2.15-20201207.063649-1</value> <value>1.2.18-20201214.104406-1</value>
<updated>20201207063649</updated> <updated>20201214104406</updated>
</snapshotVersion> </snapshotVersion>
</snapshotVersions> </snapshotVersions>
</versioning> </versioning>
......
...@@ -8,7 +8,7 @@ package com.qimai.android.fetch.Response ...@@ -8,7 +8,7 @@ package com.qimai.android.fetch.Response
@Description @Description
@email wangwei_5521@163.com @email wangwei_5521@163.com
**/ **/
data class Resource<out T>(val status: Status, val data: T?, val message: String?) { data class Resource<out T>(val status: Status, val data: T? = null, val message: String?) {
companion object { companion object {
fun <T> success(data: T?): Resource<T> { fun <T> success(data: T?): Resource<T> {
return Resource( return Resource(
...@@ -18,7 +18,7 @@ data class Resource<out T>(val status: Status, val data: T?, val message: String ...@@ -18,7 +18,7 @@ data class Resource<out T>(val status: Status, val data: T?, val message: String
) )
} }
fun <T> error(msg: String, data: T?): Resource<T> { fun <T> error(msg: String? = null, data: T? = null): Resource<T> {
return Resource( return Resource(
Status.ERROR, Status.ERROR,
data, data,
...@@ -26,7 +26,7 @@ data class Resource<out T>(val status: Status, val data: T?, val message: String ...@@ -26,7 +26,7 @@ data class Resource<out T>(val status: Status, val data: T?, val message: String
) )
} }
fun <T> loading(data: T?): Resource<T> { fun <T> loading(data: T? = null): Resource<T> {
return Resource( return Resource(
Status.LOADING, Status.LOADING,
data, data,
......
...@@ -6,10 +6,14 @@ import androidx.lifecycle.ViewModel ...@@ -6,10 +6,14 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.liveData import androidx.lifecycle.liveData
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.qimai.android.fetch.Response.Resource import com.qimai.android.fetch.Response.Resource
import com.qimai.android.fetch.Response.Status
import com.qimai.android.fetch.convert.BizException import com.qimai.android.fetch.convert.BizException
import com.qimai.android.fetch.convert.ServerResponseException import com.qimai.android.fetch.convert.ServerResponseException
import com.qimai.android.fetch.convert.TOKENExpireException import com.qimai.android.fetch.convert.TOKENExpireException
import com.qimai.android.fetch.model.BaseData import com.qimai.android.fetch.model.BaseData
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import retrofit2.HttpException import retrofit2.HttpException
import java.net.ConnectException import java.net.ConnectException
...@@ -52,6 +56,41 @@ fun <T> ViewModel.safeCall(call: suspend () -> T) = liveData(viewModelScope.coro ...@@ -52,6 +56,41 @@ fun <T> ViewModel.safeCall(call: suspend () -> T) = liveData(viewModelScope.coro
} }
} }
/***
* 基于flow实现 网络请求 这里不需要写 flow 直接写请求
*/
@ExperimentalCoroutinesApi
fun <T> ViewModel.flowSafeCall(request: () -> T) = liveData {
flow {
emit(request)
}.onStart {
this@liveData.emit(Resource.loading())
}.flowOn(Dispatchers.IO)
.catch { message ->
this@liveData.emit(handleResponseError(message))
}.collectLatest {
this@liveData.emit(Resource.success(it))
}
}
/***
* 基于flow实现 网络请求 这里需要写 flow{ emit(request)}
*/
@ExperimentalCoroutinesApi
fun <T> ViewModel.flowCall(flow: () -> Flow<T>) =
liveData {
flow().onStart {
this@liveData.emit(Resource.loading())
}
.flowOn(Dispatchers.IO)
.catch { message ->
this@liveData.emit(handleResponseError(message))
}
.collectLatest {
this@liveData.emit(Resource.success(it))
}
}
fun <T> handleResponseError(throwable: Throwable?): Resource<T> { fun <T> handleResponseError(throwable: Throwable?): Resource<T> {
return when (throwable) { return when (throwable) {
...@@ -73,7 +112,10 @@ fun <T> handleResponseError(throwable: Throwable?): Resource<T> { ...@@ -73,7 +112,10 @@ fun <T> handleResponseError(throwable: Throwable?): Resource<T> {
is ConnectException, is UnknownHostException -> is ConnectException, is UnknownHostException ->
Resource.error("当前网络状态不稳定,请检查当前网络", null) Resource.error("当前网络状态不稳定,请检查当前网络", null)
is BizException -> is BizException ->
Resource.error(throwable.message!!,BaseData(throwable.code,null,"","false") as T) Resource.error(
throwable.message!!,
BaseData(throwable.code, null, "", "false") as T
)
is UnknownHostException -> is UnknownHostException ->
Resource.error("当前网络状态不稳定,请检查当前网络", null) Resource.error("当前网络状态不稳定,请检查当前网络", null)
else -> { else -> {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment