Refactor SupportDialog

This commit is contained in:
Reinhard Pointner 2016-04-24 01:05:33 +08:00
parent 21ca4699dd
commit 44c9601173
2 changed files with 124 additions and 66 deletions

View File

@ -3,21 +3,18 @@ package net.filebot;
import static java.awt.GraphicsEnvironment.*;
import static java.util.Arrays.*;
import static java.util.stream.Collectors.*;
import static javax.swing.JOptionPane.*;
import static net.filebot.Logging.*;
import static net.filebot.Settings.*;
import static net.filebot.util.FileUtilities.*;
import static net.filebot.util.XPathUtilities.*;
import static net.filebot.util.ui.SwingUI.*;
import java.awt.Desktop;
import java.awt.Dialog.ModalityType;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
@ -36,7 +33,6 @@ import java.util.logging.Level;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
@ -55,6 +51,7 @@ import net.filebot.ui.MainFrame;
import net.filebot.ui.NotificationHandler;
import net.filebot.ui.PanelBuilder;
import net.filebot.ui.SinglePanelFrame;
import net.filebot.ui.SupportDialog;
import net.filebot.ui.transfer.FileTransferable;
import net.filebot.util.PreferencesMap.PreferencesEntry;
import net.filebot.util.TeePrintStream;
@ -210,17 +207,7 @@ public class Main {
// make sure any long running operations are done now and not later on the shutdown hook thread
HistorySpooler.getInstance().commit();
// show donation / review reminders to power users (more than 2000 renames)
int renameCount = HistorySpooler.getInstance().getPersistentHistoryTotalSize();
if (renameCount > 2000 && Math.random() <= 0.777) {
if (isAppStore()) {
showAppStoreReviewReminder();
} else {
showDonationReminder();
}
}
SupportDialog.maybeShow();
System.exit(0);
}
@ -312,57 +299,6 @@ public class Main {
}
}
private static void showDonationReminder() {
PreferencesEntry<String> donation = Settings.forPackage(Main.class).entry("donation").defaultValue("0");
int donationRev = Integer.parseInt(donation.getValue());
int currentRev = getApplicationRevisionNumber();
if (donationRev >= currentRev) {
return;
}
String message = String.format("<html><p style='font-size:16pt; font-weight:bold'>Thank you for using FileBot!</p><br><p>It has taken many nights to develop this application. If you enjoy using it,<br>please consider a donation to me and my work. It will help to<br>make FileBot even better!<p><p style='font-size:14pt; font-weight:bold'>You've renamed %,d files.</p><br><html>", HistorySpooler.getInstance().getPersistentHistoryTotalSize());
String[] actions = { "Donate! :)", donationRev > 0 ? "Not this time" : "Later" };
JOptionPane pane = new JOptionPane(message, INFORMATION_MESSAGE, YES_NO_OPTION, ResourceManager.getIcon("message.donate"), actions, actions[0]);
pane.createDialog(null, "Please Donate").setVisible(true);
if (pane.getValue() == actions[0]) {
openURI(getDonateURL());
donation.setValue(String.valueOf(currentRev));
} else {
if (donationRev > 0 && donationRev < currentRev) {
donation.setValue(String.valueOf(currentRev));
}
}
}
private static void showAppStoreReviewReminder() {
PreferencesEntry<String> donation = Settings.forPackage(Main.class).entry("review").defaultValue("0");
int donationRev = Integer.parseInt(donation.getValue());
if (donationRev > 0) {
return;
}
// make sure review reminder is shown at most once (per machine)
int currentRev = getApplicationRevisionNumber();
donation.setValue(String.valueOf(currentRev));
String message = String.format("<html><p style='font-size:16pt; font-weight:bold'>Thank you for using FileBot!</p><br><p>It has taken many nights to develop this application. If you enjoy using it,<br>please consider writing a nice little review on the %s.<p><p style='font-size:14pt; font-weight:bold'>You've renamed %,d files.</p><br><html>", getAppStoreName(), HistorySpooler.getInstance().getPersistentHistoryTotalSize());
String[] actions = { "Review! I like FileBot. :)", "Never! Don't bother me again." };
JOptionPane pane = new JOptionPane(message, INFORMATION_MESSAGE, YES_NO_OPTION, ResourceManager.getIcon("window.icon.large"), actions, actions[0]);
pane.createDialog(null, "Please rate FileBot").setVisible(true);
if (pane.getValue() == actions[0]) {
openURI(getAppStoreLink());
}
}
private static void openURI(String uri) {
try {
Desktop.getDesktop().browse(URI.create(uri));
} catch (Exception e) {
debug.log(Level.SEVERE, "Failed to open URI: " + uri, e);
}
}
private static void restoreWindowBounds(final JFrame window, final Settings settings) {
// store bounds on close
window.addWindowListener(new WindowAdapter() {

View File

@ -0,0 +1,122 @@
package net.filebot.ui;
import static javax.swing.JOptionPane.*;
import static net.filebot.Settings.*;
import static net.filebot.util.ui.SwingUI.*;
import javax.swing.Icon;
import javax.swing.JOptionPane;
import net.filebot.HistorySpooler;
import net.filebot.Main;
import net.filebot.ResourceManager;
import net.filebot.Settings;
import net.filebot.util.PreferencesMap.PreferencesEntry;
public enum SupportDialog {
Donation {
@Override
String getMessage(int renameCount) {
return String.format("<html><p style='font-size:16pt; font-weight:bold'>Thank you for using FileBot!</p><br><p>It has taken many nights to develop this application. If you enjoy using it,<br>please consider a donation to me and my work. It will help to<br>make FileBot even better!<p><p style='font-size:14pt; font-weight:bold'>You've renamed %,d files.</p><br><html>", renameCount);
}
@Override
String[] getActions() {
return new String[] { "Donate! :)", "Maybe next time." };
}
@Override
Icon getIcon() {
return ResourceManager.getIcon("message.donate");
}
@Override
String getTitle() {
return "Please Donate";
}
@Override
String getURI() {
return getDonateURL();
}
},
AppStoreReview {
@Override
String getMessage(int renameCount) {
return String.format("<html><p style='font-size:16pt; font-weight:bold'>Thank you for using FileBot!</p><br><p>It has taken many nights to develop this application. If you enjoy using it,<br>please consider writing a nice review on the %s.<p><p style='font-size:14pt; font-weight:bold'>You've renamed %,d files.</p><br><html>", getAppStoreName(), renameCount);
}
@Override
String[] getActions() {
return new String[] { "Review! I like FileBot. :)", "Nope! Maybe next time." };
}
@Override
Icon getIcon() {
return ResourceManager.getIcon("window.icon.large");
}
@Override
String getTitle() {
return "Please write a Review";
}
@Override
String getURI() {
return getAppStoreLink();
}
};
public void show(int renameCount) {
PreferencesEntry<String> support = Settings.forPackage(Main.class).entry("support.revision").defaultValue("0");
int supportRev = Integer.parseInt(support.getValue());
int currentRev = getApplicationRevisionNumber();
if (supportRev >= currentRev) {
return;
}
String message = getMessage(renameCount);
String[] actions = getActions();
JOptionPane pane = new JOptionPane(message, INFORMATION_MESSAGE, YES_NO_OPTION, getIcon(), actions, actions[0]);
pane.createDialog(null, getTitle()).setVisible(true);
// store support revision
support.setValue(String.valueOf(currentRev));
// open URI of OK
if (pane.getValue() == actions[0]) {
openURI(getURI());
}
}
abstract String getMessage(int renameCount);
abstract String[] getActions();
abstract Icon getIcon();
abstract String getTitle();
abstract String getURI();
public static void maybeShow() {
int renameCount = HistorySpooler.getInstance().getPersistentHistoryTotalSize();
// show donation / review reminders to power users (more than 2000 renames)
if (renameCount > 2000 && Math.random() >= 0.777) {
if (isAppStore()) {
AppStoreReview.show(renameCount);
} else {
Donation.show(renameCount);
}
}
}
}