diff --git a/source/net/filebot/ui/SupportDialog.java b/source/net/filebot/ui/SupportDialog.java index 1280cc3c..d17f87c4 100644 --- a/source/net/filebot/ui/SupportDialog.java +++ b/source/net/filebot/ui/SupportDialog.java @@ -1,9 +1,16 @@ package net.filebot.ui; +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.StringUtilities.*; import static net.filebot.util.ui.SwingUI.*; +import java.util.List; +import java.util.logging.Level; +import java.util.stream.Stream; + import javax.swing.Icon; import javax.swing.JOptionPane; @@ -78,30 +85,35 @@ public enum SupportDialog { }; - public void show(int totalRenameCount) { - PreferencesEntry support = Settings.forPackage(SupportDialog.class).entry("support.revision").defaultValue("0"); - int supportRev = Integer.parseInt(support.getValue()); - int currentRev = getApplicationRevisionNumber(); - - if (supportRev > 0) { - return; + public boolean feelingLucky(int sessionRenameCount, int totalRenameCount, int currentRevision, int lastSupportRevision, int supportRevisionCount) { + // ask only once per revision + if (currentRevision <= lastSupportRevision && this == AppStoreReview) { + return false; } + if (sessionRenameCount >= 2000) { + return true; + } + + return totalRenameCount >= 1000 * Math.pow(4, supportRevisionCount) && Math.random() >= 0.777; + } + + public boolean show(int totalRenameCount, boolean first) { String message = getMessage(totalRenameCount); - String[] actions = getActions(supportRev <= 0); + String[] actions = getActions(first); 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()); + return true; } + + return false; } - abstract String getMessage(int renameCount); + abstract String getMessage(int totalRenameCount); abstract String[] getActions(boolean first); @@ -112,21 +124,28 @@ public enum SupportDialog { abstract String getURI(); public static void maybeShow() { - int sessionRenameCount = HistorySpooler.getInstance().getSessionHistory().totalSize(); - int totalRenameCount = HistorySpooler.getInstance().getPersistentHistoryTotalSize(); + try { + PreferencesEntry persistentSupportRevision = Settings.forPackage(SupportDialog.class).entry("support.revision"); + List supportRevision = matchIntegers(persistentSupportRevision.getValue()); - int renameLimit = 1000; - boolean lucky = Math.random() >= 0.777; + int lastSupportRevision = supportRevision.stream().max(Integer::compare).orElse(0); + int currentRevision = getApplicationRevisionNumber(); - // show donation / review reminders to power users - if ((totalRenameCount >= renameLimit && lucky) || sessionRenameCount >= renameLimit) { - if (isAppStore()) { - AppStoreReview.show(totalRenameCount); - } else { - Donation.show(totalRenameCount); + int sessionRenameCount = HistorySpooler.getInstance().getSessionHistory().totalSize(); + int totalRenameCount = HistorySpooler.getInstance().getPersistentHistoryTotalSize(); + + // show donation / review reminders to power users + SupportDialog dialog = isAppStore() ? AppStoreReview : Donation; + + if (dialog.feelingLucky(sessionRenameCount, totalRenameCount, currentRevision, lastSupportRevision, supportRevision.size())) { + if (dialog.show(totalRenameCount, supportRevision.isEmpty())) { + supportRevision = Stream.concat(supportRevision.stream(), Stream.of(currentRevision)).sorted().distinct().collect(toList()); + persistentSupportRevision.setValue(supportRevision.toString()); + } } + } catch (Exception e) { + log.log(Level.WARNING, e, e::toString); } - } } diff --git a/test/net/filebot/AllTests.java b/test/net/filebot/AllTests.java index b7b23757..b0ddeafa 100644 --- a/test/net/filebot/AllTests.java +++ b/test/net/filebot/AllTests.java @@ -12,12 +12,13 @@ import net.filebot.mediainfo.MediaInfoTest; import net.filebot.similarity.EpisodeMetricsTest; import net.filebot.similarity.SimilarityTestSuite; import net.filebot.subtitle.SubtitleReaderTestSuite; +import net.filebot.ui.SupportDialogTest; import net.filebot.ui.rename.MatchModelTest; import net.filebot.util.UtilTestSuite; import net.filebot.web.WebTestSuite; @RunWith(Suite.class) -@SuiteClasses({ SimilarityTestSuite.class, WebTestSuite.class, ExpressionFormatTest.class, VerificationFormatTest.class, MatchModelTest.class, EpisodeMetricsTest.class, SubtitleReaderTestSuite.class, ReleaseInfoTest.class, MediaDetectionTest.class, MediaInfoTest.class, UtilTestSuite.class }) +@SuiteClasses({ ExpressionFormatTest.class, VerificationFormatTest.class, MatchModelTest.class, SupportDialogTest.class, EpisodeMetricsTest.class, ReleaseInfoTest.class, MediaDetectionTest.class, MediaInfoTest.class, SimilarityTestSuite.class, WebTestSuite.class, SubtitleReaderTestSuite.class, UtilTestSuite.class }) public class AllTests { } diff --git a/test/net/filebot/ui/SupportDialogTest.java b/test/net/filebot/ui/SupportDialogTest.java new file mode 100644 index 00000000..92af17db --- /dev/null +++ b/test/net/filebot/ui/SupportDialogTest.java @@ -0,0 +1,23 @@ +package net.filebot.ui; + +import static org.junit.Assert.*; + +import java.util.stream.IntStream; + +import org.junit.Test; + +public class SupportDialogTest { + + @Test + public void feelingLucky() { + assertTrue(SupportDialog.AppStoreReview.feelingLucky(2000, 2000, 500, 400, 0)); + assertFalse(SupportDialog.AppStoreReview.feelingLucky(2000, 2000, 400, 400, 0)); + + assertTrue(SupportDialog.Donation.feelingLucky(2000, 2000, 400, 400, 0)); + assertFalse(SupportDialog.Donation.feelingLucky(100, 100, 400, 400, 0)); + + assertTrue(IntStream.range(0, 100).anyMatch(i -> SupportDialog.Donation.feelingLucky(0, 5000, 400, 400, 0))); + assertFalse(IntStream.range(0, 100).anyMatch(i -> SupportDialog.Donation.feelingLucky(0, 5000, 400, 400, 2))); + } + +}