Skip to content

Commit

Permalink
Merge pull request #519 from yermak/development
Browse files Browse the repository at this point in the history
Tip of the day
  • Loading branch information
yermak authored Nov 18, 2023
2 parents cc35f7d + 1eb8f06 commit 2d35315
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 7 deletions.
31 changes: 29 additions & 2 deletions src/main/java/uk/yermak/audiobookconverter/AudiobookConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
import org.slf4j.LoggerFactory;
import uk.yermak.audiobookconverter.fx.ConversionContext;
import uk.yermak.audiobookconverter.fx.JfxEnv;
import uk.yermak.audiobookconverter.fx.WizardDialog;

import java.io.File;
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import java.util.Scanner;
import java.util.*;
import java.util.concurrent.Executors;

public class AudiobookConverter extends Application {
Expand Down Expand Up @@ -109,12 +109,23 @@ public void start(Stage stage) {

checkNewVersion();

loadHints();

} catch (IOException e) {
logger.error("Error initiating application", e);
e.printStackTrace();
}
}

public static void loadHints() throws IOException {
List<String> hints = readListFromURL("https://raw.githubusercontent.com/yermak/AudioBookConverter/version/hints.txt");
if (!hints.isEmpty()) {
Collections.shuffle(hints);
WizardDialog wizardDialog = new WizardDialog(hints);
wizardDialog.show();
}
}


private static String readStringFromURL(String requestURL) throws IOException {
try (Scanner scanner = new Scanner(new URL(requestURL).openStream(), StandardCharsets.UTF_8.toString())) {
Expand All @@ -137,6 +148,22 @@ public static void showNotification(String finalOutputDestination) {
.text(finalOutputDestination).show();
}

private static List<String> readListFromURL(String requestURL) throws IOException {
try {
try (Scanner scanner = new Scanner(new URL(requestURL).openStream(), StandardCharsets.UTF_8.toString())) {
scanner.useDelimiter("\\n");
List<String> result = new ArrayList<>();
while (scanner.hasNext()) {
result.add(scanner.next());
}
return result;
}
}catch (Exception e){
logger.error("Failed to read hints", e);
return Collections.emptyList();
}
}

static class VersionChecker implements Runnable {
@Override
public void run() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.io.File;
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.util.*;
import java.util.List;
Expand Down Expand Up @@ -488,4 +489,12 @@ public void repair(ActionEvent actionEvent) {
System.exit(0);
}
}

public void showHints(ActionEvent actionEvent) {
try {
AudiobookConverter.loadHints();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
75 changes: 75 additions & 0 deletions src/main/java/uk/yermak/audiobookconverter/fx/WizardDialog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package uk.yermak.audiobookconverter.fx;

import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.stage.Modality;

import java.util.List;


public class WizardDialog extends Dialog<Void> {

private final TextArea hintsArea = new TextArea();
private int currentIndex = 0;
private List<String> hints;

public WizardDialog(List<String> hints) {
this.hints = hints;
setTitle("Tip of the day...");
initModality(Modality.APPLICATION_MODAL);
hintsArea.setMinHeight(200);
hintsArea.setMinWidth(500);
hintsArea.setEditable(false);
hintsArea.setFocusTraversable(false);
hintsArea.setWrapText(true);
hintsArea.setFont(javafx.scene.text.Font.font("monospaced", 15));

DialogPane dialogPane = getDialogPane();
dialogPane.setContent(createWizardPane());

getDialogPane().getButtonTypes().addAll(ButtonType.PREVIOUS, ButtonType.NEXT, ButtonType.CLOSE);

Button prevButton = (Button) getDialogPane().lookupButton(ButtonType.PREVIOUS);
prevButton.setFocusTraversable(false);
prevButton.addEventFilter(javafx.scene.input.MouseEvent.MOUSE_PRESSED, event -> {
event.consume();
prev();

});
Button nextButton = (Button) getDialogPane().lookupButton(ButtonType.NEXT);
nextButton.setFocusTraversable(false);
nextButton.addEventFilter(javafx.scene.input.MouseEvent.MOUSE_PRESSED, event -> {
event.consume();
next();
});
}

private BorderPane createWizardPane() {
BorderPane wizardPane = new BorderPane();
wizardPane.setPadding(new javafx.geometry.Insets(20));

hintsArea.setText(hints.get(currentIndex));

wizardPane.setCenter(hintsArea);

return wizardPane;
}

private void next() {
if (currentIndex < hints.size() - 1) {
currentIndex++;
} else {
currentIndex = 0;
}
hintsArea.setText(hints.get(currentIndex));
}

private void prev() {
if (currentIndex > 0) {
currentIndex--;
} else {
currentIndex = hints.size() - 1;
}
hintsArea.setText(hints.get(currentIndex));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,18 @@
</accelerator>
</MenuItem>
</Menu>


<Menu fx:id="mAbout" text="About">
<Menu text="System">
<MenuItem text="Settings" onAction="#settings"/>
<MenuItem text="Repair" onAction="#repair"/>
<MenuItem text="Check new version" onAction="#checkVersion"/>
</Menu>
<Menu fx:id="mAbout" text="About">
<MenuItem text="Show hints" onAction="#showHints"/>
<MenuItem text="FAQ" onAction="#openFAQ"/>
<MenuItem text="Report bug" onAction="#openIssues"/>
<MenuItem text="Discussions" onAction="#openDiscussions"/>
<MenuItem text="Web-site" onAction="#openWebSite"/>
<MenuItem text="About" onAction="#openAboutPage"/>
<MenuItem text="Check new version" onAction="#checkVersion"/>
<MenuItem text="Repair" onAction="#repair"/>
</Menu>
</MenuBar>

Expand Down

0 comments on commit 2d35315

Please sign in to comment.