From 44c9601173d9b8b8e813df3e4c971447cfd88a97 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sun, 24 Apr 2016 01:05:33 +0800 Subject: [PATCH] Refactor SupportDialog --- source/net/filebot/Main.java | 68 +------------ source/net/filebot/ui/SupportDialog.java | 122 +++++++++++++++++++++++ 2 files changed, 124 insertions(+), 66 deletions(-) create mode 100644 source/net/filebot/ui/SupportDialog.java diff --git a/source/net/filebot/Main.java b/source/net/filebot/Main.java index 3e9a24ae..ebff7980 100644 --- a/source/net/filebot/Main.java +++ b/source/net/filebot/Main.java @@ -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 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("

Thank you for using FileBot!


It has taken many nights to develop this application. If you enjoy using it,
please consider a donation to me and my work. It will help to
make FileBot even better!

You've renamed %,d files.


", 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 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("

Thank you for using FileBot!


It has taken many nights to develop this application. If you enjoy using it,
please consider writing a nice little review on the %s.

You've renamed %,d files.


", 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() { diff --git a/source/net/filebot/ui/SupportDialog.java b/source/net/filebot/ui/SupportDialog.java new file mode 100644 index 00000000..aaaa767b --- /dev/null +++ b/source/net/filebot/ui/SupportDialog.java @@ -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("

Thank you for using FileBot!


It has taken many nights to develop this application. If you enjoy using it,
please consider a donation to me and my work. It will help to
make FileBot even better!

You've renamed %,d files.


", 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("

Thank you for using FileBot!


It has taken many nights to develop this application. If you enjoy using it,
please consider writing a nice review on the %s.

You've renamed %,d files.


", 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 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); + } + } + } + +}