Commit 6e708200 authored by Curt Clemens's avatar Curt Clemens Committed by Chromium LUCI CQ

[Nearby] Save contact visibility once on exit

The contact visibility page was updating the visibility pref and
making an api request to sync the allowed contacts list every time
the controls on the page were pressed/toggled. This CL instead
saves the page state once when the page closes.

Fixed: 1131269
Change-Id: Iaf6e5138b029450df083179994cba7f67ee0f5c1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2606716
Commit-Queue: Curt Clemens <cclem@google.com>
Reviewed-by: default avatarKyle Horimoto <khorimoto@chromium.org>
Cr-Commit-Position: refs/heads/master@{#839809}
parent fa8873fd
...@@ -392,8 +392,7 @@ ...@@ -392,8 +392,7 @@
<cr-toggle class="contact-toggle" <cr-toggle class="contact-toggle"
checked="{{item.checked}}" checked="{{item.checked}}"
disabled="[[!isVisibility_( disabled="[[!isVisibility_(
selectedVisibility,'some')]]" selectedVisibility,'some')]]">
on-click="syncContactToggleState_">
</cr-toggle> </cr-toggle>
</template> </template>
</div> </div>
......
...@@ -140,7 +140,6 @@ Polymer({ ...@@ -140,7 +140,6 @@ Polymer({
observers: [ observers: [
'settingsChanged_(settings.visibility)', 'settingsChanged_(settings.visibility)',
'selectedVisibilityChanged_(selectedVisibility)',
], ],
/** @override */ /** @override */
...@@ -267,23 +266,6 @@ Polymer({ ...@@ -267,23 +266,6 @@ Polymer({
clearTimeout(this.downloadTimeoutId_); clearTimeout(this.downloadTimeoutId_);
}, },
/**
* Sync the latest contact toggle states and update allowedContacts through
* the contact manager.
* @private
*/
syncContactToggleState_() {
const allowedContacts = [];
if (this.contacts) {
for (const contact of this.contacts) {
if (contact.checked) {
allowedContacts.push(contact.id);
}
}
}
this.contactManager_.setAllowedContacts(allowedContacts);
},
/** /**
* TODO(crbug.com/1128256): Remove after specs/a11y. * TODO(crbug.com/1128256): Remove after specs/a11y.
* Call from the JS debug console to test scrolling. * Call from the JS debug console to test scrolling.
...@@ -341,17 +323,6 @@ Polymer({ ...@@ -341,17 +323,6 @@ Polymer({
} }
}, },
/**
* @param {string} selectedVisibility
* @private
*/
selectedVisibilityChanged_(selectedVisibility) {
const visibility = visibilityStringToValue(this.selectedVisibility);
if (visibility) {
this.set('settings.visibility', visibility);
}
},
/** /**
* @param {string} contactsState * @param {string} contactsState
* @return {boolean} true when the radio group should be disabled * @return {boolean} true when the radio group should be disabled
...@@ -532,5 +503,26 @@ Polymer({ ...@@ -532,5 +503,26 @@ Polymer({
return ''; return '';
} }
}, },
/**
* Save visibility setting and sync allowed contacts with contact manager.
* @public
*/
saveVisibilityAndAllowedContacts() {
const visibility = visibilityStringToValue(this.selectedVisibility);
if (visibility) {
this.set('settings.visibility', visibility);
}
const allowedContacts = [];
if (this.contacts) {
for (const contact of this.contacts) {
if (contact.checked) {
allowedContacts.push(contact.id);
}
}
}
this.contactManager_.setAllowedContacts(allowedContacts);
},
}); });
})(); })();
...@@ -33,6 +33,9 @@ Polymer({ ...@@ -33,6 +33,9 @@ Polymer({
listeners: {'next': 'onNext_', 'manage-contacts': 'onManageContacts_'}, listeners: {'next': 'onNext_', 'manage-contacts': 'onManageContacts_'},
onNext_() { onNext_() {
const contactVisibility = /** @type {NearbyContactVisibilityElement} */
(this.$.contactVisibility);
contactVisibility.saveVisibilityAndAllowedContacts();
this.set('settings.enabled', true); this.set('settings.enabled', true);
this.fire('onboarding-complete'); this.fire('onboarding-complete');
}, },
......
...@@ -48,7 +48,8 @@ ...@@ -48,7 +48,8 @@
$i18n{nearbyShareVisibilityPageSubtitle} $i18n{nearbyShareVisibilityPageSubtitle}
</h2> </h2>
<div slot="body"> <div slot="body">
<nearby-contact-visibility settings="{{settings}}"> <nearby-contact-visibility id="contactVisibility"
settings="{{settings}}">
</nearby-contact-visibility> </nearby-contact-visibility>
</div> </div>
<div class="layout horizontal center" slot="button-container"> <div class="layout horizontal center" slot="button-container">
......
...@@ -20,6 +20,9 @@ Polymer({ ...@@ -20,6 +20,9 @@ Polymer({
/** @private */ /** @private */
onDoneClick_() { onDoneClick_() {
const contactVisibility = /** @type {NearbyContactVisibilityElement} */
(this.$.contactVisibility);
contactVisibility.saveVisibilityAndAllowedContacts();
const dialog = /** @type {!CrDialogElement} */ (this.$.dialog); const dialog = /** @type {!CrDialogElement} */ (this.$.dialog);
if (dialog.open) { if (dialog.open) {
dialog.close(); dialog.close();
......
...@@ -15,8 +15,8 @@ cr.define('nearby_share', function() { ...@@ -15,8 +15,8 @@ cr.define('nearby_share', function() {
constructor() { constructor() {
/** @type {?Array<!nearbyShare.mojom.ContactRecord>} */ /** @type {?Array<!nearbyShare.mojom.ContactRecord>} */
this.contactRecords = null; this.contactRecords = null;
/** @private {!Array<!string>} */ /** @type {!Array<!string>} */
this.allowedContacts_ = []; this.allowedContacts = [];
/** @private {number} */ /** @private {number} */
this.numUnreachable_ = 3; this.numUnreachable_ = 3;
/** @private {?nearbyShare.mojom.DownloadContactsObserverInterface} */ /** @private {?nearbyShare.mojom.DownloadContactsObserverInterface} */
...@@ -44,7 +44,7 @@ cr.define('nearby_share', function() { ...@@ -44,7 +44,7 @@ cr.define('nearby_share', function() {
* @param {!Array<!string>} allowedContacts * @param {!Array<!string>} allowedContacts
*/ */
setAllowedContacts(allowedContacts) { setAllowedContacts(allowedContacts) {
this.allowedContacts_ = allowedContacts; this.allowedContacts = allowedContacts;
} }
/** /**
...@@ -73,7 +73,7 @@ cr.define('nearby_share', function() { ...@@ -73,7 +73,7 @@ cr.define('nearby_share', function() {
] ]
} }
]; ];
this.allowedContacts_ = ['1']; this.allowedContacts = ['1'];
} }
failDownload() { failDownload() {
...@@ -82,7 +82,7 @@ cr.define('nearby_share', function() { ...@@ -82,7 +82,7 @@ cr.define('nearby_share', function() {
completeDownload() { completeDownload() {
this.observer_.onContactsDownloaded( this.observer_.onContactsDownloaded(
this.allowedContacts_, this.contactRecords || [], this.allowedContacts, this.contactRecords || [],
/*num_unreachable_contacts_filtered_out=*/ this.numUnreachable_); /*num_unreachable_contacts_filtered_out=*/ this.numUnreachable_);
} }
} }
......
...@@ -284,4 +284,43 @@ suite('nearby-contact-visibility', () => { ...@@ -284,4 +284,43 @@ suite('nearby-contact-visibility', () => {
assertFalse(isUnreachableMessageVisible()); assertFalse(isUnreachableMessageVisible());
}); });
test(
'Save persists visibility setting and allowed contacts',
async function() {
fakeContactManager.setupContactRecords();
fakeContactManager.setNumUnreachable(0);
fakeContactManager.completeDownload();
visibilityElement.set(
'settings.visibility', nearbyShare.mojom.Visibility.kAllContacts);
await test_util.waitAfterNextRender(visibilityElement);
// visibility setting is not immediately updated
visibilityElement.$$('#someContacts').click();
await test_util.waitAfterNextRender(visibilityElement);
assertTrue(areContactCheckBoxesVisible());
assertEquals(
visibilityElement.get('settings.visibility'),
nearbyShare.mojom.Visibility.kAllContacts);
// allow only contact 2, check that allowed contacts are not yet pushed
// to the contact manager
fakeContactManager.setAllowedContacts(['1']);
for (let i = 0; i < visibilityElement.contacts.length; ++i) {
visibilityElement.set(
['contacts', i, 'checked'],
visibilityElement.contacts[i].id === '2');
}
await test_util.waitAfterNextRender(visibilityElement);
assertEquals(fakeContactManager.allowedContacts.length, 1);
assertEquals(fakeContactManager.allowedContacts[0], '1');
// after save, ui state is persisted
visibilityElement.saveVisibilityAndAllowedContacts();
assertEquals(
visibilityElement.get('settings.visibility'),
nearbyShare.mojom.Visibility.kSelectedContacts);
assertEquals(fakeContactManager.allowedContacts.length, 1);
assertEquals(fakeContactManager.allowedContacts[0], '2');
});
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment