Communicate SettingsImportFragment result via fragment result mechanism

This commit is contained in:
cketti 2024-01-23 11:49:32 +01:00
parent 3fdd1b7963
commit 12189f7726
4 changed files with 22 additions and 34 deletions

View file

@ -3,17 +3,11 @@ package com.fsck.k9.activity
import android.content.Intent
import android.os.Bundle
import app.k9mail.feature.settings.import.ui.SettingsImportFragment
import app.k9mail.feature.settings.import.ui.SettingsImportResultViewModel
import app.k9mail.feature.settings.import.ui.SettingsImportSuccess
import com.fsck.k9.ui.R
import com.fsck.k9.ui.base.K9Activity
import org.koin.androidx.viewmodel.ext.android.viewModel
import app.k9mail.feature.settings.importing.R as SettingsImportR
class FragmentLauncherActivity : K9Activity() {
private val settingsImportResultViewModel: SettingsImportResultViewModel by viewModel()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -31,11 +25,14 @@ class FragmentLauncherActivity : K9Activity() {
.replace(R.id.fragment_launcher_container, SettingsImportFragment())
.commit()
settingsImportResultViewModel.settingsImportResult.observe(this) {
if (it == SettingsImportSuccess) {
supportFragmentManager.setFragmentResultListener(
SettingsImportFragment.FRAGMENT_RESULT_KEY,
this,
) { _, result: Bundle ->
if (result.getBoolean(SettingsImportFragment.FRAGMENT_RESULT_ACCOUNT_IMPORTED, false)) {
launchMessageList()
finish()
}
finish()
}
}

View file

@ -1,7 +1,6 @@
package app.k9mail.feature.settings.import
import app.k9mail.feature.settings.import.ui.AuthViewModel
import app.k9mail.feature.settings.import.ui.SettingsImportResultViewModel
import app.k9mail.feature.settings.import.ui.SettingsImportViewModel
import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.dsl.module
@ -9,8 +8,6 @@ import org.koin.dsl.module
val featureSettingsImportModule = module {
viewModel { SettingsImportViewModel(context = get(), accountActivator = get()) }
viewModel { SettingsImportResultViewModel() }
viewModel {
AuthViewModel(
application = get(),

View file

@ -8,23 +8,23 @@ import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.core.os.bundleOf
import androidx.core.view.isGone
import androidx.core.view.isInvisible
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.fragment.app.setFragmentResult
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.RecyclerView
import app.k9mail.feature.settings.importing.R
import com.fsck.k9.ui.base.livedata.observeNotNull
import com.mikepenz.fastadapter.FastAdapter
import com.mikepenz.fastadapter.adapters.ItemAdapter
import org.koin.androidx.viewmodel.ext.android.activityViewModel
import org.koin.androidx.viewmodel.ext.android.viewModel
import com.fsck.k9.ui.base.R as BaseR
class SettingsImportFragment : Fragment() {
private val viewModel: SettingsImportViewModel by viewModel()
private val resultViewModel: SettingsImportResultViewModel by activityViewModel()
private lateinit var settingsImportAdapter: FastAdapter<ImportListItem<*>>
private lateinit var itemAdapter: ItemAdapter<ImportListItem<*>>
@ -155,9 +155,8 @@ class SettingsImportFragment : Fragment() {
@Suppress("SwallowedException")
private fun closeImportScreen(action: Action.Close) {
if (action.importSuccess) {
resultViewModel.setSettingsImportResult()
}
setFragmentResult(action.importSuccess)
try {
findNavController().popBackStack()
} catch (e: IllegalStateException) {
@ -232,10 +231,22 @@ class SettingsImportFragment : Fragment() {
}
}
private fun setFragmentResult(accountImported: Boolean) {
setFragmentResult(
requestKey = FRAGMENT_RESULT_KEY,
result = bundleOf(
FRAGMENT_RESULT_ACCOUNT_IMPORTED to accountImported,
),
)
}
companion object {
private const val REQUEST_PICK_DOCUMENT = Activity.RESULT_FIRST_USER
private const val REQUEST_PASSWORD_PROMPT = Activity.RESULT_FIRST_USER + 1
private const val REQUEST_AUTHORIZATION = Activity.RESULT_FIRST_USER + 2
const val FRAGMENT_RESULT_KEY = "settings_import"
const val FRAGMENT_RESULT_ACCOUNT_IMPORTED = "accountImported"
}
}

View file

@ -1,17 +0,0 @@
package app.k9mail.feature.settings.import.ui
import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import com.fsck.k9.helper.SingleLiveEvent
class SettingsImportResultViewModel : ViewModel() {
private val importResult = SingleLiveEvent<SettingsImportSuccess>()
val settingsImportResult: LiveData<SettingsImportSuccess> = importResult
fun setSettingsImportResult() {
importResult.value = SettingsImportSuccess
}
}
object SettingsImportSuccess