Skip to content

Commit

Permalink
Migrate create document result to new API on settings export
Browse files Browse the repository at this point in the history
  • Loading branch information
marcRDZ committed Jan 13, 2025
1 parent 59e9e6c commit e5b0a81
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ val settingsUiModule = module {

viewModel {
SettingsExportViewModel(
context = get(),
accountManager = get(),
settingsExporter = get(),
)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -23,6 +23,16 @@ class SettingsExportFragment : Fragment() {
private lateinit var settingsExportAdapter: FastAdapter<CheckBoxItem<*>>
private lateinit var itemAdapter: ItemAdapter<CheckBoxItem<*>>

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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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"
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -22,7 +21,6 @@ private typealias AccountUuid = String
private typealias AccountNumber = Int

class SettingsExportViewModel(
val context: Context,
val accountManager: AccountManager,
val settingsExporter: SettingsExporter,
) : ViewModel() {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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"
Expand All @@ -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(
Expand Down

0 comments on commit e5b0a81

Please sign in to comment.