Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Bug 1039311 - [Collection] HomeIcons Singleton (refactor)
Browse files Browse the repository at this point in the history
  • Loading branch information
amirnissim authored and KevinGrandon committed Jul 29, 2014
1 parent 746701a commit 6cbcf69
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 54 deletions.
1 change: 1 addition & 0 deletions apps/collection/add_to_collection.html
Expand Up @@ -10,6 +10,7 @@
<script defer src="shared/js/everythingme/device.js"></script>
<script defer src="shared/js/everythingme/api.js"></script>

<script defer src="shared/js/async_storage.js"></script>
<script defer src="shared/js/bookmarks_database.js"></script>
<script defer src="shared/js/collections_database.js"></script>
<script defer src="shared/js/l10n.js"></script>
Expand Down
3 changes: 2 additions & 1 deletion apps/collection/js/add_to_collection.js
Expand Up @@ -5,6 +5,7 @@
/* global CollectionsDatabase */
/* global Common */
/* global eme */
/* global HomeIcons */
/* global PinnedHomeIcon */

(function(exports) {
Expand Down Expand Up @@ -33,7 +34,7 @@
var collection = BaseCollection.create(fresh);
var newPinned = new PinnedHomeIcon(event.data.identifier);

collection.homeIcons.init().then(() => {
HomeIcons.init().then(() => {

// If a record is already pinned, delete it so it appears first.
for (var i = 0, iLen = collection.pinned.length; i < iLen; i++) {
Expand Down
8 changes: 5 additions & 3 deletions apps/collection/js/home_icons.js
Expand Up @@ -15,15 +15,17 @@
var initPromise;

function HomeIcons() {
this.ready = false;
this.gridItemsByIdentifier = {};
this.recordsByManifestUrl = {};
this.recordsByBookmarkUrl = {};
}

HomeIcons.prototype = {
init: function init() {
initPromise = Promise.all([this.collectBookmarkURLs(),
this.collectManifestURLs()]);
initPromise =
Promise.all([this.collectBookmarkURLs(), this.collectManifestURLs()])
.then(() => this.ready = true);

this.init = function noop() {
return initPromise;
Expand Down Expand Up @@ -116,6 +118,6 @@

};

exports.HomeIcons = HomeIcons;
exports.HomeIcons = new HomeIcons();

}(window));
62 changes: 27 additions & 35 deletions apps/collection/js/native_info.js
Expand Up @@ -9,8 +9,6 @@

const SETUP_KEY = 'NativeInfo-setup';

var homeIcons;

function onerror(e) {
eme.error('NativeInfo error', e.name || e.message || e);
}
Expand Down Expand Up @@ -74,16 +72,13 @@
// we are going to traverse all the collections on device
for (var id in collections) {
var collection = collections[id];
collection.homeIcons = homeIcons;
collection = BaseCollection.create(collection);

if (collection.cName) {
var guids = guidsByCname[collection.cName] || [];

if (guids.length) {
// identifiers is directly guids when we process an app
var identifiers = !homeIcons ? guids :
guids.map(homeIcons.getIdentifier.bind(homeIcons));
var identifiers = guids;

eme.log('NativeInfo', identifiers.length, 'matches for',
collection.cName, JSON.stringify(identifiers));
Expand All @@ -100,56 +95,53 @@
return Promise.reject();
}

function unpinFromCollection(collection) {
collection.homeIcons.init().then(function() {
collection.unpin(identifier);
});
}

return CollectionsDatabase.getAll().then(function(collections) {
// we are going to traverse all the collections on device
for (var id in collections) {
unpinFromCollection(BaseCollection.create(collections[id]));
var collection = BaseCollection.create(collections[id]);
collection.unpin(identifier);
}
});
},

// on app install/uninstall
// id should be a guid (manifest or bookmark URL)
processApp: function processApp(action, id) {
if (action === 'install') {
// id should be a guid (manifest or bookmark URL)
homeIcons = new HomeIcons();
return homeIcons.init().then(this.getInfo.bind(this, [id]))
.then(this.addToCollections).catch(onerror);
return HomeIcons.init()
.then(() => this.getInfo([id]))
.then(this.addToCollections)
.catch(onerror);

} else if (action === 'uninstall') {
return this.removeFromCollections(id).catch(onerror);
return HomeIcons.init()
.then(() => this.removeFromCollections(id))
.catch(onerror);
}
},

// on collection install
processCollection: function processCollection(collection) {
return this.collectGuids()
.then(this.getInfo)
.then(function addToCollection(guidsByCname) {
// cName for suggested collections or query for custom collections
var key = collection.cName || collection.query;
var guids = guidsByCname[key] || [];

if (guids.length) {
var identifiers =
guids.map(homeIcons.getIdentifier.bind(homeIcons));
eme.log('NativeInfo', identifiers);
collection.pinHomeIcons(identifiers);
}
});
.then(this.getInfo)
.then(function addToCollection(guidsByCname) {
// cName for suggested collections or query for custom collections
var key = collection.cName || collection.query;
var guids = guidsByCname[key] || [];

if (guids.length) {
var identifiers = guids;
eme.log('NativeInfo', identifiers);
collection.pinHomeIcons(identifiers);
}
});
},

// returns a promise resolved with all guids for all apps and bookmarks
collectGuids: function collectGuids() {
homeIcons = new HomeIcons();
return homeIcons.init().then(function success() {
var manifestURLs = homeIcons.manifestURLs;
var bookmarkURLs = homeIcons.bookmarkURLs;
return HomeIcons.init().then(function success() {
var manifestURLs = HomeIcons.manifestURLs;
var bookmarkURLs = HomeIcons.bookmarkURLs;
eme.log('NativeInfo found', manifestURLs.length, 'apps');
eme.log('NativeInfo found', bookmarkURLs.length, 'bookmarks');

Expand Down
16 changes: 7 additions & 9 deletions apps/collection/js/objects.js
Expand Up @@ -58,12 +58,6 @@
if (window.SearchDedupe) {
this.dedupe = new SearchDedupe();
}

// for rendering pinned homescreen apps/bookmarks
if (window.HomeIcons) {
this.homeIcons = props.homeIcons || new HomeIcons();
this.homeIcons.init();
}
}

BaseCollection.create = function create(data) {
Expand Down Expand Up @@ -200,8 +194,8 @@
var idx = this.pinnedIdentifiers.indexOf(identifier);
if (idx !== -1) {
this.pinned.splice(idx, 1);
eme.log('removed pinned item', identifier);
return this.save();
return this.save()
.then(() => eme.log('removed pinned item', identifier));
}
},

Expand Down Expand Up @@ -262,7 +256,11 @@
toGridObject: function(item) {
var icon;
if (item.type === 'homeIcon') {
icon = this.homeIcons.get(item.identifier);
if (!HomeIcons.ready) {
eme.warn('HomeIcons not ready, pinned apps may not render properly');
}

icon = HomeIcons.get(item.identifier);
} else if (item.type === 'webResult') {
item.features = item.features || {};
item.features.isEditable = false;
Expand Down
8 changes: 4 additions & 4 deletions apps/collection/js/view_apps.js
@@ -1,8 +1,9 @@
'use strict';
/* global CollectionIcon */
/* global CollectionsDatabase */
/* global eme */
/* global HomeIcons */
/* global NativeInfo */
/* global CollectionsDatabase */

(function(exports) {

Expand Down Expand Up @@ -61,9 +62,8 @@
// Update Collection from db
// Render grid for the first time
// Go get web results
NativeInfo.setup()
// Ensure homeIcons are initialized
.then(() => collection.homeIcons.init())
HomeIcons.init()
.then(() => NativeInfo.setup())
.then(() => collection.refresh())
.then(() => collection.render(grid))
.then(() => {
Expand Down
3 changes: 2 additions & 1 deletion apps/collection/synchronize.html
Expand Up @@ -7,6 +7,7 @@

<script defer src="shared/js/bookmarks_database.js"></script>
<script defer src="shared/js/collections_database.js"></script>
<script defer src="shared/js/async_storage.js"></script>

<script defer src="shared/js/l10n.js"></script>
<script defer src="shared/js/url_helper.js"></script>
Expand All @@ -26,7 +27,7 @@
<script defer src="js/objects.js"></script>
<script defer src="js/home_icons.js"></script>
<script defer src="js/native_info.js"></script>

<script defer src="js/synchronize.js"></script>
</head>
<body>
Expand Down
5 changes: 4 additions & 1 deletion build/test/integration/distribution.test.js
Expand Up @@ -262,7 +262,10 @@ suite('Distribution mechanism', function() {
assert.equal(config.grid[0][1].entry_point, 'dialer');
assert.equal(config.grid[0][2].name, 'Messages');
assert.equal(config.grid[0][3].name, 'Marketplace');
assert.equal(config.grid[1][0].name, 'Gallery');
assert.equal(config.grid[1][0].name, 'Social');
assert.equal(config.grid[1][1].name, 'Games');
assert.equal(config.grid[1][2].name, 'Music');
assert.equal(config.grid[2][0].name, 'Gallery');

assert.isTrue(fs.existsSync(path.join(process.cwd(), 'profile',
'svoperapps', 'Twitter')),
Expand Down
4 changes: 4 additions & 0 deletions customization/homescreens.json
Expand Up @@ -5,6 +5,10 @@
["apps", "communications", "dialer"],
["apps", "sms"],
["external-apps", "marketplace.firefox.com"]
], [
["apps/collection/collections", "social"],
["apps/collection/collections", "games"],
["apps/collection/collections", "music"]
], [
["apps", "gallery"],
["apps", "fm"],
Expand Down

0 comments on commit 6cbcf69

Please sign in to comment.