@@ -20,6 +20,7 @@ import android.view.ViewGroup
2020import android.view.inputmethod.InputMethodManager
2121import android.widget.Toast
2222import androidx.activity.result.ActivityResultLauncher
23+ import androidx.activity.result.contract.ActivityResultContracts
2324import androidx.compose.runtime.collectAsState
2425import androidx.compose.runtime.getValue
2526import androidx.compose.ui.platform.ComposeView
@@ -119,6 +120,10 @@ class MessageViewFragment :
119120 registerForActivityResult(CreateDocumentResultContract ()) { documentUri ->
120121 onCreateDocumentResult(documentUri)
121122 }
123+ private val openDocumentTreeLauncher: ActivityResultLauncher <Uri ?> =
124+ registerForActivityResult(ActivityResultContracts .OpenDocumentTree ()) { directoryUri ->
125+ onOpenDocumentTreeResult(directoryUri)
126+ }
122127 private val chooseFolderForCopyLauncher: ActivityResultLauncher <ChooseFolderResultContract .Input > =
123128 registerForActivityResult(ChooseFolderResultContract (ChooseFolderActivity .Action .COPY )) { result ->
124129 onChooseFolderCopyResult(result)
@@ -245,11 +250,7 @@ class MessageViewFragment :
245250 onSaveClick = { attachment ->
246251 onSaveAttachment(attachment)
247252 },
248- onSaveAllClick = {
249- attachments.forEach { item ->
250- onSaveAttachment(item.attachment)
251- }
252- },
253+ onSaveAllClick = { onSaveAllAttachments() },
253254 )
254255 }
255256 }
@@ -828,6 +829,18 @@ class MessageViewFragment :
828829 }
829830 }
830831
832+ private fun onOpenDocumentTreeResult (directoryUri : Uri ? ) {
833+ if (directoryUri == null ) return
834+
835+ val messageView = mMessageViewInfo ? : return
836+ val attachments = messageView.attachments.filter { ! it.inlineAttachment }
837+ attachments.forEach {
838+ currentAttachmentViewInfo = it
839+ createAttachmentController(it)
840+ .saveAttachmentToDirectory(lifecycleScope ,directoryUri)
841+ }
842+ }
843+
831844 private fun onChooseFolderMoveResult (result : ChooseFolderResultContract .Result ? ) {
832845 if (result == null ) return
833846
@@ -1190,6 +1203,14 @@ class MessageViewFragment :
11901203 createAttachmentController(attachment).viewAttachment(lifecycleScope)
11911204 }
11921205
1206+ fun onSaveAllAttachments () {
1207+ try {
1208+ openDocumentTreeLauncher.launch(null )
1209+ } catch (_: ActivityNotFoundException ) {
1210+ Toast .makeText(requireContext(), R .string.error_activity_not_found, Toast .LENGTH_LONG ).show()
1211+ }
1212+ }
1213+
11931214 override fun onSaveAttachment (attachment : AttachmentViewInfo ) {
11941215 currentAttachmentViewInfo = attachment
11951216
0 commit comments