Skip to content

Commit

Permalink
perf: upload github file link
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Nov 13, 2024
1 parent c07d29e commit 53a035b
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 89 deletions.
3 changes: 3 additions & 0 deletions app/src/main/kotlin/li/songe/gkd/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import li.songe.gkd.service.fixRestartService
import li.songe.gkd.service.updateLauncherAppId
import li.songe.gkd.ui.component.BuildDialog
import li.songe.gkd.ui.theme.AppTheme
import li.songe.gkd.util.EditGithubCookieDlg
import li.songe.gkd.util.LocalNavController
import li.songe.gkd.util.UpgradeDialog
import li.songe.gkd.util.appInfoCacheFlow
Expand Down Expand Up @@ -93,6 +94,8 @@ class MainActivity : ComponentActivity() {
ShizukuErrorDialog(mainVm.shizukuErrorFlow)
AuthDialog(mainVm.authReasonFlow)
BuildDialog(mainVm.dialogFlow)
mainVm.uploadOptions.ShowDialog()
EditGithubCookieDlg(mainVm.showEditCookieDlgFlow)
if (META.updateEnabled) {
UpgradeDialog(mainVm.updateStatus)
}
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/kotlin/li/songe/gkd/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import li.songe.gkd.data.SubsItem
import li.songe.gkd.db.DbSet
import li.songe.gkd.permission.AuthReason
import li.songe.gkd.ui.component.AlertDialogOptions
import li.songe.gkd.ui.component.UploadOptions
import li.songe.gkd.util.LOCAL_SUBS_ID
import li.songe.gkd.util.UpdateStatus
import li.songe.gkd.util.checkUpdate
Expand Down Expand Up @@ -43,6 +44,10 @@ class MainViewModel : ViewModel() {

val shizukuErrorFlow = MutableStateFlow(false)

val uploadOptions = UploadOptions(this)

val showEditCookieDlgFlow = MutableStateFlow(false)

init {
viewModelScope.launchTry(Dispatchers.IO) {
val subsItems = DbSet.subsItemDao.queryAll()
Expand Down
7 changes: 3 additions & 4 deletions app/src/main/kotlin/li/songe/gkd/ui/AboutPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.compose.viewModel
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.annotation.RootGraph
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -88,10 +87,8 @@ import li.songe.gkd.util.toast
fun AboutPage() {
val navController = LocalNavController.current
val context = LocalContext.current as MainActivity
val vm = viewModel<AboutVm>()
val store by storeFlow.collectAsState()

vm.uploadOptions.ShowDialog()
var showInfoDlg by remember { mutableStateOf(false) }
if (showInfoDlg) {
AlertDialog(
Expand Down Expand Up @@ -176,7 +173,9 @@ fun AboutPage() {
modifier = Modifier
.clickable(onClick = throttle {
showShareLogDlg = false
vm.uploadOptions.startTask(getFile = { buildLogFile() })
context.mainVm.uploadOptions.startTask(
getFile = { buildLogFile() }
)
})
.then(modifier)
)
Expand Down
9 changes: 0 additions & 9 deletions app/src/main/kotlin/li/songe/gkd/ui/AboutVm.kt

This file was deleted.

62 changes: 1 addition & 61 deletions app/src/main/kotlin/li/songe/gkd/ui/AdvancedPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import android.content.Context
import android.media.projection.MediaProjectionManager
import android.os.Build
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
Expand All @@ -19,7 +18,6 @@ import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.automirrored.outlined.HelpOutline
import androidx.compose.material.icons.filled.Edit
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Icon
Expand Down Expand Up @@ -58,7 +56,6 @@ import com.ramcosta.composedestinations.generated.destinations.ActivityLogPageDe
import com.ramcosta.composedestinations.generated.destinations.SnapshotPageDestination
import com.ramcosta.composedestinations.utils.toDestinationsNavigator
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.update
import li.songe.gkd.MainActivity
import li.songe.gkd.appScope
import li.songe.gkd.debug.FloatingService
Expand All @@ -81,7 +78,6 @@ import li.songe.gkd.util.LocalNavController
import li.songe.gkd.util.ProfileTransitions
import li.songe.gkd.util.launchAsFn
import li.songe.gkd.util.openUri
import li.songe.gkd.util.privacyStoreFlow
import li.songe.gkd.util.storeFlow
import li.songe.gkd.util.throttle
import li.songe.gkd.util.toast
Expand Down Expand Up @@ -155,62 +151,6 @@ fun AdvancedPage() {
})
}

var showEditCookieDlg by remember { mutableStateOf(false) }
if (showEditCookieDlg) {
val privacyStore by privacyStoreFlow.collectAsState()
var value by remember {
mutableStateOf(privacyStore.githubCookie ?: "")
}
AlertDialog(
onDismissRequest = {
if (value.isEmpty()) {
showEditCookieDlg = false
}
},
title = {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween,
modifier = Modifier.fillMaxWidth(),
) {
Text(text = "Github Cookie")
IconButton(onClick = throttle {
context.openUri("https://gkd.li/?r=1")
}) {
Icon(
imageVector = Icons.AutoMirrored.Outlined.HelpOutline,
contentDescription = null,
)
}
}
},
text = {
OutlinedTextField(
value = value,
onValueChange = {
value = it.filter { c -> c != '\n' && c != '\r' }
},
placeholder = { Text(text = "请输入 Github Cookie") },
modifier = Modifier.fillMaxWidth(),
maxLines = 10,
)
},
confirmButton = {
TextButton(onClick = {
showEditCookieDlg = false
privacyStoreFlow.update { it.copy(githubCookie = value.trim()) }
}) {
Text(text = "确认")
}
},
dismissButton = {
TextButton(onClick = { showEditCookieDlg = false }) {
Text(text = "取消")
}
}
)
}

val scrollBehavior = TopAppBarDefaults.enterAlwaysScrollBehavior()
Scaffold(
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
Expand Down Expand Up @@ -454,7 +394,7 @@ fun AdvancedPage() {
},
imageVector = Icons.Default.Edit,
onClick = {
showEditCookieDlg = true
context.mainVm.showEditCookieDlgFlow.value = true
}
)

Expand Down
5 changes: 2 additions & 3 deletions app/src/main/kotlin/li/songe/gkd/ui/SnapshotPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ fun SnapshotPage() {
val vm = viewModel<SnapshotVm>()
val snapshots by vm.snapshotsState.collectAsState()

vm.uploadOptions.ShowDialog()

var selectedSnapshot by remember {
mutableStateOf<Snapshot?>(null)
}
Expand Down Expand Up @@ -246,8 +244,9 @@ fun SnapshotPage() {
text = "生成链接(需科学上网)", modifier = Modifier
.clickable(onClick = throttle {
selectedSnapshot = null
vm.uploadOptions.startTask(
context.mainVm.uploadOptions.startTask(
getFile = { SnapshotExt.getSnapshotZipFile(snapshotVal.id) },
showHref = { IMPORT_SHORT_URL + it.id },
onSuccessResult = vm.viewModelScope.launchAsFn<GithubPoliciesAsset>(
Dispatchers.IO
) {
Expand Down
7 changes: 0 additions & 7 deletions app/src/main/kotlin/li/songe/gkd/ui/SnapshotVm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,8 @@ import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.stateIn
import li.songe.gkd.db.DbSet
import li.songe.gkd.ui.component.UploadOptions
import li.songe.gkd.util.IMPORT_SHORT_URL

class SnapshotVm : ViewModel() {
val snapshotsState = DbSet.snapshotDao.query()
.stateIn(viewModelScope, SharingStarted.Eagerly, emptyList())

val uploadOptions = UploadOptions(
scope = viewModelScope,
showHref = { IMPORT_SHORT_URL + it.id }
)
}
24 changes: 20 additions & 4 deletions app/src/main/kotlin/li/songe/gkd/ui/component/UploadOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.ClipboardUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.MutableStateFlow
import li.songe.gkd.MainViewModel
import li.songe.gkd.data.GithubPoliciesAsset
import li.songe.gkd.util.GithubCookieException
import li.songe.gkd.util.LoadStatus
import li.songe.gkd.util.launchTry
import li.songe.gkd.util.privacyStoreFlow
Expand All @@ -21,16 +23,15 @@ import li.songe.gkd.util.uploadFileToGithub
import java.io.File

class UploadOptions(
private val scope: CoroutineScope,
private val showHref: (GithubPoliciesAsset) -> String = { it.shortHref }
private val mainVm: MainViewModel,
) {
private val statusFlow = MutableStateFlow<LoadStatus<GithubPoliciesAsset>?>(null)
private var job: Job? = null
private fun buildTask(
cookie: String,
getFile: suspend () -> File,
onSuccessResult: ((GithubPoliciesAsset) -> Unit)?
) = scope.launchTry(Dispatchers.IO) {
) = mainVm.viewModelScope.launchTry(Dispatchers.IO) {
statusFlow.value = LoadStatus.Loading()
try {
val policiesAsset = uploadFileToGithub(cookie, getFile()) {
Expand All @@ -47,18 +48,23 @@ class UploadOptions(
}
}


private var showHref: (GithubPoliciesAsset) -> String = { it.shortHref }
fun startTask(
getFile: suspend () -> File,
showHref: (GithubPoliciesAsset) -> String = { it.shortHref },
onSuccessResult: ((GithubPoliciesAsset) -> Unit)? = null
) {
val cookie = privacyStoreFlow.value.githubCookie
if (cookie.isNullOrBlank()) {
toast("请先设置 cookie 后再上传")
mainVm.showEditCookieDlgFlow.value = true
return
}
if (job != null || statusFlow.value is LoadStatus.Loading) {
return
}
this.showHref = showHref
job = buildTask(cookie, getFile, onSuccessResult)
}

Expand Down Expand Up @@ -123,6 +129,16 @@ class UploadOptions(
})
},
onDismissRequest = { statusFlow.value = null },
dismissButton = if (status.exception is GithubCookieException) ({
TextButton(onClick = {
statusFlow.value = null
mainVm.showEditCookieDlgFlow.value = true
}) {
Text(text = "更换 Cookie")
}
}) else {
null
},
confirmButton = {
TextButton(onClick = {
statusFlow.value = null
Expand Down
Loading

0 comments on commit 53a035b

Please sign in to comment.