Skip to content

Commit

Permalink
Merge pull request #1519 from k9mail/recipient-inject-loaderman
Browse files Browse the repository at this point in the history
Inject LoaderManager to RecipientSelectView via presenter
  • Loading branch information
cketti authored Jul 24, 2016
2 parents 2abcbf9 + 034b1ed commit 807f041
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 22 deletions.
4 changes: 2 additions & 2 deletions k9mail/src/main/java/com/fsck/k9/activity/MessageCompose.java
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
37 changes: 20 additions & 17 deletions k9mail/src/main/java/com/fsck/k9/view/RecipientSelectView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -56,13 +55,14 @@ public class RecipientSelectView extends TokenCompleteTextView<Recipient> 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<Recipient> listener;


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

Expand All @@ -274,7 +273,7 @@ public Address[] getAddresses() {
}

private void showAlternates(Recipient recipient) {
if (!attachedToWindow) {
if (loaderManager == null) {
return;
}

Expand All @@ -296,7 +295,7 @@ public void run() {
}

public void showAlternatesPopup(List<Recipient> data) {
if (!attachedToWindow) {
if (loaderManager == null) {
return;
}

Expand Down Expand Up @@ -338,6 +337,10 @@ public Loader<List<Recipient>> onCreateLoader(int id, Bundle args) {

@Override
public void onLoadFinished(Loader<List<Recipient>> loader, List<Recipient> data) {
if (loaderManager == null) {
return;
}

switch (loader.getId()) {
case LOADER_ID_FILTERING: {
adapter.setRecipients(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -41,6 +42,7 @@ public class RecipientPresenterTest {
private ComposePgpInlineDecider composePgpInlineDecider;
private Account account;
private RecipientMvpView recipientMvpView;
private LoaderManager loaderManager;


@Before
Expand All @@ -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
Expand Down

0 comments on commit 807f041

Please sign in to comment.