Skip to content

Commit

Permalink
Promisify function for getting apps list
Browse files Browse the repository at this point in the history
[Issue] #1432
[Problem] Function getting apps list id modifying global variable.
It is a problem for future changes in order to resolve #1432 issue.
[Solution] Promisify function.

Signed-off-by: Lukasz Slachciak <l.slachciak@samsung.com>
  • Loading branch information
lmslachciak committed Sep 23, 2020
1 parent b60a4c6 commit b5d7030
Showing 1 changed file with 71 additions and 72 deletions.
143 changes: 71 additions & 72 deletions examples/mobile/HomeApp/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,82 +9,76 @@ import Storage from "./clipping-storage.js";
appsList = [];

const defaultList = [
{
"appID": "vUf39tzQ3s.UIComponents",
"isInstalled": true,
"isActive": true,
"webClipsList": [
{
url: "webclip/apps-on-tv",
isSelected: false
},
{
url: "webclip/latest-news",
isSelected: false
}
]
},
{
"appID": "vUf39tzQ3t.UIComponents",
"isInstalled": true,
"isActive": false,
"webClipsList": [
{
url: "webclip/now-on-tv",
isSelected: false
},
{
url: "webclip/restaurant",
isSelected: true
}
]
},
{
"appID": "vUf39tzQ3r.UIComponents",
"isInstalled": false,
"isActive": false,
"webClipsList": [
{
url: "webclip/tv-remote-control",
isSelected: true
},
{
url: "webclip/weather",
isSelected: true
}
]
}
];
{
"appID": "vUf39tzQ3s.UIComponents",
"isInstalled": true,
"isActive": true,
"webClipsList": [
{
url: "webclip/apps-on-tv",
isSelected: false
},
{
url: "webclip/latest-news",
isSelected: false
}
]
},
{
"appID": "vUf39tzQ3t.UIComponents",
"isInstalled": true,
"isActive": false,
"webClipsList": [
{
url: "webclip/now-on-tv",
isSelected: false
},
{
url: "webclip/restaurant",
isSelected: true
}
]
},
{
"appID": "vUf39tzQ3r.UIComponents",
"isInstalled": false,
"isActive": false,
"webClipsList": [
{
url: "webclip/tv-remote-control",
isSelected: true
},
{
url: "webclip/weather",
isSelected: true
}
]
}
],
getAppsList = new Promise((resolve) => {
const requestURL = "api/appslist";

fetch(requestURL)
.then((response) => response.json())
.then((data) => {
data.forEach((app) => {
app.webClipsList.forEach((webclip) => {
webclip.isSelected = false;
});
});
resolve(data);
})
.catch(() => {
// use default apps list if sth wrong
resolve(defaultList);
})
});


function changeTheme(event) {
tau.theme.setTheme(event.target.value);
}

function loadWebClipList() {
const requestURL = "api/appslist";

fetch(requestURL)
.then((response) => response.json())
.then((data) => {
appsList = data;
appsList.forEach((app) => {
app.webClipsList.forEach((webclip) => {
webclip.isSelected = false;
});
});
})
.catch(() => {
// use default apps list if sth wrong
appsList = defaultList;
})
.finally(() => {

storage.refreshStorage(Storage.elements.APPSLIST, appsList);

updateWebClipsUI();
updateWebClipListPopup();
});
}

function onPopupSubmit() {
appsList.forEach(function (app) {
Expand Down Expand Up @@ -223,7 +217,12 @@ import Storage from "./clipping-storage.js";
//TODO: all webclips url should checked if they are valid

if (!appsList.length) {
loadWebClipList();
getAppsList((apps) => {
appsList = apps;
updateWebClipsUI();
updateWebClipListPopup();
storage.refreshStorage(Storage.elements.APPSLIST, apps);
});
} else {
updateWebClipsUI();
updateWebClipListPopup();
Expand Down

0 comments on commit b5d7030

Please sign in to comment.