diff --git a/legacy/ui/legacy/src/main/java/com/fsck/k9/ui/settings/KoinModule.kt b/legacy/ui/legacy/src/main/java/com/fsck/k9/ui/settings/KoinModule.kt index e294aa618a6..e0e223b718f 100644 --- a/legacy/ui/legacy/src/main/java/com/fsck/k9/ui/settings/KoinModule.kt +++ b/legacy/ui/legacy/src/main/java/com/fsck/k9/ui/settings/KoinModule.kt @@ -43,7 +43,6 @@ val settingsUiModule = module { viewModel { SettingsExportViewModel( - context = get(), accountManager = get(), settingsExporter = get(), ) diff --git a/legacy/ui/legacy/src/main/java/com/fsck/k9/ui/settings/export/SettingsExportFragment.kt b/legacy/ui/legacy/src/main/java/com/fsck/k9/ui/settings/export/SettingsExportFragment.kt index 664d3eaa11e..eed919dc28b 100644 --- a/legacy/ui/legacy/src/main/java/com/fsck/k9/ui/settings/export/SettingsExportFragment.kt +++ b/legacy/ui/legacy/src/main/java/com/fsck/k9/ui/settings/export/SettingsExportFragment.kt @@ -1,12 +1,12 @@ package com.fsck.k9.ui.settings.export -import android.app.Activity import android.content.Intent import android.net.Uri import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import androidx.activity.result.contract.ActivityResultContracts import androidx.core.view.isVisible import androidx.fragment.app.Fragment import androidx.recyclerview.widget.RecyclerView @@ -23,6 +23,16 @@ class SettingsExportFragment : Fragment() { private lateinit var settingsExportAdapter: FastAdapter> private lateinit var itemAdapter: ItemAdapter> + private val createDocumentResultLauncher = registerForActivityResult( + ActivityResultContracts.CreateDocument(SETTINGS_MIME_TYPE), + ) { contentUri -> + if (contentUri != null) { + viewModel.onDocumentPicked(contentUri) + } else { + viewModel.onDocumentPickCanceled() + } + } + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { return inflater.inflate(R.layout.fragment_settings_export, container, false) } @@ -121,23 +131,18 @@ class SettingsExportFragment : Fragment() { private fun handleActionEvents(action: Action) { when (action) { - is Action.PickDocument -> pickDocument(action.fileNameSuggestion, action.mimeType) - is Action.ShareDocument -> shareDocument(action.contentUri, action.mimeType) + is Action.PickDocument -> pickDocument(action.fileNameSuggestion) + is Action.ShareDocument -> shareDocument(action.contentUri) } } - private fun pickDocument(fileNameSuggestion: String, mimeType: String) { - val createDocumentIntent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply { - type = mimeType - putExtra(Intent.EXTRA_TITLE, fileNameSuggestion) - addCategory(Intent.CATEGORY_OPENABLE) - } - startActivityForResult(createDocumentIntent, RESULT_PICK_DOCUMENT) + private fun pickDocument(fileNameSuggestion: String) { + createDocumentResultLauncher.launch(fileNameSuggestion) } - private fun shareDocument(contentUri: Uri, mimeType: String) { + private fun shareDocument(contentUri: Uri) { val shareIntent = Intent(Intent.ACTION_SEND).apply { - type = mimeType + type = SETTINGS_MIME_TYPE putExtra(Intent.EXTRA_STREAM, contentUri) addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) } @@ -150,19 +155,8 @@ class SettingsExportFragment : Fragment() { viewModel.saveInstanceState(outState) } - override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - if (requestCode == RESULT_PICK_DOCUMENT) { - val contentUri = data?.data - if (resultCode == Activity.RESULT_OK && contentUri != null) { - viewModel.onDocumentPicked(contentUri) - } else { - viewModel.onDocumentPickCanceled() - } - } - } - companion object { - private const val RESULT_PICK_DOCUMENT = Activity.RESULT_FIRST_USER + private const val SETTINGS_MIME_TYPE = "application/octet-stream" } } diff --git a/legacy/ui/legacy/src/main/java/com/fsck/k9/ui/settings/export/SettingsExportViewModel.kt b/legacy/ui/legacy/src/main/java/com/fsck/k9/ui/settings/export/SettingsExportViewModel.kt index 8a228aeb6d7..cf5fd24312e 100644 --- a/legacy/ui/legacy/src/main/java/com/fsck/k9/ui/settings/export/SettingsExportViewModel.kt +++ b/legacy/ui/legacy/src/main/java/com/fsck/k9/ui/settings/export/SettingsExportViewModel.kt @@ -1,6 +1,5 @@ package com.fsck.k9.ui.settings.export -import android.content.Context import android.net.Uri import android.os.Bundle import androidx.core.os.BundleCompat @@ -22,7 +21,6 @@ private typealias AccountUuid = String private typealias AccountNumber = Int class SettingsExportViewModel( - val context: Context, val accountManager: AccountManager, val settingsExporter: SettingsExporter, ) : ViewModel() { @@ -114,12 +112,12 @@ class SettingsExportViewModel( } fun onShareButtonClicked() { - sendActionEvent(Action.ShareDocument(contentUri!!, SETTINGS_MIME_TYPE)) + sendActionEvent(Action.ShareDocument(contentUri!!)) } private fun startExportSettings() { val exportFileName = settingsExporter.generateDatedExportFileName() - sendActionEvent(Action.PickDocument(exportFileName, SETTINGS_MIME_TYPE)) + sendActionEvent(Action.PickDocument(exportFileName)) } fun onDocumentPicked(contentUri: Uri) { @@ -195,7 +193,6 @@ class SettingsExportViewModel( companion object { private const val MIN_PROGRESS_DURATION = 1000L - private const val SETTINGS_MIME_TYPE = "application/octet-stream" private const val STATE_SETTINGS_LIST_ENABLED = "settingsListEnabled" private const val STATE_EXPORT_BUTTON = "exportButton" @@ -209,8 +206,8 @@ class SettingsExportViewModel( } sealed class Action { - class PickDocument(val fileNameSuggestion: String, val mimeType: String) : Action() - class ShareDocument(val contentUri: Uri, val mimeType: String) : Action() + class PickDocument(val fileNameSuggestion: String) : Action() + class ShareDocument(val contentUri: Uri) : Action() } private data class SavedListItemSelection(