Skip to content

Commit

Permalink
Merge pull request #4 from gekoramy/button-revert
Browse files Browse the repository at this point in the history
Button revert
  • Loading branch information
gekoramy authored Sep 9, 2018
2 parents 41ca1e1 + 5c622b6 commit 90a6637
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class DownloadPresenter implements Initializable {

public DownloadPresenter() {
this.fileChooser = new FileChooser();
this.fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Questions file", Constants.MIME));
this.fileChooser.getExtensionFilters().add(Constants.QUESTIONS_FILE);
this.fileChooser.setTitle("Store to quiz yourself offline");
}

Expand All @@ -58,7 +58,7 @@ private void onFailed() {
}

private void onSucceeded() {
store.setOutput(new FileChooser().showSaveDialog(null));
store.setOutput(fileChooser.showSaveDialog(null));
store.setQuestionPool(download.getValue().getValue());
store.start();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import me.gekoramy.github.quiz.app.AppRepositories;
import me.gekoramy.github.quiz.exception.NotLoggedException;
import me.gekoramy.github.quiz.service.Read;
import me.gekoramy.github.quiz.util.Constants;
import me.gekoramy.github.quiz.util.GitHubClients;

import java.net.URL;
Expand All @@ -31,6 +32,13 @@ public class HomePresenter implements Initializable {
private Hyperlink btnOpen;

private final Read read = new Read();
private final FileChooser fileChooser;

public HomePresenter() {
fileChooser = new FileChooser();
fileChooser.getExtensionFilters().add(Constants.QUESTIONS_FILE);
fileChooser.setTitle("Choose a questions file");
}

@Override
public void initialize(URL location, ResourceBundle resources) {
Expand Down Expand Up @@ -79,7 +87,7 @@ private void onAdd() {

private void onOpen() {
if (!read.isRunning()) {
read.setSource(new FileChooser().showOpenDialog(null));
read.setSource(fileChooser.showOpenDialog(null));
read.reset();
read.start();
}
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/me/gekoramy/github/quiz/app/home/home.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
<?import javafx.scene.control.Label?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>

<GridPane fx:id="root" alignment="CENTER" prefHeight="320.0" prefWidth="200.0" stylesheets="@home.css" vgap="20.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="me.gekoramy.github.quiz.app.home.HomePresenter">

<rowConstraints>
Expand Down
12 changes: 2 additions & 10 deletions src/main/java/me/gekoramy/github/quiz/app/login/login.fxml
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>

<VBox alignment="CENTER" prefHeight="400.0" prefWidth="300.0" spacing="10" stylesheets="@login.css" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="me.gekoramy.github.quiz.app.login.LoginPresenter">

<ImageView fitHeight="60.0" fitWidth="60.0" preserveRatio="true">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public class PreviewPresenter implements Initializable {
private Hyperlink btnDownload;
@FXML
private Hyperlink btnStart;
@FXML
private Hyperlink btnRevert;

@Inject
private UpdateAvailable updateAvailable;
Expand All @@ -67,10 +69,12 @@ public class PreviewPresenter implements Initializable {
private final RotateTransition rotate;
private final FileChooser fileChooser;

private static final int DEFAULT_SLIDE = 12;

public PreviewPresenter() {
this.fileChooser = new FileChooser();
this.fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Questions file", Constants.MIME));
this.fileChooser.setTitle("Store to quiz yourself offline");
fileChooser = new FileChooser();
fileChooser.getExtensionFilters().add(Constants.QUESTIONS_FILE);
fileChooser.setTitle("Store to quiz yourself offline");

rotate = new RotateTransition(Duration.millis(750));
rotate.setByAngle(360);
Expand All @@ -92,29 +96,26 @@ public void initialize(URL location, ResourceBundle resources) {

lblRepo.setText(updateAvailable.getRepo().getName());

progress.progressProperty().bind(examStarter.getQuestionPool().progressProperty());
progress.progressProperty().bind(examStarter.getQuestionPool().todoProperty().divide(examStarter.getQuestionPool().total()));

btnRefresh.managedProperty().bindBidirectional(btnRefresh.visibleProperty());
btnDownload.managedProperty().bindBidirectional(btnDownload.visibleProperty());
btnRevert.visibleProperty().bind(Bindings.equal(0, examStarter.getQuestionPool().todoProperty()));
btnStart.visibleProperty().bind(btnRevert.visibleProperty().not());

updateAvailable.valueProperty().addListener((value, oldValue, newValue) -> {
if (newValue != null && newValue) {
btnDownload.visibleProperty().set(true);
btnRefresh.visibleProperty().set(false);
}
});
btnDownload.visibleProperty().bind(updateAvailable.valueProperty());
btnRefresh.visibleProperty().bind(btnDownload.visibleProperty().not());
btnRefresh.disableProperty().bind(updateAvailable.runningProperty());

lblQuestions.textProperty().bind(Bindings.createIntegerBinding(() -> new Double(sldQuestions.valueProperty().get()).intValue(), sldQuestions.valueProperty()).asString());
sldQuestions.setMax(examStarter.getQuestionPool().total());
sldQuestions.setValue(12);
sldQuestions.maxProperty().bind(examStarter.getQuestionPool().todoProperty());
sldQuestions.setValue(DEFAULT_SLIDE);

lblRepo.setOnAction(e -> onRepo());
btnStore.setOnAction(e -> onStore());
btnDownload.setOnAction(e -> onDownload());
btnRefresh.setOnAction(e -> onRefresh());
btnShuffle.setOnAction(e -> onShuffle());
btnStart.setOnAction(e -> onStart());
btnRevert.setOnAction(e -> onRevert());
}

public void setAccelerators(Scene scene) {
Expand Down Expand Up @@ -163,6 +164,12 @@ private void onStart() {
}
}

private void onRevert() {
examStarter.getQuestionPool().revert();
sldQuestions.setMin(1.0);
sldQuestions.setValue(DEFAULT_SLIDE);
}

private void onDownload() {
Platform.runLater(() -> {
try {
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/me/gekoramy/github/quiz/app/preview/preview.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<Font size="35.0"/>
</font>
</Hyperlink>
<Hyperlink fx:id="btnRefresh">
<Hyperlink fx:id="btnRefresh" managed="${btnRefresh.visible}">
<graphic>
<SVGPath
content="M17.65 6.35C16.2 4.9 14.21 4 12 4c-4.42 0-7.99 3.58-7.99 8s3.57 8 7.99 8c3.73 0 6.84-2.55 7.73-6h-2.08c-.82 2.33-3.04 4-5.65 4-3.31 0-6-2.69-6-6s2.69-6 6-6c1.66 0 3.14.69 4.22 1.78L13 11h7V4l-2.35 2.35z"
Expand All @@ -41,7 +41,7 @@
<Font size="35.0"/>
</font>
</Hyperlink>
<Hyperlink fx:id="btnDownload" visible="false" managed="false">
<Hyperlink fx:id="btnDownload" visible="false" managed="${btnDownload.visible}">
<graphic>
<SVGPath
content="M19.35 10.04C18.67 6.59 15.64 4 12 4 9.11 4 6.6 5.64 5.35 8.04 2.34 8.36 0 10.91 0 14c0 3.31 2.69 6 6 6h13c2.76 0 5-2.24 5-5 0-2.64-2.05-4.78-4.65-4.96zM17 13l-5 5-5-5h3V9h4v4h3z"
Expand All @@ -61,14 +61,24 @@
<Font size="35.0"/>
</font>
</Hyperlink>
<Hyperlink fx:id="btnStart">
<Hyperlink fx:id="btnStart" managed="${btnStart.visible}">
<graphic>
<SVGPath content="M8 5v14l11-7z" scaleX="1.5" scaleY="1.5" style="-fx-fill: #424242;"/>
</graphic>
<font>
<Font size="35.0"/>
</font>
</Hyperlink>
<Hyperlink fx:id="btnRevert" visible="false" managed="${btnRevert.visible}">
<graphic>
<SVGPath
content="M12 5V1L7 6l5 5V7c3.31 0 6 2.69 6 6s-2.69 6-6 6-6-2.69-6-6H4c0 4.42 3.58 8 8 8s8-3.58 8-8-3.58-8-8-8z"
scaleX="1.5" scaleY="1.5" style="-fx-fill: #424242;"/>
</graphic>
<font>
<Font size="35.0"/>
</font>
</Hyperlink>
<Label fx:id="lblQuestions" minWidth="50">
<font>
<Font size="35.0"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.ProgressIndicator?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.StackPane?>

<?import javafx.scene.layout.*?>
<StackPane prefHeight="370.0" stylesheets="@repositories.css" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="me.gekoramy.github.quiz.app.repositories.RepositoriesPresenter">
<GridPane vgap="10.0">
<rowConstraints>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import javafx.util.Pair;
import me.gekoramy.github.quiz.pojo.Question;
import me.gekoramy.github.quiz.util.Constants;
import me.gekoramy.github.quiz.util.Pool;
import org.eclipse.egit.github.core.Repository;

Expand Down Expand Up @@ -34,11 +33,6 @@ public File store(File output, Repository repo, Pool<Question> questionPool) thr
return output;
}

public File store(Repository repo, Pool<Question> questionPool) throws IOException {
String fileName = repo.getName().replaceFirst(Constants.PREFIX, "") + Constants.MIME;
return store(new File(fileName), repo, questionPool);
}

public Pair<Repository, Pool<Question>> read(File file) throws IOException, ClassNotFoundException {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
return new Pair<>((Repository) ois.readObject(), new Pool<>((List<Question>) ois.readObject(), (LinkedList<Question>) ois.readObject()));
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/me/gekoramy/github/quiz/service/Download.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.eclipse.egit.github.core.RepositoryContents;
import org.eclipse.egit.github.core.client.GitHubClient;
import org.eclipse.egit.github.core.service.ContentsService;
import org.eclipse.egit.github.core.service.RepositoryService;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -70,7 +71,7 @@ protected Pair<Repository, Pool<Question>> call() throws Exception {
}));
}

return new Pair<>(repo,
return new Pair<>(new RepositoryService(client).getRepository(repo),
new Pool<>(CompletableFuture.supplyAsync(() -> futures.stream()
.map(CompletableFuture::join)
.collect(Collectors.toList()))
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/me/gekoramy/github/quiz/util/Constants.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package me.gekoramy.github.quiz.util;

import javafx.stage.FileChooser;

/**
* @author Luca Mosetti
*/
public class Constants {

public static final String MIME = ".questions";
public static final FileChooser.ExtensionFilter QUESTIONS_FILE = new FileChooser.ExtensionFilter("Questions file", "*.questions");
public static final String PREFIX = "questions.";

}
28 changes: 12 additions & 16 deletions src/main/java/me/gekoramy/github/quiz/util/Pool.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package me.gekoramy.github.quiz.util;

import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
* @author Luca Mosetti
Expand All @@ -16,19 +16,19 @@ public class Pool<T> implements Serializable {

private final List<T> totalList;
private final LinkedList<T> toDoList;
private final DoubleProperty progress;
private final IntegerProperty todoProperty;

public Pool(List<T> totalList) {
this.totalList = totalList;
this.toDoList = new LinkedList<>(totalList);
this.progress = new SimpleDoubleProperty(1);
this.todoProperty = new SimpleIntegerProperty(toDoList.size());
}

public Pool(List<T> totalList, LinkedList<T> toDoList) {
this.totalList = totalList;
this.toDoList = toDoList;
toDoList.retainAll(totalList);
this.progress = new SimpleDoubleProperty((double) toDo() / (double) total());
this.todoProperty = new SimpleIntegerProperty(toDo());
}

public List<T> getTotalList() {
Expand All @@ -47,29 +47,25 @@ public int toDo() {
return toDoList.size();
}

public DoubleProperty progressProperty() {
return progress;
public ReadOnlyIntegerProperty todoProperty() {
return todoProperty;
}

public void revert() {
toDoList.clear();
toDoList.addAll(totalList);
this.progress.set(1);
this.todoProperty.set(toDo());
}

public List<T> retrieve(int many) {
if (many > toDoList.size())
throw new ArrayIndexOutOfBoundsException(toDoList.size());

List<T> questions = new ArrayList<>();
List<T> subList = IntStream.range(0, many).mapToObj(i -> toDoList.poll()).collect(Collectors.toList());

for (int i = 0; i < many; i++) {
questions.add(toDoList.poll());
}
this.todoProperty.set(toDo());

this.progress.set((double) toDo() / (double) total());

return questions;
return subList;
}

public boolean isDone() {
Expand Down

0 comments on commit 90a6637

Please sign in to comment.