Skip to content

Commit

Permalink
修改配置类的代码逻辑:
Browse files Browse the repository at this point in the history
设立一个全局的类(GlobalConfig)专门存放相关的配置类,以静态变量的方式访问这些配置实例。
而不是像以前那样一个动态变量传来传去,眼都看花了...
  • Loading branch information
lensferno committed Mar 12, 2022
1 parent 1a09158 commit 58d2dbb
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 202 deletions.
4 changes: 4 additions & 0 deletions Dogename/dogename.iml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,9 @@
<orderEntry type="library" name="Maven: org.json:json:20160810" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.25" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-simple:1.7.25" level="project" />
<orderEntry type="library" name="Maven: com.googlecode.soundlibs:mp3spi:1.9.5.4" level="project" />
<orderEntry type="library" name="Maven: com.googlecode.soundlibs:jlayer:1.0.1.4" level="project" />
<orderEntry type="library" name="Maven: junit:junit:3.8.2" level="project" />
<orderEntry type="library" name="Maven: com.googlecode.soundlibs:tritonus-share:0.3.7.4" level="project" />
</component>
</module>
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
public class ConfigLoader {
private final String mainConfigLocation = FilePath.toSpecificPathForm("files/Config.json");
private final String voiceConfigLocation = FilePath.toSpecificPathForm("files/VoiceConfig.json");
//ConfigValuesBean config;
private MainConfig mainConfig;

public String getMainConfigLocation() {
return mainConfigLocation;
Expand All @@ -31,11 +29,7 @@ public String getVoiceConfigLocation() {
return voiceConfigLocation;
}

public MainConfig getMainConfig() {
return mainConfig;
}

public MainConfig readConfigFromFile(String fileLocation) {
public void readMainConfig(String fileLocation) {
//property属性应该要自定义一个json适配器才能解析出来
Gson gson = new GsonBuilder()
.registerTypeAdapter(SimpleBooleanProperty.class, new BooleanPropertyAdapter())
Expand All @@ -52,34 +46,29 @@ public MainConfig readConfigFromFile(String fileLocation) {
if (!configFile.exists()) {
configFile.getParentFile().mkdirs();
configFile.createNewFile();
mainConfig = new MainConfig();
GlobalConfig.mainConfig = new MainConfig();
writeMainConfigToFile(mainConfigLocation);
return mainConfig;
return;
}
InputStream inputStream = new FileInputStream(configFile);
configJson = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
inputStream.close();

mainConfig = gson.fromJson(configJson, MainConfig.class);
GlobalConfig.mainConfig = gson.fromJson(configJson, MainConfig.class);

if (mainConfig == null) {
mainConfig = new MainConfig();
if (GlobalConfig.mainConfig == null) {
GlobalConfig.mainConfig = new MainConfig();
writeMainConfigToFile(mainConfigLocation);
return mainConfig;
}

} catch (Exception e) {
System.out.println("Error to load config file:" + e + "\nUse Default config.");

mainConfig = new MainConfig();
GlobalConfig.mainConfig = new MainConfig();
writeMainConfigToFile(mainConfigLocation);
return mainConfig;
}

return this.mainConfig;
}

public VoiceConfig readVoiceConfigFromFile(String fileLocation) {
public void loadVoiceConfig(String fileLocation) {
//property属性应该要自定义一个json适配器才能解析出来
Gson gson = new GsonBuilder()
.registerTypeAdapter(SimpleBooleanProperty.class, new BooleanPropertyAdapter())
Expand All @@ -97,31 +86,26 @@ public VoiceConfig readVoiceConfigFromFile(String fileLocation) {
configFile.getParentFile().mkdirs();
configFile.createNewFile();

VoicePlayer.voiceConfig = new VoiceConfig();
GlobalConfig.voiceConfig = new VoiceConfig();
writeVoiceConfigToFile(voiceConfigLocation);
return VoicePlayer.voiceConfig;
return;
}
InputStream inputStream = new FileInputStream(configFile);
configJson = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
inputStream.close();

VoicePlayer.voiceConfig = gson.fromJson(configJson, VoiceConfig.class);
GlobalConfig.voiceConfig = gson.fromJson(configJson, VoiceConfig.class);

if (VoicePlayer.voiceConfig == null) {
VoicePlayer.voiceConfig = new VoiceConfig();
if (GlobalConfig.voiceConfig == null) {
GlobalConfig.voiceConfig = new VoiceConfig();
writeVoiceConfigToFile(voiceConfigLocation);
return VoicePlayer.voiceConfig;
}

} catch (Exception e) {
System.out.println("Error to load voice config file:" + e + "\nUse Default voice config.");

VoicePlayer.voiceConfig = new VoiceConfig();
GlobalConfig.voiceConfig = new VoiceConfig();
writeVoiceConfigToFile(voiceConfigLocation);
return VoicePlayer.voiceConfig;
}

return VoicePlayer.voiceConfig;
}

private String toJSON(MainConfig config) {
Expand Down Expand Up @@ -154,10 +138,10 @@ public void writeAllConfigToFile(String outputLocation, String voiceConfigFile)
outputFile.createNewFile();
}
OutputStream stream = new FileOutputStream(outputFile);
IOUtils.write(toJSON(this.mainConfig).getBytes(StandardCharsets.UTF_8), stream);
IOUtils.write(toJSON(GlobalConfig.mainConfig).getBytes(StandardCharsets.UTF_8), stream);

OutputStream voiceConfigFileStream = new FileOutputStream(voiceConfigFile);
IOUtils.write(getConfigJson(VoicePlayer.voiceConfig).getBytes(StandardCharsets.UTF_8), voiceConfigFileStream);
IOUtils.write(getConfigJson(GlobalConfig.voiceConfig).getBytes(StandardCharsets.UTF_8), voiceConfigFileStream);

} catch (Exception e) {
System.out.println("Error in writing all config:" + e);
Expand All @@ -174,7 +158,7 @@ public void writeMainConfigToFile(String outputLocation) {
}

OutputStream stream = new FileOutputStream(outputFile);
IOUtils.write(toJSON(this.mainConfig).getBytes(StandardCharsets.UTF_8), stream);
IOUtils.write(toJSON(GlobalConfig.mainConfig).getBytes(StandardCharsets.UTF_8), stream);

} catch (Exception e) {
System.out.println("Error in writing main config:" + e);
Expand All @@ -191,7 +175,7 @@ public void writeVoiceConfigToFile(String voiceConfigFile) {
outputFile.createNewFile();
}
OutputStream voiceConfigFileStream = new FileOutputStream(voiceConfigFile);
IOUtils.write(getConfigJson(VoicePlayer.voiceConfig).getBytes(StandardCharsets.UTF_8), voiceConfigFileStream);
IOUtils.write(getConfigJson(GlobalConfig.voiceConfig).getBytes(StandardCharsets.UTF_8), voiceConfigFileStream);

} catch (Exception e) {
System.out.println("Error in writing voice config:" + e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package me.lensferno.dogename.configs;

public class GlobalConfig {
public static VoiceConfig voiceConfig = null;
public static MainConfig mainConfig = null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
public class MainConfig {

// ---------------------- Default values ---------------------------------------------------------

@Expose
public static final boolean DEFAULT_NAME_CHOOSE = true;
public static final int METHOD_NAME = 0; // 名字挑选法
public static final int METHOD_NUMBER = 1; // 数字挑选法
public static final int DEFAULT_MAX_TOTAL_COUNT = 120; // 默认轮回次数:120
Expand All @@ -21,13 +19,10 @@ public class MainConfig {
public static final boolean DEFAULT_NEW_ALGO = true; // 默认使用新算法"Java sec random"
public static final boolean DEFAULT_VOICE_PLAY = true; // 默认使用语音播报
public static final boolean DEFAULT_SHOW_SAYING = true;
private final int currentVersion = 3;

// ----------------------Properties----------------------------------------------------------------
private final SimpleBooleanProperty nameChoose;

private final SimpleBooleanProperty randomCount; // 挑选次数是否随机
private final SimpleBooleanProperty passSelectedResult; // 是否忽略已经被点过的名字/数字
private final SimpleBooleanProperty ignoreSelectedResult; // 是否忽略已经被点过的名字/数字

private final SimpleIntegerProperty chooseMethod; // 挑选方式: 0->名字挑选法 1->数字挑选法
private final SimpleIntegerProperty maxTotalCount; // 挑选轮回次数
Expand All @@ -47,10 +42,9 @@ public class MainConfig {
// -------------------------- 初始化 --------------------------------------------------------------
public MainConfig() {
randomCount = new SimpleBooleanProperty(DEFAULT_RANDOM_TIMES);
passSelectedResult = new SimpleBooleanProperty(DEFAULT_IGNORE_PAST);
ignoreSelectedResult = new SimpleBooleanProperty(DEFAULT_IGNORE_PAST);

chooseMethod = new SimpleIntegerProperty(METHOD_NAME);
nameChoose = new SimpleBooleanProperty(DEFAULT_NAME_CHOOSE);

maxTotalCount = new SimpleIntegerProperty(DEFAULT_MAX_TOTAL_COUNT);

Expand All @@ -68,19 +62,6 @@ public MainConfig() {
}

// -------------------------- Getters and Setters ---------------------------------------------

public boolean getNameChoose() {
return nameChoose.get();
}

public void setNameChoose(boolean nameChoose) {
this.nameChoose.set(nameChoose);
}

public SimpleBooleanProperty nameChooseProperty() {
return nameChoose;
}

public boolean getRandomCount() {
return randomCount.get();
}
Expand All @@ -93,16 +74,16 @@ public SimpleBooleanProperty randomCountProperty() {
return randomCount;
}

public boolean getPassSelectedResult() {
return passSelectedResult.get();
public boolean getIgnoreSelectedResult() {
return ignoreSelectedResult.get();
}

public void setPassSelectedResult(boolean passSelectedResult) {
this.passSelectedResult.set(passSelectedResult);
public void setIgnoreSelectedResult(boolean ignoreSelectedResult) {
this.ignoreSelectedResult.set(ignoreSelectedResult);
}

public SimpleBooleanProperty passSelectedResultProperty() {
return passSelectedResult;
public SimpleBooleanProperty ignoreSelectedResultProperty() {
return ignoreSelectedResult;
}

public int getChooseMethod() {
Expand Down Expand Up @@ -212,9 +193,4 @@ public void setShowSaying(boolean showSaying) {
public SimpleBooleanProperty showSayingProperty() {
return showSaying;
}

public int getCurrentConfigVersion() {
return currentVersion;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import me.lensferno.dogename.configs.ConfigLoader;
import me.lensferno.dogename.configs.GlobalConfig;
import me.lensferno.dogename.configs.MainConfig;
import me.lensferno.dogename.configs.VoiceConfig;
import me.lensferno.dogename.data.Data;
import me.lensferno.dogename.data.History;
import me.lensferno.dogename.select.Selector;
import me.lensferno.dogename.utils.DialogMaker;
import me.lensferno.dogename.utils.FilePath;
import me.lensferno.dogename.utils.ocr.OcrTool;
import me.lensferno.dogename.voice.VoicePlayer;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Random;
Expand All @@ -38,7 +36,6 @@ public final class MainInterfaceController {
OcrTool ocrTool = null;

History history = new History();
MainConfig mainConfig;
Random random = new Random();
Data data = new Data();
Selector selector = new Selector();
Expand Down Expand Up @@ -72,14 +69,15 @@ public MainInterfaceController() {
history.loadHistory();
}

private final ToggleGroup toggleGroup = new ToggleGroup();
public void bindProperties() {
nameChoose.selectedProperty().bindBidirectional(mainConfig.nameChooseProperty());

numbChoose.selectedProperty().bind(mainConfig.nameChooseProperty().not());
numbChoose.selectedProperty().unbind();
nameChoose.setToggleGroup(toggleGroup);
numbChoose.setToggleGroup(toggleGroup);

mainConfig.nameChooseProperty().addListener((observable, oldValue, newValue) -> {
mainConfig.setChooseMethod(mainConfig.getNameChoose() ? MainConfig.METHOD_NAME : MainConfig.METHOD_NUMBER);
toggleGroup.selectToggle(GlobalConfig.mainConfig.getChooseMethod() == MainConfig.METHOD_NAME ? nameChoose : numbChoose);
toggleGroup.selectedToggleProperty().addListener((observable, oldValue, newValue) -> {
GlobalConfig.mainConfig.setChooseMethod(newValue == nameChoose ? MainConfig.METHOD_NAME : MainConfig.METHOD_NUMBER);
System.out.println("change!"+(newValue == nameChoose));
});
}

Expand All @@ -88,8 +86,8 @@ public void setImg(InputStream stream) {
}

public void setUpConfig(ConfigLoader configLoader) {
mainConfig = configLoader.readConfigFromFile(FilePath.toSpecificPathForm("files/Config.json"));
VoicePlayer.voiceConfig = configLoader.readVoiceConfigFromFile(FilePath.toSpecificPathForm("files/VoiceConfig.json"));
configLoader.readMainConfig(FilePath.toSpecificPathForm("files/Config.json"));
configLoader.loadVoiceConfig(FilePath.toSpecificPathForm("files/VoiceConfig.json"));
}

@FXML
Expand All @@ -116,9 +114,8 @@ void showNumberSetting(ActionEvent event) {
return;
}
NumberSettingsPaneController numberSettingsPaneController = new NumberSettingsPaneController(data);
numberSettingsPaneController.bindProperties(mainConfig);
numberSettingsPaneController.bindProperties(GlobalConfig.mainConfig);
new DialogMaker(rootPane).createDialogWithOneBtn("调整数字", numberSettingsPaneController);

}

@FXML
Expand All @@ -138,7 +135,7 @@ void miniMode(ActionEvent event) {
miniStage.initStyle(StageStyle.UNDECORATED);

MiniPaneController miniPaneController = loader.getController();
miniPaneController.setBase(data, mainConfig, selector);
miniPaneController.setBase(data, selector);

Stage currentStage = (Stage) anPaiBtn.getScene().getWindow();
miniPaneController.setOldStage(currentStage);
Expand All @@ -154,11 +151,10 @@ void miniMode(ActionEvent event) {

@FXML
void showSettings(ActionEvent event) {

SettingsPaneController settingsPaneController = new SettingsPaneController();

settingsPaneController.setToggleGroup();
settingsPaneController.bindProperties(mainConfig);
settingsPaneController.bindProperties();

settingsPaneController.setRootPane(rootPane);

Expand All @@ -177,7 +173,7 @@ void showHistory(ActionEvent event) {

public void init() {

selector.initialVariable(mainConfig, data, history, upperLabel.textProperty(), downLabel.textProperty());
selector.initialVariable(data, history, upperLabel.textProperty(), downLabel.textProperty());
selector.addStoppedEventListener((observableValue, oldValue, stop) -> {
if (stop) {
anPaiBtn.setText("安排一下");
Expand All @@ -200,11 +196,11 @@ void anPai() {
return;
}

if (mainConfig.getRandomCount()) {
mainConfig.setMaxTotalCount(100 + random.nextInt(151));
if (GlobalConfig.mainConfig.getRandomCount()) {
GlobalConfig.mainConfig.setMaxTotalCount(100 + random.nextInt(151));
}

if (mainConfig.getNameChoose()) {
if (GlobalConfig.mainConfig.getChooseMethod() == MainConfig.METHOD_NAME) {
runNameMode();
} else {
runNumberMode();
Expand All @@ -225,9 +221,9 @@ private void runNameMode() {
return;
}

if (data.compareNameIgnoreList() && mainConfig.getPassSelectedResult()) {
if (data.compareNameIgnoreList() && GlobalConfig.mainConfig.getIgnoreSelectedResult()) {

if (mainConfig.getEqualMode()) {
if (GlobalConfig.mainConfig.getEqualMode()) {
new DialogMaker(rootPane).createDialogWithOKAndCancel("啊?", "全部名字都被点完啦!\n要把名字的忽略列表重置吗?", e -> data.clearNameIgnoreList());
} else {
new DialogMaker(rootPane).createMessageDialog("啊?", "全部名字都被点完啦!\n请多添加几个名字 或 点击“机会均等”的“重置”按钮。");
Expand All @@ -244,16 +240,16 @@ private void runNumberMode() {

try {

int minNumber = Integer.parseInt(mainConfig.getMinNumber());
int maxNumber = Integer.parseInt(mainConfig.getMaxNumber());
int minNumber = Integer.parseInt(GlobalConfig.mainConfig.getMinNumber());
int maxNumber = Integer.parseInt(GlobalConfig.mainConfig.getMaxNumber());

if (maxNumber - minNumber <= 0) {
new DialogMaker(rootPane).createMessageDialog("嗯哼?", "数字要前小后大啊~");
return;
}

if (data.getNumberIgnoreListSize() >= (maxNumber - minNumber + 1) && mainConfig.getPassSelectedResult()) {
if (mainConfig.getEqualMode()) {
if (data.getNumberIgnoreListSize() >= (maxNumber - minNumber + 1) && GlobalConfig.mainConfig.getIgnoreSelectedResult()) {
if (GlobalConfig.mainConfig.getEqualMode()) {
new DialogMaker(rootPane).createDialogWithOKAndCancel("啊?", "全部数字都被点完啦!\n要把数字的忽略列表重置吗?", e -> data.clearNumberIgnoreList());
} else {
new DialogMaker(rootPane).createMessageDialog("啊?", "全部数字都被点完啦!\n请扩大数字范围 或 点击“机会均等”的“重置”按钮。");
Expand All @@ -280,6 +276,6 @@ public Pane getRootPane() {
}

public MainConfig getMainConfig() {
return mainConfig;
return GlobalConfig.mainConfig;
}
}
Loading

0 comments on commit 58d2dbb

Please sign in to comment.