Start recording only if the user clicked on OK in the record until

dialog
This commit is contained in:
0xb00bface 2021-08-27 18:58:27 +02:00
parent 9d5d719e3c
commit f577983305
2 changed files with 37 additions and 13 deletions

View File

@ -8,6 +8,7 @@ import java.security.NoSuchAlgorithmException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import org.slf4j.Logger;
@ -36,6 +37,9 @@ public class SetStopDateAction {
private Model model;
private Recorder recorder;
private RadioButton pauseButton;
private DateTimePicker datePicker;
public SetStopDateAction(Node source, Model model, Recorder recorder) {
this.source = source;
this.model = model;
@ -44,7 +48,7 @@ public class SetStopDateAction {
public CompletableFuture<Boolean> execute() {
source.setCursor(Cursor.WAIT);
var datePicker = new DateTimePicker();
datePicker = new DateTimePicker();
var gridPane = new GridPane();
gridPane.setHgap(10);
gridPane.setVgap(10);
@ -53,7 +57,7 @@ public class SetStopDateAction {
gridPane.add(datePicker, 1, 0);
gridPane.add(new Label("And then"), 0, 1);
var toggleGroup = new ToggleGroup();
var pauseButton = new RadioButton("pause recording");
pauseButton = new RadioButton("pause recording");
pauseButton.setSelected(model.getRecordUntilSubsequentAction() == PAUSE);
pauseButton.setToggleGroup(toggleGroup);
var removeButton = new RadioButton("remove model");
@ -69,18 +73,13 @@ public class SetStopDateAction {
datePicker.setDateTimeValue(localDate);
}
boolean userClickedOk = Dialogs.showCustomInput(source.getScene(), "Stop Recording of " + model.getDisplayName() + " at", gridPane);
return createAsyncTask(userClickedOk);
}
private CompletableFuture<Boolean> createAsyncTask(boolean userClickedOk) {
return CompletableFuture.supplyAsync(() -> {
if (userClickedOk) {
SubsequentAction action = pauseButton.isSelected() ? PAUSE : REMOVE;
LOG.info("Stop at {} and {}", datePicker.getDateTimeValue(), action);
var stopAt = Instant.from(datePicker.getDateTimeValue().atZone(ZoneId.systemDefault()));
model.setRecordUntil(stopAt);
model.setRecordUntilSubsequentAction(action);
try {
recorder.stopRecordingAt(model);
} catch (InvalidKeyException | NoSuchAlgorithmException | IOException e) {
Dialogs.showError(source.getScene(), "Error", "Couln't set stop date", e);
}
setRecordingTimeLimit();
}
return true;
}, GlobalThreadPool.get()).whenComplete((r, e) -> {
@ -91,6 +90,32 @@ public class SetStopDateAction {
});
}
private void setRecordingTimeLimit() {
SubsequentAction action = pauseButton.isSelected() ? PAUSE : REMOVE;
LOG.info("Stop at {} and {}", datePicker.getDateTimeValue(), action);
var stopAt = Instant.from(datePicker.getDateTimeValue().atZone(ZoneId.systemDefault()));
model.setRecordUntil(stopAt);
model.setRecordUntilSubsequentAction(action);
try {
if (!recorder.isTracked(model)) {
new StartRecordingAction(source, List.of(model), recorder).execute(m -> {
try {
recorder.stopRecordingAt(m);
} catch (InvalidKeyException | NoSuchAlgorithmException | IOException e1) {
showError(e1);
}
});
} else {
recorder.stopRecordingAt(model);
}
} catch (InvalidKeyException | NoSuchAlgorithmException | IOException e) {
showError(e);
}
}
private void showError(Exception e) {
Dialogs.showError(source.getScene(), "Error", "Couln't set stop date", e);
}
}

View File

@ -403,7 +403,6 @@ public class ModelMenuContributor {
m.setMarkedForLaterRecording(false);
m.setSuspended(false);
});
startStopAction(selectedModels, true);
selectedModels.forEach(m -> new SetStopDateAction(source, m, recorder).execute() //
.thenAccept(b -> executeCallback()));
});