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>
<activity <activity
android:name="li.songe.gkd.OpenSchemeActivity" android:name=".OpenSchemeActivity"
android:exported="true" android:exported="true"
android:theme="@style/TransparentTheme"> android:theme="@style/TransparentTheme">
<intent-filter> <intent-filter>

View File

@ -108,7 +108,7 @@ fun Activity.navToMainActivity() {
if (intent != null) { if (intent != null) {
intent.component = ComponentName(this, MainActivity::class.java) intent.component = ComponentName(this, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK 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) startActivity(intent)
} }
finish() 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 // TODO transaction
val localIds = arrayOf(-1L, -2L) val localIds = arrayOf(-1L, -2L)
val maxOrder = (subsItemsFlow.value.maxOfOrNull { it.order } ?: -1) + 1 val maxOrder = (subsItemsFlow.value.maxOfOrNull { it.order } ?: -1) + 1
val subsItems = transferData.subsItems.mapIndexed { i, s -> val subsItems = transferData.subsItems.filter { s -> s.id >= 0 || localIds.contains(s.id) }
.mapIndexed { i, s ->
s.copy(order = maxOrder + i) 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.subsItemDao.insertOrIgnore(*subsItems.toTypedArray())
DbSet.subsConfigDao.insertOrIgnore(*transferData.subsConfigs.toTypedArray()) DbSet.subsConfigDao.insertOrIgnore(*transferData.subsConfigs.toTypedArray())
DbSet.categoryConfigDao.insertOrIgnore(*transferData.categoryConfigs.toTypedArray()) DbSet.categoryConfigDao.insertOrIgnore(*transferData.categoryConfigs.toTypedArray())
@ -45,4 +48,5 @@ suspend fun importTransferData(transferData: TransferData) {
updateSubscription(subscription) 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.Destination
import com.ramcosta.composedestinations.annotation.RootNavGraph import com.ramcosta.composedestinations.annotation.RootNavGraph
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.withContext
import li.songe.gkd.MainActivity 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.TransferData
import li.songe.gkd.data.importTransferData import li.songe.gkd.data.importTransferData
import li.songe.gkd.util.ProfileTransitions import li.songe.gkd.util.ProfileTransitions
@ -54,27 +58,35 @@ fun HomePage() {
val intent = context.intent val intent = context.intent
LaunchedEffect(key1 = intent, block = { LaunchedEffect(key1 = intent, block = {
if (intent != null) { intent ?: return@LaunchedEffect
context.intent = null context.intent = null
LogUtils.d(intent) LogUtils.d(intent)
val uri = intent.data val uri = intent.data ?: return@LaunchedEffect
if (uri != null && intent.scheme == "content" && (intent.type == "application/zip" || intent.type == "application/x-zip-compressed")) { val source = intent.getStringExtra("source")
if (source == OpenFileActivity::class.qualifiedName) {
vm.viewModelScope.launchTry(Dispatchers.IO) { vm.viewModelScope.launchTry(Dispatchers.IO) {
toast("加载导入...")
vm.tabFlow.value = subsPage.navItem
val string = readFileZipByteArray( val string = readFileZipByteArray(
UriUtils.uri2Bytes(uri), UriUtils.uri2Bytes(uri),
"${TransferData.TYPE}.json" "${TransferData.TYPE}.json"
) )
if (string != null) { if (string != null) {
val transferData = json.decodeFromString<TransferData>(string) val transferData = withContext(Dispatchers.Default) {
importTransferData(transferData) json.decodeFromString<TransferData>(string)
}
val hasNewSubsItem = importTransferData(transferData)
toast("导入成功") toast("导入成功")
vm.tabFlow.value = subsPage.navItem if (hasNewSubsItem) {
delay(1000)
checkSubsUpdate(true) checkSubsUpdate(true)
}
} else { } else {
toast("导入文件无数据") toast("导入失败")
}
} }
} }
} else if (source == OpenSchemeActivity::class.qualifiedName) {
LogUtils.d(uri)
} }
}) })
@ -86,7 +98,7 @@ fun HomePage() {
NavigationBar { NavigationBar {
pages.forEach { page -> pages.forEach { page ->
NavigationBarItem( NavigationBarItem(
selected = tab == page.navItem, selected = tab.label == page.navItem.label,
modifier = Modifier, modifier = Modifier,
onClick = { onClick = {
vm.tabFlow.value = page.navItem vm.tabFlow.value = page.navItem
@ -99,7 +111,8 @@ fun HomePage() {
}, },
label = { label = {
Text(text = page.navItem.label) Text(text = page.navItem.label)
}) }
)
} }
} }
}, },