Remove Settings.save()

This commit is contained in:
yushijinhun
2018-07-29 12:16:16 +08:00
parent b98a4b11a3
commit 45c7e0c231
2 changed files with 7 additions and 13 deletions

View File

@@ -38,7 +38,7 @@ public final class ConfigHolder {
public static final Path CONFIG_PATH = Paths.get(CONFIG_FILENAME).toAbsolutePath(); public static final Path CONFIG_PATH = Paths.get(CONFIG_FILENAME).toAbsolutePath();
public static final Config CONFIG = initSettings(); public static final Config CONFIG = initSettings();
private static Config upgradeSettings(Config deserialized, Map rawJson) { private static Config upgradeSettings(Config deserialized, Map<?, ?> rawJson) {
if (!rawJson.containsKey("commonDirType")) if (!rawJson.containsKey("commonDirType"))
deserialized.setCommonDirType(deserialized.getCommonDirectory().equals(Settings.getDefaultCommonDirectory()) ? EnumCommonDirectory.DEFAULT : EnumCommonDirectory.CUSTOM); deserialized.setCommonDirType(deserialized.getCommonDirectory().equals(Settings.getDefaultCommonDirectory()) ? EnumCommonDirectory.DEFAULT : EnumCommonDirectory.CUSTOM);
return deserialized; return deserialized;
@@ -49,7 +49,7 @@ public final class ConfigHolder {
if (Files.exists(CONFIG_PATH)) { if (Files.exists(CONFIG_PATH)) {
try { try {
String json = new String(Files.readAllBytes(CONFIG_PATH), UTF_8); String json = new String(Files.readAllBytes(CONFIG_PATH), UTF_8);
Map raw = new Gson().fromJson(json, Map.class); Map<?, ?> raw = new Gson().fromJson(json, Map.class);
Config deserialized = Config.fromJson(json); Config deserialized = Config.fromJson(json);
if (deserialized == null) { if (deserialized == null) {
LOG.finer("Settings file is empty, use the default settings."); LOG.finer("Settings file is empty, use the default settings.");
@@ -64,9 +64,10 @@ public final class ConfigHolder {
return config; return config;
} }
static void saveConfig(Config config) { static void saveConfig() {
LOG.info("Saving config");
try { try {
Files.write(CONFIG_PATH, config.toJson().getBytes(UTF_8)); Files.write(CONFIG_PATH, CONFIG.toJson().getBytes(UTF_8));
} catch (IOException ex) { } catch (IOException ex) {
LOG.log(Level.SEVERE, "Failed to save config", ex); LOG.log(Level.SEVERE, "Failed to save config", ex);
} }

View File

@@ -27,12 +27,10 @@ import org.jackhuang.hmcl.task.Schedulers;
import org.jackhuang.hmcl.util.*; import org.jackhuang.hmcl.util.*;
import org.jackhuang.hmcl.util.i18n.Locales; import org.jackhuang.hmcl.util.i18n.Locales;
import java.io.File;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static org.jackhuang.hmcl.setting.ConfigHolder.CONFIG; import static org.jackhuang.hmcl.setting.ConfigHolder.CONFIG;
import static org.jackhuang.hmcl.util.Logging.LOG;
public class Settings { public class Settings {
@@ -51,18 +49,13 @@ public class Settings {
for (Map.Entry<String, Profile> profileEntry : getProfileMap().entrySet()) { for (Map.Entry<String, Profile> profileEntry : getProfileMap().entrySet()) {
profileEntry.getValue().setName(profileEntry.getKey()); profileEntry.getValue().setName(profileEntry.getKey());
profileEntry.getValue().nameProperty().setChangedListener(this::profileNameChanged); profileEntry.getValue().nameProperty().setChangedListener(this::profileNameChanged);
profileEntry.getValue().addPropertyChangedListener(e -> save()); profileEntry.getValue().addPropertyChangedListener(e -> ConfigHolder.saveConfig());
} }
CONFIG.addListener(source -> save()); CONFIG.addListener(source -> ConfigHolder.saveConfig());
CONFIG.setFirstLaunch(false); CONFIG.setFirstLaunch(false);
} }
private void save() {
LOG.info("Saving config");
ConfigHolder.saveConfig(CONFIG);
}
public boolean isFirstLaunch() { public boolean isFirstLaunch() {
return firstLaunch; return firstLaunch;
} }