optimize performance

This commit is contained in:
Evan You
2025-04-02 13:39:29 +08:00
parent 759b173cf0
commit de5f9fb2da
8 changed files with 52 additions and 33 deletions

View File

@@ -15,8 +15,8 @@ android {
applicationId = "com.donut.mixfile"
minSdk = 26
targetSdk = 35
versionCode = 100
versionName = "1.13.0"
versionCode = 101
versionName = "1.13.1"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {

View File

@@ -10,8 +10,10 @@ import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.app.NotificationManagerCompat
import com.donut.mixfile.ui.component.MainContent
import com.donut.mixfile.ui.theme.MainTheme
import com.donut.mixfile.util.file.MixFileSelector
import com.donut.mixfile.util.file.uploadFileUris
import com.donut.mixfile.util.objects.MixActivity
@@ -40,13 +42,13 @@ class MainActivity : MixActivity(MAIN_ID) {
super.onCreate(savedInstanceState)
val intentFilter = IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)
networkChangeReceiver = NetworkChangeReceiver
//请求通知权限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (!NotificationManagerCompat.from(this).areNotificationsEnabled()) {
requestNotificationPermissionLauncher.launch(android.Manifest.permission.POST_NOTIFICATIONS)
}
}
registerReceiver(networkChangeReceiver, intentFilter)
enableEdgeToEdge()
setContent {

View File

@@ -20,13 +20,13 @@ abstract class Uploader(val name: String) {
val refererTransforms = mutableMapOf<String, (url: String, referer: String) -> String>()
fun transformUrl(url: String): String {
return urlTransforms.entries.fold(url) { acc, (name, transform) ->
return urlTransforms.entries.fold(url) { acc, (_, transform) ->
transform(acc)
}
}
fun transformReferer(url: String, referer: String): String {
return refererTransforms.entries.fold(referer) { acc, (name, transform) ->
return refererTransforms.entries.fold(referer) { acc, (_, transform) ->
transform(url, acc)
}
}

View File

@@ -1,12 +1,15 @@
package com.donut.mixfile.util
import android.os.Parcelable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import com.alibaba.fastjson2.into
import com.alibaba.fastjson2.toJSONString
import com.donut.mixfile.appScope
import com.donut.mixfile.kv
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
fun <T> constructCachedMutableValue(
value: T,
@@ -46,20 +49,14 @@ fun cachedMutableOf(value: Set<String>, key: String) =
{ kv.decodeStringSet(key, value)!! },
)
fun cachedMutableOf(value: Parcelable, key: String) =
constructCachedMutableValue(
value,
key,
{ kv.encode(key, it) },
getter@{
kv.decodeParcelable(key, value.javaClass)
})
inline fun <reified T, reified C : Iterable<T>> cachedMutableOf(value: C, key: String) =
constructCachedMutableValue(
value,
key,
{ kv.encode(key, it.toJSONString()) },
{
kv.encode(key, it.toJSONString())
},
getter@{
var result = value
catchError {
@@ -76,6 +73,7 @@ abstract class CachedMutableValue<T>(
private val key: String,
) {
var value by mutableStateOf(value)
private var saveJob: Job? = null
private var loaded = false
abstract fun readCachedValue(): T
@@ -91,6 +89,13 @@ abstract class CachedMutableValue<T>(
operator fun setValue(thisRef: Any?, property: Any?, value: T) {
this.value = value
writeCachedValue(value)
synchronized(key) {
saveJob?.cancel()
saveJob = appScope.launch(Dispatchers.IO) {
catchError {
writeCachedValue(value)
}
}
}
}
}

View File

@@ -7,6 +7,7 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import com.alibaba.fastjson2.annotation.JSONField
import com.donut.mixfile.server.core.utils.bean.MixShareInfo
import com.donut.mixfile.server.core.utils.resolveMixShareInfo
@@ -44,13 +45,13 @@ data class FileDataLog(
list: List<FileDataLog>,
action: (FileDataLog) -> FileDataLog
): List<FileDataLog> {
return list.map {
if (it.shareInfoData == this.shareInfoData) {
action(it)
} else {
it
}
val newList = list.toMutableList()
val index = newList.indexOfFirst { it.shareInfoData == this.shareInfoData }
if (index == -1) {
return newList
}
newList[index] = action(newList[index])
return newList
}
fun rename(callback: (FileDataLog) -> Unit = {}) {

View File

@@ -71,11 +71,6 @@ fun showFileInfoDialog(
InfoText(key = "大小: ", value = formatFileSize(shareInfo.fileSize))
InfoText(key = "密钥: ", value = shareInfo.key)
FlowRow(horizontalArrangement = Arrangement.spacedBy(10.dp)) {
// AssistChip(onClick = {
// shareInfo.shareCode(useShortCode).copyToClipboard()
// }, label = {
// Text(text = "复制分享码", color = colorScheme.primary)
// })
if (fileName.startsWith("__mixfile_list") || fileName.endsWith(".mix_list")) {
AssistChip(onClick = {
importFileList(shareInfo.downloadUrl)

View File

@@ -102,15 +102,18 @@ fun showFileList(fileList: List<FileDataLog>) {
}
}
setPositiveButton("导入文件") {
val prevSize = favorites.size
val fileMap = favorites.map { it.shareInfoData }.toSet()
val newFiles = mutableSetOf<FileDataLog>()
val newCategories = mutableSetOf<String>()
fileList.forEach {
favCategories += it.category
newCategories += it.category
if (!fileMap.contains(it.shareInfoData)) {
favorites += it
newFiles += it
}
}
showToast("导入了 ${favorites.size - prevSize} 个文件")
favCategories += newCategories
favorites += newFiles
showToast("导入了 ${newFiles.size} 个文件")
closeDialog()
}
show()

View File

@@ -1,9 +1,20 @@
package com.donut.mixfile
import com.donut.mixfile.server.core.utils.bean.MixShareInfo
import org.junit.Test
import java.util.Date
//appScope.launch(Dispatchers.IO) {
// repeat(100) {
// favorites += List(1000) {
// FileDataLog(
// genRandomString(32),
// "test-data",
// 1,
// category = "test"
// )
// }
// }
//}
/**
* Example local unit test, which will execute on the development machine (host).
@@ -18,10 +29,12 @@ class ExampleUnitTest {
val date: Date = Date()
)
val map = mapOf(1 to "aa", 2 to "bb")
@Test
fun main() {
println(map.values)
}