From f577983305cb634ea6835d1955595509acd57336 Mon Sep 17 00:00:00 2001 From: 0xb00bface <0xboobface@gmail.com> Date: Fri, 27 Aug 2021 18:58:27 +0200 Subject: [PATCH] Start recording only if the user clicked on OK in the record until dialog --- .../ctbrec/ui/action/SetStopDateAction.java | 49 ++++++++++++++----- .../ctbrec/ui/menu/ModelMenuContributor.java | 1 - 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/client/src/main/java/ctbrec/ui/action/SetStopDateAction.java b/client/src/main/java/ctbrec/ui/action/SetStopDateAction.java index df183fad..b4983763 100644 --- a/client/src/main/java/ctbrec/ui/action/SetStopDateAction.java +++ b/client/src/main/java/ctbrec/ui/action/SetStopDateAction.java @@ -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 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 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); + } } diff --git a/client/src/main/java/ctbrec/ui/menu/ModelMenuContributor.java b/client/src/main/java/ctbrec/ui/menu/ModelMenuContributor.java index 9fa7c61b..ab0a74d5 100644 --- a/client/src/main/java/ctbrec/ui/menu/ModelMenuContributor.java +++ b/client/src/main/java/ctbrec/ui/menu/ModelMenuContributor.java @@ -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())); });