From 99c74f7bcbde37bd03766b3e6d1ad8b4c870e094 Mon Sep 17 00:00:00 2001 From: Mariotaku Lee Date: Wed, 22 Mar 2017 20:00:29 +0800 Subject: [PATCH] fixed some progress dialog crash --- twidere/build.gradle | 4 +-- .../twidere/activity/BaseActivity.kt | 5 ++-- .../twidere/activity/DataExportActivity.kt | 3 +-- .../twidere/activity/DataImportActivity.kt | 6 ++--- .../activity/PremiumDashboardActivity.kt | 6 +++-- .../twidere/activity/SignInActivity.kt | 6 +++-- .../TrendsLocationSelectorActivity.kt | 8 +++--- .../twidere/activity/iface/IBaseActivity.kt | 27 ++++++++++++++----- .../CreateQuickAccessShortcutActivity.kt | 6 +++-- .../twidere/fragment/BaseFragment.kt | 6 +++-- .../fragment/BasePreferenceFragment.kt | 6 +++-- .../twidere/fragment/UserFragment.kt | 14 +++------- .../filter/BaseFiltersImportFragment.kt | 6 +++-- .../twidere/fragment/iface/IBaseFragment.kt | 26 +++++++++++++----- .../fragment/media/ExoPlayerPageFragment.kt | 4 +-- .../fragment/media/VideoPageFragment.kt | 4 +-- .../twidere/task/twitter/GetTrendsTask.kt | 5 ++-- 17 files changed, 86 insertions(+), 56 deletions(-) diff --git a/twidere/build.gradle b/twidere/build.gradle index 368edb17d..b3bb988fc 100644 --- a/twidere/build.gradle +++ b/twidere/build.gradle @@ -36,8 +36,8 @@ android { applicationId "org.mariotaku.twidere" minSdkVersion project.properties['overrideMinSdkVersion'] ?: 14 targetSdkVersion 25 - versionCode 305 - versionName '3.4.43' + versionCode 306 + versionName '3.4.44' multiDexEnabled true buildConfigField 'boolean', 'LEAK_CANARY_ENABLED', 'Boolean.parseBoolean("true")' diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/BaseActivity.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/BaseActivity.kt index 2b4c8dee1..99be68e0e 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/BaseActivity.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/BaseActivity.kt @@ -41,6 +41,7 @@ import android.view.KeyEvent import android.view.MotionEvent import android.view.View import com.squareup.otto.Bus +import nl.komponents.kovenant.Promise import org.mariotaku.chameleon.Chameleon import org.mariotaku.chameleon.Chameleon.Theme.LightStatusBarMode import org.mariotaku.chameleon.ChameleonActivity @@ -264,8 +265,8 @@ open class BaseActivity : ChameleonActivity(), IBaseActivity, IThe actionHelper.dispatchOnResumeFragments() } - override fun executeAfterFragmentResumed(useHandler: Boolean, action: (BaseActivity) -> Unit) { - actionHelper.executeAfterFragmentResumed(useHandler, action) + override fun executeAfterFragmentResumed(useHandler: Boolean, action: (BaseActivity) -> Unit): Promise { + return actionHelper.executeAfterFragmentResumed(useHandler, action) } override final val currentThemeBackgroundAlpha by lazy { diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/DataExportActivity.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/DataExportActivity.kt index 878ae2df4..bed276b84 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/DataExportActivity.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/DataExportActivity.kt @@ -121,8 +121,7 @@ class DataExportActivity : BaseActivity(), DataExportImportTypeSelectorDialogFra override fun onPreExecute() { activity.executeAfterFragmentResumed { - val activity = it as DataExportActivity - ProgressDialogFragment.show(activity.supportFragmentManager, FRAGMENT_TAG).isCancelable = false + ProgressDialogFragment.show(it.supportFragmentManager, FRAGMENT_TAG).isCancelable = false } } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/DataImportActivity.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/DataImportActivity.kt index 7f31cb1c6..0ac05fdbb 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/DataImportActivity.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/DataImportActivity.kt @@ -123,8 +123,7 @@ class DataImportActivity : BaseActivity(), DataExportImportTypeSelectorDialogFra override fun onPreExecute() { activity.executeAfterFragmentResumed { - val activity = it as DataImportActivity - ProgressDialogFragment.show(activity.supportFragmentManager, FRAGMENT_TAG).isCancelable = false + ProgressDialogFragment.show(it.supportFragmentManager, FRAGMENT_TAG).isCancelable = false } } @@ -170,8 +169,7 @@ class DataImportActivity : BaseActivity(), DataExportImportTypeSelectorDialogFra override fun onPreExecute() { activity.executeAfterFragmentResumed { - val activity = it as DataImportActivity - ProgressDialogFragment.show(activity.supportFragmentManager, FRAGMENT_TAG).isCancelable = false + ProgressDialogFragment.show(it.supportFragmentManager, FRAGMENT_TAG).isCancelable = false } } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/PremiumDashboardActivity.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/PremiumDashboardActivity.kt index faa5b4d42..1a4b2241e 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/PremiumDashboardActivity.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/PremiumDashboardActivity.kt @@ -16,6 +16,7 @@ import com.bumptech.glide.Glide import com.bumptech.glide.RequestManager import kotlinx.android.synthetic.main.activity_premium_dashboard.* import kotlinx.android.synthetic.main.card_item_extra_feature.view.* +import nl.komponents.kovenant.combine.and import nl.komponents.kovenant.task import nl.komponents.kovenant.ui.alwaysUi import nl.komponents.kovenant.ui.failUi @@ -110,10 +111,11 @@ class PremiumDashboardActivity : BaseActivity() { if (!BuildConfig.DEBUG) { return true } - ProgressDialogFragment.show(supportFragmentManager, "consume_purchase_progress") val weakThis = WeakReference(this) val recreate = AtomicBoolean() - task { + executeAfterFragmentResumed { + ProgressDialogFragment.show(it.supportFragmentManager, "consume_purchase_progress") + } and task { val activity = weakThis.get() ?: throw IllegalStateException() if (!activity.extraFeaturesService.destroyPurchase()) { throw IllegalStateException() diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/SignInActivity.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/SignInActivity.kt index 9d3e2a25a..ec6b6aba4 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/SignInActivity.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/SignInActivity.kt @@ -51,6 +51,7 @@ import android.widget.TextView import android.widget.Toast import com.bluelinelabs.logansquare.LoganSquare import kotlinx.android.synthetic.main.activity_sign_in.* +import nl.komponents.kovenant.combine.and import nl.komponents.kovenant.task import nl.komponents.kovenant.ui.alwaysUi import nl.komponents.kovenant.ui.successUi @@ -410,8 +411,9 @@ class SignInActivity : BaseActivity(), OnClickListener, TextWatcher, APIEditorDi private fun updateDefaultFeatures() { val weakThis = WeakReference(this) - ProgressDialogFragment.show(supportFragmentManager, FRAGMENT_TAG_LOADING_DEFAULT_FEATURES) - task { + executeAfterFragmentResumed { + ProgressDialogFragment.show(it.supportFragmentManager, FRAGMENT_TAG_LOADING_DEFAULT_FEATURES) + } and task { val activity = weakThis.get() ?: return@task if (activity.isFinishing) return@task activity.defaultFeatures.loadRemoteSettings(activity.restHttpClient) diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/TrendsLocationSelectorActivity.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/TrendsLocationSelectorActivity.kt index 335cac51d..499f235e6 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/TrendsLocationSelectorActivity.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/TrendsLocationSelectorActivity.kt @@ -17,6 +17,7 @@ import android.view.ViewGroup import android.widget.BaseExpandableListAdapter import android.widget.ExpandableListView import android.widget.TextView +import nl.komponents.kovenant.combine.and import nl.komponents.kovenant.task import nl.komponents.kovenant.ui.alwaysUi import nl.komponents.kovenant.ui.failUi @@ -54,10 +55,9 @@ class TrendsLocationSelectorActivity : BaseActivity() { if (savedInstanceState != null) return val weakThis = WeakReference(this) - ProgressDialogFragment.show(supportFragmentManager, PROGRESS_FRAGMENT_TAG).apply { - isCancelable = false - } - task { + executeAfterFragmentResumed { + ProgressDialogFragment.show(it.supportFragmentManager, PROGRESS_FRAGMENT_TAG).isCancelable = false + } and task { val activity = weakThis.get() ?: throw InterruptedException() val twitter = MicroBlogAPIFactory.getInstance(activity, accountKey) ?: throw MicroBlogException("No account") diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/iface/IBaseActivity.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/iface/IBaseActivity.kt index 6ab187ef7..fe659ad34 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/iface/IBaseActivity.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/iface/IBaseActivity.kt @@ -22,6 +22,8 @@ package org.mariotaku.twidere.activity.iface import android.os.Handler import android.os.Looper import android.support.v4.app.FragmentActivity +import nl.komponents.kovenant.Promise +import nl.komponents.kovenant.deferred import java.util.* /** @@ -29,7 +31,7 @@ import java.util.* */ interface IBaseActivity { - fun executeAfterFragmentResumed(useHandler: Boolean = false, action: (A) -> Unit) + fun executeAfterFragmentResumed(useHandler: Boolean = false, action: (A) -> Unit): Promise class ActionHelper(private val activity: A) { @@ -54,20 +56,33 @@ interface IBaseActivity { val cur = actionQueue.poll() cur?.let { cur -> if (cur.useHandler) { - handler.post { cur.action(activity) } + handler.post { cur.invoke(activity) } } else { - cur.action(activity) + cur.invoke(activity) } } info = cur } while (info != null) } - fun executeAfterFragmentResumed(useHandler: Boolean = false, action: (A) -> Unit) { - actionQueue.add(ExecuteInfo(action, useHandler)) + fun executeAfterFragmentResumed(useHandler: Boolean = false, action: (A) -> Unit) + : Promise { + val info = ExecuteInfo(action, useHandler) + actionQueue.add(info) executePending() + return info.promise } - private data class ExecuteInfo(val action: (A) -> Unit, val useHandler: Boolean) + private data class ExecuteInfo(private val action: (A) -> Unit, val useHandler: Boolean) { + + private val deferredInstance = deferred() + + val promise: Promise get() = deferredInstance.promise + + fun invoke(activity: A) { + action(activity) + deferredInstance.resolve(Unit) + } + } } } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/shortcut/CreateQuickAccessShortcutActivity.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/shortcut/CreateQuickAccessShortcutActivity.kt index f50f25d9a..2f8db9a1f 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/shortcut/CreateQuickAccessShortcutActivity.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/shortcut/CreateQuickAccessShortcutActivity.kt @@ -28,6 +28,7 @@ import android.graphics.Canvas import android.os.Bundle import android.support.v7.app.AlertDialog import com.bumptech.glide.Glide +import nl.komponents.kovenant.combine.and import nl.komponents.kovenant.ui.failUi import nl.komponents.kovenant.ui.successUi import org.mariotaku.kpreferences.get @@ -161,8 +162,9 @@ class CreateQuickAccessShortcutActivity : BaseActivity() { shapeStyle = preferences[profileImageStyleKey], cornerRadiusRatio = 0.1f, size = getString(R.string.profile_image_size)).into(DeferredTarget()) val weakThis = WeakReference(this) - ProgressDialogFragment.show(supportFragmentManager, TAG_LOAD_ICON_PROGRESS) - deferred.promise.successUi { drawable -> + executeAfterFragmentResumed { + ProgressDialogFragment.show(it.supportFragmentManager, TAG_LOAD_ICON_PROGRESS) + } and deferred.promise.successUi { drawable -> val activity = weakThis.get() ?: return@successUi val launchIntent = IntentUtils.userProfile(accountKey, user.key, user.screen_name, profileUrl = user.extras?.statusnet_profile_url) diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/BaseFragment.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/BaseFragment.kt index c4cee9ccd..26ac9fc32 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/BaseFragment.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/BaseFragment.kt @@ -25,6 +25,7 @@ import android.support.v4.app.Fragment import android.support.v4.text.BidiFormatter import com.squareup.otto.Bus import com.twitter.Validator +import nl.komponents.kovenant.Promise import org.mariotaku.twidere.fragment.iface.IBaseFragment import org.mariotaku.twidere.model.DefaultFeatures import org.mariotaku.twidere.util.* @@ -76,8 +77,9 @@ open class BaseFragment : Fragment(), IBaseFragment { GeneralComponentHelper.build(context!!).inject(this) } - override fun executeAfterFragmentResumed(useHandler: Boolean, action: (BaseFragment) -> Unit) { - actionHelper.executeAfterFragmentResumed(useHandler, action) + override fun executeAfterFragmentResumed(useHandler: Boolean, action: (BaseFragment) -> Unit) + : Promise { + return actionHelper.executeAfterFragmentResumed(useHandler, action) } override fun onResume() { diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/BasePreferenceFragment.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/BasePreferenceFragment.kt index 135bf0471..9721e3fab 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/BasePreferenceFragment.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/BasePreferenceFragment.kt @@ -30,6 +30,7 @@ import android.provider.Settings import android.support.v7.preference.Preference import android.support.v7.preference.PreferenceFragmentCompat import com.squareup.otto.Bus +import nl.komponents.kovenant.Promise import org.mariotaku.kpreferences.KPreferences import org.mariotaku.twidere.fragment.iface.IBaseFragment import org.mariotaku.twidere.preference.RingtonePreference @@ -119,8 +120,9 @@ abstract class BasePreferenceFragment : PreferenceFragmentCompat(), IBaseFragmen return super.onPreferenceTreeClick(preference) } - override fun executeAfterFragmentResumed(useHandler: Boolean, action: (BasePreferenceFragment) -> Unit) { - actionHelper.executeAfterFragmentResumed(useHandler, action) + override fun executeAfterFragmentResumed(useHandler: Boolean, action: (BasePreferenceFragment) -> Unit) + : Promise { + return actionHelper.executeAfterFragmentResumed(useHandler, action) } override fun fitSystemWindows(insets: Rect) { diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/UserFragment.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/UserFragment.kt index 88e8ffaea..65a165e32 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/UserFragment.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/UserFragment.kt @@ -78,7 +78,6 @@ import nl.komponents.kovenant.task import nl.komponents.kovenant.then import nl.komponents.kovenant.ui.alwaysUi import nl.komponents.kovenant.ui.failUi -import nl.komponents.kovenant.ui.promiseOnUi import nl.komponents.kovenant.ui.successUi import org.mariotaku.chameleon.Chameleon import org.mariotaku.chameleon.ChameleonUtils @@ -945,10 +944,8 @@ class UserFragment : BaseFragment(), OnClickListener, OnLinkClickListener, SetUserNicknameDialogFragment.show(fragmentManager, user.key, nick) } R.id.add_to_list -> { - promiseOnUi { - executeAfterFragmentResumed { - ProgressDialogFragment.show(fragmentManager, "get_list_progress") - } + executeAfterFragmentResumed { + ProgressDialogFragment.show(it.fragmentManager, "get_list_progress") }.then { fun MicroBlog.getUserListOwnerMemberships(id: String): ArrayList { val result = ArrayList() @@ -1671,11 +1668,8 @@ class UserFragment : BaseFragment(), OnClickListener, OnLinkClickListener, dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener { val checkedPositions = dialog.listView.checkedItemPositions val weakActivity = WeakReference(activity) - promiseOnUi { - val activity = weakActivity.get() as? IBaseActivity<*> ?: return@promiseOnUi - activity.executeAfterFragmentResumed { activity -> - ProgressDialogFragment.show(activity.supportFragmentManager, "update_lists_progress") - } + (activity as IBaseActivity<*>).executeAfterFragmentResumed { + ProgressDialogFragment.show(it.supportFragmentManager, "update_lists_progress") }.then { val activity = weakActivity.get() ?: throw IllegalStateException() val twitter = MicroBlogAPIFactory.getInstance(activity, accountKey) diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/filter/BaseFiltersImportFragment.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/filter/BaseFiltersImportFragment.kt index 43a93bb31..672742f9b 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/filter/BaseFiltersImportFragment.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/filter/BaseFiltersImportFragment.kt @@ -15,6 +15,7 @@ import android.widget.CheckBox import android.widget.TextView import android.widget.Toast import com.bumptech.glide.Glide +import nl.komponents.kovenant.combine.and import nl.komponents.kovenant.task import nl.komponents.kovenant.ui.alwaysUi import org.mariotaku.ktextension.* @@ -194,9 +195,10 @@ abstract class BaseFiltersImportFragment : AbsContentListRecyclerViewFragment { fun getSystemWindowsInsets(insets: Rect): Boolean } - fun executeAfterFragmentResumed(useHandler: Boolean = false, action: (F) -> Unit) + fun executeAfterFragmentResumed(useHandler: Boolean = false, action: (F) -> Unit): Promise class ActionHelper(private val fragment: F) { @@ -89,20 +91,32 @@ interface IBaseFragment { val cur = actionQueue.poll() cur?.let { cur -> if (cur.useHandler) { - handler.post { cur.action(fragment) } + handler.post { cur.invoke(fragment) } } else { - cur.action(fragment) + cur.invoke(fragment) } } info = cur } while (info != null) } - fun executeAfterFragmentResumed(useHandler: Boolean = false, action: (F) -> Unit) { - actionQueue.add(ExecuteInfo(action, useHandler)) + fun executeAfterFragmentResumed(useHandler: Boolean = false, action: (F) -> Unit) + : Promise { + val info = ExecuteInfo(action, useHandler) + actionQueue.add(info) executePending() + return info.promise } - private data class ExecuteInfo(val action: (F) -> Unit, val useHandler: Boolean) + private data class ExecuteInfo(private val action: (F) -> Unit, val useHandler: Boolean) { + private val deferredInstance = deferred() + + val promise: Promise get() = deferredInstance.promise + + fun invoke(fragment: F) { + action(fragment) + deferredInstance.resolve(Unit) + } + } } } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/media/ExoPlayerPageFragment.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/media/ExoPlayerPageFragment.kt index 8c9945e99..1ac109025 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/media/ExoPlayerPageFragment.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/media/ExoPlayerPageFragment.kt @@ -242,9 +242,7 @@ class ExoPlayerPageFragment : MediaViewerFragment(), IBaseFragment Unit) { - // No-op - } + override fun executeAfterFragmentResumed(useHandler: Boolean, action: (ExoPlayerPageFragment) -> Unit) = TODO() override fun isMediaLoaded(): Boolean { return !playerHasError diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/media/VideoPageFragment.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/media/VideoPageFragment.kt index b3cbd716e..fc70173e5 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/media/VideoPageFragment.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/media/VideoPageFragment.kt @@ -337,9 +337,7 @@ class VideoPageFragment : CacheDownloadMediaViewerFragment(), IBaseFragment Unit) { - // No-op - } + override fun executeAfterFragmentResumed(useHandler: Boolean, action: (VideoPageFragment) -> Unit) = TODO() private fun updatePlayerState() { diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/task/twitter/GetTrendsTask.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/task/twitter/GetTrendsTask.kt index 1012ceab6..ae9601d4f 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/task/twitter/GetTrendsTask.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/task/twitter/GetTrendsTask.kt @@ -47,6 +47,7 @@ import org.mariotaku.twidere.util.content.ContentResolverUtils.bulkInsert import java.util.* /** + * Get local trends * Created by mariotaku on 16/2/24. */ class GetTrendsTask( @@ -59,8 +60,8 @@ class GetTrendsTask( val details = getAccountDetails(AccountManager.get(context), accountKey, true) ?: return val twitter = details.newMicroBlogInstance(context, cls = MicroBlog::class.java) try { - val trends = when { - details.type == FANFOU -> twitter.fanfouTrends + val trends = when (details.type) { + FANFOU -> twitter.fanfouTrends else -> twitter.getLocationTrends(woeId).firstOrNull() } ?: return storeTrends(context.contentResolver, CachedTrends.Local.CONTENT_URI, trends)