diff --git a/src/js/dao/email-dao.js b/src/js/dao/email-dao.js index 0b803f0..dd8f32e 100644 --- a/src/js/dao/email-dao.js +++ b/src/js/dao/email-dao.js @@ -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) { diff --git a/src/js/dao/lawnchair-dao.js b/src/js/dao/lawnchair-dao.js index 5280b8d..741276c 100644 --- a/src/js/dao/lawnchair-dao.js +++ b/src/js/dao/lawnchair-dao.js @@ -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); - }); + }); + }); }); }; diff --git a/src/js/view/messagelist-view.js b/src/js/view/messagelist-view.js index ad16d5a..1ba535d 100644 --- a/src/js/view/messagelist-view.js +++ b/src/js/view/messagelist-view.js @@ -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); diff --git a/test/test-data.js b/test/test-data.js index 656654d..0325382 100644 --- a/test/test-data.js +++ b/test/test-data.js @@ -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 }); diff --git a/test/unit/email-dao-test.js b/test/unit/email-dao-test.js index 6100d41..63e65a3 100644 --- a/test/unit/email-dao-test.js +++ b/test/unit/email-dao-test.js @@ -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(); });