Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 64cf6069 authored by cketti's avatar cketti Committed by GitHub
Browse files

Merge pull request #8749 from marcRDZ/task-7758_choose-folder-result

Switch Unread Widget choose folder setting to Activity Result API
parents 2da1cabe aeb3b17f
Loading
Loading
Loading
Loading
+14 −20
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@ import androidx.preference.Preference
import app.k9mail.legacy.search.SearchAccount
import com.fsck.k9.Preferences
import com.fsck.k9.ui.choosefolder.ChooseFolderActivity
import com.fsck.k9.ui.choosefolder.ChooseFolderResultContract
import com.takisoft.preferencex.PreferenceFragmentCompat
import org.koin.android.ext.android.inject

@@ -28,6 +29,15 @@ class UnreadWidgetConfigurationFragment : PreferenceFragmentCompat() {
        registerForActivityResult(UnreadWidgetChooseAccountResultContract()) { accountUuid ->
            handleChooseAccount(accountUuid)
        }
    private val chooseFolderResultLauncher: ActivityResultLauncher<ChooseFolderResultContract.Input> =
        registerForActivityResult(ChooseFolderResultContract(action = ChooseFolderActivity.Action.CHOOSE)) { result ->
            if (result != null) {
                handleChooseFolder(
                    folderId = result.folderId,
                    folderDisplayName = result.folderDisplayName,
                )
            }
        }

    private var appWidgetId: Int = AppWidgetManager.INVALID_APPWIDGET_ID
    private lateinit var unreadAccount: Preference
@@ -60,12 +70,11 @@ class UnreadWidgetConfigurationFragment : PreferenceFragmentCompat() {

        unreadFolder = findPreference(PREFERENCE_UNREAD_FOLDER)!!
        unreadFolder.onPreferenceClickListener = Preference.OnPreferenceClickListener {
            val intent = ChooseFolderActivity.buildLaunchIntent(
                context = requireContext(),
                action = ChooseFolderActivity.Action.CHOOSE,
            chooseFolderResultLauncher.launch(
                input = ChooseFolderResultContract.Input(
                    accountUuid = selectedAccountUuid!!,
                ),
            )
            startActivityForResult(intent, REQUEST_CHOOSE_FOLDER)
            false
        }

@@ -93,19 +102,6 @@ class UnreadWidgetConfigurationFragment : PreferenceFragmentCompat() {
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (resultCode == Activity.RESULT_OK && data != null) {
            when (requestCode) {
                REQUEST_CHOOSE_FOLDER -> {
                    val folderId = data.getLongExtra(ChooseFolderActivity.RESULT_SELECTED_FOLDER_ID, -1L)
                    val folderDisplayName = data.getStringExtra(ChooseFolderActivity.RESULT_FOLDER_DISPLAY_NAME)!!
                    handleChooseFolder(folderId, folderDisplayName)
                }
            }
        }
        super.onActivityResult(requestCode, resultCode, data)
    }

    private fun handleChooseAccount(accountUuid: String?) {
        val userSelectedSameAccount = accountUuid == selectedAccountUuid
        if (userSelectedSameAccount) {
@@ -210,8 +206,6 @@ class UnreadWidgetConfigurationFragment : PreferenceFragmentCompat() {
        private const val PREFERENCE_UNREAD_FOLDER_ENABLED = "unread_folder_enabled"
        private const val PREFERENCE_UNREAD_FOLDER = "unread_folder"

        private const val REQUEST_CHOOSE_FOLDER = 1

        private const val STATE_SELECTED_ACCOUNT_UUID = "com.fsck.k9.widget.unread.selectedAccountUuid"
        private const val STATE_SELECTED_FOLDER_ID = "com.fsck.k9.widget.unread.selectedFolderId"
        private const val STATE_SELECTED_FOLDER_DISPLAY_NAME = "com.fsck.k9.widget.unread.selectedFolderDisplayName"
+47 −0
Original line number Diff line number Diff line
package com.fsck.k9.ui.choosefolder

import android.app.Activity
import android.content.Context
import android.content.Intent
import androidx.activity.result.contract.ActivityResultContract
import app.k9mail.legacy.message.controller.MessageReference
import com.fsck.k9.ui.choosefolder.ChooseFolderActivity.Action

class ChooseFolderResultContract(
    private val action: Action,
) : ActivityResultContract<ChooseFolderResultContract.Input, ChooseFolderResultContract.Result?>() {

    override fun createIntent(context: Context, input: Input): Intent {
        return ChooseFolderActivity.buildLaunchIntent(
            context = context,
            action = action,
            accountUuid = input.accountUuid,
            currentFolderId = input.currentFolderId,
            scrollToFolderId = input.scrollToFolderId,
            messageReference = input.messageReference,
        )
    }

    override fun parseResult(resultCode: Int, intent: Intent?): Result? {
        return intent.takeIf { resultCode == Activity.RESULT_OK }?.let {
            Result(
                folderId = it.getLongExtra(ChooseFolderActivity.RESULT_SELECTED_FOLDER_ID, -1L),
                folderDisplayName = it.getStringExtra(ChooseFolderActivity.RESULT_FOLDER_DISPLAY_NAME)!!,
                messageReference = it.getStringExtra(ChooseFolderActivity.RESULT_MESSAGE_REFERENCE),
            )
        }
    }

    data class Input(
        val accountUuid: String,
        val currentFolderId: Long? = null,
        val scrollToFolderId: Long? = null,
        val messageReference: MessageReference? = null,
    )

    data class Result(
        val folderId: Long,
        val folderDisplayName: String,
        val messageReference: String?,
    )
}