MOBILE-3200 database: Improve add/edit offline action handling

main
Pau Ferrer Ocaña 2020-09-29 15:39:39 +02:00
parent 6d7a96d8df
commit b0b806280b
2 changed files with 51 additions and 43 deletions

View File

@ -52,6 +52,8 @@ export class AddonModDataEditPage {
protected siteId: string; protected siteId: string;
protected offline: boolean; protected offline: boolean;
protected forceLeave = false; // To allow leaving the page without checking for changes. protected forceLeave = false; // To allow leaving the page without checking for changes.
protected initialSelectedGroup = null;
protected isEditing = false;
title = ''; title = '';
component = AddonModDataProvider.COMPONENT; component = AddonModDataProvider.COMPONENT;
@ -75,7 +77,10 @@ export class AddonModDataEditPage {
this.module = params.get('module') || {}; this.module = params.get('module') || {};
this.entryId = params.get('entryId') || null; this.entryId = params.get('entryId') || null;
this.courseId = params.get('courseId'); this.courseId = params.get('courseId');
this.selectedGroup = params.get('group') || 0; this.selectedGroup = this.entryId ? null : (params.get('group') || 0);
// If entryId is lower than 0 or null, it is a new entry or an offline entry.
this.isEditing = this.entryId && this.entryId > 0;
this.siteId = sitesProvider.getCurrentSiteId(); this.siteId = sitesProvider.getCurrentSiteId();
@ -103,7 +108,8 @@ export class AddonModDataEditPage {
const inputData = this.editForm.value; const inputData = this.editForm.value;
const changed = await this.dataHelper.hasEditDataChanged(inputData, this.fieldsArray, this.data.id, this.entry.contents); let changed = await this.dataHelper.hasEditDataChanged(inputData, this.fieldsArray, this.data.id, this.entry.contents);
changed = changed || (!this.isEditing && this.initialSelectedGroup != this.selectedGroup);
if (changed) { if (changed) {
// Show confirmation if some data has been modified. // Show confirmation if some data has been modified.
@ -169,6 +175,7 @@ export class AddonModDataEditPage {
return this.dataHelper.hasEditDataChanged(inputData, this.fieldsArray, this.data.id, return this.dataHelper.hasEditDataChanged(inputData, this.fieldsArray, this.data.id,
this.entry.contents).then((changed) => { this.entry.contents).then((changed) => {
changed = changed || (!this.isEditing && this.initialSelectedGroup != this.selectedGroup);
if (!changed) { if (!changed) {
if (this.entryId) { if (this.entryId) {
return this.returnToEntryList(); return this.returnToEntryList();
@ -196,7 +203,7 @@ export class AddonModDataEditPage {
return Promise.reject(e); return Promise.reject(e);
}).then((editData) => { }).then((editData) => {
if (editData.length > 0) { if (editData.length > 0) {
if (this.entryId) { if (this.isEditing) {
return this.dataProvider.editEntry(this.data.id, this.entryId, this.courseId, editData, this.fields, return this.dataProvider.editEntry(this.data.id, this.entryId, this.courseId, editData, this.fields,
undefined, this.offline); undefined, this.offline);
} }
@ -213,20 +220,20 @@ export class AddonModDataEditPage {
} }
// This is done if entry is updated when editing or creating if not. // This is done if entry is updated when editing or creating if not.
if ((this.entryId && result.updated) || (!this.entryId && result.newentryid)) { if ((this.isEditing && result.updated) || (!this.isEditing && result.newentryid)) {
this.domUtils.triggerFormSubmittedEvent(this.formElement, result.sent, this.siteId); this.domUtils.triggerFormSubmittedEvent(this.formElement, result.sent, this.siteId);
if (result.sent) {
this.eventsProvider.trigger(CoreEventsProvider.ACTIVITY_DATA_SENT, { module: 'data' });
}
const promises = []; const promises = [];
this.entryId = this.entryId || result.newentryid; if (result.sent) {
this.eventsProvider.trigger(CoreEventsProvider.ACTIVITY_DATA_SENT, { module: 'data' });
if (this.isEditing) {
promises.push(this.dataProvider.invalidateEntryData(this.data.id, this.entryId, this.siteId)); promises.push(this.dataProvider.invalidateEntryData(this.data.id, this.entryId, this.siteId));
}
promises.push(this.dataProvider.invalidateEntriesData(this.data.id, this.siteId)); promises.push(this.dataProvider.invalidateEntriesData(this.data.id, this.siteId));
}
return Promise.all(promises).then(() => { return Promise.all(promises).then(() => {
this.eventsProvider.trigger(AddonModDataProvider.ENTRY_CHANGED, this.eventsProvider.trigger(AddonModDataProvider.ENTRY_CHANGED,
@ -264,7 +271,7 @@ export class AddonModDataEditPage {
* @param groupId Group identifier to set. * @param groupId Group identifier to set.
* @return Resolved when done. * @return Resolved when done.
*/ */
setGroup(groupId: number): Promise<any> { setGroup(groupId: number): Promise<void> {
this.selectedGroup = groupId; this.selectedGroup = groupId;
this.loaded = false; this.loaded = false;
@ -322,7 +329,7 @@ export class AddonModDataEditPage {
* *
* @return Resolved when done. * @return Resolved when done.
*/ */
protected returnToEntryList(): Promise<any> { protected returnToEntryList(): Promise<void> {
const inputData = this.editForm.value; const inputData = this.editForm.value;
return this.dataHelper.getEditTmpFiles(inputData, this.fieldsArray, this.data.id, return this.dataHelper.getEditTmpFiles(inputData, this.fieldsArray, this.data.id,

View File

@ -133,6 +133,7 @@ export class AddonModDataProvider {
}); });
}; };
// Checks to store offline.
if (!this.appProvider.isOnline() || forceOffline) { if (!this.appProvider.isOnline() || forceOffline) {
const notifications = this.checkFields(fields, contents); const notifications = this.checkFields(fields, contents);
if (notifications) { if (notifications) {
@ -140,7 +141,24 @@ export class AddonModDataProvider {
fieldnotifications: notifications fieldnotifications: notifications
}); });
} }
}
// Get other not synced actions.
return this.dataOffline.getEntryActions(dataId, entryId, siteId).then((entries) => {
if (entries && entries.length) {
// Found. Delete add and edit actions first.
const proms = [];
entries.forEach((entry) => {
if (entry.action == 'add') {
proms.push(this.dataOffline.deleteEntry(dataId, entryId, entry.action, siteId));
}
});
return Promise.all(proms);
}
}).then(() => {
// App is offline, store the action.
if (!this.appProvider.isOnline() || forceOffline) {
return storeOffline(); return storeOffline();
} }
@ -157,6 +175,7 @@ export class AddonModDataProvider {
// Couldn't connect to server, store in offline. // Couldn't connect to server, store in offline.
return storeOffline(); return storeOffline();
}); });
});
} }
/** /**
@ -398,9 +417,6 @@ export class AddonModDataProvider {
}); });
}; };
let justAdded = false,
groupId;
if (!this.appProvider.isOnline() || forceOffline) { if (!this.appProvider.isOnline() || forceOffline) {
const notifications = this.checkFields(fields, contents); const notifications = this.checkFields(fields, contents);
if (notifications) { if (notifications) {
@ -416,11 +432,7 @@ export class AddonModDataProvider {
// Found. Delete add and edit actions first. // Found. Delete add and edit actions first.
const proms = []; const proms = [];
entries.forEach((entry) => { entries.forEach((entry) => {
if (entry.action == 'add') { if (entry.action == 'edit') {
justAdded = true;
groupId = entry.groupid;
proms.push(this.dataOffline.deleteEntry(dataId, entryId, entry.action, siteId));
} else if (entry.action == 'edit') {
proms.push(this.dataOffline.deleteEntry(dataId, entryId, entry.action, siteId)); proms.push(this.dataOffline.deleteEntry(dataId, entryId, entry.action, siteId));
} }
}); });
@ -428,17 +440,6 @@ export class AddonModDataProvider {
return Promise.all(proms); return Promise.all(proms);
} }
}).then(() => { }).then(() => {
if (justAdded) {
// The field was added offline, add again and stop.
return this.addEntry(dataId, entryId, courseId, contents, groupId, fields, siteId, forceOffline)
.then((result) => {
result.updated = true;
result.sent = true;
return result;
});
}
if (!this.appProvider.isOnline() || forceOffline) { if (!this.appProvider.isOnline() || forceOffline) {
// App is offline, store the action. // App is offline, store the action.
return storeOffline(); return storeOffline();