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

Commit

Permalink
Bug 1038578 - [Collection App] Icons are not updated after installing…
Browse files Browse the repository at this point in the history
… hosted apps from marketplace
  • Loading branch information
amirnissim authored and KevinGrandon committed Jul 29, 2014
1 parent 6cbcf69 commit 9bb159a
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 91 deletions.
46 changes: 4 additions & 42 deletions apps/collection/js/add_to_collection.js
Expand Up @@ -3,10 +3,8 @@
/* global BaseCollection */
/* global CollectionIcon */
/* global CollectionsDatabase */
/* global Common */
/* global eme */
/* global HomeIcons */
/* global PinnedHomeIcon */

(function(exports) {

Expand All @@ -32,49 +30,13 @@

CollectionsDatabase.get(event.data.collectionId).then(fresh => {
var collection = BaseCollection.create(fresh);
var newPinned = new PinnedHomeIcon(event.data.identifier);

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++) {
if (collection.pinned[i].identifier === newPinned.identifier) {
collection.pinned.splice(i, 1);
break;
}
}

// If we don't have webicons, then we have likely never fetched this
//collection. Make a call to the server to fetch the apps.
if (!collection.webicons.length) {
var options = collection.categoryId ?
{categoryId: collection.categoryId} :
{query: collection.query};

eme.init()
.then(() => eme.api.Apps.search(options))
.then((response) => {
collection.addWebResults(response.response.apps);
})
.then(() => {
Common.getBackground(collection, grid.maxIconSize)
.then((bgObject) => {
collection.background = bgObject;
this.pinAndSave(newPinned, collection);
});
});
} else {
this.pinAndSave(newPinned, collection);
}
eme.init()
.then(() => HomeIcons.init())
.then(() => {
collection.dropHomeIcon(event.data.identifier);
});
});
},

pinAndSave: function(newPinned, collection) {
collection.pinned.unshift(newPinned);
collection.renderIcon().then(() => {
collection.save();
});
}
};

Expand Down
9 changes: 0 additions & 9 deletions apps/collection/js/collection_icon.js
Expand Up @@ -31,9 +31,6 @@

/* constants */

// number of app icons in the collection icon
const numAppIcons = 3;

// darkness of to side icons
const sideIconDarken = 0.65;

Expand Down Expand Up @@ -102,12 +99,6 @@
this.bgSrc = config.bgSrc || null;
}

Object.defineProperty(CollectionIcon, 'numAppIcons', {
get: function get() {
return numAppIcons;
}
});

CollectionIcon.init = function init(maxIconSize) {
// measurements are based on a 60px icon (cf. bug 965711)
scale = maxIconSize/60;
Expand Down
51 changes: 48 additions & 3 deletions apps/collection/js/common.js
Expand Up @@ -20,11 +20,16 @@
const mozBgUrl =
'http://fxos.cdn.mozilla.net/collection/background/{categoryId}' + suffix;

const APPS_IN_ICON = 3;

function Common() {}

Common.prototype = {

APPS_IN_ICON: APPS_IN_ICON,

chooseBackgroundRatio: chooseBackgroundRatio,

b64toBlob: function b64toBlob(b64) {
return new Promise((resolve, reject) => {
var img = new Image();
Expand All @@ -46,8 +51,6 @@
});
},

chooseBackgroundRatio: chooseBackgroundRatio,

// TODO
// add support for 'size' parameter (like getEmeBackground has) to fetch
// smaller backgrounds when the full size image is not required
Expand All @@ -59,7 +62,7 @@

var url = mozBgUrl.replace('{categoryId}', collection.categoryId);
var xhr = new XMLHttpRequest({mozSystem: true});
xhr.open('GET', url);
xhr.open('GET', url, true);
xhr.responseType = 'blob';

return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -153,6 +156,48 @@
.catch(function (e) {
eme.log('getBackground', 'failed', e);
});
},

getWebIcons: function getWebIcons(collection) {
var options =
(collection.categoryId) ? {categoryId: collection.categoryId} :
{query: collection.query};

options.limit = APPS_IN_ICON;

return eme.api.Apps.search(options).then(response => {
var webicons =
response.response.apps.slice(0, APPS_IN_ICON).map(app => app.icon);

return webicons;
});
},

/**
* prepares the assets needed for rendering a collection's icon:
* 1. background
* 2. web icons
*
* returns a promise resolved when all assets requests are done and
* updates the collection instance but not the db
*
*/
prepareAssets: function prepareAssets(collection) {
var ready = Promise.resolve(null);

var backgroundPromise = collection.backgroundReady ?
ready : this.getBackground(collection);

var iconsPromise = collection.iconsReady ?
ready : this.getWebIcons(collection);

return Promise.all([iconsPromise, backgroundPromise])
.then((results) => {
// results are null if not fetched
collection.webicons = results[0] || collection.webicons;
collection.background = results[1] || collection.background;
return collection;
});
}
};

Expand Down
6 changes: 3 additions & 3 deletions apps/collection/js/create_collection.js
Expand Up @@ -32,7 +32,7 @@
var maxIconSize = activity.source.data.maxIconSize;

CollectionIcon.init(maxIconSize);
var numAppIcons = CollectionIcon.numAppIcons;
const APPS_IN_ICON = Common.APPS_IN_ICON;

cancel.addEventListener('click', function() {
// TODO request should always have an 'abort' method
Expand Down Expand Up @@ -75,10 +75,10 @@
// collection from custom query
// we make another request to get web app icons
dataReady = new Promise(function getIcons(resolve) {
eme.api.Apps.search({query: selected, limit: numAppIcons})
eme.api.Apps.search({query: selected, limit: APPS_IN_ICON})
.then(function success(response) {
var webicons =
response.response.apps.slice(0,numAppIcons).map(
response.response.apps.slice(0,APPS_IN_ICON).map(
function each(app) {
return app.icon;
});
Expand Down
2 changes: 1 addition & 1 deletion apps/collection/js/native_info.js
Expand Up @@ -10,7 +10,7 @@
const SETUP_KEY = 'NativeInfo-setup';

function onerror(e) {
eme.error('NativeInfo error', e.name || e.message || e);
eme.error('NativeInfo error', e && (e.name || e.message || e));
}

// Provides information about native apps in order to match them against
Expand Down

0 comments on commit 9bb159a

Please sign in to comment.