11package com.fsck.k9.ui.messagelist
22
3- import android.app.Activity
43import android.app.SearchManager
54import android.content.Context
65import android.content.Intent
@@ -12,6 +11,7 @@ import android.view.MenuItem
1211import android.view.View
1312import android.view.ViewGroup
1413import android.widget.Toast
14+ import androidx.activity.result.ActivityResultLauncher
1515import androidx.annotation.StringRes
1616import androidx.appcompat.view.ActionMode
1717import androidx.core.os.BundleCompat
@@ -51,6 +51,7 @@ import com.fsck.k9.ui.R
5151import com.fsck.k9.ui.changelog.RecentChangesActivity
5252import com.fsck.k9.ui.changelog.RecentChangesViewModel
5353import com.fsck.k9.ui.choosefolder.ChooseFolderActivity
54+ import com.fsck.k9.ui.choosefolder.ChooseFolderResultContract
5455import com.fsck.k9.ui.helper.RelativeDateTimeFormatter
5556import com.fsck.k9.ui.messagelist.MessageListFragment.MessageListFragmentListener.Companion.MAX_PROGRESS
5657import com.google.android.material.floatingactionbutton.FloatingActionButton
@@ -87,6 +88,19 @@ class MessageListFragment :
8788 private val activityListener = MessageListActivityListener ()
8889 private val actionModeCallback = ActionModeCallback ()
8990
91+ private val chooseFolderForMoveLauncher: ActivityResultLauncher <ChooseFolderResultContract .Input > =
92+ registerForActivityResult(ChooseFolderResultContract (ChooseFolderActivity .Action .MOVE )) { result ->
93+ handleChooseFolderResult(result) { folderId, messages ->
94+ move(messages, folderId)
95+ }
96+ }
97+ private val chooseFolderForCopyLauncher: ActivityResultLauncher <ChooseFolderResultContract .Input > =
98+ registerForActivityResult(ChooseFolderResultContract (ChooseFolderActivity .Action .COPY )) { result ->
99+ handleChooseFolderResult(result) { folderId, messages ->
100+ copy(messages, folderId)
101+ }
102+ }
103+
90104 private lateinit var fragmentListener: MessageListFragmentListener
91105
92106 private lateinit var recentChangesSnackbar: Snackbar
@@ -705,33 +719,6 @@ class MessageListFragment :
705719 }
706720 }
707721
708- override fun onActivityResult (requestCode : Int , resultCode : Int , data : Intent ? ) {
709- if (resultCode != Activity .RESULT_OK ) return
710-
711- when (requestCode) {
712- ACTIVITY_CHOOSE_FOLDER_MOVE ,
713- ACTIVITY_CHOOSE_FOLDER_COPY ,
714- -> {
715- if (data == null ) return
716-
717- val destinationFolderId = data.getLongExtra(ChooseFolderActivity .RESULT_SELECTED_FOLDER_ID , - 1L )
718- val messages = activeMessages!!
719- if (destinationFolderId != - 1L ) {
720- activeMessages = null
721-
722- if (messages.isNotEmpty()) {
723- MlfUtils .setLastSelectedFolder(accountManager, messages, destinationFolderId)
724- }
725-
726- when (requestCode) {
727- ACTIVITY_CHOOSE_FOLDER_MOVE -> move(messages, destinationFolderId)
728- ACTIVITY_CHOOSE_FOLDER_COPY -> copy(messages, destinationFolderId)
729- }
730- }
731- }
732- }
733- }
734-
735722 private fun onExpunge () {
736723 currentFolder?.let { folderInfoHolder ->
737724 messagingController.expunge(account, folderInfoHolder.databaseId)
@@ -1073,7 +1060,6 @@ class MessageListFragment :
10731060
10741061 displayFolderChoice(
10751062 operation = FolderOperation .MOVE ,
1076- requestCode = ACTIVITY_CHOOSE_FOLDER_MOVE ,
10771063 sourceFolderId = folderId,
10781064 accountUuid = messages.first().accountUuid,
10791065 lastSelectedFolderId = null ,
@@ -1096,7 +1082,6 @@ class MessageListFragment :
10961082
10971083 displayFolderChoice(
10981084 operation = FolderOperation .COPY ,
1099- requestCode = ACTIVITY_CHOOSE_FOLDER_COPY ,
11001085 sourceFolderId = folderId,
11011086 accountUuid = messages.first().accountUuid,
11021087 lastSelectedFolderId = null ,
@@ -1106,29 +1091,43 @@ class MessageListFragment :
11061091
11071092 private fun displayFolderChoice (
11081093 operation : FolderOperation ,
1109- requestCode : Int ,
11101094 sourceFolderId : Long? ,
11111095 accountUuid : String ,
11121096 lastSelectedFolderId : Long? ,
11131097 messages : List <MessageReference >,
11141098 ) {
1115- val action = when (operation) {
1116- FolderOperation .COPY -> ChooseFolderActivity .Action .COPY
1117- FolderOperation .MOVE -> ChooseFolderActivity .Action .MOVE
1118- }
1119- val intent = ChooseFolderActivity .buildLaunchIntent(
1120- context = requireContext(),
1121- action = action,
1099+ // Remember the selected messages so they are available in the registerForActivityResult() callbacks
1100+ activeMessages = messages
1101+
1102+ val input = ChooseFolderResultContract .Input (
11221103 accountUuid = accountUuid,
11231104 currentFolderId = sourceFolderId,
11241105 scrollToFolderId = lastSelectedFolderId,
1125- messageReference = null ,
11261106 )
1107+ when (operation) {
1108+ FolderOperation .COPY -> chooseFolderForCopyLauncher.launch(input)
1109+ FolderOperation .MOVE -> chooseFolderForMoveLauncher.launch(input)
1110+ }
1111+ }
11271112
1128- // remember the selected messages for #onActivityResult
1129- activeMessages = messages
1113+ private fun handleChooseFolderResult (
1114+ result : ChooseFolderResultContract .Result ? ,
1115+ action : (Long , List <MessageReference >) -> Unit ,
1116+ ) {
1117+ if (result == null ) return
1118+
1119+ val destinationFolderId = result.folderId
1120+ val messages = activeMessages!!
1121+
1122+ if (destinationFolderId != - 1L ) {
1123+ activeMessages = null
11301124
1131- startActivityForResult(intent, requestCode)
1125+ if (messages.isNotEmpty()) {
1126+ MlfUtils .setLastSelectedFolder(accountManager, messages, destinationFolderId)
1127+ }
1128+
1129+ action(destinationFolderId, messages)
1130+ }
11321131 }
11331132
11341133 private fun onArchive (message : MessageReference ) {
@@ -2100,8 +2099,6 @@ class MessageListFragment :
21002099 }
21012100
21022101 companion object {
2103- private const val ACTIVITY_CHOOSE_FOLDER_MOVE = 1
2104- private const val ACTIVITY_CHOOSE_FOLDER_COPY = 2
21052102
21062103 private const val ARG_SEARCH = " searchObject"
21072104 private const val ARG_THREADED_LIST = " showingThreadedList"
0 commit comments