sort emails by sent date when fetching them from the data-store

This commit is contained in:
Tankred Hase 2013-03-21 20:18:03 +01:00
parent 34a84a6b9d
commit 5b98cd692a
5 changed files with 41 additions and 21 deletions

View File

@ -60,7 +60,10 @@ app.dao.EmailDAO = function(_, crypto, devicestorage, cloudstorage) {
// get items from storage
devicestorage.listItems('email_' + folderName ,offset ,num, function(decryptedList) {
// parse to backbone model collection
collection = new app.model.EmailCollection(decryptedList);
collection = new app.model.EmailCollection(decryptedList);
// reverse order so that mails with the most recent dat will be displayed first
collection.models.reverse();
// cache collection in folder memory
if (decryptedList.length > 0) {

View File

@ -58,19 +58,7 @@ app.dao.LawnchairDAO = function(window) {
if (keys[i].indexOf(type) === 0) {
matchingKeys.push(keys[i]);
}
}
// if num is null, list all items
num = (num !== null) ? num : matchingKeys.length;
// set window of items to fetch
if (offset + num < matchingKeys.length) {
matchingKeys = matchingKeys.splice(matchingKeys.length - offset - num, num);
} else if (offset + num >= matchingKeys.length && offset < matchingKeys.length) {
matchingKeys = matchingKeys.splice(0, matchingKeys.length - offset);
} else {
matchingKeys = [];
}
}
// return if there are no matching keys
if (matchingKeys.length === 0) {
@ -78,14 +66,37 @@ app.dao.LawnchairDAO = function(window) {
return;
}
// get matching objects from data-store
// fetch all items from data-store with matching key
self.get(matchingKeys, function(matchingList) {
for (i = 0; i < matchingList.length; i++) {
list.push(matchingList[i].object);
}
// sort items by date
if (list[0].sentDate) {
list = _.sortBy(list, function(item) {
var parts = item.sentDate.match(/(\d+)/g);
var date = new Date(parts[0], parts[1] - 1, parts[2], parts[3], parts[4], parts[5]);
return date.getTime();
});
}
// if num is null, list all items
num = (num !== null) ? num : list.length;
// set window of items to fetch
if (offset + num < list.length) {
list = list.splice(list.length - offset - num, num);
} else if (offset + num >= list.length && offset < list.length) {
list = list.splice(0, list.length - offset);
} else {
list = [];
}
// return only the interval between offset and num
callback(list);
});
});
});
});
};

View File

@ -53,10 +53,11 @@ app.view.MessageListView = Backbone.View.extend({
listItemArgs;
$.mobile.loading('show', { text: 'decrypting...', textVisible: true });
this.dao.listItems(this.folder, 0, 20, function(collection) {
this.dao.listItems(this.folder, 0, 10, function(collection) {
// clear list
list.html('');
// append items to list
_.each(collection.models, function(email) {
listItemArgs = {account: self.options.account, folder: self.folder, model: email};
list.append(new app.view.MessageListItemView(listItemArgs).render().el);

View File

@ -13,7 +13,7 @@ var TestData = function() {
from:'john@from.com',
to:['jack@to.com'],
subject: 'Important stuff ' + i,
sentDate: '15:00',
sentDate: (1900 + i) + '-03-13 18:17:53',
body: bigAssString
});

View File

@ -20,7 +20,7 @@ asyncTest("Init", 2, function() {
emaildao_test.emailDao = new app.dao.EmailDAO(_, emaildao_test.crypto, emaildao_test.storage, cloudstorageStub);
// generate test data
emaildao_test.list = new TestData().getEmailCollection(10);
emaildao_test.list = new TestData().getEmailCollection(100);
var account = new app.model.Account({
emailAddress: emaildao_test.user,
@ -44,6 +44,11 @@ asyncTest("Persist test emails", 2, function() {
emaildao_test.crypto.aesEncryptListForUser(emaildao_test.list.toJSON(), function(encryptedList) {
equal(encryptedList.length, emaildao_test.list.length, 'Encrypt list');
// add sent date to encrypted items
for (var i = 0; i < encryptedList.length; i++) {
encryptedList[i].sentDate = emaildao_test.list.at(i).get('sentDate');
}
emaildao_test.storage.storeEcryptedList(encryptedList, 'email_inbox', function() {
ok(true, 'Store encrypted list');
@ -55,9 +60,9 @@ asyncTest("Persist test emails", 2, function() {
asyncTest("List Email models", 1, function() {
emaildao_test.emailDao.listItems('inbox', 0, emaildao_test.list.length, function(collection) {
var gotten = collection.toJSON(),
reference = emaildao_test.list.toJSON();
reference = emaildao_test.list.toJSON().reverse();
deepEqual(gotten, reference, 'Collection length');
deepEqual(gotten, reference, 'Compare collection');
start();
});