@@ -29,47 +29,50 @@ import li.songe.gkd.util.toast
29
29
import java.io.File
30
30
31
31
class UploadOptions (
32
- private val scope : CoroutineScope
32
+ private val scope : CoroutineScope ,
33
+ private val showHref : (GithubPoliciesAsset ) -> String = { it.shortHref }
33
34
) {
34
- private val statusFlow = MutableStateFlow <LoadStatus <GithubPoliciesAsset >? > (null )
35
+ val statusFlow = MutableStateFlow <LoadStatus <GithubPoliciesAsset >? > (null )
35
36
private var job: Job ? = null
36
- private fun buildTask (file : File ) = scope.launchTry(Dispatchers .IO ) {
37
- statusFlow.value = LoadStatus .Loading ()
38
- try {
39
- val response =
40
- client.submitFormWithBinaryData(url = FILE_UPLOAD_URL , formData = formData {
41
- append(" \" file\" " , file.readBytes(), Headers .build {
42
- append(HttpHeaders .ContentType , " application/x-zip-compressed" )
43
- append(HttpHeaders .ContentDisposition , " filename=\" file.zip\" " )
44
- })
45
- }) {
46
- onUpload { bytesSentTotal, contentLength ->
47
- if (statusFlow.value is LoadStatus .Loading ) {
48
- statusFlow.value =
49
- LoadStatus .Loading (bytesSentTotal / contentLength.toFloat())
37
+ private fun buildTask (file : File , onSuccessResult : ((GithubPoliciesAsset ) -> Unit )? ) =
38
+ scope.launchTry(Dispatchers .IO ) {
39
+ statusFlow.value = LoadStatus .Loading ()
40
+ try {
41
+ val response =
42
+ client.submitFormWithBinaryData(url = FILE_UPLOAD_URL , formData = formData {
43
+ append(" \" file\" " , file.readBytes(), Headers .build {
44
+ append(HttpHeaders .ContentType , " application/x-zip-compressed" )
45
+ append(HttpHeaders .ContentDisposition , " filename=\" file.zip\" " )
46
+ })
47
+ }) {
48
+ onUpload { bytesSentTotal, contentLength ->
49
+ if (statusFlow.value is LoadStatus .Loading ) {
50
+ statusFlow.value =
51
+ LoadStatus .Loading (bytesSentTotal / contentLength.toFloat())
52
+ }
50
53
}
51
54
}
55
+ if (response.headers[" X_RPC_OK" ] == " true" ) {
56
+ val policiesAsset = response.body<GithubPoliciesAsset >()
57
+ statusFlow.value = LoadStatus .Success (policiesAsset)
58
+ onSuccessResult?.invoke(policiesAsset)
59
+ } else if (response.headers[" X_RPC_OK" ] == " false" ) {
60
+ statusFlow.value = LoadStatus .Failure (response.body<RpcError >())
61
+ } else {
62
+ statusFlow.value = LoadStatus .Failure (Exception (response.bodyAsText()))
52
63
}
53
- if (response.headers[" X_RPC_OK" ] == " true" ) {
54
- val policiesAsset = response.body<GithubPoliciesAsset >()
55
- statusFlow.value = LoadStatus .Success (policiesAsset)
56
- } else if (response.headers[" X_RPC_OK" ] == " false" ) {
57
- statusFlow.value = LoadStatus .Failure (response.body<RpcError >())
58
- } else {
59
- statusFlow.value = LoadStatus .Failure (Exception (response.bodyAsText()))
64
+ } catch (e: Exception ) {
65
+ statusFlow.value = LoadStatus .Failure (e)
66
+ } finally {
67
+ job = null
60
68
}
61
- } catch (e: Exception ) {
62
- statusFlow.value = LoadStatus .Failure (e)
63
- } finally {
64
- job = null
65
69
}
66
- }
67
70
68
- fun startTask (file : File ) {
71
+ fun startTask (file : File , onSuccessResult : (( GithubPoliciesAsset ) -> Unit ) ? = null ) {
69
72
if (job != null || statusFlow.value is LoadStatus .Loading ) {
70
73
return
71
74
}
72
- job = buildTask(file)
75
+ job = buildTask(file, onSuccessResult )
73
76
}
74
77
75
78
private fun stopTask () {
@@ -104,8 +107,9 @@ class UploadOptions(
104
107
}
105
108
106
109
is LoadStatus .Success -> {
110
+ val href = showHref(status.result)
107
111
AlertDialog (title = { Text (text = " 上传完成" ) }, text = {
108
- Text (text = status.result.shortHref )
112
+ Text (text = href )
109
113
}, onDismissRequest = {}, dismissButton = {
110
114
TextButton (onClick = {
111
115
statusFlow.value = null
@@ -114,7 +118,7 @@ class UploadOptions(
114
118
}
115
119
}, confirmButton = {
116
120
TextButton (onClick = {
117
- ClipboardUtils .copyText(status.result.shortHref )
121
+ ClipboardUtils .copyText(href )
118
122
toast(" 复制成功" )
119
123
statusFlow.value = null
120
124
}) {
0 commit comments