Commit ebfeb811 authored by 王雷's avatar 王雷

新增请求方法

parent 0f83da82
......@@ -37,7 +37,7 @@ repositories {
}
group 'com.qmai.android.widget'
version '1.1.25-SNAPSHOT'
version '1.1.30-SNAPSHOT'
gradlePublish {
......
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -55,7 +55,7 @@ repositories {
mavenCentral()
}
group 'com.qmai.android.fetch'
version '1.2.15-SNAPSHOT'
version '1.2.18-SNAPSHOT'
gradlePublish {
......
36
0
\ No newline at end of file
38
2
\ No newline at end of file
......@@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.qmai.android.fetch</groupId>
<artifactId>fetch</artifactId>
<version>1.2.15-SNAPSHOT</version>
<version>1.2.18-SNAPSHOT</version>
<packaging>aar</packaging>
<dependencies>
<dependency>
......
......@@ -3,7 +3,7 @@
<groupId>com.qmai.android.fetch</groupId>
<artifactId>fetch</artifactId>
<versioning>
<latest>1.2.15-SNAPSHOT</latest>
<latest>1.2.18-SNAPSHOT</latest>
<versions>
<version>1.2.3-SNAPSHOT</version>
<version>1.2.4-SNAPSHOT</version>
......@@ -19,7 +19,10 @@
<version>1.2.14-SNAPSHOT</version>
<version>1.2.2-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>
<lastUpdated>20201207063657</lastUpdated>
<lastUpdated>20201214104412</lastUpdated>
</versioning>
</metadata>
......@@ -2,29 +2,29 @@
<metadata modelVersion="1.1.0">
<groupId>com.qmai.android.fetch</groupId>
<artifactId>fetch</artifactId>
<version>1.2.15-SNAPSHOT</version>
<version>1.2.18-SNAPSHOT</version>
<versioning>
<snapshot>
<timestamp>20201207.063649</timestamp>
<timestamp>20201214.104406</timestamp>
<buildNumber>1</buildNumber>
</snapshot>
<lastUpdated>20201207063649</lastUpdated>
<lastUpdated>20201214104406</lastUpdated>
<snapshotVersions>
<snapshotVersion>
<classifier>sources</classifier>
<extension>jar</extension>
<value>1.2.15-20201207.063649-1</value>
<updated>20201207063649</updated>
<value>1.2.18-20201214.104406-1</value>
<updated>20201214104406</updated>
</snapshotVersion>
<snapshotVersion>
<extension>pom</extension>
<value>1.2.15-20201207.063649-1</value>
<updated>20201207063649</updated>
<extension>aar</extension>
<value>1.2.18-20201214.104406-1</value>
<updated>20201214104406</updated>
</snapshotVersion>
<snapshotVersion>
<extension>aar</extension>
<value>1.2.15-20201207.063649-1</value>
<updated>20201207063649</updated>
<extension>pom</extension>
<value>1.2.18-20201214.104406-1</value>
<updated>20201214104406</updated>
</snapshotVersion>
</snapshotVersions>
</versioning>
......
......@@ -8,7 +8,7 @@ package com.qimai.android.fetch.Response
@Description
@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 {
fun <T> success(data: T?): Resource<T> {
return Resource(
......@@ -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(
Status.ERROR,
data,
......@@ -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(
Status.LOADING,
data,
......
......@@ -6,10 +6,14 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.liveData
import androidx.lifecycle.viewModelScope
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.ServerResponseException
import com.qimai.android.fetch.convert.TOKENExpireException
import com.qimai.android.fetch.model.BaseData
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import retrofit2.HttpException
import java.net.ConnectException
......@@ -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> {
return when (throwable) {
......@@ -73,7 +112,10 @@ fun <T> handleResponseError(throwable: Throwable?): Resource<T> {
is ConnectException, is UnknownHostException ->
Resource.error("当前网络状态不稳定,请检查当前网络", null)
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 ->
Resource.error("当前网络状态不稳定,请检查当前网络", null)
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