From cdf819fa0d34435f54f73fee6d217f05716f402d Mon Sep 17 00:00:00 2001 From: Mariotaku Lee Date: Mon, 24 Apr 2017 16:56:51 +0800 Subject: [PATCH] fixed a possible memleak in login activity hides dm for mastodon --- .../twidere/activity/SignInActivity.kt | 17 ++------------- .../fragment/AccountsDashboardFragment.kt | 7 +++++-- .../util/MastodonApplicationRegistry.kt | 21 +++++++++++++++++-- 3 files changed, 26 insertions(+), 19 deletions(-) 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 13a6626a8..680d48ad2 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/SignInActivity.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/SignInActivity.kt @@ -354,15 +354,7 @@ class SignInActivity : BaseActivity(), OnClickListener, TextWatcher, } and task { val activity = weakThis.get() ?: throw InterruptedException() val registry = activity.mastodonApplicationRegistry - return@task Pair(host, registry[host] ?: run { - val endpoint = Endpoint("https://$host/api/") - val mastodon = newMicroBlogInstance(activity, endpoint, EmptyAuthorization(), - AccountType.MASTODON, Mastodon::class.java) - val registered = mastodon.registerApplication("Twidere for Android", - MASTODON_CALLBACK_URL, scopes, TWIDERE_PROJECT_URL) - registry[host] = registered - return@run registered - }) + return@task Pair(host, registry[host] ?: registry.fetch(host, scopes)) }.successUi { (host, app) -> val activity = weakThis.get() ?: return@successUi val endpoint = Endpoint("https://$host/") @@ -417,12 +409,7 @@ class SignInActivity : BaseActivity(), OnClickListener, TextWatcher, private fun dismissDialogFragment(tag: String) { executeAfterFragmentResumed { - val fm = supportFragmentManager - val f = fm.findFragmentByTag(tag) - if (f is DialogFragment) { - f.dismiss() - } - Unit + it.supportFragmentManager.dismissDialogFragment(tag) } } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/AccountsDashboardFragment.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/AccountsDashboardFragment.kt index 7143a1132..25700623a 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/AccountsDashboardFragment.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/AccountsDashboardFragment.kt @@ -364,8 +364,6 @@ class AccountsDashboardFragment : BaseFragment(), LoaderCallbacks, } val menu = navigationView.menu menu.setItemAvailability(R.id.interactions, !hasInteractionsTab) - menu.setItemAvailability(R.id.messages, !hasDmTab) - menu.setItemAvailability(R.id.favorites, useStarsForLikes) menu.setItemAvailability(R.id.likes, !useStarsForLikes) menu.setItemAvailability(R.id.premium_features, extraFeaturesService.isSupported()) @@ -381,16 +379,20 @@ class AccountsDashboardFragment : BaseFragment(), LoaderCallbacks, var hasGroups = false var hasPublicTimeline = false var hasNetworkPublicTimeline = false + var hasDirectMessages = false when (account.type) { AccountType.TWITTER -> { + hasDirectMessages = !hasDmTab hasLists = true } AccountType.STATUSNET -> { + hasDirectMessages = !hasDmTab hasGroups = true hasPublicTimeline = !hasPublicTimelineTab hasNetworkPublicTimeline = !hasNetworkPublicTimelineTab } AccountType.FANFOU -> { + hasDirectMessages = !hasDmTab hasPublicTimeline = !hasPublicTimelineTab } AccountType.MASTODON -> { @@ -398,6 +400,7 @@ class AccountsDashboardFragment : BaseFragment(), LoaderCallbacks, hasNetworkPublicTimeline = !hasNetworkPublicTimelineTab } } + menu.setItemAvailability(R.id.messages, hasDirectMessages) menu.setItemAvailability(R.id.groups, hasGroups) menu.setItemAvailability(R.id.lists, hasLists) menu.setItemAvailability(R.id.public_timeline, hasPublicTimeline) diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/util/MastodonApplicationRegistry.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/util/MastodonApplicationRegistry.kt index 524c16d5a..29e2874d5 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/util/MastodonApplicationRegistry.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/util/MastodonApplicationRegistry.kt @@ -20,15 +20,21 @@ package org.mariotaku.twidere.util import android.content.Context +import org.mariotaku.microblog.library.MicroBlogException +import org.mariotaku.microblog.library.mastodon.Mastodon import org.mariotaku.microblog.library.mastodon.model.RegisteredApplication -import org.mariotaku.twidere.TwidereConstants.ETAG_MASTODON_APPS_PREFERENCES_NAME +import org.mariotaku.microblog.library.twitter.auth.EmptyAuthorization +import org.mariotaku.restfu.http.Endpoint +import org.mariotaku.twidere.TwidereConstants.* +import org.mariotaku.twidere.annotation.AccountType +import org.mariotaku.twidere.extension.model.newMicroBlogInstance import java.io.IOException /** * Created by mariotaku on 2017/4/17. */ -class MastodonApplicationRegistry(context: Context) { +class MastodonApplicationRegistry(private val context: Context) { private val preferences = context.getSharedPreferences(ETAG_MASTODON_APPS_PREFERENCES_NAME, Context.MODE_PRIVATE) @@ -51,4 +57,15 @@ class MastodonApplicationRegistry(context: Context) { editor.apply() return true } + + @Throws(MicroBlogException::class) + fun fetch(host: String, scopes: Array): RegisteredApplication { + val endpoint = Endpoint("https://$host/api/") + val mastodon = newMicroBlogInstance(context, endpoint, EmptyAuthorization(), + AccountType.MASTODON, Mastodon::class.java) + val registered = mastodon.registerApplication("Twidere for Android", MASTODON_CALLBACK_URL, + scopes, TWIDERE_PROJECT_URL) + this[host] = registered + return registered + } }