Skip to content

Commit

Permalink
Now TimeLocks can use Date and Time
Browse files Browse the repository at this point in the history
  • Loading branch information
emlautarom1 committed Jun 25, 2019
1 parent fd97c12 commit 7ccead7
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 17 deletions.
60 changes: 59 additions & 1 deletion src/main/java/app/controller/MainController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import app.model.ProtectionCustomSetting;
import app.model.TimeLockSettings;
import app.service.TaskManager;
import app.time.converter.SafeLocalTimeStringConverter;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
Expand All @@ -20,6 +21,10 @@
import java.io.File;
import java.net.URI;
import java.net.URL;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.LinkedHashMap;
import java.util.ResourceBundle;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -56,6 +61,9 @@ public class MainController implements Initializable {
@FXML
private DatePicker timeLockUnlockDate;

@FXML
private Spinner<LocalTime> timeLockUnlockTime;

@FXML
private Button runButton;

Expand All @@ -64,6 +72,7 @@ public void initialize(URL location, ResourceBundle resources) {
setOperations();
setProtectionLevelOptions();
setProtectionDefaultCustomSetting();
setTimePickerSettings();
}

@FXML
Expand Down Expand Up @@ -209,6 +218,46 @@ private void setProtectionDefaultCustomSetting() {
protectionCustomSetting.setText(protectionAddRandomError);
}

private void setTimePickerSettings() {
SafeLocalTimeStringConverter safeConverter = new SafeLocalTimeStringConverter(
DateTimeFormatter.ofPattern("HH:mm"),
DateTimeFormatter.ofPattern("HH:mm")
);

timeLockUnlockTime.valueFactoryProperty().setValue(
new SpinnerValueFactory<LocalTime>() {
{
setConverter(safeConverter);
}

@Override
public void decrement(int steps) {
if (getValue() == null)
setValue(LocalTime.now());
else {
LocalTime time = getValue();
setValue(time.minusMinutes(steps));
}
}

@Override
public void increment(int steps) {
if (this.getValue() == null)
setValue(LocalTime.now());
else {
LocalTime time = getValue();
setValue(time.plusMinutes(steps));
}
}
}
);
// Ensure the last "Time" in the spinner is a valid one.
timeLockUnlockTime.focusedProperty().addListener(observable -> {
String lastInput = timeLockUnlockTime.getEditor().getCharacters().toString();
timeLockUnlockTime.getValueFactory().setValue(safeConverter.fromString(lastInput));
});
}

private Operations getOperations() {
String selectedValue = operationsChoiceBox.getValue();
return Operations.fromString(selectedValue);
Expand All @@ -230,9 +279,18 @@ private ProtectionCustomSetting getProtectionCustomSetting() {
}

private TimeLockSettings getTimeLockSettings() {
LocalDateTime unlockDateTime = null;

LocalDate unlockDate = timeLockUnlockDate.getValue();
LocalTime unlockTime = timeLockUnlockTime.getValue();

if (unlockDate != null && unlockTime != null) {
unlockDateTime = LocalDateTime.of(unlockDate, unlockTime);
}

return new TimeLockSettings(
timeLockCheckBox.isSelected(),
timeLockUnlockDate.getValue()
unlockDateTime
);
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/app/model/TimeLockSettings.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package app.model;

import java.time.LocalDate;
import java.time.LocalDateTime;

public class TimeLockSettings {
private final boolean enabled;
private final LocalDate unlockDate;
private final LocalDateTime unlockDate;

public TimeLockSettings(boolean enabled, LocalDate unlockDate) {
public TimeLockSettings(boolean enabled, LocalDateTime unlockDate) {
this.enabled = enabled;
this.unlockDate = unlockDate;
}
Expand All @@ -15,7 +15,7 @@ public boolean isEnabled() {
return enabled;
}

public LocalDate getUnlockDate() {
public LocalDateTime getUnlockDate() {
return unlockDate;
}
}
8 changes: 4 additions & 4 deletions src/main/java/app/service/timelock/TimeLockedFile.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package app.service.timelock;

import java.io.*;
import java.time.LocalDate;
import java.time.LocalDateTime;

class TimeLockedFile implements Serializable {
private final boolean locked;
private final LocalDate unlockDate;
private final LocalDateTime unlockDate;
private final byte[] data;

TimeLockedFile(boolean locked, LocalDate unlockDate, byte[] data) {
TimeLockedFile(boolean locked, LocalDateTime unlockDate, byte[] data) {
this.locked = locked;
this.unlockDate = unlockDate;
this.data = data;
Expand All @@ -18,7 +18,7 @@ boolean isLocked() {
return locked;
}

LocalDate getUnlockDate() {
LocalDateTime getUnlockDate() {
return unlockDate;
}

Expand Down
21 changes: 13 additions & 8 deletions src/main/java/app/service/timelock/TimeLocker.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package app.service.timelock;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class TimeLocker {
public static byte[] lock(byte[] dataBytes, LocalDate lockDate) {
private static final String DATE_FORMATTER = "dd/MM/yyyy HH:mm";

public static byte[] lock(byte[] dataBytes, LocalDateTime lockDate) {
if (isValidLockDate(lockDate)) {
TimeLockedFile lockedFile = new TimeLockedFile(
true,
Expand All @@ -16,19 +19,21 @@ public static byte[] lock(byte[] dataBytes, LocalDate lockDate) {
throw new Error("Failed to Time Lock the File.");
}
} else {
throw new Error("Invalid Time Lock Date.");
throw new Error("Invalid Time Lock Date or Time.");
}
}

public static byte[] unlock(byte[] dataBytes) throws Error {
try {
TimeLockedFile timeLockedFile = TimeLockedFile.fromByteArray(dataBytes);
if (timeLockedFile.isLocked()) {
LocalDate unlockDate = timeLockedFile.getUnlockDate();
if (LocalDate.now().isAfter(unlockDate)) {
LocalDateTime unlockDate = timeLockedFile.getUnlockDate();
if (LocalDateTime.now().isAfter(unlockDate)) {
return timeLockedFile.getData();
} else {
throw new Error("File is Time Locked until " + unlockDate.toString() + "!.");
throw new Error("File is Time Locked until "
+ unlockDate.format(DateTimeFormatter.ofPattern(DATE_FORMATTER))
+ "!");
}
} else {
return timeLockedFile.getData();
Expand All @@ -47,7 +52,7 @@ public static byte[] buildUnlockedFile(byte[] dataBytes) throws Error {
}
}

private static boolean isValidLockDate(LocalDate lockDate) {
return LocalDate.now().isBefore(lockDate);
private static boolean isValidLockDate(LocalDateTime lockDate) {
return LocalDateTime.now().isBefore(lockDate);
}
}
2 changes: 2 additions & 0 deletions src/main/resources/app/fxml/main.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<?import javafx.scene.control.DatePicker?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.Spinner?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.image.Image?>
Expand Down Expand Up @@ -86,6 +87,7 @@
<Label text="Unlock Date:" />
<DatePicker fx:id="timeLockUnlockDate" />
</HBox>
<Spinner fx:id="timeLockUnlockTime" editable="true" prefWidth="100.0" />
</HBox>
</TitledPane>
</panes>
Expand Down

0 comments on commit 7ccead7

Please sign in to comment.