From d4f0b76266c35500b07cbc6aeb9645162b1dda1a Mon Sep 17 00:00:00 2001 From: machiav3lli Date: Mon, 16 Dec 2024 02:06:56 +0100 Subject: [PATCH] Update: Opml export/import handling --- .../java/com/saulhdev/feeder/MainActivity.kt | 41 +++++++++---------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/com/saulhdev/feeder/MainActivity.kt b/app/src/main/java/com/saulhdev/feeder/MainActivity.kt index 8f4684c..ed39819 100644 --- a/app/src/main/java/com/saulhdev/feeder/MainActivity.kt +++ b/app/src/main/java/com/saulhdev/feeder/MainActivity.kt @@ -73,7 +73,10 @@ class MainActivity : ComponentActivity(), SavedStateRegistryOwner { registerForActivityResult(ActivityResultContracts.OpenDocument()) { uri -> if (uri != null) { CoroutineScope(Dispatchers.Main).launch { - importOpml(uri) + when (pendingOperation) { + DocumentOperation.IMPORT -> importOpml(uri) + else -> {} // No relevant operation pending + } } } } @@ -83,16 +86,25 @@ class MainActivity : ComponentActivity(), SavedStateRegistryOwner { ) { uri -> if (uri != null) { CoroutineScope(Dispatchers.Main).launch { - exportOpml(uri) + when (pendingOperation) { + DocumentOperation.EXPORT -> exportOpml(uri) + else -> {} // No relevant operation pending + } } } } + private enum class DocumentOperation { IMPORT, EXPORT } + + private var pendingOperation: DocumentOperation? = null + private fun launchOpmlImporter() { + pendingOperation = DocumentOperation.IMPORT opmlImporterLauncher.launch(arrayOf("application/xml", "text/xml", "text/opml")) } private fun launchOpmlExporter() { + pendingOperation = DocumentOperation.EXPORT opmlExporter.launch("NF-${localTime}.opml") } @@ -101,9 +113,12 @@ class MainActivity : ComponentActivity(), SavedStateRegistryOwner { WindowCompat.setDecorFitsSystemWindows(window, false) prefs = FeedPreferences.getInstance(this) - if (intent.hasExtra("import") || intent.hasExtra("export")) { - startTargetActivity() + + when { + intent.hasExtra("import") -> launchOpmlImporter() + intent.hasExtra("export") -> launchOpmlExporter() } + setContent { navController = rememberNavController() TransparentSystemBars() @@ -128,24 +143,6 @@ class MainActivity : ComponentActivity(), SavedStateRegistryOwner { NFApplication.mainActivity = this } - private fun startTargetActivity() { - when { - intent.hasExtra("import") -> { - registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { - launchOpmlImporter() - finish() - }.launch(intent.getParcelableExtra("import")) - } - - else -> { - registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { - launchOpmlExporter() - finish() - }.launch(intent.getParcelableExtra("export")) - } - } - } - @Composable fun TransparentSystemBars() { DisposableEffect(isDarkTheme, prefs.overlayTheme.getValue()) {