Display correct new email count for per-identity views

This commit is contained in:
str4d
2015-06-02 04:52:30 +00:00
parent 70fc503121
commit 131b321757
3 changed files with 42 additions and 8 deletions

View File

@@ -38,6 +38,8 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.mail.MessagingException;
import i2p.bote.I2PBote;
import i2p.bote.android.addressbook.AddressBookActivity;
import i2p.bote.android.config.SettingsActivity;
@@ -330,6 +332,8 @@ public class EmailListActivity extends BoteActivityBase implements
.putString(Constants.PREF_SELECTED_IDENTITY,
identity == null ? null : identity.getKey())
.apply();
// Trigger the drawer folder loader to update the drawer badges
getSupportLoaderManager().restartLoader(LOADER_DRAWER_FOLDERS, null, new DrawerFolderLoaderCallbacks());
EmailListFragment f = (EmailListFragment) getSupportFragmentManager()
.findFragmentById(R.id.list_fragment);
f.onIdentitySelected();
@@ -441,10 +445,10 @@ public class EmailListActivity extends BoteActivityBase implements
// Fetch the identities first, so we trigger any exceptions
Collection<EmailIdentity> identities = I2PBote.getInstance().getIdentities().getAll();
profiles.add(new ProfileDrawerItem()
.withIdentifier(ID_ALL_MAIL)
.withTag(null)
.withEmail(getContext().getString(R.string.all_mail))
.withIcon(getContext().getResources().getDrawable(R.drawable.ic_contact_picture))
.withIdentifier(ID_ALL_MAIL)
.withTag(null)
.withEmail(getContext().getString(R.string.all_mail))
.withIcon(getContext().getResources().getDrawable(R.drawable.ic_contact_picture))
);
for (EmailIdentity identity : identities) {
profiles.add(getIdentityDrawerItem(identity));
@@ -539,12 +543,13 @@ public class EmailListActivity extends BoteActivityBase implements
.withName(BoteHelper.getFolderDisplayName(getContext(), folder));
try {
// TODO change this when per-identity new emails can be determined
int numNew = folder.getNumNewEmails();
int numNew = BoteHelper.getNumNewEmails(getContext(), folder);
if (numNew > 0)
item.withBadge("" + numNew);
} catch (PasswordException e) {
// Password fetching is handled in EmailListFragment
} catch (MessagingException | GeneralSecurityException | IOException e) {
e.printStackTrace();
}
return item;

View File

@@ -498,6 +498,8 @@ public class EmailListFragment extends AuthenticatedFragment implements
Log log = I2PAppContext.getGlobalContext().logManager().getLog(EmailListFragment.class);
if (log.shouldLog(Log.WARN))
log.warn("Email list loader finished, but password is no longer cached", e);
} catch (MessagingException | GeneralSecurityException | IOException e) {
e.printStackTrace();
}
}

View File

@@ -51,6 +51,33 @@ import i2p.bote.util.GeneralHelper;
import im.delight.android.identicons.Identicon;
public class BoteHelper extends GeneralHelper {
public static int getNumNewEmails(Context ctx, EmailFolder folder) throws PasswordException, GeneralSecurityException, IOException, MessagingException {
String selectedIdentityKey = ctx.getSharedPreferences(Constants.SHARED_PREFS, 0)
.getString(Constants.PREF_SELECTED_IDENTITY, null);
if (selectedIdentityKey == null)
return folder.getNumNewEmails();
int numNew = 0;
for (Email email : BoteHelper.getEmails(folder, null, true)) {
if (email.getMetadata().isUnread()) {
if (BoteHelper.isSentEmail(email)) {
String senderDest = BoteHelper.extractEmailDestination(email.getOneFromAddress());
if (selectedIdentityKey.equals(senderDest))
numNew++;
} else {
for (Address recipient : email.getAllRecipients()) {
String recipientDest = BoteHelper.extractEmailDestination(recipient.toString());
if (selectedIdentityKey.equals(recipientDest)) {
numNew++;
break;
}
}
}
}
}
return numNew;
}
/**
* Get the translated name of the folder.
* Built-in folders are special-cased; other folders are created by the
@@ -83,10 +110,10 @@ public class BoteHelper extends GeneralHelper {
* @return The name of the folder.
* @throws PasswordException
*/
public static String getFolderDisplayNameWithNew(Context ctx, EmailFolder folder) throws PasswordException {
public static String getFolderDisplayNameWithNew(Context ctx, EmailFolder folder) throws PasswordException, GeneralSecurityException, IOException, MessagingException {
String displayName = getFolderDisplayName(ctx, folder);
int numNew = folder.getNumNewEmails();
int numNew = getNumNewEmails(ctx, folder);
if (numNew > 0)
displayName = displayName + " (" + numNew + ")";