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