Load lists asynchronously
This commit is contained in:
parent
18d72d727c
commit
65c03bd638
@ -18,6 +18,7 @@ package ch.dissem.apps.abit;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.graphics.Point;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
@ -53,8 +54,9 @@ import org.slf4j.LoggerFactory;
|
||||
import java.io.Serializable;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import ch.dissem.apps.abit.dialog.AddIdentityDialogFragment;
|
||||
import ch.dissem.apps.abit.dialog.FullNodeDialogActivity;
|
||||
@ -125,12 +127,6 @@ public class MainActivity extends AppCompatActivity
|
||||
super.onCreate(savedInstanceState);
|
||||
instance = new WeakReference<>(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);
|
||||
|
||||
@ -155,7 +151,7 @@ public class MainActivity extends AppCompatActivity
|
||||
listFragment.setActivateOnItemClick(true);
|
||||
}
|
||||
|
||||
createDrawer(toolbar, labels);
|
||||
createDrawer(toolbar);
|
||||
|
||||
// handle intents
|
||||
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<>();
|
||||
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()
|
||||
.withName(getString(R.string.add_identity))
|
||||
.withDescription(getString(R.string.add_identity_summary))
|
||||
@ -288,43 +270,7 @@ public class MainActivity extends AppCompatActivity
|
||||
accountHeader.setActiveProfile(profiles.get(0), true);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
final ArrayList<IDrawerItem> drawerItems = new ArrayList<>();
|
||||
drawerItems.add(new PrimaryDrawerItem()
|
||||
.withName(R.string.archive)
|
||||
.withTag(null)
|
||||
@ -397,6 +343,59 @@ public class MainActivity extends AppCompatActivity
|
||||
})
|
||||
.withShowDrawerOnFirstLaunch(true)
|
||||
.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() {
|
||||
@ -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) {
|
||||
for (IProfile profile : accountHeader.getProfiles()) {
|
||||
if (profile instanceof ProfileDrawerItem) {
|
||||
|
@ -17,6 +17,7 @@
|
||||
package ch.dissem.apps.abit;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
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.utils.WrapperAdapterUtils;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import ch.dissem.apps.abit.adapter.SwipeableMessageAdapter;
|
||||
import ch.dissem.apps.abit.listener.ActionBarListener;
|
||||
@ -97,20 +100,29 @@ public class MessageListFragment extends Fragment implements ListHolder {
|
||||
MainActivity activity = (MainActivity) getActivity();
|
||||
messageRepo = Singleton.getMessageRepository(activity);
|
||||
|
||||
doUpdateList(activity.getSelectedLabel());
|
||||
currentLabel = activity.getSelectedLabel();
|
||||
doUpdateList(currentLabel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateList(Label label) {
|
||||
currentLabel = label;
|
||||
|
||||
if (!isVisible()) return;
|
||||
if (!isResumed()) {
|
||||
currentLabel = label;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Objects.equals(currentLabel, label)) {
|
||||
adapter.setData(label, Collections.<Plaintext>emptyList());
|
||||
adapter.notifyDataSetChanged();
|
||||
}
|
||||
doUpdateList(label);
|
||||
}
|
||||
|
||||
private void doUpdateList(Label label) {
|
||||
List<Plaintext> messages = Singleton.getMessageRepository(getContext()).findMessages(label);
|
||||
private void doUpdateList(final Label label) {
|
||||
currentLabel = label;
|
||||
if (emptyTrashMenuItem != null) {
|
||||
emptyTrashMenuItem.setVisible(label != null && label.getType() == Label.Type.TRASH);
|
||||
}
|
||||
if (getActivity() instanceof ActionBarListener) {
|
||||
if (label != null) {
|
||||
((ActionBarListener) getActivity()).updateTitle(label.toString());
|
||||
@ -118,11 +130,21 @@ public class MessageListFragment extends Fragment implements ListHolder {
|
||||
((ActionBarListener) getActivity()).updateTitle(getString(R.string.archive));
|
||||
}
|
||||
}
|
||||
if (emptyTrashMenuItem != null) {
|
||||
emptyTrashMenuItem.setVisible(label != null && label.getType() == Label.Type.TRASH);
|
||||
}
|
||||
adapter.setData(label, messages);
|
||||
adapter.notifyDataSetChanged();
|
||||
new AsyncTask<Void, Void, List<Plaintext>>() {
|
||||
|
||||
@Override
|
||||
protected List<Plaintext> doInBackground(Void... params) {
|
||||
return messageRepo.findMessages(label);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(List<Plaintext> messages) {
|
||||
if (adapter != null) {
|
||||
adapter.setData(label, messages);
|
||||
adapter.notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
}.execute();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -276,11 +298,20 @@ public class MessageListFragment extends Fragment implements ListHolder {
|
||||
case R.id.empty_trash:
|
||||
if (currentLabel.getType() != Label.Type.TRASH) return true;
|
||||
|
||||
MessageRepository repo = Singleton.getMessageRepository(getContext());
|
||||
for (Plaintext message : repo.findMessages(currentLabel)) {
|
||||
repo.remove(message);
|
||||
}
|
||||
updateList(currentLabel);
|
||||
new AsyncTask<Void, Void, Void>() {
|
||||
@Override
|
||||
protected Void doInBackground(Void... params) {
|
||||
for (Plaintext message : messageRepo.findMessages(currentLabel)) {
|
||||
messageRepo.remove(message);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Void aVoid) {
|
||||
updateList(currentLabel);
|
||||
}
|
||||
}.execute();
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
|
@ -67,23 +67,25 @@ public class SettingsFragment
|
||||
@Override
|
||||
public boolean onPreferenceClick(Preference preference) {
|
||||
new AsyncTask<Void, Void, Void>() {
|
||||
private Context ctx = getActivity().getApplicationContext();
|
||||
|
||||
@Override
|
||||
protected void onPreExecute() {
|
||||
cleanup.setEnabled(false);
|
||||
Toast.makeText(getActivity(), R.string.cleanup_notification_start, Toast
|
||||
Toast.makeText(ctx, R.string.cleanup_notification_start, Toast
|
||||
.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Void doInBackground(Void... voids) {
|
||||
Singleton.getBitmessageContext(getActivity()).cleanup();
|
||||
Singleton.getBitmessageContext(ctx).cleanup();
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Void aVoid) {
|
||||
Toast.makeText(
|
||||
getActivity(),
|
||||
ctx,
|
||||
R.string.cleanup_notification_end,
|
||||
Toast.LENGTH_LONG
|
||||
).show();
|
||||
|
Loading…
Reference in New Issue
Block a user