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 #10321 from borjasalguero/message_activity_proposal
Browse files Browse the repository at this point in the history
Bug 880115 - [SMS / MMS] Activity handling does nothing when triggering ...
  • Loading branch information
Francisco Borja Salguero Castellano committed Jun 26, 2013
2 parents dea3178 + de6e0e4 commit 5026dfa
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 173 deletions.
68 changes: 36 additions & 32 deletions apps/sms/js/activity_handler.js
Expand Up @@ -4,6 +4,7 @@
'use strict';

var ActivityHandler = {
isLocked: false,
init: function() {
window.navigator.mozSetMessageHandler('activity', this.global.bind(this));

Expand Down Expand Up @@ -34,12 +35,12 @@ var ActivityHandler = {
_handlers: {
'new': function newHandler(activity) {

// XXX This lock is about https://github.com/mozilla-b2g/gaia/issues/5405
if (MessageManager.activity.isLocked) {
// This lock is for avoiding several calls at the same time.
if (this.isLocked) {
return;
}

MessageManager.activity.isLocked = true;
this.isLocked = true;

var number = activity.source.data.number;
var body = activity.source.data.body;
Expand Down Expand Up @@ -126,11 +127,12 @@ var ActivityHandler = {
// The unsent confirmation dialog provides 2 options: edit and discard
// discard: clear the message user typed
// edit: continue to edit the unsent message and ignore the activity
displayUnsentConfirmation: function ah_displayUnsentConfirmtion() {
displayUnsentConfirmation: function ah_displayUnsentConfirmtion(activity) {
var _ = navigator.mozL10n.get;
var msgDiv = document.createElement('div');
msgDiv.innerHTML = '<h1>' + _('unsent-message-title') +
'</h1><p>' + _('unsent-message-description') + '</p>';
var self = this;
var options = new OptionMenu({
type: 'confirm',
section: msgDiv,
Expand All @@ -139,25 +141,31 @@ var ActivityHandler = {
method: function editOptionMethod() {
// it already in message app, we don't need to do anything but
// clearing activity variables in MessageManager.
MessageManager.activity.body = null;
MessageManager.activity.number = null;
MessageManager.activity.contact = null;
MessageManager.activity = null;
}
},
{
name: _('unsent-message-option-discard'),
method: function discardOptionMethod() {
if (window.location.hash === '#new') {
setTimeout(MessageManager.onHashChange.bind(MessageManager));
} else {
window.location.hash = '#new';
}
}
method: this.launchComposer.bind(this),
params: [activity]
}]
});
options.show();
},
// prepare new message and show new message UI

// Launch the UI properly taking into account the hash
launchComposer: function ah_launchComposer(activity) {
if (location.hash === '#new') {
MessageManager.launchComposer(activity);
} else {
// Move to new message
MessageManager.activity = activity;
window.location.hash = '#new';
}
},

// Check if we want to go directly to the composer or if we
// want to keep the previously typed text
triggerNewMessage: function ah_triggerNewMessage(body, number, contact) {
/**
* case 1: hash === #new
Expand All @@ -166,19 +174,17 @@ var ActivityHandler = {
* check compose is empty or show dialog, and change hash to #new
* case 3: others, change hash to #new
*/
MessageManager.activity.body = body || null;
MessageManager.activity.number = number || null;
MessageManager.activity.contact = contact || null;
var activity = {
body: body || null,
number: number || null,
contact: contact || null
};

if (Compose.isEmpty()) {
if (location.hash === '#new') {
setTimeout(MessageManager.onHashChange.bind(MessageManager));
} else {
window.location.hash = '#new';
}
this.launchComposer(activity);
} else {
// ask user how should we do
ActivityHandler.displayUnsentConfirmation();
ActivityHandler.displayUnsentConfirmation(activity);
}
},
// Deliver the user to the correct view
Expand Down Expand Up @@ -209,7 +215,7 @@ var ActivityHandler = {
if (!message) {
return;
}

this.isLocked = false;
var threadId = message.threadId ? message.threadId : null;
var body = message.body ? Utils.escapeHTML(message.body) : '';
var number = message.number ? message.number : '';
Expand All @@ -218,31 +224,29 @@ var ActivityHandler = {

var showAction = function act_action() {
// If we only have a body, just trigger a new message.
var locationHash = window.location.hash;
if (!threadId) {
ActivityHandler.triggerNewMessage(body, number, contact);
return;
}
var locationHash = window.location.hash;

switch (locationHash) {
case '#thread-list':
case '#new':
window.location.hash = threadHash;
MessageManager.activity.isLocked = false;
break;
default:
if (locationHash.indexOf('#thread=') !== -1) {
// Don't switch back to thread list if we're
// already displaying the requested threadId.
if (locationHash === threadHash) {
MessageManager.activity.isLocked = false;
} else {
MessageManager.activity.threadId = threadId;
if (locationHash !== threadHash) {
MessageManager.activity = {
threadId: threadId
};
window.location.hash = '#thread-list';
}
} else {
window.location.hash = threadHash;
MessageManager.activity.isLocked = false;
}
break;
}
Expand Down
123 changes: 57 additions & 66 deletions apps/sms/js/message_manager.js
Expand Up @@ -5,14 +5,7 @@

var MessageManager = {

activity: {
body: null,
number: null,
contact: null,
recipients: null,
threadId: null,
isLocked: false
},
activity: null,

init: function mm_init(callback) {
if (this.initialized) {
Expand All @@ -32,7 +25,12 @@ var MessageManager = {
window.addEventListener('hashchange', this.onHashChange.bind(this));
document.addEventListener('mozvisibilitychange',
this.onVisibilityChange.bind(this));

// Initialize DOM elements which will be used in this code
[
'main-wrapper', 'thread-messages'
].forEach(function(id) {
this[Utils.camelCase(id)] = document.getElementById(id);
}, this);
// Callback if needed
if (typeof callback === 'function') {
callback();
Expand Down Expand Up @@ -165,29 +163,27 @@ var MessageManager = {
},

slide: function mm_slide(direction, callback) {
var mainWrapper = document.getElementById('main-wrapper');

// If no sliding is necessary, schedule the callback to be invoked as soon
// as possible (maintaining the asynchronous API of this method)
if (mainWrapper.dataset.position === direction) {
if (this.mainWrapper.dataset.position === direction) {
setTimeout(callback);
return;
}

mainWrapper.classList.add('peek');
mainWrapper.dataset.position = direction;

this.mainWrapper.classList.add('peek');
this.mainWrapper.dataset.position = direction;
var self = this;
// We have 2 panels, so we get 2 transitionend for each step
var trEndCount = 0;
mainWrapper.addEventListener('transitionend', function trWait() {
this.mainWrapper.addEventListener('transitionend', function trWait() {
trEndCount++;

switch (trEndCount) {
case 2:
mainWrapper.classList.remove('peek');
self.mainWrapper.classList.remove('peek');
break;
case 4:
mainWrapper.removeEventListener('transitionend', trWait);
self.mainWrapper.removeEventListener('transitionend', trWait);
if (callback) {
callback();
}
Expand All @@ -196,11 +192,40 @@ var MessageManager = {
});
},

onHashChange: function mm_onHashChange(e) {
var mainWrapper = document.getElementById('main-wrapper');
var threadMessages = document.getElementById('thread-messages');
var recipient;
launchComposer: function mm_openComposer(activity) {
// Do we have to handle a pending activity?
ThreadUI.cleanFields(true);
Compose.clear();
this.threadMessages.classList.add('new');

var self = this;
MessageManager.slide('left', function() {
ThreadUI.initRecipients();
if (!activity) {
return;
}

if (activity.number || activity.contact) {
var recipient = activity.contact || {
number: activity.number,
source: 'manual'
};

ThreadUI.recipients.add(recipient);
}

// If the message has a body, use it to populate the input field.
if (activity.body) {
ThreadUI.setMessageBody(
activity.body
);
}
// Clean activity object
self.activity = null;
});
},

onHashChange: function mm_onHashChange(e) {
// Group Participants should never persist any hash changes
ThreadUI.groupView.reset();

Expand All @@ -211,60 +236,26 @@ var MessageManager = {

switch (window.location.hash) {
case '#new':

ThreadUI.cleanFields(true);
threadMessages.classList.add('new');

MessageManager.activity.recipients = null;

MessageManager.slide('left', function() {
ThreadUI.initRecipients();

if (MessageManager.activity.number ||
MessageManager.activity.contact) {

recipient = MessageManager.activity.contact || {
number: MessageManager.activity.number,
source: 'manual'
};

ThreadUI.recipients.add(recipient);

MessageManager.activity.number = null;
MessageManager.activity.contact = null;
// already have recipients, move focus to input
ThreadUI.input.focus();
}

// If the message has a body, use it to popuplate the input field.
if (MessageManager.activity.body) {
ThreadUI.setMessageBody(
MessageManager.activity.body
);
MessageManager.activity.body = null;
}
});
this.launchComposer(this.activity);
break;
case '#thread-list':
ThreadUI.inThread = false;

var self = this;
//Keep the visible button the :last-child
var editButton = document.getElementById('messages-edit-icon');
editButton.parentNode.appendChild(editButton);
if (threadMessages.classList.contains('new')) {
if (this.threadMessages.classList.contains('new')) {
MessageManager.slide('right', function() {
threadMessages.classList.remove('new');
self.threadMessages.classList.remove('new');
});
} else {
// Clear it before sliding.
ThreadUI.container.textContent = '';

var self = this;
MessageManager.slide('right', function() {
if (MessageManager.activity.threadId) {
window.location.hash =
'#thread=' + MessageManager.activity.threadId;
MessageManager.activity.threadId = null;
MessageManager.activity.isLocked = false;
if (self.activity && self.activity.threadId) {
window.location.hash = '#thread=' + self.activity.threadId;
self.activity = null;
}
});
}
Expand All @@ -280,10 +271,10 @@ var MessageManager = {
filter = new MozSmsFilter();
filter.threadId = threadId;

if (threadMessages.classList.contains('new')) {
if (this.threadMessages.classList.contains('new')) {
// After a message is sent...
//
threadMessages.classList.remove('new');
this.threadMessages.classList.remove('new');

ThreadUI.updateHeaderData(function() {
ThreadUI.renderMessages(filter);
Expand Down
3 changes: 0 additions & 3 deletions apps/sms/js/thread_ui.js
Expand Up @@ -1343,9 +1343,6 @@ var ThreadUI = global.ThreadUI = {

this.updateHeaderData();

// Hold onto the recipients until
MessageManager.activity.recipients = recipients;

// Send the Message
if (messageType === 'sms') {
MessageManager.sendSMS(recipients, content[0]);
Expand Down

0 comments on commit 5026dfa

Please sign in to comment.