diff --git a/src/main/java/es/kauron/jstudy/controller/TestController.java b/src/main/java/es/kauron/jstudy/controller/TestController.java index 0f2d830..c9dec7a 100644 --- a/src/main/java/es/kauron/jstudy/controller/TestController.java +++ b/src/main/java/es/kauron/jstudy/controller/TestController.java @@ -2,11 +2,11 @@ package es.kauron.jstudy.controller; import es.kauron.jstudy.model.AppPrefs; import es.kauron.jstudy.model.TestItem; -import javafx.beans.property.SimpleBooleanProperty; +import javafx.beans.binding.Bindings; +import javafx.beans.property.*; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; -import javafx.geometry.Insets; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.ProgressBar; @@ -31,8 +31,9 @@ public class TestController implements Initializable { private SimpleBooleanProperty correctingError = new SimpleBooleanProperty(false); private List list; - private int total, current = -1; - private int done = 0, errors = 0; + private IntegerProperty errors = new SimpleIntegerProperty(0); + private ObjectProperty item = new SimpleObjectProperty<>(); + private IntegerProperty done = new SimpleIntegerProperty(0); @Override public void initialize(URL url, ResourceBundle resourceBundle) { @@ -42,24 +43,32 @@ public class TestController implements Initializable { void setList(List list) { this.list = list; - total = list.size(); + int total = list.size(); progress.setProgress(0); - progressLabel.setText("0/" + total); + progressLabel.textProperty().bind(Bindings.format( + String.format("%%.2f%%%% %%d / %d %%d mistakes", total), + done.multiply(100.0).divide(total), done, errors)); + progress.progressProperty().bind(done.divide((double) total)); + item.addListener((obj, o, n) -> { + if (o != null) { + prevQuestion.setText(o.getQuestion()); + correctAnswer.setText(o.getAnswer()); + } + question.setText(n.getQuestion()); + }); onSkipAction(null); } @FXML private void onNextAction(ActionEvent event) { - prevQuestion.setText(list.get(current).getQuestion()); prevAnswer.setText(answer.getText()); - correctAnswer.setText(list.get(current).getAnswer()); - boolean right = list.get(current).getAnswer().equals(answer.getText()); + boolean right = item.get().getAnswer().equals(answer.getText()); correctAnswer.setVisible(!right); correctLabel.setVisible(!right); if (!right) { - errors++; + errors.set(errors.get() + 1); prevAnswer.setStyle("-fx-text-fill: #C40000;"); } else { prevAnswer.setStyle("-fx-text-fill: #00C400;"); @@ -67,9 +76,8 @@ public class TestController implements Initializable { // Remove the question from the pool if the question was correctly answered or there is no repetition if ((right && !correctingError.get()) || !AppPrefs.repeatWrong.get()) { - list.remove(current); - progress.setProgress(++done / (double) total); - progressLabel.setText(done + "/" + total); + list.remove(item.get()); + done.set(done.get() + 1); if (list.size() == 0) { onEndAction(null); return; @@ -79,27 +87,24 @@ public class TestController implements Initializable { chooseQuestion(); } correctingError.set(!right && AppPrefs.repeatImmediately.get()); - setQuestion(); + answer.setText(""); + answer.requestFocus(); } private void chooseQuestion() { answer.setText(""); - int previous = current; + TestItem next = item.get(); do - current = (int) (Math.random() * list.size()); - while (list.size() > 1 && current == previous); - } - - private void setQuestion() { - answer.setText(""); - question.setText(list.get(current).getQuestion()); - answer.requestFocus(); + next = list.get((int) (Math.random() * list.size())); + while (list.size() > 1 && item.get() == next); + item.set(next); } @FXML private void onSkipAction(ActionEvent event) { chooseQuestion(); - setQuestion(); + answer.setText(""); + answer.requestFocus(); } @FXML @@ -107,10 +112,5 @@ public class TestController implements Initializable { answer.setText(""); answer.setDisable(true); question.setText("That's it!"); - feedback.getChildren().clear(); - feedback.setSpacing(5); - feedback.setPadding(new Insets(5, 5, 5, 5)); - feedback.getChildren().add(new Label("Mistakes: " + errors)); - feedback.getChildren().add(new Label(String.format("Mark: %.2f%%", (total - errors) / (double) total * 100))); } } diff --git a/src/main/resources/es/kauron/jstudy/css/test.css b/src/main/resources/es/kauron/jstudy/css/test.css index 235fe8a..bca46b8 100644 --- a/src/main/resources/es/kauron/jstudy/css/test.css +++ b/src/main/resources/es/kauron/jstudy/css/test.css @@ -1,3 +1,7 @@ .answer { -fx-font-weight: bold; +} + +.feedbackText { + -fx-font-size: 20; } \ No newline at end of file diff --git a/src/main/resources/es/kauron/jstudy/view/test.fxml b/src/main/resources/es/kauron/jstudy/view/test.fxml index 9e3e59c..835252b 100644 --- a/src/main/resources/es/kauron/jstudy/view/test.fxml +++ b/src/main/resources/es/kauron/jstudy/view/test.fxml @@ -4,7 +4,7 @@ - +
@@ -21,10 +21,10 @@ - + - + @@ -32,12 +32,12 @@ - @@ -64,7 +64,7 @@