Load lists asynchronously

This commit is contained in:
Christian Basler 2017-02-23 17:38:00 +01:00
parent 18d72d727c
commit 65c03bd638
3 changed files with 148 additions and 79 deletions

View File

@ -18,6 +18,7 @@ package ch.dissem.apps.abit;
import android.content.Intent; import android.content.Intent;
import android.graphics.Point; import android.graphics.Point;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
@ -53,8 +54,9 @@ import org.slf4j.LoggerFactory;
import java.io.Serializable; import java.io.Serializable;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Objects;
import ch.dissem.apps.abit.dialog.AddIdentityDialogFragment; import ch.dissem.apps.abit.dialog.AddIdentityDialogFragment;
import ch.dissem.apps.abit.dialog.FullNodeDialogActivity; import ch.dissem.apps.abit.dialog.FullNodeDialogActivity;
@ -125,12 +127,6 @@ public class MainActivity extends AppCompatActivity
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
instance = new WeakReference<>(this); instance = new WeakReference<>(this);
bmc = Singleton.getBitmessageContext(this); bmc = Singleton.getBitmessageContext(this);
List<Label> labels = bmc.messages().getLabels();
if (getIntent().hasExtra(EXTRA_SHOW_LABEL)) {
selectedLabel = (Label) getIntent().getSerializableExtra(EXTRA_SHOW_LABEL);
} else if (selectedLabel == null) {
selectedLabel = labels.get(0);
}
setContentView(R.layout.activity_message_list); setContentView(R.layout.activity_message_list);
@ -155,7 +151,7 @@ public class MainActivity extends AppCompatActivity
listFragment.setActivateOnItemClick(true); listFragment.setActivateOnItemClick(true);
} }
createDrawer(toolbar, labels); createDrawer(toolbar);
// handle intents // handle intents
Intent intent = getIntent(); Intent intent = getIntent();
@ -217,22 +213,8 @@ public class MainActivity extends AppCompatActivity
} }
} }
private void createDrawer(Toolbar toolbar, Collection<Label> labels) { private void createDrawer(Toolbar toolbar) {
final ArrayList<IProfile> profiles = new ArrayList<>(); final ArrayList<IProfile> profiles = new ArrayList<>();
for (BitmessageAddress identity : bmc.addresses().getIdentities()) {
LOG.info("Adding identity " + identity.getAddress());
profiles.add(new ProfileDrawerItem()
.withIcon(new Identicon(identity))
.withName(identity.toString())
.withNameShown(true)
.withEmail(identity.getAddress())
.withTag(identity)
);
}
if (profiles.isEmpty()) {
// Create an initial identity
Singleton.getIdentity(this);
}
profiles.add(new ProfileSettingDrawerItem() profiles.add(new ProfileSettingDrawerItem()
.withName(getString(R.string.add_identity)) .withName(getString(R.string.add_identity))
.withDescription(getString(R.string.add_identity_summary)) .withDescription(getString(R.string.add_identity_summary))
@ -288,43 +270,7 @@ public class MainActivity extends AppCompatActivity
accountHeader.setActiveProfile(profiles.get(0), true); accountHeader.setActiveProfile(profiles.get(0), true);
} }
ArrayList<IDrawerItem> drawerItems = new ArrayList<>(); final ArrayList<IDrawerItem> drawerItems = new ArrayList<>();
for (Label label : labels) {
PrimaryDrawerItem item = new PrimaryDrawerItem()
.withName(label.toString())
.withTag(label);
if (label.getType() == null) {
item.withIcon(CommunityMaterial.Icon.cmd_label)
.withIconColor(label.getColor());
} else {
switch (label.getType()) {
case INBOX:
item.withIcon(GoogleMaterial.Icon.gmd_inbox);
break;
case DRAFT:
item.withIcon(CommunityMaterial.Icon.cmd_file);
break;
case OUTBOX:
item.withIcon(CommunityMaterial.Icon.cmd_outbox);
break;
case SENT:
item.withIcon(CommunityMaterial.Icon.cmd_send);
break;
case BROADCAST:
item.withIcon(CommunityMaterial.Icon.cmd_rss);
break;
case UNREAD:
item.withIcon(GoogleMaterial.Icon.gmd_markunread_mailbox);
break;
case TRASH:
item.withIcon(GoogleMaterial.Icon.gmd_delete);
break;
default:
item.withIcon(CommunityMaterial.Icon.cmd_label);
}
}
drawerItems.add(item);
}
drawerItems.add(new PrimaryDrawerItem() drawerItems.add(new PrimaryDrawerItem()
.withName(R.string.archive) .withName(R.string.archive)
.withTag(null) .withTag(null)
@ -397,6 +343,59 @@ public class MainActivity extends AppCompatActivity
}) })
.withShowDrawerOnFirstLaunch(true) .withShowDrawerOnFirstLaunch(true)
.build(); .build();
new AsyncTask<Void, Void, List<BitmessageAddress>>() {
@Override
protected List<BitmessageAddress> doInBackground(Void... params) {
List<BitmessageAddress> identities = bmc.addresses().getIdentities();
if (identities.isEmpty()) {
// Create an initial identity
Singleton.getIdentity(MainActivity.this);
}
return identities;
}
@Override
protected void onPostExecute(List<BitmessageAddress> identities) {
for (BitmessageAddress identity : identities) {
addIdentityEntry(identity);
}
}
}.execute();
new AsyncTask<Void, Void, List<Label>>() {
@Override
protected List<Label> doInBackground(Void... params) {
return bmc.messages().getLabels();
}
@Override
protected void onPostExecute(List<Label> labels) {
if (getIntent().hasExtra(EXTRA_SHOW_LABEL)) {
selectedLabel = (Label) getIntent().getSerializableExtra(EXTRA_SHOW_LABEL);
} else if (selectedLabel == null) {
selectedLabel = labels.get(0);
}
for (Label label : labels) {
addLabelEntry(label);
}
showSelectedLabel();
}
}.execute();
}
@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putSerializable("selectedLabel", selectedLabel);
}
@Override
@SuppressWarnings("unchecked")
protected void onRestoreInstanceState(Bundle savedInstanceState) {
selectedLabel = (Label) savedInstanceState.getSerializable("selectedLabel");
showSelectedLabel();
super.onRestoreInstanceState(savedInstanceState);
} }
private void addIdentityDialog() { private void addIdentityDialog() {
@ -429,6 +428,43 @@ public class MainActivity extends AppCompatActivity
} }
} }
public void addLabelEntry(Label label) {
PrimaryDrawerItem item = new PrimaryDrawerItem()
.withName(label.toString())
.withTag(label);
if (label.getType() == null) {
item.withIcon(CommunityMaterial.Icon.cmd_label)
.withIconColor(label.getColor());
} else {
switch (label.getType()) {
case INBOX:
item.withIcon(GoogleMaterial.Icon.gmd_inbox);
break;
case DRAFT:
item.withIcon(CommunityMaterial.Icon.cmd_file);
break;
case OUTBOX:
item.withIcon(CommunityMaterial.Icon.cmd_outbox);
break;
case SENT:
item.withIcon(CommunityMaterial.Icon.cmd_send);
break;
case BROADCAST:
item.withIcon(CommunityMaterial.Icon.cmd_rss);
break;
case UNREAD:
item.withIcon(GoogleMaterial.Icon.gmd_markunread_mailbox);
break;
case TRASH:
item.withIcon(GoogleMaterial.Icon.gmd_delete);
break;
default:
item.withIcon(CommunityMaterial.Icon.cmd_label);
}
}
drawer.addItemAtPosition(item, drawer.getDrawerItems().size() - 3);
}
public void updateIdentityEntry(BitmessageAddress identity) { public void updateIdentityEntry(BitmessageAddress identity) {
for (IProfile profile : accountHeader.getProfiles()) { for (IProfile profile : accountHeader.getProfiles()) {
if (profile instanceof ProfileDrawerItem) { if (profile instanceof ProfileDrawerItem) {

View File

@ -17,6 +17,7 @@
package ch.dissem.apps.abit; package ch.dissem.apps.abit;
import android.content.Intent; import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
@ -37,7 +38,9 @@ import com.h6ah4i.android.widget.advrecyclerview.swipeable.RecyclerViewSwipeMana
import com.h6ah4i.android.widget.advrecyclerview.touchguard.RecyclerViewTouchActionGuardManager; import com.h6ah4i.android.widget.advrecyclerview.touchguard.RecyclerViewTouchActionGuardManager;
import com.h6ah4i.android.widget.advrecyclerview.utils.WrapperAdapterUtils; import com.h6ah4i.android.widget.advrecyclerview.utils.WrapperAdapterUtils;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Objects;
import ch.dissem.apps.abit.adapter.SwipeableMessageAdapter; import ch.dissem.apps.abit.adapter.SwipeableMessageAdapter;
import ch.dissem.apps.abit.listener.ActionBarListener; import ch.dissem.apps.abit.listener.ActionBarListener;
@ -97,20 +100,29 @@ public class MessageListFragment extends Fragment implements ListHolder {
MainActivity activity = (MainActivity) getActivity(); MainActivity activity = (MainActivity) getActivity();
messageRepo = Singleton.getMessageRepository(activity); messageRepo = Singleton.getMessageRepository(activity);
doUpdateList(activity.getSelectedLabel()); currentLabel = activity.getSelectedLabel();
doUpdateList(currentLabel);
} }
@Override @Override
public void updateList(Label label) { public void updateList(Label label) {
currentLabel = label; if (!isResumed()) {
currentLabel = label;
if (!isVisible()) return; return;
}
if (!Objects.equals(currentLabel, label)) {
adapter.setData(label, Collections.<Plaintext>emptyList());
adapter.notifyDataSetChanged();
}
doUpdateList(label); doUpdateList(label);
} }
private void doUpdateList(Label label) { private void doUpdateList(final Label label) {
List<Plaintext> messages = Singleton.getMessageRepository(getContext()).findMessages(label); currentLabel = label;
if (emptyTrashMenuItem != null) {
emptyTrashMenuItem.setVisible(label != null && label.getType() == Label.Type.TRASH);
}
if (getActivity() instanceof ActionBarListener) { if (getActivity() instanceof ActionBarListener) {
if (label != null) { if (label != null) {
((ActionBarListener) getActivity()).updateTitle(label.toString()); ((ActionBarListener) getActivity()).updateTitle(label.toString());
@ -118,11 +130,21 @@ public class MessageListFragment extends Fragment implements ListHolder {
((ActionBarListener) getActivity()).updateTitle(getString(R.string.archive)); ((ActionBarListener) getActivity()).updateTitle(getString(R.string.archive));
} }
} }
if (emptyTrashMenuItem != null) { new AsyncTask<Void, Void, List<Plaintext>>() {
emptyTrashMenuItem.setVisible(label != null && label.getType() == Label.Type.TRASH);
} @Override
adapter.setData(label, messages); protected List<Plaintext> doInBackground(Void... params) {
adapter.notifyDataSetChanged(); return messageRepo.findMessages(label);
}
@Override
protected void onPostExecute(List<Plaintext> messages) {
if (adapter != null) {
adapter.setData(label, messages);
adapter.notifyDataSetChanged();
}
}
}.execute();
} }
@Override @Override
@ -276,11 +298,20 @@ public class MessageListFragment extends Fragment implements ListHolder {
case R.id.empty_trash: case R.id.empty_trash:
if (currentLabel.getType() != Label.Type.TRASH) return true; if (currentLabel.getType() != Label.Type.TRASH) return true;
MessageRepository repo = Singleton.getMessageRepository(getContext()); new AsyncTask<Void, Void, Void>() {
for (Plaintext message : repo.findMessages(currentLabel)) { @Override
repo.remove(message); protected Void doInBackground(Void... params) {
} for (Plaintext message : messageRepo.findMessages(currentLabel)) {
updateList(currentLabel); messageRepo.remove(message);
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
updateList(currentLabel);
}
}.execute();
return true; return true;
default: default:
return false; return false;

View File

@ -67,23 +67,25 @@ public class SettingsFragment
@Override @Override
public boolean onPreferenceClick(Preference preference) { public boolean onPreferenceClick(Preference preference) {
new AsyncTask<Void, Void, Void>() { new AsyncTask<Void, Void, Void>() {
private Context ctx = getActivity().getApplicationContext();
@Override @Override
protected void onPreExecute() { protected void onPreExecute() {
cleanup.setEnabled(false); cleanup.setEnabled(false);
Toast.makeText(getActivity(), R.string.cleanup_notification_start, Toast Toast.makeText(ctx, R.string.cleanup_notification_start, Toast
.LENGTH_SHORT).show(); .LENGTH_SHORT).show();
} }
@Override @Override
protected Void doInBackground(Void... voids) { protected Void doInBackground(Void... voids) {
Singleton.getBitmessageContext(getActivity()).cleanup(); Singleton.getBitmessageContext(ctx).cleanup();
return null; return null;
} }
@Override @Override
protected void onPostExecute(Void aVoid) { protected void onPostExecute(Void aVoid) {
Toast.makeText( Toast.makeText(
getActivity(), ctx,
R.string.cleanup_notification_end, R.string.cleanup_notification_end,
Toast.LENGTH_LONG Toast.LENGTH_LONG
).show(); ).show();