Commit e066dc0e authored by Evan W. Patton's avatar Evan W. Patton

Update settings on server only if changes occur

Switching between tabs will save settings even if nothing has
changed. This results in more network operations than is necessary to
maintain correct behavior of the application.

This commit updates the CommonSettings class hierarchy to record when
changes have been made. UserSettings and ProjectSettings will perform
RPC if and only if the changed flag is true. Otherwise, saving
settings is a no-op.

Change-Id: I1f15d39418036b320ee3050003034b97e46415be
parent 09591fd8
...@@ -10,6 +10,7 @@ import com.google.appinventor.client.ErrorReporter; ...@@ -10,6 +10,7 @@ import com.google.appinventor.client.ErrorReporter;
import static com.google.appinventor.client.Ode.MESSAGES; import static com.google.appinventor.client.Ode.MESSAGES;
import com.google.appinventor.client.output.OdeLog; import com.google.appinventor.client.output.OdeLog;
import com.google.appinventor.client.properties.json.ClientJsonParser; import com.google.appinventor.client.properties.json.ClientJsonParser;
import com.google.appinventor.client.widgets.properties.PropertyChangeListener;
import com.google.appinventor.shared.properties.json.JSONObject; import com.google.appinventor.shared.properties.json.JSONObject;
import com.google.appinventor.shared.properties.json.JSONValue; import com.google.appinventor.shared.properties.json.JSONValue;
...@@ -21,9 +22,10 @@ import java.util.Map; ...@@ -21,9 +22,10 @@ import java.util.Map;
* Superclass for collections of settings. * Superclass for collections of settings.
* *
*/ */
public abstract class CommonSettings { public abstract class CommonSettings implements PropertyChangeListener {
// Mapping from category (class) to actual settings singleton instance // Mapping from category (class) to actual settings singleton instance
private final Map<String, Settings> settingsMap; private final Map<String, Settings> settingsMap;
protected transient boolean changed = false;
/** /**
* Creates new settings object. * Creates new settings object.
...@@ -60,6 +62,8 @@ public abstract class CommonSettings { ...@@ -60,6 +62,8 @@ public abstract class CommonSettings {
*/ */
protected void addSettings(String category, Settings settings) { protected void addSettings(String category, Settings settings) {
settingsMap.put(category, settings); settingsMap.put(category, settings);
settings.addPropertyChangeListener(this);
changed = true;
} }
/** /**
...@@ -121,4 +125,9 @@ public abstract class CommonSettings { ...@@ -121,4 +125,9 @@ public abstract class CommonSettings {
protected static void reportSaveError() { protected static void reportSaveError() {
ErrorReporter.reportError(MESSAGES.settingsSaveError()); ErrorReporter.reportError(MESSAGES.settingsSaveError());
} }
@Override
public void onPropertyChange(String name, String newValue) {
changed = true;
}
} }
...@@ -56,6 +56,7 @@ public final class ProjectSettings extends CommonSettings implements SettingsAcc ...@@ -56,6 +56,7 @@ public final class ProjectSettings extends CommonSettings implements SettingsAcc
public void onSuccess(String result) { public void onSuccess(String result) {
OdeLog.log("Loaded project settings: " + result); OdeLog.log("Loaded project settings: " + result);
decodeSettings(result); decodeSettings(result);
changed = false;
} }
}); });
} }
...@@ -64,6 +65,9 @@ public final class ProjectSettings extends CommonSettings implements SettingsAcc ...@@ -64,6 +65,9 @@ public final class ProjectSettings extends CommonSettings implements SettingsAcc
public void saveSettings(final Command command) { public void saveSettings(final Command command) {
if (Ode.getInstance().isReadOnly()) { if (Ode.getInstance().isReadOnly()) {
return; // No changes when in read only mode return; // No changes when in read only mode
} else if (!changed) {
// Do not save project settings if they haven't changed.
return;
} }
String s = encodeSettings(); String s = encodeSettings();
OdeLog.log("Saving project settings: " + s); OdeLog.log("Saving project settings: " + s);
...@@ -75,6 +79,7 @@ public final class ProjectSettings extends CommonSettings implements SettingsAcc ...@@ -75,6 +79,7 @@ public final class ProjectSettings extends CommonSettings implements SettingsAcc
MESSAGES.settingsSaveError()) { MESSAGES.settingsSaveError()) {
@Override @Override
public void onSuccess(Void result) { public void onSuccess(Void result) {
changed = false;
if (command != null) { if (command != null) {
command.execute(); command.execute();
} }
......
...@@ -46,6 +46,7 @@ public final class UserSettings extends CommonSettings implements SettingsAccess ...@@ -46,6 +46,7 @@ public final class UserSettings extends CommonSettings implements SettingsAccess
OdeLog.log("Loaded global settings: " + result); OdeLog.log("Loaded global settings: " + result);
decodeSettings(result); decodeSettings(result);
changed = false;
loaded = true; loaded = true;
loading = false; loading = false;
} }
...@@ -80,6 +81,9 @@ public final class UserSettings extends CommonSettings implements SettingsAccess ...@@ -80,6 +81,9 @@ public final class UserSettings extends CommonSettings implements SettingsAccess
// settings with this empty version, so we just return. // settings with this empty version, so we just return.
return; return;
} else if (!changed) {
// Do not save UserSettings if they haven't changed.
return;
} else { } else {
String s = encodeSettings(); String s = encodeSettings();
OdeLog.log("Saving global settings: " + s); OdeLog.log("Saving global settings: " + s);
...@@ -90,6 +94,7 @@ public final class UserSettings extends CommonSettings implements SettingsAccess ...@@ -90,6 +94,7 @@ public final class UserSettings extends CommonSettings implements SettingsAccess
MESSAGES.settingsSaveError()) { MESSAGES.settingsSaveError()) {
@Override @Override
public void onSuccess(Void result) { public void onSuccess(Void result) {
changed = false;
if (command != null) { if (command != null) {
command.execute(); command.execute();
} }
......
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