From 034b1eda410d3756ef1f7a073661f1c3776f96ad Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Fri, 22 Jul 2016 15:02:42 +0200 Subject: [PATCH] compose: inject loader manager to RecipientSelectView via presenter (fixes #1251) Conflicts: k9mail/src/main/java/com/fsck/k9/view/RecipientSelectView.java --- .../com/fsck/k9/activity/MessageCompose.java | 4 +- .../k9/activity/compose/RecipientMvpView.java | 7 ++++ .../activity/compose/RecipientPresenter.java | 7 +++- .../com/fsck/k9/view/RecipientSelectView.java | 37 ++++++++++--------- .../compose/RecipientPresenterTest.java | 5 ++- 5 files changed, 38 insertions(+), 22 deletions(-) diff --git a/k9mail/src/main/java/com/fsck/k9/activity/MessageCompose.java b/k9mail/src/main/java/com/fsck/k9/activity/MessageCompose.java index 70a62f0c96a..335f6c862a8 100644 --- a/k9mail/src/main/java/com/fsck/k9/activity/MessageCompose.java +++ b/k9mail/src/main/java/com/fsck/k9/activity/MessageCompose.java @@ -364,8 +364,8 @@ public void onCreate(Bundle savedInstanceState) { RecipientMvpView recipientMvpView = new RecipientMvpView(this); ComposePgpInlineDecider composePgpInlineDecider = new ComposePgpInlineDecider(); - recipientPresenter = new RecipientPresenter(this, recipientMvpView, mAccount, - composePgpInlineDecider, new ReplyToParser()); + recipientPresenter = new RecipientPresenter(getApplicationContext(), getLoaderManager(), recipientMvpView, + mAccount, composePgpInlineDecider, new ReplyToParser()); mSubjectView = (EditText) findViewById(R.id.subject); mSubjectView.getInputExtras(true).putBoolean("allowEmoji", true); diff --git a/k9mail/src/main/java/com/fsck/k9/activity/compose/RecipientMvpView.java b/k9mail/src/main/java/com/fsck/k9/activity/compose/RecipientMvpView.java index 151ecfb738e..a8889a3888b 100644 --- a/k9mail/src/main/java/com/fsck/k9/activity/compose/RecipientMvpView.java +++ b/k9mail/src/main/java/com/fsck/k9/activity/compose/RecipientMvpView.java @@ -4,6 +4,7 @@ import java.util.Arrays; import java.util.List; +import android.app.LoaderManager; import android.app.PendingIntent; import android.text.TextWatcher; import android.view.View; @@ -379,6 +380,12 @@ public void launchUserInteractionPendingIntent(PendingIntent pendingIntent, int activity.launchUserInteractionPendingIntent(pendingIntent, requestCode); } + public void setLoaderManager(LoaderManager loaderManager) { + toView.setLoaderManager(loaderManager); + ccView.setLoaderManager(loaderManager); + bccView.setLoaderManager(loaderManager); + } + public enum CryptoStatusDisplayType { UNCONFIGURED(VIEW_INDEX_HIDDEN), UNINITIALIZED(VIEW_INDEX_HIDDEN), diff --git a/k9mail/src/main/java/com/fsck/k9/activity/compose/RecipientPresenter.java b/k9mail/src/main/java/com/fsck/k9/activity/compose/RecipientPresenter.java index 1944be571ab..b8bb623f4d8 100644 --- a/k9mail/src/main/java/com/fsck/k9/activity/compose/RecipientPresenter.java +++ b/k9mail/src/main/java/com/fsck/k9/activity/compose/RecipientPresenter.java @@ -7,6 +7,7 @@ import java.util.List; import android.app.Activity; +import android.app.LoaderManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; @@ -21,6 +22,7 @@ import com.fsck.k9.Identity; import com.fsck.k9.K9; import com.fsck.k9.R; +import com.fsck.k9.activity.MessageCompose; import com.fsck.k9.activity.compose.ComposeCryptoStatus.AttachErrorState; import com.fsck.k9.activity.compose.ComposeCryptoStatus.ComposeCryptoStatusBuilder; import com.fsck.k9.activity.compose.ComposeCryptoStatus.SendErrorState; @@ -78,14 +80,15 @@ public class RecipientPresenter implements PermissionPingCallback { private boolean cryptoEnablePgpInline = false; - public RecipientPresenter(Context context, RecipientMvpView recipientMvpView, Account account, - ComposePgpInlineDecider composePgpInlineDecider, ReplyToParser replyToParser) { + public RecipientPresenter(Context context, LoaderManager loaderManager, RecipientMvpView recipientMvpView, + Account account, ComposePgpInlineDecider composePgpInlineDecider, ReplyToParser replyToParser) { this.recipientMvpView = recipientMvpView; this.context = context; this.composePgpInlineDecider = composePgpInlineDecider; this.replyToParser = replyToParser; recipientMvpView.setPresenter(this); + recipientMvpView.setLoaderManager(loaderManager); onSwitchAccount(account); updateCryptoStatus(); } diff --git a/k9mail/src/main/java/com/fsck/k9/view/RecipientSelectView.java b/k9mail/src/main/java/com/fsck/k9/view/RecipientSelectView.java index f7cfbd651fc..5cacbeafaf1 100644 --- a/k9mail/src/main/java/com/fsck/k9/view/RecipientSelectView.java +++ b/k9mail/src/main/java/com/fsck/k9/view/RecipientSelectView.java @@ -8,7 +8,6 @@ import java.util.List; import android.annotation.SuppressLint; -import android.app.Activity; import android.app.LoaderManager; import android.app.LoaderManager.LoaderCallbacks; import android.content.Context; @@ -56,13 +55,14 @@ public class RecipientSelectView extends TokenCompleteTextView implem private RecipientAdapter adapter; + @Nullable private String cryptoProvider; + @Nullable private LoaderManager loaderManager; private ListPopupWindow alternatesPopup; private AlternateRecipientAdapter alternatesAdapter; private Recipient alternatesPopupRecipient; - private boolean attachedToWindow = true; private TokenListener listener; @@ -178,22 +178,18 @@ public boolean isEmpty() { return getObjects().isEmpty(); } - @Override - protected void onAttachedToWindow() { - super.onAttachedToWindow(); - - attachedToWindow = true; - - if (getContext() instanceof Activity) { - Activity activity = (Activity) getContext(); - loaderManager = activity.getLoaderManager(); - } + public void setLoaderManager(@Nullable LoaderManager loaderManager) { + this.loaderManager = loaderManager; } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); - attachedToWindow = false; + if (loaderManager != null) { + loaderManager.destroyLoader(LOADER_ID_ALTERNATES); + loaderManager.destroyLoader(LOADER_ID_FILTERING); + loaderManager = null; + } } @Override @@ -241,8 +237,11 @@ public void performCompletion() { @Override protected void performFiltering(@NonNull CharSequence text, int start, int end, int keyCode) { - String query = text.subSequence(start, end).toString(); + if (loaderManager == null) { + return; + } + String query = text.subSequence(start, end).toString(); if (TextUtils.isEmpty(query) || query.length() < MINIMUM_LENGTH_FOR_FILTERING) { loaderManager.destroyLoader(LOADER_ID_FILTERING); return; @@ -253,7 +252,7 @@ protected void performFiltering(@NonNull CharSequence text, int start, int end, loaderManager.restartLoader(LOADER_ID_FILTERING, args, this); } - public void setCryptoProvider(String cryptoProvider) { + public void setCryptoProvider(@Nullable String cryptoProvider) { this.cryptoProvider = cryptoProvider; } @@ -274,7 +273,7 @@ public Address[] getAddresses() { } private void showAlternates(Recipient recipient) { - if (!attachedToWindow) { + if (loaderManager == null) { return; } @@ -296,7 +295,7 @@ public void run() { } public void showAlternatesPopup(List data) { - if (!attachedToWindow) { + if (loaderManager == null) { return; } @@ -338,6 +337,10 @@ public Loader> onCreateLoader(int id, Bundle args) { @Override public void onLoadFinished(Loader> loader, List data) { + if (loaderManager == null) { + return; + } + switch (loader.getId()) { case LOADER_ID_FILTERING: { adapter.setRecipients(data); diff --git a/k9mail/src/test/java/com/fsck/k9/activity/compose/RecipientPresenterTest.java b/k9mail/src/test/java/com/fsck/k9/activity/compose/RecipientPresenterTest.java index 8e7108bc9cc..32656064f29 100644 --- a/k9mail/src/test/java/com/fsck/k9/activity/compose/RecipientPresenterTest.java +++ b/k9mail/src/test/java/com/fsck/k9/activity/compose/RecipientPresenterTest.java @@ -4,6 +4,7 @@ import java.util.Arrays; import java.util.List; +import android.app.LoaderManager; import android.content.Context; import com.fsck.k9.Account; @@ -41,6 +42,7 @@ public class RecipientPresenterTest { private ComposePgpInlineDecider composePgpInlineDecider; private Account account; private RecipientMvpView recipientMvpView; + private LoaderManager loaderManager; @Before @@ -51,9 +53,10 @@ public void setUp() throws Exception { account = mock(Account.class); composePgpInlineDecider = mock(ComposePgpInlineDecider.class); replyToParser = mock(ReplyToParser.class); + loaderManager = mock(LoaderManager.class); recipientPresenter = new RecipientPresenter( - context, recipientMvpView, account, composePgpInlineDecider, replyToParser); + context, loaderManager, recipientMvpView, account, composePgpInlineDecider, replyToParser); } @Test