Skip to content

Commit

Permalink
feat(DevHttpManager): 完成 ProgressRequestBody 代码
Browse files Browse the repository at this point in the history
Former-commit-id: 3214581
Former-commit-id: 8601b08
  • Loading branch information
afkT committed Apr 26, 2022
1 parent 6781461 commit 00e39bb
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 176 deletions.
10 changes: 5 additions & 5 deletions lib/DevAssist/src/main/java/dev/base/DevHistory.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.Stack;

import dev.utils.DevFinal;
import dev.utils.JCLogUtils;
import dev.utils.LogPrintUtils;
import dev.utils.common.StringUtils;

/**
Expand Down Expand Up @@ -614,15 +614,15 @@ private T get(
try {
return mBack.get(realIndex);
} catch (Exception e) {
JCLogUtils.eTag(TAG, e, "get - Back");
LogPrintUtils.eTag(TAG, e, "get - Back");
}
case FORWARD:
realIndex = calculateRealIndex(sizeForward(), index);
if (realIndex < 0) return null;
try {
return mForward.get(realIndex);
} catch (Exception e) {
JCLogUtils.eTag(TAG, e, "get - Forward");
LogPrintUtils.eTag(TAG, e, "get - Forward");
}
}
return null;
Expand Down Expand Up @@ -675,7 +675,7 @@ private T gotoBack(final int index) {
Collections.reverse(temps);
lists.addAll(temps);
} catch (Exception e) {
JCLogUtils.eTag(TAG, e, "gotoBack - subList");
LogPrintUtils.eTag(TAG, e, "gotoBack - subList");
}
for (int i = realIndex; i < size; i++) {
try {
Expand Down Expand Up @@ -727,7 +727,7 @@ private T gotoForward(final int index) {
Collections.reverse(temps);
lists.addAll(temps);
} catch (Exception e) {
JCLogUtils.eTag(TAG, e, "gotoForward - subList");
LogPrintUtils.eTag(TAG, e, "gotoForward - subList");
}
// 是否需要把 beforeCurrent 添加到回退栈中
if (mListener != null) {
Expand Down
57 changes: 52 additions & 5 deletions lib/DevHttpManager/src/main/java/dev/http/progress/Progress.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.http.progress

import android.os.Parcelable
import android.os.SystemClock
import dev.utils.DevFinal
import dev.utils.common.FileUtils
import dev.utils.common.NumberUtils
Expand Down Expand Up @@ -375,23 +376,69 @@ class Progress private constructor(
return this
}

/**
* 设置进度异常信息
* @param value 进度异常信息
* @return Progress
*/
internal fun setException(value: Throwable): Progress {
exception = value
return this
}

// ==========
// = 更新状态 =
// ==========

/**
* 设置当前状态
* @param value 当前状态
* @return Progress
*/
internal fun setStatus(value: Int): Progress {
private fun setStatus(value: Int): Progress {
status = value
return this
}

/**
* 设置进度异常信息
* @param value 进度异常信息
* 设置为 [Progress.START] 状态
* @return Progress
*/
internal fun setException(value: Throwable): Progress {
exception = value
internal fun toStart(): Progress {
if (isNORMAL()) {
setStatus(START)
.setLastRefreshTime(SystemClock.elapsedRealtime())
}
return this
}

/**
* 设置为 [Progress.ING] 状态
* @return Progress
*/
internal fun toIng(): Progress {
if (isSTART()) setStatus(ING)
return this
}

/**
* 设置为 [Progress.ERROR] 状态
* @param exception 进度异常信息
* @return Progress
*/
internal fun toError(
exception: Throwable
): Progress {
if (isING()) setStatus(ERROR).setException(exception)
return this
}

/**
* 设置为 [Progress.FINISH] 状态
* @return Progress
*/
internal fun toFinish(): Progress {
if (isING()) setStatus(FINISH)
return this
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package dev.http.progress

import android.os.Handler
import dev.DevUtils
import dev.utils.LogPrintUtils
import okhttp3.MediaType
import okhttp3.RequestBody
import okio.Buffer
import okio.BufferedSink
import okio.ForwardingSink
import okio.Sink
import okio.*
import java.io.IOException

/**
Expand All @@ -26,12 +24,35 @@ open class ProgressRequestBody(
protected val refreshTime: Long = Progress.REFRESH_TIME
) : RequestBody() {

// 日志 TAG
protected val TAG = ProgressRequestBody::class.java.simpleName

// ===============
// = RequestBody =
// ===============

override fun contentType(): MediaType? {
TODO("Not yet implemented")
return delegate.contentType()
}

override fun contentLength(): Long {
return try {
delegate.contentLength()
} catch (e: IOException) {
LogPrintUtils.eTag(TAG, e, "contentLength")
-1L
}
}

@Throws(IOException::class)
override fun writeTo(sink: BufferedSink) {
TODO("Not yet implemented")
val countingSink = CountingSink(sink)
val bufferedSink = countingSink.buffer()
delegate.writeTo(bufferedSink)
bufferedSink.flush()

countingSink.progress.toFinish()
.callback(callback, handler)
}

// ============
Expand All @@ -47,6 +68,11 @@ open class ProgressRequestBody(
// 进度信息存储类
val progress = Progress()

init {
progress.setTotalSize(contentLength())
.toStart().callback(callback, handler)
}

// ==================
// = ForwardingSink =
// ==================
Expand All @@ -59,20 +85,19 @@ open class ProgressRequestBody(
try {
super.write(source, byteCount)
} catch (e: Exception) {
ProgressUtils.toError(progress, e)
ProgressUtils.callback(progress, callback, handler)
progress.toError(e)
.callback(callback, handler)
throw e
}
if (progress.getTotalSize() <= 0) {
progress.setTotalSize(contentLength())
}
val allowCallback = ProgressUtils.changeProgress(
val allowCallback = changeProgress(
progress, refreshTime, byteCount
)
if (allowCallback) {
ProgressUtils.callback(
progress, callback, handler
)
progress.toIng()
.callback(callback, handler)
}
}
}
Expand Down
Loading

0 comments on commit 00e39bb

Please sign in to comment.