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

Commit

Permalink
Merge pull request #8040 from lodr/bug-838629-countdown
Browse files Browse the repository at this point in the history
Bug 838629 - Enabling countdown for top up with code
Cause r+
  • Loading branch information
delapuente committed Feb 14, 2013
2 parents b4a08eb + de70cb1 commit f6f9084
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
6 changes: 6 additions & 0 deletions apps/costcontrol/js/common.js
Expand Up @@ -60,6 +60,12 @@ function setNextReset(when, callback) {
window.setNextReset(when, callback);
}

function getTopUpTimeout(callback) {
var proxy = document.getElementById('message-handler');
return proxy ? proxy.contentWindow.getTopUpTimeout(callback) :
window.getTopUpTimeout(callback);
}

// Next automatic reset date based on user preferences
function updateNextReset(trackingPeriod, value, callback) {
if (trackingPeriod === 'never') {
Expand Down
29 changes: 29 additions & 0 deletions apps/costcontrol/js/message_handler.js
Expand Up @@ -82,6 +82,35 @@
}
window.setNextReset = setNextReset;

function getTopUpTimeout(callback) {
ConfigManager.requestSettings(function _onSettings(settings) {
var request = navigator.mozAlarms.getAll();
request.onsuccess = function (e) {
var alarms = e.target.result;
var length = alarms.length;
if (!length) {
callback(null);
return;
}

var refId = settings.waitingForTopUp;
var index = 0, alarm, found = false;
while (index < length && !found) {
alarm = alarms[index];
found = (alarm.id === refId);
index++;
}
if (found) {
debug('TopUp timeout found:', alarm.date);
callback(alarm.date);
} else {
callback(null);
}
};
});
}
window.getTopUpTimeout = getTopUpTimeout;

// Update the nextResetAlarm and nextReset values and request for
// synchronization.
function updateResetAttributes(alarmId, date, callback) {
Expand Down
40 changes: 36 additions & 4 deletions apps/costcontrol/js/views/balance.js
Expand Up @@ -13,7 +13,7 @@ var BalanceTab = (function() {
'use strict';

var view, updateButton;
var topUpUSSD, topUp, topUpDialog, topUpCodeInput, sendCode;
var topUpUSSD, topUp, topUpDialog, topUpCodeInput, sendCode, countdownSpan;
var costcontrol, tabmanager, vmanager, initialized;

function setupTab(tmgr, vmgr) {
Expand All @@ -34,6 +34,7 @@ var BalanceTab = (function() {
topUpUSSD = document.getElementById('balance-tab-topup-ussd-button');
topUp = document.getElementById('balance-tab-topup-button');
topUpCodeInput = document.getElementById('topup-code-input');
countdownSpan = document.getElementById('top-up-countdown');

window.addEventListener('localized', localize);

Expand Down Expand Up @@ -97,6 +98,8 @@ var BalanceTab = (function() {
function updateWhenVisible() {
if (!document.mozHidden && initialized) {
updateUI();
} else {
clearInterval(topUpCountdown);
}
}

Expand Down Expand Up @@ -151,8 +154,9 @@ var BalanceTab = (function() {
// On tapping Top Up with code
function topUpWithCode(lastWasError) {
vmanager.changeViewTo('topup-dialog');
if (lastWasError)
if (lastWasError) {
setTopUpMode('incorrect_code');
}

topUpCodeInput.focus();
}
Expand Down Expand Up @@ -228,7 +232,7 @@ var BalanceTab = (function() {
function updateUI(force) {
ConfigManager.requestSettings(function _onSettings(settings) {

// TODO: restore the clock
resetTopUpCountdown();
updateBalance(settings.lastBalance,
settings.lowLimit && settings.lowLimitThreshold);

Expand Down Expand Up @@ -346,14 +350,42 @@ var BalanceTab = (function() {
var countdownArea = document.getElementById('cost-control-topup-countdown');
isShown = (mode === 'in_progress');
countdownArea.setAttribute('aria-hidden', !isShown);
// TODO: Set the clock
if (isShown) {
resetTopUpCountdown();
}

// Messages in error message area
if (mode === 'topup_timeout') {
setError(mode);
}
}

var topUpCountdown, countdown;
function resetTopUpCountdown() {
getTopUpTimeout(function (timeout) {
if (!timeout) {
return;
}
countdown = Math.floor((timeout.getTime() - Date.now())/1000);
if (countdown < 0) {
return;
}
clearInterval(topUpCountdown);
topUpCountdown = setInterval(function _updateCountdown() {
var minutes = Math.floor(countdown / 60);
var seconds = (countdown % 60).toFixed(0);
var padding = '';
if (seconds < 10) {
padding = '0';
}
countdownSpan.textContent = (minutes + ' : ' + padding + seconds);
if (countdown > 0) {
countdown -= 1;
}
}, 1000);
})
}

var ERRORS = {
'airplane_mode': { priority: 1, string: 'airplane-mode-error-message' },
'no_service': { priority: 2, string: 'no-coverage-error-message' },
Expand Down

0 comments on commit f6f9084

Please sign in to comment.