Skip to content

Commit 4eefca3

Browse files
authored
Merge pull request #650 from AustinShalit/editedBug
Fix bug introduced in #647
2 parents 12d76af + 7896dd8 commit 4eefca3

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

core/src/main/java/edu/wpi/grip/core/serialization/Project.java

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
import java.io.StringReader;
2222
import java.io.Writer;
2323
import java.nio.charset.StandardCharsets;
24+
import java.util.LinkedList;
25+
import java.util.List;
2426
import java.util.Optional;
27+
import java.util.function.Consumer;
2528
import javax.inject.Inject;
2629
import javax.inject.Singleton;
2730

@@ -37,7 +40,7 @@ public class Project {
3740
@Inject
3841
private PipelineRunner pipelineRunner;
3942
private Optional<File> file = Optional.empty();
40-
private boolean saveIsDirty = false;
43+
private final ObservableBoolean saveIsDirty = new ObservableBoolean();
4144

4245
@Inject
4346
public void initialize(StepConverter stepConverter,
@@ -111,7 +114,7 @@ void open(Reader reader) {
111114
this.pipeline.clear();
112115
this.xstream.fromXML(reader);
113116
pipelineRunner.startAsync();
114-
saveIsDirty = false;
117+
saveIsDirty.set(false);
115118
}
116119

117120
/**
@@ -127,19 +130,42 @@ public void save(File file) throws IOException {
127130

128131
public void save(Writer writer) {
129132
this.xstream.toXML(this.pipeline, writer);
130-
saveIsDirty = false;
133+
saveIsDirty.set(false);
131134
}
132135

133136
public boolean isSaveDirty() {
134-
return saveIsDirty;
137+
return saveIsDirty.get();
138+
}
139+
140+
public void addIsSaveDirtyConsumer(Consumer<Boolean> consumer) {
141+
saveIsDirty.addConsumer(consumer);
135142
}
136143

137144
@Subscribe
138145
public void onDirtiesSaveEvent(DirtiesSaveEvent dirtySaveEvent) {
139146
// Only update the flag the save isn't already dirty
140147
// We don't need to be redundantly checking if the event dirties the save
141-
if (!saveIsDirty && dirtySaveEvent.doesDirtySave()) {
142-
saveIsDirty = true;
148+
if (!saveIsDirty.get() && dirtySaveEvent.doesDirtySave()) {
149+
saveIsDirty.set(true);
150+
}
151+
}
152+
153+
private static final class ObservableBoolean {
154+
155+
private boolean b = false;
156+
private final List<Consumer<Boolean>> consumers = new LinkedList<>();
157+
158+
public void set(boolean b) {
159+
this.b = b;
160+
consumers.parallelStream().forEach(c -> c.accept(b));
161+
}
162+
163+
public boolean get() {
164+
return b;
165+
}
166+
167+
public void addConsumer(Consumer<Boolean> consumer) {
168+
consumers.add(consumer);
143169
}
144170
}
145171
}

ui/src/main/java/edu/wpi/grip/ui/Main.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import edu.wpi.grip.core.GripCoreModule;
44
import edu.wpi.grip.core.GripFileModule;
55
import edu.wpi.grip.core.PipelineRunner;
6-
import edu.wpi.grip.core.events.DirtiesSaveEvent;
76
import edu.wpi.grip.core.events.UnexpectedThrowableEvent;
87
import edu.wpi.grip.core.http.GripServer;
98
import edu.wpi.grip.core.http.HttpPipelineSwitcher;
@@ -32,7 +31,6 @@
3231
import javafx.application.Application;
3332
import javafx.application.Platform;
3433
import javafx.application.Preloader;
35-
import javafx.beans.property.SimpleBooleanProperty;
3634
import javafx.fxml.FXMLLoader;
3735
import javafx.scene.Parent;
3836
import javafx.scene.Scene;
@@ -63,7 +61,6 @@ public class Main extends Application {
6361
@Inject private HttpPipelineSwitcher pipelineSwitcher;
6462
private Parent root;
6563
private boolean headless;
66-
private final SimpleBooleanProperty dirty = new SimpleBooleanProperty(false);
6764

6865
public static void main(String[] args) {
6966
launch(args);
@@ -127,11 +124,11 @@ public void start(Stage stage) throws IOException {
127124
cvOperations.addOperations();
128125
notifyPreloader(new Preloader.ProgressNotification(0.9));
129126

130-
dirty.addListener((observable, oldValue, newValue) -> {
127+
project.addIsSaveDirtyConsumer(newValue -> {
131128
if (newValue) {
132-
stage.setTitle(MAIN_TITLE + " | Edited");
129+
Platform.runLater(() -> stage.setTitle(MAIN_TITLE + " | Edited"));
133130
} else {
134-
stage.setTitle(MAIN_TITLE);
131+
Platform.runLater(() -> stage.setTitle(MAIN_TITLE));
135132
}
136133
});
137134

@@ -151,13 +148,6 @@ public void stop() {
151148
SafeShutdown.flagStopping();
152149
}
153150

154-
@Subscribe
155-
public void onDirtiesSaveEvent(DirtiesSaveEvent dirtySaveEvent) {
156-
if (dirty.get() != dirtySaveEvent.doesDirtySave()) {
157-
dirty.set(dirtySaveEvent.doesDirtySave());
158-
}
159-
}
160-
161151
@Subscribe
162152
@SuppressWarnings("PMD.AvoidCatchingThrowable")
163153
public final void onUnexpectedThrowableEvent(UnexpectedThrowableEvent event) {

0 commit comments

Comments
 (0)