Add notification manager to home page

Framework7
Skylar Ittner 6 years ago
parent 6f79fb7b2a
commit 2c3092ecb4

@ -16,7 +16,7 @@ function loadHomePage(reload) {
}, function (data) { }, function (data) {
if (data.status === 'OK') { if (data.status === 'OK') {
if (!reload) { if (!reload) {
loadingProgressDialog.setProgress(80); loadingProgressDialog.setProgress(70);
} }
var appcards = []; var appcards = [];
Object.keys(data.apps).forEach(function (k) { Object.keys(data.apps).forEach(function (k) {
@ -37,30 +37,53 @@ function loadHomePage(reload) {
color: app.card.color color: app.card.color
}); });
}); });
if (reload) { if (!reload) {
// Make it seem like it's doing something on fast connections loadingProgressDialog.setProgress(80);
setTimeout(function () { loadingProgressDialog.setText('Notifications');
app.ptr.done(); }
getNotifications(function (notifs) {
notifications = [];
if (notifs != false) {
Object.keys(notifs).forEach(function (k) {
var notif = notifs[k];
if (!notif.seen) {
notifications.push(notif);
}
});
}
if (notifications == []) {
notifications = false;
}
if (reload) {
// Make it seem like it's doing something on fast connections
setTimeout(function () {
app.ptr.done();
router.navigate("/home", {
context: {
appcards: appcards,
notifications: notifications,
unreadnotifications: (notifications != false && notifications != [])
},
reloadCurrent: true
});
}, 500);
} else {
loadingProgressDialog.setText('Home');
loadingProgressDialog.setProgress(90);
router.navigate("/home", { router.navigate("/home", {
context: { context: {
appcards: appcards appcards: appcards,
}, notifications: notifications,
reloadCurrent: true unreadnotifications: (notifications != false && notifications != [])
}
}); });
}, 500); $(".view-main").on("click", "#applist .applist-item", function () {
} else { launchapp($(this).data("api"), $(this).data("url"), $(this).data("icon"), $(this).data("title"));
loadingProgressDialog.setProgress(90); });
router.navigate("/home", { loadingProgressDialog.setProgress(100);
context: { loadingProgressDialog.close();
appcards: appcards }
} });
});
$(".view-main").on("click", "#applist .applist-item", function () {
launchapp($(this).data("api"), $(this).data("url"), $(this).data("icon"), $(this).data("title"));
});
loadingProgressDialog.setProgress(100);
loadingProgressDialog.close();
}
} else { } else {
if (reload) { if (reload) {
app.ptr.done(); app.ptr.done();
@ -83,4 +106,9 @@ function loadHomePage(reload) {
$(".view-main").on("ptr:refresh", ".ptr-content", function () { $(".view-main").on("ptr:refresh", ".ptr-content", function () {
loadHomePage(true); loadHomePage(true);
});
$(".view-main").on("swipeout:deleted", ".notificationlist-item", function () {
var id = $(this).data("id");
deleteNotification(id);
}); });

@ -4,8 +4,8 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/ */
function getNotifications(callback) {
function displayNotifications(callback) { var notifications = [];
$.post(localStorage.getItem("syncurl"), { $.post(localStorage.getItem("syncurl"), {
username: localStorage.getItem("username"), username: localStorage.getItem("username"),
key: localStorage.getItem("key"), key: localStorage.getItem("key"),
@ -14,30 +14,76 @@ function displayNotifications(callback) {
if (data.status === 'OK') { if (data.status === 'OK') {
for (var i = 0; i < data.notifications.length; i++) { for (var i = 0; i < data.notifications.length; i++) {
var n = data.notifications[i]; var n = data.notifications[i];
notifications.push({
id: n.id,
title: n.title,
text: n.content,
sensitive: n.sensitive,
seen: n.seen
});
}
callback(notifications);
return;
}
if (typeof callback == 'function') {
callback(false);
}
}, "json").fail(function () {
if (typeof callback == 'function') {
callback(false);
}
});
}
function displayNotifications(callback) {
getNotifications(function (notifications) {
if (notifications != false) {
for (var i = 0; i < notifications.length; i++) {
var n = notifications[i];
if (n.seen || shown_notifications.includes(n.id)) { if (n.seen || shown_notifications.includes(n.id)) {
continue; continue;
} }
cordova.plugins.notification.local.schedule([{ cordova.plugins.notification.local.schedule([{
title: n.title, title: n.title,
text: n.content, text: n.text,
actions: [ // actions: [
{id: 'mark_read', title: "Mark Read"} // {id: 'mark_read', title: "Mark Read"}
], // ],
id: n.id, id: n.id,
lockscreen: !n.sensitive lockscreen: !n.sensitive
}]); }]);
shown_notifications.push(n.id); shown_notifications.push(n.id);
} }
if (typeof callback == 'function') {
callback();
}
return;
} }
if (typeof callback == 'function') { if (typeof callback == 'function') {
callback(); callback();
} }
}, "json").fail(function () { });
if (typeof callback == 'function') { }
callback();
} function deleteNotification(id) {
$.post(localStorage.getItem("syncurl"), {
username: localStorage.getItem("username"),
key: localStorage.getItem("key"),
password: localStorage.getItem("password"),
action: "deletenotification",
id: id
});
}
function readNotification(id) {
$.post(localStorage.getItem("syncurl"), {
username: localStorage.getItem("username"),
key: localStorage.getItem("key"),
password: localStorage.getItem("password"),
action: "readnotification",
id: id
}); });
} }

@ -25,6 +25,38 @@
<div class="ptr-arrow"></div> <div class="ptr-arrow"></div>
</div> </div>
{{#if unreadnotifications}}
<div class="card">
<div class="card-header">Notifications</div>
<div class="card-content">
<div class="list media-list">
<ul id="notificationlist">
{{#each notifications}}
<li class="swipeout notificationlist-item" data-id="{{id}}">
<div class="swipeout-content">
<div class="item-content">
<div class="item-inner">
<div class="item-title-row">
<div class="item-title">{{title}}</div>
</div>
<div class="item-text">{{text}}</div>
</div>
</div>
</div>
<div class="swipeout-actions-left">
<a href="#" class="swipeout-delete">Delete</a>
</div>
<div class="swipeout-actions-right">
<a href="#" class="swipeout-delete">Delete</a>
</div>
</li>
{{/each}}
</ul>
</div>
</div>
</div>
{{/if}}
<div class="card"> <div class="card">
<div class="card-header">Apps</div> <div class="card-header">Apps</div>
<div class="card-content"> <div class="card-content">

Loading…
Cancel
Save