From a0528496eb94a1c6cbe01e9b778ad11f7bb4c729 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Thu, 9 Apr 2020 23:46:42 +0200 Subject: [PATCH] Fix crash caused by updating view in not attached fragment (#763) --- .../ui/modules/settings/SettingsFragment.kt | 2 ++ .../ui/modules/settings/SettingsPresenter.kt | 25 ++++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/settings/SettingsFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/settings/SettingsFragment.kt index 5a8593a0..fe0a9763 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/settings/SettingsFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/settings/SettingsFragment.kt @@ -82,6 +82,8 @@ class SettingsFragment : PreferenceFragmentCompat(), } override fun setSyncInProgress(inProgress: Boolean) { + if (activity == null || !isAdded) return + findPreference(getString(R.string.pref_key_services_force_sync))?.run { isEnabled = !inProgress summary = if (inProgress) getString(R.string.pref_services_sync_in_progress) else "" diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/settings/SettingsPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/settings/SettingsPresenter.kt index ce5100ea..c8545ac0 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/settings/SettingsPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/settings/SettingsPresenter.kt @@ -58,16 +58,29 @@ class SettingsPresenter @Inject constructor( fun onForceSyncDialogSubmit() { view?.run { - Timber.i("Setting sync now started") - analytics.logEvent("sync_now_started") + val successString = syncSuccessString + val failedString = syncFailedString disposable.add(syncManager.startOneTimeSyncWorker() - .doOnSubscribe { setSyncInProgress(true) } + .doOnSubscribe { + setSyncInProgress(true) + Timber.i("Setting sync now started") + analytics.logEvent("sync_now", "status" to "started") + } .doFinally { setSyncInProgress(false) } .subscribe({ workInfo -> - if (workInfo.state == WorkInfo.State.SUCCEEDED) showMessage(syncSuccessString) - else if (workInfo.state == WorkInfo.State.FAILED) showError(syncFailedString, Throwable(workInfo.outputData.getString("error"))) + when (workInfo.state) { + WorkInfo.State.SUCCEEDED -> { + showMessage(successString) + analytics.logEvent("sync_now", "status" to "success") + } + WorkInfo.State.FAILED -> { + showError(failedString, Throwable(workInfo.outputData.getString("error"))) + analytics.logEvent("sync_now", "status" to "failed") + } + else -> Timber.d("Sync now state: ${workInfo.state}") + } }, { - Timber.e("Sync now failed") + Timber.e(it, "Sync now failed") }) ) }