perf: upload lazy get file
Some checks failed
Build-Apk / build (push) Has been cancelled

This commit is contained in:
lisonge 2024-09-17 02:46:05 +08:00
parent b09d48b44c
commit de140488aa
3 changed files with 23 additions and 25 deletions

View File

@ -211,10 +211,7 @@ fun AdvancedPage() {
modifier = Modifier
.clickable(onClick = throttle {
showShareLogDlg = false
vm.viewModelScope.launchTry(Dispatchers.IO) {
val logZipFile = buildLogFile()
vm.uploadOptions.startTask(logZipFile)
}
vm.uploadOptions.startTask(getFile = { buildLogFile() })
})
.then(modifier)
)

View File

@ -204,7 +204,7 @@ fun SnapshotPage() {
Text(
text = "分享到其他应用",
modifier = Modifier
.clickable(onClick = vm.viewModelScope.launchAsFn {
.clickable(onClick = throttle(fn = vm.viewModelScope.launchAsFn {
selectedSnapshot = null
val zipFile = SnapshotExt.getSnapshotZipFile(
snapshotVal.id,
@ -212,14 +212,14 @@ fun SnapshotPage() {
snapshotVal.activityId
)
context.shareFile(zipFile, "分享快照文件")
})
}))
.then(modifier)
)
HorizontalDivider()
Text(
text = "保存到下载",
modifier = Modifier
.clickable(onClick = vm.viewModelScope.launchAsFn {
.clickable(onClick = throttle(fn = vm.viewModelScope.launchAsFn {
selectedSnapshot = null
val zipFile = SnapshotExt.getSnapshotZipFile(
snapshotVal.id,
@ -227,14 +227,14 @@ fun SnapshotPage() {
snapshotVal.activityId
)
context.saveFileToDownloads(zipFile)
})
}))
.then(modifier)
)
HorizontalDivider()
if (snapshotVal.githubAssetId != null) {
Text(
text = "复制链接", modifier = Modifier
.clickable(onClick = {
.clickable(onClick = throttle {
selectedSnapshot = null
ClipboardUtils.copyText(IMPORT_SHORT_URL + snapshotVal.githubAssetId)
toast("复制成功")
@ -244,12 +244,10 @@ fun SnapshotPage() {
} else {
Text(
text = "生成链接(需科学上网)", modifier = Modifier
.clickable(onClick = vm.viewModelScope.launchAsFn(Dispatchers.IO) {
.clickable(onClick = throttle {
selectedSnapshot = null
vm.uploadOptions.startTask(
file = SnapshotExt.getSnapshotZipFile(
snapshotVal.id
),
getFile = { SnapshotExt.getSnapshotZipFile(snapshotVal.id) },
onSuccessResult = vm.viewModelScope.launchAsFn<GithubPoliciesAsset>(
Dispatchers.IO
) {
@ -265,7 +263,7 @@ fun SnapshotPage() {
Text(
text = "保存截图到相册",
modifier = Modifier
.clickable(onClick = vm.viewModelScope.launchAsFn {
.clickable(onClick = throttle(fn = vm.viewModelScope.launchAsFn {
requiredPermission(context, canWriteExternalStorage)
ImageUtils.save2Album(
ImageUtils.getBitmap(snapshotVal.screenshotFile),
@ -274,14 +272,14 @@ fun SnapshotPage() {
)
toast("保存成功")
selectedSnapshot = null
})
}))
.then(modifier)
)
HorizontalDivider()
Text(
text = "替换截图(去除隐私)",
modifier = Modifier
.clickable(onClick = vm.viewModelScope.launchAsFn {
.clickable(onClick = throttle(fn = vm.viewModelScope.launchAsFn {
val uri = context.pickContentLauncher.launchForImageResult()
withContext(Dispatchers.IO) {
val oldBitmap = ImageUtils.getBitmap(snapshotVal.screenshotFile)
@ -305,19 +303,19 @@ fun SnapshotPage() {
}
toast("替换成功")
selectedSnapshot = null
})
}))
.then(modifier)
)
HorizontalDivider()
Text(
text = "删除", modifier = Modifier
.clickable(onClick = vm.viewModelScope.launchAsFn {
.clickable(onClick = throttle(fn = vm.viewModelScope.launchAsFn {
DbSet.snapshotDao.delete(snapshotVal)
withContext(Dispatchers.IO) {
SnapshotExt.removeAssets(snapshotVal.id)
}
selectedSnapshot = null
})
}))
.then(modifier), color = colorScheme.error
)
}

View File

@ -24,16 +24,16 @@ class UploadOptions(
private val scope: CoroutineScope,
private val showHref: (GithubPoliciesAsset) -> String = { it.shortHref }
) {
val statusFlow = MutableStateFlow<LoadStatus<GithubPoliciesAsset>?>(null)
private val statusFlow = MutableStateFlow<LoadStatus<GithubPoliciesAsset>?>(null)
private var job: Job? = null
private fun buildTask(
cookie: String,
file: File,
getFile: suspend () -> File,
onSuccessResult: ((GithubPoliciesAsset) -> Unit)?
) = scope.launchTry(Dispatchers.IO) {
statusFlow.value = LoadStatus.Loading()
try {
val policiesAsset = uploadFileToGithub(cookie, file) {
val policiesAsset = uploadFileToGithub(cookie, getFile()) {
if (statusFlow.value is LoadStatus.Loading) {
statusFlow.value = LoadStatus.Loading(it)
}
@ -47,16 +47,19 @@ class UploadOptions(
}
}
fun startTask(file: File, onSuccessResult: ((GithubPoliciesAsset) -> Unit)? = null) {
fun startTask(
getFile: suspend () -> File,
onSuccessResult: ((GithubPoliciesAsset) -> Unit)? = null
) {
val cookie = privacyStoreFlow.value.githubCookie
if (cookie == null || cookie.isBlank()) {
if (cookie.isNullOrBlank()) {
toast("请先设置 cookie 后再上传")
return
}
if (job != null || statusFlow.value is LoadStatus.Loading) {
return
}
job = buildTask(cookie, file, onSuccessResult)
job = buildTask(cookie, getFile, onSuccessResult)
}
private fun stopTask() {