Merge branch 'feature/load-lists-asynchronously' into develop

This commit is contained in:
Christian Basler 2017-02-24 17:34:14 +01:00
commit cd8192e99c
5 changed files with 179 additions and 92 deletions

View File

@ -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,13 +54,13 @@ import org.slf4j.LoggerFactory;
import java.io.Serializable;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import ch.dissem.apps.abit.dialog.AddIdentityDialogFragment;
import ch.dissem.apps.abit.dialog.FullNodeDialogActivity;
import ch.dissem.apps.abit.listener.ActionBarListener;
import ch.dissem.apps.abit.listener.ListSelectionListener;
import ch.dissem.apps.abit.repository.AndroidMessageRepository;
import ch.dissem.apps.abit.service.BitmessageService;
import ch.dissem.apps.abit.service.Singleton;
import ch.dissem.apps.abit.synchronization.SyncAdapter;
@ -125,12 +126,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 +150,7 @@ public class MainActivity extends AppCompatActivity
listFragment.setActivateOnItemClick(true);
}
createDrawer(toolbar, labels);
createDrawer(toolbar);
// handle intents
Intent intent = getIntent();
@ -217,22 +212,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,46 +269,10 @@ 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)
.withTag(AndroidMessageRepository.LABEL_ARCHIVE)
.withIcon(CommunityMaterial.Icon.cmd_archive)
);
drawerItems.add(new DividerDrawerItem());
@ -384,10 +329,6 @@ public class MainActivity extends AppCompatActivity
startActivity(new Intent(MainActivity.this, SettingsActivity
.class));
break;
case R.string.archive:
selectedLabel = null;
showSelectedLabel();
break;
case R.string.full_node:
return true;
}
@ -397,6 +338,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 +423,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) {

View File

@ -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;
@ -102,27 +105,54 @@ public class MessageListFragment extends Fragment implements ListHolder {
@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) {
if (label == null) {
if (getActivity() instanceof ActionBarListener) {
((ActionBarListener) getActivity()).updateTitle(getString(R.string.app_name));
}
adapter.setData(null, Collections.<Plaintext>emptyList());
adapter.notifyDataSetChanged();
return;
}
currentLabel = label;
if (emptyTrashMenuItem != null) {
emptyTrashMenuItem.setVisible(label.getType() == Label.Type.TRASH);
}
if (getActivity() instanceof ActionBarListener) {
if (label != null) {
((ActionBarListener) getActivity()).updateTitle(label.toString());
ActionBarListener actionBarListener = (ActionBarListener) getActivity();
if ("archive".equals(label.toString())) {
actionBarListener.updateTitle(getString(R.string.archive));
} else {
((ActionBarListener) getActivity()).updateTitle(getString(R.string.archive));
actionBarListener.updateTitle(label.toString());
}
}
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 +306,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;

View File

@ -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();

View File

@ -45,6 +45,7 @@ import ch.dissem.apps.abit.util.Assets;
import ch.dissem.bitmessage.entity.Plaintext;
import ch.dissem.bitmessage.entity.valueobject.Label;
import static ch.dissem.apps.abit.repository.AndroidMessageRepository.LABEL_ARCHIVE;
import static ch.dissem.apps.abit.util.Strings.normalizeWhitespaces;
/**
@ -199,7 +200,7 @@ public class SwipeableMessageAdapter
@Override
public int onGetSwipeReactionType(ViewHolder holder, int position, int x, int y) {
if (label == null || label.getType() == Label.Type.TRASH) {
if (label == LABEL_ARCHIVE || label.getType() == Label.Type.TRASH) {
return REACTION_CAN_SWIPE_LEFT | REACTION_CAN_NOT_SWIPE_RIGHT_WITH_RUBBER_BAND_EFFECT;
}
return REACTION_CAN_SWIPE_BOTH_H;
@ -217,7 +218,7 @@ public class SwipeableMessageAdapter
bgRes = R.drawable.bg_swipe_item_left;
break;
case DRAWABLE_SWIPE_RIGHT_BACKGROUND:
if (label == null || label.getType() == Label.Type.TRASH) {
if (label == LABEL_ARCHIVE || label.getType() == Label.Type.TRASH) {
bgRes = R.drawable.bg_swipe_item_neutral;
} else {
bgRes = R.drawable.bg_swipe_item_right;

View File

@ -48,6 +48,8 @@ import static java.lang.String.valueOf;
public class AndroidMessageRepository extends AbstractMessageRepository {
private static final Logger LOG = LoggerFactory.getLogger(AndroidMessageRepository.class);
public static final Label LABEL_ARCHIVE = new Label("archive", null, 0);
private static final String TABLE_NAME = "Message";
private static final String COLUMN_ID = "id";
private static final String COLUMN_IV = "iv";
@ -82,6 +84,15 @@ public class AndroidMessageRepository extends AbstractMessageRepository {
this.context = ctx;
}
@Override
public List<Plaintext> findMessages(Label label) {
if (label == LABEL_ARCHIVE) {
return super.findMessages((Label) null);
} else {
return super.findMessages(label);
}
}
public List<Label> findLabels(String where) {
List<Label> result = new LinkedList<>();
@ -153,15 +164,18 @@ public class AndroidMessageRepository extends AbstractMessageRepository {
public int countUnread(Label label) {
String[] args;
String where;
if (label != null) {
where = "id IN (SELECT message_id FROM Message_Label WHERE label_id=?) AND ";
if (label == null){
return 0;
}
if (label == LABEL_ARCHIVE) {
where = "";
args = new String[]{
label.getId().toString(),
Label.Type.UNREAD.name()
};
} else {
where = "";
where = "id IN (SELECT message_id FROM Message_Label WHERE label_id=?) AND ";
args = new String[]{
label.getId().toString(),
Label.Type.UNREAD.name()
};
}