Revise SupportDialog behaviour

This commit is contained in:
Reinhard Pointner 2016-09-20 00:24:15 +08:00
parent 4778e9ec63
commit de21009913
3 changed files with 67 additions and 24 deletions

View File

@ -1,9 +1,16 @@
package net.filebot.ui; package net.filebot.ui;
import static java.util.stream.Collectors.*;
import static javax.swing.JOptionPane.*; import static javax.swing.JOptionPane.*;
import static net.filebot.Logging.*;
import static net.filebot.Settings.*; import static net.filebot.Settings.*;
import static net.filebot.util.StringUtilities.*;
import static net.filebot.util.ui.SwingUI.*; 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.Icon;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
@ -78,30 +85,35 @@ public enum SupportDialog {
}; };
public void show(int totalRenameCount) { public boolean feelingLucky(int sessionRenameCount, int totalRenameCount, int currentRevision, int lastSupportRevision, int supportRevisionCount) {
PreferencesEntry<String> support = Settings.forPackage(SupportDialog.class).entry("support.revision").defaultValue("0"); // ask only once per revision
int supportRev = Integer.parseInt(support.getValue()); if (currentRevision <= lastSupportRevision && this == AppStoreReview) {
int currentRev = getApplicationRevisionNumber(); return false;
if (supportRev > 0) {
return;
} }
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 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]); JOptionPane pane = new JOptionPane(message, INFORMATION_MESSAGE, YES_NO_OPTION, getIcon(), actions, actions[0]);
pane.createDialog(null, getTitle()).setVisible(true); pane.createDialog(null, getTitle()).setVisible(true);
// store support revision
support.setValue(String.valueOf(currentRev));
// open URI of OK // open URI of OK
if (pane.getValue() == actions[0]) { if (pane.getValue() == actions[0]) {
openURI(getURI()); openURI(getURI());
return true;
} }
return false;
} }
abstract String getMessage(int renameCount); abstract String getMessage(int totalRenameCount);
abstract String[] getActions(boolean first); abstract String[] getActions(boolean first);
@ -112,21 +124,28 @@ public enum SupportDialog {
abstract String getURI(); abstract String getURI();
public static void maybeShow() { public static void maybeShow() {
int sessionRenameCount = HistorySpooler.getInstance().getSessionHistory().totalSize(); try {
int totalRenameCount = HistorySpooler.getInstance().getPersistentHistoryTotalSize(); PreferencesEntry<String> persistentSupportRevision = Settings.forPackage(SupportDialog.class).entry("support.revision");
List<Integer> supportRevision = matchIntegers(persistentSupportRevision.getValue());
int renameLimit = 1000; int lastSupportRevision = supportRevision.stream().max(Integer::compare).orElse(0);
boolean lucky = Math.random() >= 0.777; int currentRevision = getApplicationRevisionNumber();
// show donation / review reminders to power users int sessionRenameCount = HistorySpooler.getInstance().getSessionHistory().totalSize();
if ((totalRenameCount >= renameLimit && lucky) || sessionRenameCount >= renameLimit) { int totalRenameCount = HistorySpooler.getInstance().getPersistentHistoryTotalSize();
if (isAppStore()) {
AppStoreReview.show(totalRenameCount); // show donation / review reminders to power users
} else { SupportDialog dialog = isAppStore() ? AppStoreReview : Donation;
Donation.show(totalRenameCount);
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);
} }
} }
} }

View File

@ -12,12 +12,13 @@ import net.filebot.mediainfo.MediaInfoTest;
import net.filebot.similarity.EpisodeMetricsTest; import net.filebot.similarity.EpisodeMetricsTest;
import net.filebot.similarity.SimilarityTestSuite; import net.filebot.similarity.SimilarityTestSuite;
import net.filebot.subtitle.SubtitleReaderTestSuite; import net.filebot.subtitle.SubtitleReaderTestSuite;
import net.filebot.ui.SupportDialogTest;
import net.filebot.ui.rename.MatchModelTest; import net.filebot.ui.rename.MatchModelTest;
import net.filebot.util.UtilTestSuite; import net.filebot.util.UtilTestSuite;
import net.filebot.web.WebTestSuite; import net.filebot.web.WebTestSuite;
@RunWith(Suite.class) @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 { public class AllTests {
} }

View File

@ -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)));
}
}