diff --git a/src/dev.html b/src/dev.html index 547382d..91efa95 100644 --- a/src/dev.html +++ b/src/dev.html @@ -38,6 +38,7 @@ + diff --git a/src/media/js/featured.js b/src/media/js/featured.js new file mode 100644 index 0000000..98cb52a --- /dev/null +++ b/src/media/js/featured.js @@ -0,0 +1,88 @@ +define('featured', ['settings', 'storage', 'log'], function(settings, storage, log) { + + var console = log('featured'); + + return { + + all: function() { + // This will eventually fetch from the JSON database. For now... + var cached = JSON.parse(storage.getItem('all_featured')); + if (!cached) { + cached = [ + { name: 'trenta', weight: 10 }, + { name: 'venti', weight: 8 }, + { name: 'grande', weight: 6 }, + { name: 'tall', weight: 4 }, + { name: 'nessuno', weight: 2 } + ]; + storage.setItem('all_featured', JSON.stringify(cached)); + } + return cached; + }, + + get: function() { + // Retrieve a subset of the featured apps from cache. If the cache has + // expired, regenerate a random subset and return the featured apps. + var cached = JSON.parse(storage.getItem(this.cacheKey())) + if (!cached) { + cached = this.regenerate(); + this.removeYesterday(); + } + return cached; + }, + + cacheKey: function(date) { + // Generate a date-based cache key for the passed Date object (default: today). + if(typeof date === 'undefined') { + date = new Date(); + }; + return 'featured_' + date.toDateString().replace(' ', '_').toLowerCase(); + }, + + regenerate: function() { + // From the pool of featured apps, returned a randomized, weighted selection to be + // displayed on the homepage. Store + console.log('Generating a new selection of featured apps.'); + var all = this.all(); + + // Create a weighted array of all the available items' indexes in `all`. + var weighted_index = []; + for (var i = 0; i < all.length; i++) { + for (var n = 0; n < Math.ceil(all[i].weight); n++) { + weighted_index.push(i); + } + } + + // Choose the appropriate number of random unique indexes from the weighted array. + var chosen_items = []; + while (chosen_items.length < settings.numberFeatured) { + var random = weighted_index[Math.floor(Math.random() * weighted_index.length)]; + if (chosen_items.indexOf(random) === -1) { + chosen_items.push(random); + } + } + + // Map the chosen indexes back to their original objects. + var featured = []; + for (var i = 0; i < chosen_items.length; i++) { + featured.push(all[chosen_items[i]]); + } + + // Store and return the original objects, removing yesterday's cache. + storage.setItem(this.cacheKey(), JSON.stringify(featured)); + return featured; + + }, + + removeYesterday: function() { + // Remove the cache for yesterday. + console.log('Removing yesterday\'s selection of featured apps.'); + var date = new Date(); + date.setDate(date.getDate() - 1); + var key = this.cacheKey(date); + storage.removeItem(key); + } + + }; + +}); diff --git a/src/media/js/settings.js b/src/media/js/settings.js index fdb5f25..7ba03ec 100644 --- a/src/media/js/settings.js +++ b/src/media/js/settings.js @@ -11,7 +11,10 @@ define('settings', ['settings_local', 'utils'], function(settings_local, utils) // How often (in milliseconds) to check if we have queued apps to install // when we return online. - offlineInterval: 1000 + offlineInterval: 1000, + + // Number of apps featured on the homepage. + numberFeatured: 6 }); // The version number for `localStorage` data. Bump when the schema for