Show number of unread messages rather than total number of messages.

This commit is contained in:
akwizgran
2013-03-12 11:26:46 +00:00
parent 768bd8d603
commit 127ed22c40
10 changed files with 121 additions and 120 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

View File

@@ -0,0 +1,19 @@
package net.sf.briar.android;
import java.util.Comparator;
import net.sf.briar.api.db.MessageHeader;
public class AscendingHeaderComparator implements Comparator<MessageHeader> {
public static final AscendingHeaderComparator INSTANCE =
new AscendingHeaderComparator();
public int compare(MessageHeader a, MessageHeader b) {
// The oldest message comes first
long aTime = a.getTimestamp(), bTime = b.getTimestamp();
if(aTime < bTime) return -1;
if(aTime > bTime) return 1;
return 0;
}
}

View File

@@ -0,0 +1,19 @@
package net.sf.briar.android;
import java.util.Comparator;
import net.sf.briar.api.db.MessageHeader;
public class DescendingHeaderComparator implements Comparator<MessageHeader> {
public static final DescendingHeaderComparator INSTANCE =
new DescendingHeaderComparator();
public int compare(MessageHeader a, MessageHeader b) {
// The newest message comes first
long aTime = a.getTimestamp(), bTime = b.getTimestamp();
if(aTime > bTime) return -1;
if(aTime < bTime) return 1;
return 0;
}
}

View File

@@ -1,19 +0,0 @@
package net.sf.briar.android.messages;
import java.util.Comparator;
import net.sf.briar.api.db.PrivateMessageHeader;
class AscendingHeaderComparator implements Comparator<PrivateMessageHeader> {
static final AscendingHeaderComparator INSTANCE =
new AscendingHeaderComparator();
public int compare(PrivateMessageHeader a, PrivateMessageHeader b) {
// The oldest message comes first
long aTime = a.getTimestamp(), bTime = b.getTimestamp();
if(aTime < bTime) return -1;
if(aTime > bTime) return 1;
return 0;
}
}

View File

@@ -13,6 +13,7 @@ import java.util.concurrent.Executor;
import java.util.logging.Logger; import java.util.logging.Logger;
import net.sf.briar.R; import net.sf.briar.R;
import net.sf.briar.android.AscendingHeaderComparator;
import net.sf.briar.android.BriarActivity; import net.sf.briar.android.BriarActivity;
import net.sf.briar.android.BriarService; import net.sf.briar.android.BriarService;
import net.sf.briar.android.BriarService.BriarServiceConnection; import net.sf.briar.android.BriarService.BriarServiceConnection;

View File

