Commit 0a2a6fb2 authored by Volker Krause's avatar Volker Krause
Browse files

Implement repeated encouragements

parent ce92092d
......@@ -35,6 +35,7 @@
#include <QtTest/qtest.h>
#include <QNetworkReply>
#include <QObject>
#include <QSettings>
#include <QSignalSpy>
#include <QStandardPaths>
......@@ -162,6 +163,56 @@ private slots:
QCOMPARE(p.surveyInterval(), 90);
}
}
void testEncouragement()
{
{
QSettings s;
s.beginGroup(QLatin1String("UserFeedback"));
s.remove(QLatin1String("LastEncouragement"));
}
{
Provider p;
QSignalSpy spy(&p, SIGNAL(showEncouragementMessage()));
QVERIFY(spy.isValid());
p.setEncouragementDelay(0);
QVERIFY(!spy.wait(10));
p.setApplicationStartsUntilEncouragement(0);
p.setApplicationUsageTimeUntilEncouragement(0);
QVERIFY(spy.wait(10));
QCOMPARE(spy.count(), 1);
}
{
Provider p;
QSignalSpy spy(&p, SIGNAL(showEncouragementMessage()));
QVERIFY(spy.isValid());
p.setEncouragementDelay(0);
p.setApplicationStartsUntilEncouragement(0);
p.setApplicationUsageTimeUntilEncouragement(0);
QVERIFY(!spy.wait(10));
}
{
QSettings s;
s.beginGroup(QLatin1String("UserFeedback"));
s.remove(QLatin1String("LastEncouragement"));
}
{
Provider p;
QSignalSpy spy(&p, SIGNAL(showEncouragementMessage()));
QVERIFY(spy.isValid());
p.setEncouragementDelay(1);
p.setApplicationStartsUntilEncouragement(0);
p.setApplicationUsageTimeUntilEncouragement(0);
QVERIFY(!spy.wait(10));
QVERIFY(spy.wait(1200));
QCOMPARE(spy.count(), 1);
}
}
};
QTEST_MAIN(ProviderTest)
......
......@@ -170,7 +170,6 @@ MainWindow::MainWindow() :
m_feedbackProvider->setFeedbackServer(QUrl(QStringLiteral("https://feedback.volkerkrause.eu")));
m_feedbackProvider->setSubmissionInterval(1);
m_feedbackProvider->setApplicationUsageTimeUntilEncouragement(10);
auto viewModeSource = new UserFeedback::PropertyRatioSource(ui->viewStack, "currentIndex", QStringLiteral("viewRatio"));
viewModeSource->setDescription(tr("Usage ratio of the analytics view, survey editor and schema editor."));
viewModeSource->addValueMapping(0, QStringLiteral("analytics"));
......@@ -183,6 +182,11 @@ MainWindow::MainWindow() :
m_feedbackProvider->addDataSource(new StartCountSource, Provider::BasicUsageStatistics);
m_feedbackProvider->addDataSource(new UsageTimeSource, Provider::BasicUsageStatistics);
m_feedbackProvider->setEncouragementDelay(60);
m_feedbackProvider->setEncouragementInterval(5);
m_feedbackProvider->setApplicationStartsUntilEncouragement(5);
m_feedbackProvider->setApplicationUsageTimeUntilEncouragement(10);
auto notifyPopup = new UserFeedback::NotificationPopup(this);
notifyPopup->setFeedbackProvider(m_feedbackProvider);
}
......
......@@ -54,7 +54,7 @@ ProviderPrivate::ProviderPrivate(Provider *qq)
, encouragementStarts(-1)
, encouragementTime(-1)
, encouragementDelay(300)
, encouragementDisplayed(false)
, encouragementInterval(-1)
{
auto domain = QCoreApplication::organizationDomain().split(QLatin1Char('.'));
std::reverse(domain.begin(), domain.end());
......@@ -109,7 +109,7 @@ void ProviderPrivate::load()
startCount = std::max(settings.value(QStringLiteral("ApplicationStartCount"), 0).toInt() + 1, 1);
usageTime = std::max(settings.value(QStringLiteral("ApplicationTime"), 0).toInt(), 0);
encouragementDisplayed = settings.value(QStringLiteral("EncouragementDisplayed"), false).toBool();
lastEncouragementTime = settings.value(QStringLiteral("LastEncouragement")).toDateTime();
}
void ProviderPrivate::store()
......@@ -126,7 +126,7 @@ void ProviderPrivate::store()
settings.setValue(QStringLiteral("ApplicationStartCount"), startCount);
settings.setValue(QStringLiteral("ApplicationTime"), currentApplicationTime());
settings.setValue(QStringLiteral("EncouragementDisplayed"), encouragementDisplayed);
settings.setValue(QStringLiteral("LastEncouragement"), lastEncouragementTime);
foreach (auto source, dataSources)
source->store(&settings);
......@@ -219,7 +219,9 @@ bool ProviderPrivate::selectSurvey(const SurveyInfo &survey) const
void ProviderPrivate::scheduleEncouragement()
{
encouragementTimer.stop();
if (encouragementDisplayed)
// already done, not repetition
if (lastEncouragementTime.isValid() && encouragementInterval <= 0)
return;
if (encouragementStarts < 0 && encouragementTime < 0) // encouragement disabled
......@@ -230,18 +232,27 @@ void ProviderPrivate::scheduleEncouragement()
if (statisticsMode == Provider::DetailedUsageStatistics && surveyInterval == 0) // already everything enabled
return;
// no repetition if some feedback is enabled
if (lastEncouragementTime.isValid() && (statisticsMode > Provider::NoStatistics || surveyInterval >= 0))
return;
Q_ASSERT(encouragementDelay >= 0);
int timeToEncouragement = encouragementDelay;
if (encouragementTime > 0)
timeToEncouragement = std::max(timeToEncouragement, (encouragementTime * 60) - currentApplicationTime());
encouragementTimer.start(timeToEncouragement);
if (lastEncouragementTime.isValid()) {
Q_ASSERT(encouragementInterval > 0);
const auto targetTime = lastEncouragementTime.addDays(encouragementDelay);
timeToEncouragement = std::max(timeToEncouragement, (int)QDateTime::currentDateTime().secsTo(targetTime));
}
encouragementTimer.start(timeToEncouragement * 1000);
}
void ProviderPrivate::emitShowEncouragementMessage()
{
encouragementDisplayed = true; // TODO make this explicit, in case the host application decides to delay?
lastEncouragementTime = QDateTime::currentDateTime(); // TODO make this explicit, in case the host application decides to delay?
emit q->showEncouragementMessage();
store();
}
......@@ -365,6 +376,12 @@ void Provider::setEncouragementDelay(int secs)
d->scheduleEncouragement();
}
void Provider::setEncouragementInterval(int days)
{
d->encouragementInterval = days;
d->scheduleEncouragement();
}
void Provider::setSurveyCompleted(const SurveyInfo &info)
{
d->completedSurveys.push_back(QString::number(info.id()));
......
......@@ -101,6 +101,12 @@ public:
/** Set the delay after application start for the earliest display of the encouragement message. */
void setEncouragementDelay(int secs);
/** Sets the interval after the encouragement should be repeated.
* Encouragement messages are only repeated if no feedback options have been enabled.
* @param days Days between encouragement messages, 0 disables repeated encouragements.
*/
void setEncouragementInterval(int days);
public slots:
/** Manually submit currently recorded data. */
void submit();
......
......@@ -74,10 +74,11 @@ public:
int usageTime;
QTimer encouragementTimer;
QDateTime lastEncouragementTime;
int encouragementStarts;
int encouragementTime;
int encouragementDelay;
bool encouragementDisplayed;
int encouragementInterval;
QVector<AbstractDataSource*> dataSources;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment