Repository: thunderbird/thunderbird-android
File: feature/widget/unread/src/main/kotlin/app/k9mail/feature/widget/unread/UnreadWidgetConfigurationFragment.kt (Kotlin)

Code before:
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.core.os.bundleOf
import androidx.preference.CheckBoxPreference
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

@Suppress("TooManyFunctions")
class UnreadWidgetConfigurationFragment : PreferenceFragmentCompat() {
    private val preferences: Preferences by inject()
    private val repository: UnreadWidgetRepository by inject()
    private val unreadWidgetUpdater: UnreadWidgetUpdater by inject()

    private val chooseAccountResultLauncher: ActivityResultLauncher<Unit> =
        registerForActivityResult(UnreadWidgetChooseAccountResultContract()) { accountUuid ->
            handleChooseAccount(accountUuid)
        }
    private val chooseFolderResultLauncher: ActivityResultLauncher<ChooseFolderResultContract.Input> =
        registerForActivityResult(ChooseFolderResultContract()) { result ->
            handleChooseFolder(
                folderId = result!!.folderId,
                folderDisplayName = result.folderDisplayName,
            )
        }

    private var appWidgetId: Int = AppWidgetManager.INVALID_APPWIDGET_ID
    private lateinit var unreadAccount: Preference
    private lateinit var unreadFolderEnabled: CheckBoxPreference
    private lateinit var unreadFolder: Preference

    private var selectedAccountUuid: String? = null
    private var selectedFolderId: Long? = null
    private var selectedFolderDisplayName: String? = null

    override fun onCreatePreferencesFix(savedInstanceState: Bundle?, rootKey: String?) {
        setHasOptionsMenu(true)
        setPreferencesFromResource(R.xml.unread_widget_configuration, rootKey)

        appWidgetId = arguments?.getInt(ARGUMENT_APP_WIDGET_ID) ?: error("Missing argument '$ARGUMENT_APP_WIDGET_ID'")

        unreadAccount = findPreference(PREFERENCE_UNREAD_ACCOUNT)!!
        unreadAccount.onPreferenceClickListener = Preference.OnPreferenceClickListener {
            chooseAccountResultLauncher.launch(Unit)
            false
        }


Review comment (bug):
Using `result!!` will crash the app when the user cancels the folder selection. Wrap the `handleChooseFolder()` call in a `if (result != null) …` check instead.

Code after:
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.core.os.bundleOf
import androidx.preference.CheckBoxPreference
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

@Suppress("TooManyFunctions")
class UnreadWidgetConfigurationFragment : PreferenceFragmentCompat() {
    private val preferences: Preferences by inject()
    private val repository: UnreadWidgetRepository by inject()
    private val unreadWidgetUpdater: UnreadWidgetUpdater by inject()

    private val chooseAccountResultLauncher: ActivityResultLauncher<Unit> =
        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
    private lateinit var unreadFolderEnabled: CheckBoxPreference
    private lateinit var unreadFolder: Preference

    private var selectedAccountUuid: String? = null
    private var selectedFolderId: Long? = null
    private var selectedFolderDisplayName: String? = null

    override fun onCreatePreferencesFix(savedInstanceState: Bundle?, rootKey: String?) {
        setHasOptionsMenu(true)
        setPreferencesFromResource(R.xml.unread_widget_configuration, rootKey)

        appWidgetId = arguments?.getInt(ARGUMENT_APP_WIDGET_ID) ?: error("Missing argument '$ARGUMENT_APP_WIDGET_ID'")

        unreadAccount = findPreference(PREFERENCE_UNREAD_ACCOUNT)!!
        unreadAccount.onPreferenceClickListener = Preference.OnPreferenceClickListener {
            chooseAccountResultLauncher.launch(Unit)
            false