perf: import data

This commit is contained in:
lisonge 2024-05-30 21:48:55 +08:00
parent df11c84cfb
commit a1b59a086e
4 changed files with 43 additions and 26 deletions

View File

@ -54,7 +54,7 @@
</activity>
<activity
android:name="li.songe.gkd.OpenSchemeActivity"
android:name=".OpenSchemeActivity"
android:exported="true"
android:theme="@style/TransparentTheme">
<intent-filter>

View File

@ -108,7 +108,7 @@ fun Activity.navToMainActivity() {
if (intent != null) {
intent.component = ComponentName(this, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
intent.putExtra("source", this::class.java.name)
intent.putExtra("source", this::class.qualifiedName)
startActivity(intent)
}
finish()

View File

@ -30,13 +30,16 @@ suspend fun exportTransferData(subsItemIds: List<Long>): TransferData {
)
}
suspend fun importTransferData(transferData: TransferData) {
suspend fun importTransferData(transferData: TransferData): Boolean {
// TODO transaction
val localIds = arrayOf(-1L, -2L)
val maxOrder = (subsItemsFlow.value.maxOfOrNull { it.order } ?: -1) + 1
val subsItems = transferData.subsItems.mapIndexed { i, s ->
s.copy(order = maxOrder + i)
}
val subsItems = transferData.subsItems.filter { s -> s.id >= 0 || localIds.contains(s.id) }
.mapIndexed { i, s ->
s.copy(order = maxOrder + i)
}
val hasNewSubsItem =
subsItems.any { newSubs -> newSubs.id >= 0 && subsItemsFlow.value.all { oldSubs -> oldSubs.id != newSubs.id } }
DbSet.subsItemDao.insertOrIgnore(*subsItems.toTypedArray())
DbSet.subsConfigDao.insertOrIgnore(*transferData.subsConfigs.toTypedArray())
DbSet.categoryConfigDao.insertOrIgnore(*transferData.categoryConfigs.toTypedArray())
@ -45,4 +48,5 @@ suspend fun importTransferData(transferData: TransferData) {
updateSubscription(subscription)
}
}
return hasNewSubsItem
}

View File

@ -19,7 +19,11 @@ import com.blankj.utilcode.util.UriUtils
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.annotation.RootNavGraph
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.withContext
import li.songe.gkd.MainActivity
import li.songe.gkd.OpenFileActivity
import li.songe.gkd.OpenSchemeActivity
import li.songe.gkd.data.TransferData
import li.songe.gkd.data.importTransferData
import li.songe.gkd.util.ProfileTransitions
@ -54,27 +58,35 @@ fun HomePage() {
val intent = context.intent
LaunchedEffect(key1 = intent, block = {
if (intent != null) {
context.intent = null
LogUtils.d(intent)
val uri = intent.data
if (uri != null && intent.scheme == "content" && (intent.type == "application/zip" || intent.type == "application/x-zip-compressed")) {
vm.viewModelScope.launchTry(Dispatchers.IO) {
val string = readFileZipByteArray(
UriUtils.uri2Bytes(uri),
"${TransferData.TYPE}.json"
)
if (string != null) {
val transferData = json.decodeFromString<TransferData>(string)
importTransferData(transferData)
toast("导入成功")
vm.tabFlow.value = subsPage.navItem
checkSubsUpdate(true)
} else {
toast("导入文件无数据")
intent ?: return@LaunchedEffect
context.intent = null
LogUtils.d(intent)
val uri = intent.data ?: return@LaunchedEffect
val source = intent.getStringExtra("source")
if (source == OpenFileActivity::class.qualifiedName) {
vm.viewModelScope.launchTry(Dispatchers.IO) {
toast("加载导入...")
vm.tabFlow.value = subsPage.navItem
val string = readFileZipByteArray(
UriUtils.uri2Bytes(uri),
"${TransferData.TYPE}.json"
)
if (string != null) {
val transferData = withContext(Dispatchers.Default) {
json.decodeFromString<TransferData>(string)
}
val hasNewSubsItem = importTransferData(transferData)
toast("导入成功")
if (hasNewSubsItem) {
delay(1000)
checkSubsUpdate(true)
}
} else {
toast("导入失败")
}
}
} else if (source == OpenSchemeActivity::class.qualifiedName) {
LogUtils.d(uri)
}
})
@ -86,7 +98,7 @@ fun HomePage() {
NavigationBar {
pages.forEach { page ->
NavigationBarItem(
selected = tab == page.navItem,
selected = tab.label == page.navItem.label,
modifier = Modifier,
onClick = {
vm.tabFlow.value = page.navItem
@ -99,7 +111,8 @@ fun HomePage() {
},
label = {
Text(text = page.navItem.label)
})
}
)
}
}
},