@@ -91,7 +91,11 @@ implements OnClickListener, DatabaseListener {
serviceConnection, 0); serviceConnection, 0);
// Add some fake messages to the database in a background thread // Add some fake messages to the database in a background thread
// FIXME: Remove this insertFakeMessages();
}
// FIXME: Remove this
private void insertFakeMessages() {
final DatabaseComponent db = this.db; final DatabaseComponent db = this.db;
final MessageFactory messageFactory = this.messageFactory; final MessageFactory messageFactory = this.messageFactory;
dbExecutor.execute(new Runnable() { dbExecutor.execute(new Runnable() {
@@ -102,42 +106,39 @@ implements OnClickListener, DatabaseListener {
// If there are no messages in the DB, create some fake ones // If there are no messages in the DB, create some fake ones
Collection<PrivateMessageHeader> headers = Collection<PrivateMessageHeader> headers =
db.getPrivateMessageHeaders(); db.getPrivateMessageHeaders();
if(headers.isEmpty()) { if(!headers.isEmpty()) return;
if(LOG.isLoggable(INFO)) if(LOG.isLoggable(INFO))
LOG.info("Inserting fake contact and messages"); LOG.info("Inserting fake private messages");
// Insert a fake contact // We'll also need a contact to exchange messages with
ContactId contactId = db.addContact("Carol"); ContactId contactId = db.addContact("Carol");
// Insert some text messages to and from the contact // Insert some text messages to and from the contact
for(int i = 0; i < 20; i++) { for(int i = 0; i < 20; i++) {
String body; String body;
if(i % 3 == 0) { if(i % 3 == 0) {
body = "Message " + i + " is short."; body = "Message " + i + " is short.";
} else { } else {
body = "Message " + i + " is long enough to" body = "Message " + i + " is long enough to"
+ " wrap onto a second line on some" + " wrap onto a second line on some"
+ " screens."; + " screens.";
}
Message m = messageFactory.createPrivateMessage(
null, "text/plain", body.getBytes("UTF-8"));
if(Math.random() < 0.5)
db.addLocalPrivateMessage(m, contactId);
else db.receiveMessage(contactId, m);
db.setReadFlag(m.getId(), i != 3);
} }
// Insert a non-text message
Message m = messageFactory.createPrivateMessage(null, Message m = messageFactory.createPrivateMessage(null,
"image/jpeg", new byte[1000]); "text/plain", body.getBytes("UTF-8"));
db.receiveMessage(contactId, m); if(Math.random() < 0.5)
db.setStarredFlag(m.getId(), true); db.addLocalPrivateMessage(m, contactId);
// Insert a long text message else db.receiveMessage(contactId, m);
StringBuilder s = new StringBuilder(); db.setReadFlag(m.getId(), i % 4 == 0);
for(int i = 0; i < 100; i++)
s.append("This is a very tedious message. ");
m = messageFactory.createPrivateMessage(m.getId(),
"text/plain", s.toString().getBytes("UTF-8"));
db.addLocalPrivateMessage(m, contactId);
db.setStarredFlag(m.getId(), true);
} }
// Insert a non-text message
Message m = messageFactory.createPrivateMessage(null,
"image/jpeg", new byte[1000]);
db.receiveMessage(contactId, m);
// Insert a long text message
StringBuilder s = new StringBuilder();
for(int i = 0; i < 100; i++)
s.append("This is a very tedious message. ");
m = messageFactory.createPrivateMessage(m.getId(),
"text/plain", s.toString().getBytes("UTF-8"));
db.addLocalPrivateMessage(m, contactId);
} catch(DbException e) { } catch(DbException e) {
if(LOG.isLoggable(WARNING)) if(LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e); LOG.log(WARNING, e.toString(), e);
@@ -162,27 +163,6 @@ implements OnClickListener, DatabaseListener {
reloadMessageHeaders(); reloadMessageHeaders();
} }
@Override
public void onDestroy() {
super.onDestroy();
db.removeListener(this);
unbindService(serviceConnection);
}
public void onClick(View view) {
startActivity(new Intent(this, WriteMessageActivity.class));
}
public void eventOccurred(DatabaseEvent e) {
if(e instanceof MessageAddedEvent) {
if(LOG.isLoggable(INFO)) LOG.info("Message added, reloading");
reloadMessageHeaders();
} else if(e instanceof MessageExpiredEvent) {
if(LOG.isLoggable(INFO)) LOG.info("Message removed, reloading");
reloadMessageHeaders();
}
}
private void reloadMessageHeaders() { private void reloadMessageHeaders() {
final DatabaseComponent db = this.db; final DatabaseComponent db = this.db;
dbExecutor.execute(new Runnable() { dbExecutor.execute(new Runnable() {
@@ -249,4 +229,25 @@ implements OnClickListener, DatabaseListener {
} }
return list; return list;
} }
@Override
public void onDestroy() {
super.onDestroy();
db.removeListener(this);
unbindService(serviceConnection);
}
public void onClick(View view) {
startActivity(new Intent(this, WriteMessageActivity.class));
}
public void eventOccurred(DatabaseEvent e) {
if(e instanceof MessageAddedEvent) {
if(LOG.isLoggable(INFO)) LOG.info("Message added, reloading");
reloadMessageHeaders();
} else if(e instanceof MessageExpiredEvent) {
if(LOG.isLoggable(INFO)) LOG.info("Message removed, reloading");
reloadMessageHeaders();
}
}
} }

View File

@@ -10,6 +10,7 @@ import static java.text.DateFormat.SHORT;
import java.util.ArrayList; import java.util.ArrayList;
import net.sf.briar.android.widgets.CommonLayoutParams; import net.sf.briar.android.widgets.CommonLayoutParams;
import net.sf.briar.util.StringUtils;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.text.format.DateUtils; import android.text.format.DateUtils;
@@ -45,17 +46,21 @@ implements OnItemClickListener {
TextView name = new TextView(ctx); TextView name = new TextView(ctx);
name.setTextSize(18); name.setTextSize(18);
name.setPadding(10, 10, 10, 0); name.setPadding(10, 10, 10, 10);
name.setText(item.getName() + " (" + item.getLength() + ")"); int unread = item.getUnreadCount();
if(unread > 0) name.setText(item.getName() + " (" + unread + ")");
else name.setText(item.getName());
innerLayout.addView(name); innerLayout.addView(name);
TextView subject = new TextView(ctx); if(!StringUtils.isNullOrEmpty(item.getSubject())) {
subject.setTextSize(14); TextView subject = new TextView(ctx);
subject.setMaxLines(2); subject.setTextSize(14);
subject.setPadding(10, 0, 10, 10); subject.setMaxLines(2);
if(!item.isRead()) subject.setTypeface(null, BOLD); subject.setPadding(10, 0, 10, 10);
subject.setText(item.getSubject()); if(unread > 0) subject.setTypeface(null, BOLD);
innerLayout.addView(subject); subject.setText(item.getSubject());
innerLayout.addView(subject);
}
layout.addView(innerLayout); layout.addView(innerLayout);
TextView date = new TextView(ctx); TextView date = new TextView(ctx);

View File

@@ -3,37 +3,35 @@ package net.sf.briar.android.messages;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import net.sf.briar.android.DescendingHeaderComparator;
import net.sf.briar.api.Contact; import net.sf.briar.api.Contact;
import net.sf.briar.api.ContactId; import net.sf.briar.api.ContactId;
import net.sf.briar.api.db.PrivateMessageHeader; import net.sf.briar.api.db.PrivateMessageHeader;
class ConversationListItem { class ConversationListItem {
private final ContactId contactId; private final Contact contact;
private final String name, subject; private final String subject;
private final long timestamp; private final long timestamp;
private final int length; private final int unread;
private final boolean read;
ConversationListItem(Contact contact, List<PrivateMessageHeader> headers) { ConversationListItem(Contact contact, List<PrivateMessageHeader> headers) {
if(headers.isEmpty()) throw new IllegalArgumentException(); if(headers.isEmpty()) throw new IllegalArgumentException();
contactId = contact.getId(); this.contact = contact;
name = contact.getName();
Collections.sort(headers, DescendingHeaderComparator.INSTANCE); Collections.sort(headers, DescendingHeaderComparator.INSTANCE);
subject = headers.get(0).getSubject(); subject = headers.get(0).getSubject();
timestamp = headers.get(0).getTimestamp(); timestamp = headers.get(0).getTimestamp();
length = headers.size(); int unread = 0;
boolean allRead = true; for(PrivateMessageHeader h : headers) if(!h.isRead()) unread++;
for(PrivateMessageHeader h : headers) allRead &= h.isRead(); this.unread = unread;
read = allRead;
} }
ContactId getContactId() { ContactId getContactId() {
return contactId; return contact.getId();
} }
String getName() { String getName() {
return name; return contact.getName();
} }
String getSubject() { String getSubject() {
@@ -44,11 +42,7 @@ class ConversationListItem {
return timestamp; return timestamp;
} }
boolean isRead() { int getUnreadCount() {
return read; return unread;
}
int getLength() {
return length;
} }
} }

View File

@@ -1,19 +0,0 @@
package net.sf.briar.android.messages;
import java.util.Comparator;
import net.sf.briar.api.db.PrivateMessageHeader;
class DescendingHeaderComparator implements Comparator<PrivateMessageHeader> {
static final DescendingHeaderComparator INSTANCE =
new DescendingHeaderComparator();
public int compare(PrivateMessageHeader a, PrivateMessageHeader b) {
// The newest message comes first
long aTime = a.getTimestamp(), bTime = b.getTimestamp();
if(aTime > bTime) return -1;
if(aTime < bTime) return 1;
return 0;
}
}