Skip to content

Commit d5c2fe1

Browse files
committed
Use message passing for showing toast objects
This switches the process of showing a toast message in the UI to be done based on a core-signal being fired instead of based on variables changing.
1 parent 0029164 commit d5c2fe1

File tree

7 files changed

+60
-37
lines changed

7 files changed

+60
-37
lines changed

src/generic_ui/polymer/cloud-install.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/// <reference path='../../../../third_party/typings/browser.d.ts' />
33
/// <reference path='../../../../third_party/polymer/polymer.d.ts' />
44

5+
import translator = require('../scripts/translator');
56
import uproxy_core_api = require('../../interfaces/uproxy_core_api');
67
import ui_constants = require('../../interfaces/ui');
78
import user = require('../scripts/user');
@@ -129,10 +130,20 @@ Polymer({
129130
providerName: DEFAULT_PROVIDER
130131
}).then(() => {
131132
this.closeOverlays();
132-
ui.toastMessage = ui.i18n_t('CLOUD_INSTALL_CANCEL_SUCCESS');
133+
this.fire('core-signal', {
134+
name: 'show-toast',
135+
data: {
136+
toastMessage: translator.i18n_t('CLOUD_INSTALL_CANCEL_SUCCESS')
137+
}
138+
});
133139
}).catch((e: Error) => {
134140
this.$.cancelingOverlay.close();
135-
ui.toastMessage = ui.i18n_t('CLOUD_INSTALL_CANCEL_FAILURE');
141+
this.fire('core-signal', {
142+
name: 'show-toast',
143+
data: {
144+
toastMessage: translator.i18n_t('CLOUD_INSTALL_CANCEL_FAILURE')
145+
}
146+
});
136147
});
137148
},
138149
select: function(e: Event, d: Object, input: HTMLInputElement) {

src/generic_ui/polymer/contact.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,12 @@ Polymer({
138138
userId: this.contact.userId
139139
});
140140
}).then(() => {
141-
this.ui.toastMessage = translator.i18n_t('REMOVE_CLOUD_SERVER_SUCCESS');
141+
this.fire('core-signal', {
142+
name: 'show-toast',
143+
data: {
144+
toastMessage: translator.i18n_t('REMOVE_CLOUD_SERVER_SUCCESS')
145+
}
146+
});
142147
}).catch((e: Error) => {
143148
if (!e) {
144149
return;
@@ -174,7 +179,12 @@ Polymer({
174179
},
175180
destroyCloudServerIfNeeded: function() {
176181
if (this.contact.status === this.UserStatus.CLOUD_INSTANCE_CREATED_BY_LOCAL) {
177-
this.ui.toastMessage = translator.i18n_t('REMOVING_UPROXY_CLOUD_STATUS');
182+
this.fire('core-signal', {
183+
name: 'show-toast',
184+
data: {
185+
toastMessage: translator.i18n_t('REMOVING_UPROXY_CLOUD_STATUS')
186+
}
187+
});
178188
return ui_context.core.cloudUpdate({
179189
operation: uproxy_core_api.CloudOperationType.CLOUD_DESTROY,
180190
providerName: DEFAULT_PROVIDER

src/generic_ui/polymer/instance.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import social = require('../../interfaces/social');
55
import ui_constants = require('../../interfaces/ui');
66
import net = require('../../lib/net/net.types');
77
import uproxy_core_api = require('../../interfaces/uproxy_core_api');
8+
import translator = require('../scripts/translator');
89
import user_interface = require('../scripts/ui');
910

1011
// generic_ui/scripts/ui.ts: UserInterface
@@ -37,8 +38,13 @@ Polymer({
3738
},
3839
start: function() {
3940
if (!this.instance.isOnline) {
40-
this.ui.toastMessage = ui.i18n_t('FRIEND_OFFLINE',
41-
{ name: this.user.name });
41+
this.fire('core-signal', {
42+
name: 'show-toast',
43+
data: {
44+
toastMessage: translator.i18n_t('FRIEND_OFFLINE',
45+
{ name: this.user.name })
46+
}
47+
});
4248
return;
4349
}
4450

src/generic_ui/polymer/root.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,10 @@
289289

290290
<core-signals on-core-signal-close-settings="{{ closeSettings }}"></core-signals>
291291

292-
<!-- Required for cases where an open-dialog signal is sent from the UI via ui.fireSignal -->
293292
<core-signals on-core-signal-open-dialog="{{ openDialog }}"></core-signals>
294293

294+
<core-signals on-core-signal-show-toast="{{ showToast }}"></core-signal>
295+
295296
<div id='browserElementContainer' hidden?='{{ui_constants.View.BROWSER_ERROR !== ui.view}}'></div>
296297

297298
<uproxy-copypaste id='copypaste' hidden?='{{ ui_constants.View.COPYPASTE !== ui.view }}'>

src/generic_ui/polymer/root.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -179,21 +179,14 @@ Polymer({
179179
restartProxying: function() {
180180
this.ui.restartProxying();
181181
},
182-
toastMessageChanged: function(oldVal :string, newVal :string) {
183-
if (newVal) {
184-
this.toastMessage = newVal;
185-
this.unableToShare = ui.unableToShare;
186-
this.unableToGet = ui.unableToGet;
187-
this.$.toast.show();
188-
189-
// clear the message so we can pick up on other changes
190-
ui.toastMessage = null;
191-
ui.unableToShare = false;
192-
ui.unableToGet = false;
193-
}
182+
showToast: function(e: Event, detail: { toastMessage: string, unableToGet?: boolean, unableToShare?: boolean }) {
183+
this.toastMessage = detail.toastMessage;
184+
this.unableToGet = detail.unableToGet || false;
185+
this.unableToShare = detail.unableToShare || false;
186+
this.$.toast.show();
194187
},
195188
openTroubleshoot: function() {
196-
if (this.ui.unableToGet) {
189+
if (this.unableToGet) {
197190
this.troubleshootTitle = ui.i18n_t('UNABLE_TO_GET');
198191
} else {
199192
this.troubleshootTitle = ui.i18n_t('UNABLE_TO_SHARE');
@@ -250,7 +243,6 @@ Polymer({
250243
},
251244
observe: {
252245
'$.mainPanel.selected': 'drawerToggled',
253-
'ui.toastMessage': 'toastMessageChanged',
254246
'ui.view': 'viewChanged',
255247
// Use an observer on model.contacts.shareAccessContacts.trustedUproxy
256248
// so that we can detect any time elements are added or removed from this

src/generic_ui/scripts/background_ui.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,25 @@ export class BackgroundUi {
6969
}
7070

7171
/* actual BackgroundUi methods */
72-
public fireSignal(signalName: string, data?: Object): void {
72+
public fireSignal = (signalName: string, data?: Object): void => {
7373
this.doInPanel_('fire-signal', { name: signalName, data: data });
7474
}
7575

76-
public openDialog(data: ui.DialogDescription): Promise<any> {
76+
public openDialog = (data: ui.DialogDescription): Promise<any> => {
7777
return this.doInPanel_('open-dialog', data, true);
7878
}
7979

80+
public showToast = (
81+
message: string,
82+
unableToGet: boolean = false,
83+
unableToShare: boolean = false): void => {
84+
return this.fireSignal('show-toast', {
85+
message: message,
86+
unableToGet: unableToGet,
87+
unableToShare: unableToShare
88+
});
89+
}
90+
8091
private wrapPromise_ = (promise: Promise<any>, promiseId: number) => {
8192
promise.then((data) => {
8293
this.panelConnector_.send('promise-response', {

src/generic_ui/scripts/ui.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,6 @@ export class UserInterface implements ui_constants.UiApi {
102102
/* Getting and sharing */
103103
public gettingStatus :string = null;
104104
public sharingStatus :string = null;
105-
public unableToGet :boolean = false;
106-
public unableToShare :boolean = false;
107105
public isSharingDisabled :boolean = false;
108106
public proxyingId: string; // ID of the most recent failed proxying attempt.
109107
private userCancelledGetAttempt_ :boolean = false;
@@ -123,8 +121,6 @@ export class UserInterface implements ui_constants.UiApi {
123121
public portControlSupport = uproxy_core_api.PortControlSupport.PENDING;
124122
public availableVersion :string = null;
125123

126-
public toastMessage :string = null;
127-
128124
// Please note that this value is updated periodically so may not reflect current reality.
129125
private isConnectedToCellular_ :boolean = false;
130126

@@ -267,10 +263,8 @@ export class UserInterface implements ui_constants.UiApi {
267263
(info:uproxy_core_api.FailedToGetOrGive) => {
268264
console.error('proxying attempt ' + info.proxyingId + ' failed (giving)');
269265

270-
this.toastMessage = this.i18n_t('UNABLE_TO_SHARE_WITH', {
271-
name: info.name
272-
});
273-
this.unableToShare = true;
266+
let toastMessage = translator_module.i18n_t('UNABLE_TO_SHARE_WITH', { name: info.name });
267+
this.backgroundUi.showToast(toastMessage, false, true);
274268
this.proxyingId = info.proxyingId;
275269
});
276270

@@ -291,10 +285,8 @@ export class UserInterface implements ui_constants.UiApi {
291285
if (user.status === social.UserStatus.CLOUD_INSTANCE_CREATED_BY_LOCAL) {
292286
this.restartServer_('digitalocean');
293287
} else {
294-
this.toastMessage = this.i18n_t('UNABLE_TO_GET_FROM', {
295-
name: info.name
296-
});
297-
this.unableToGet = true;
288+
let toastMessage = translator_module.i18n_t('UNABLE_TO_GET_FROM', { name: info.name });
289+
this.backgroundUi.showToast(toastMessage, true, false);
298290
}
299291
}
300292
}
@@ -350,12 +342,12 @@ export class UserInterface implements ui_constants.UiApi {
350342
this.i18n_t('CANCEL'),
351343
this.i18n_t('RESTART_SERVER')
352344
).then(() => {
353-
this.toastMessage = this.i18n_t('RESTARTING_SERVER');
345+
this.backgroundUi.showToast(translator_module.i18n_t('RESTARTING_SERVER'));
354346
return this.core.cloudUpdate({
355347
operation: uproxy_core_api.CloudOperationType.CLOUD_REBOOT,
356348
providerName: providerName
357349
}).then(() => {
358-
this.toastMessage = this.i18n_t('RESTART_SUCCESS');
350+
this.backgroundUi.showToast(translator_module.i18n_t('RESTART_SUCCESS'));
359351
}).catch((e: Error) => {
360352
this.showDialog(this.i18n_t('RESTART_FAILURE_TITLE'), this.i18n_t('RESTART_FAILURE_TEXT'));
361353
});

0 commit comments

Comments
 (0)