Skip to content

Commit

Permalink
Fallback to sport name when session name is absent #91
Browse files Browse the repository at this point in the history
  • Loading branch information
pcolby committed Mar 15, 2019
1 parent 61ec8a8 commit 368fc08
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
41 changes: 38 additions & 3 deletions src/polar/v2/trainingsession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1838,9 +1838,44 @@ QString TrainingSession::getOutputBaseFileName(const QString &format)
fileName.replace(QLatin1String("$sessionId"), inputFileNameParts.cap(2));
}

fileName.replace(QLatin1String("$sessionName"),
first(firstMap(parsedSession.value(QLatin1String("session-name")))
.value(QLatin1String("text"))).toString());
// If there are any $sessionName references
if (fileName.contains(QStringLiteral("$sessionName"))) {
// Fetch the session name from the sesion.
QString sessionName = first(firstMap(parsedSession.value(QLatin1String("session-name")))
.value(QLatin1String("text"))).toString();

// If session name is empty (eg common for Vantage V), then fallback to the exercise name.
if (sessionName.isEmpty()) {
// If we haven't parsed the exercise data yet (really only happens in unit test), do so.
if (exerciseCount() < 1) {
parse();
}

// Build a unique set of sport names from the individual exercises in the session.
QSet<QString> sportNames;
foreach (const QVariant &exercise, parsedExercises) {
const QString sportName = getPolarSportName(first(firstMap(exercise.toMap()
.value(CREATE).toMap().value(QStringLiteral("sport")))
.value(QStringLiteral("value"))).toULongLong());
qDebug() << "No session name, found Polar sport name" << sportName;
if (!sportName.isNull()) {
sportNames.insert(sportName);
}
}

// Pick an appropriate session name from the sport names.
if (sportNames.isEmpty()) {
qWarning() << "No session name, and no recognised sport names either";
sessionName = QStringLiteral("Unknown session");
} else if (sportNames.size() > 1) {
qWarning() << "No session name, and multiple unique sport names";
sessionName = QStringLiteral("Multisport");
} else {
sessionName = *sportNames.constBegin();
}
}
fileName.replace(QLatin1String("$sessionName"), sessionName);
}
return fileName;
}

Expand Down
2 changes: 1 addition & 1 deletion test/polar/v2/testtrainingsession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ void TestTrainingSession::getOutputBaseFileName_data()
"|5|191341"
"|C|21:13:41"
"|D|19:13:41"
"|7|"
"|7|Strength training"
"|8|$invalid"
"|9|$$$foo");
}
Expand Down

0 comments on commit 368fc08

Please sign in to comment.