|
| 1 | +package rxhttp.wrapper.param |
| 2 | + |
| 3 | +import com.example.httpsender.parser.ResponseParser |
| 4 | +import io.reactivex.Observable |
| 5 | +import io.reactivex.Scheduler |
| 6 | +import io.reactivex.functions.Consumer |
| 7 | +import kotlin.Any |
| 8 | +import kotlin.Deprecated |
| 9 | +import kotlin.Unit |
| 10 | +import kotlinx.coroutines.CoroutineScope |
| 11 | +import kotlinx.coroutines.launch |
| 12 | +import rxhttp.BaseRxHttp |
| 13 | +import rxhttp.IRxHttp |
| 14 | +import rxhttp.await |
| 15 | +import rxhttp.wrapper.callback.ProgressCallback |
| 16 | +import rxhttp.wrapper.entity.Progress |
| 17 | +import rxhttp.wrapper.parse.Parser |
| 18 | +import rxhttp.wrapper.parse.SimpleParser |
| 19 | + |
| 20 | +inline fun <reified T : Any> BaseRxHttp.asResponse() = asParser(object: ResponseParser<T>() {}) |
| 21 | + |
| 22 | +suspend inline fun <reified T : Any> IRxHttp.awaitResponse() = await(object: ResponseParser<T>() {}) |
| 23 | + |
| 24 | +/** |
| 25 | + * 调用此方法监听上传进度 |
| 26 | + * @param coroutine CoroutineScope对象,用于开启协程回调进度,进度回调所在线程取决于协程所在线程 |
| 27 | + * @param progress 进度回调 |
| 28 | + * 注意:此方法仅在协程环境下才生效 |
| 29 | + */ |
| 30 | +fun RxHttpFormParam.upload(coroutine: CoroutineScope? = null, progress: (Progress) -> Unit) = |
| 31 | + param.setProgressCallback(ProgressCallback { currentProgress, currentSize, totalSize -> |
| 32 | + val p = Progress(currentProgress, currentSize, totalSize) |
| 33 | + coroutine?.launch { progress(p) } ?: progress(p) |
| 34 | +}) |
| 35 | +/** |
| 36 | + * 调用此方法监听上传进度 |
| 37 | + * @param observeOnScheduler 用于控制下游回调所在线程(包括进度回调) |
| 38 | + * @param progress 进度回调 |
| 39 | + */ |
| 40 | +fun RxHttpFormParam.upload(observeOnScheduler: Scheduler? = null, progress: (Progress) -> Unit) = |
| 41 | + upload(Consumer{ progress(it) }, observeOnScheduler) |
| 42 | + |
| 43 | +/** |
| 44 | + * please use [upload] + asXxx method instead |
| 45 | + */ |
| 46 | +@Deprecated("Will be removed in a future release") |
| 47 | +inline fun <reified T : Any> RxHttpFormParam.asUpload(observeOnScheduler: Scheduler? = null, |
| 48 | + noinline progress: (Progress) -> Unit) = asUpload(object: SimpleParser<T>() {}, |
| 49 | + observeOnScheduler, progress) |
| 50 | + |
| 51 | +/** |
| 52 | + * please use [upload] + asXxx method instead |
| 53 | + */ |
| 54 | +@Deprecated("Will be removed in a future release") |
| 55 | +fun <T : Any> RxHttpFormParam.asUpload( |
| 56 | + parser: Parser<T>, |
| 57 | + observeOnScheduler: Scheduler? = null, |
| 58 | + progress: (Progress) -> Unit |
| 59 | +): Observable<T> = asUpload(parser, Consumer{ progress(it) }, observeOnScheduler) |
| 60 | + |
| 61 | +/** |
| 62 | + * please use [upload] + awaitXxx method instead |
| 63 | + */ |
| 64 | +@Deprecated("Will be removed in a future release") |
| 65 | +suspend inline fun <reified T : Any> RxHttpFormParam.awaitUpload(coroutine: CoroutineScope? = null, |
| 66 | + noinline progress: (Progress) -> Unit): T = awaitUpload(object: SimpleParser<T>() {}, coroutine, |
| 67 | + progress) |
| 68 | + |
| 69 | +/** |
| 70 | + * please use [upload] + awaitXxx method instead |
| 71 | + */ |
| 72 | +@Deprecated("Will be removed in a future release") |
| 73 | +suspend fun <T : Any> RxHttpFormParam.awaitUpload( |
| 74 | + parser: Parser<T>, |
| 75 | + coroutine: CoroutineScope? = null, |
| 76 | + progress: (Progress) -> Unit |
| 77 | +): T { |
| 78 | + upload(coroutine, progress) |
| 79 | + return await(parser) |
| 80 | +} |
0 commit comments