/
DemienMedich
/
BodyweightBase
Обзор
Документация
Войти
/
DemienMedich
/
BodyweightBase
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
develop
app/main.cpp
109 строк
5 KB
DemienMedich
feat: add workout reminder actions
03 авг 2026, 00:22
03 авг 2026, 00:22
fced454
Код
Авторство
О чём код?
#include <QGuiApplication> #include <QFileInfo> #include <QFile> #include <QJsonDocument> #include <QJsonObject> #include <QQmlApplicationEngine> #include <QQmlContext> #include <QQuickStyle> #include <QTimer> #include <QTemporaryDir> #include <memory> #ifdef Q_OS_ANDROID #include <QJniObject> #include <QtCore/qcoreapplication_platform.h> #endif #include "session_controller.h" int main(int argc, char *argv[]) { QQuickStyle::setStyle("Basic"); QGuiApplication app(argc, argv); app.setApplicationName("BodyweightBaseCpp"); const QString executableName = QFileInfo(app.applicationFilePath()).baseName(); const bool isolatedUiTest = app.arguments().contains(QStringLiteral("--ui-test")) || executableName.contains(QStringLiteral("MobileTest"), Qt::CaseInsensitive); QTemporaryDir uiTestDirectory; std::unique_ptr<SessionController> controllerOwner; if (isolatedUiTest) { const QString statePath = uiTestDirectory.filePath(QStringLiteral("native-state.json")); QFile::copy(QStringLiteral(":/resources/default-state.json"), statePath); controllerOwner = std::make_unique<SessionController>( uiTestDirectory.path(), QStringLiteral(":/resources/exercises.json")); } else { controllerOwner = std::make_unique<SessionController>(); } SessionController &controller = *controllerOwner; QObject::connect(&app, &QCoreApplication::aboutToQuit, &controller, &SessionController::prepareForExit); QTimer dayRefreshTimer; dayRefreshTimer.setInterval(60000); QObject::connect(&dayRefreshTimer, &QTimer::timeout, &controller, &SessionController::refreshForCurrentDay); dayRefreshTimer.start(); QQmlApplicationEngine engine; engine.rootContext()->setContextProperty("sessionController", &controller); #ifdef Q_OS_ANDROID const auto androidContext = QNativeInterface::QAndroidApplication::context(); if (androidContext.isValid()) { QJniObject::callStaticMethod<void>( "org/bodyweightbase/android/HealthConnectExporter", "scheduleAutomation", "(Landroid/content/Context;)V", androidContext.object<jobject>()); } engine.loadFromModule("BodyweightBase.App", "MobileMain"); const auto applyWorkoutLaunch = [&controller, &engine]() { const auto context = QNativeInterface::QAndroidApplication::context(); if (!context.isValid()) return; const QJniObject payload = QJniObject::callStaticObjectMethod( "org/bodyweightbase/android/MainActivity", "consumeWorkoutLaunch", "(Landroid/content/Context;)Ljava/lang/String;", context.object<jobject>()); if (!payload.isValid()) return; QJsonParseError parseError; const QJsonDocument document = QJsonDocument::fromJson( payload.toString().toUtf8(), &parseError); if (parseError.error != QJsonParseError::NoError || !document.isObject()) return; const QJsonObject request = document.object(); if (!request.value("openWorkout").toBool()) return; const QString profileId = request.value("profileId").toString().trimmed(); const QString planId = request.value("planId").toString().trimmed(); if (!profileId.isEmpty()) controller.selectProfile(profileId); if (!planId.isEmpty()) controller.selectPlan(planId); if (!engine.rootObjects().isEmpty()) engine.rootObjects().first()->setProperty("currentTab", 0); if (request.value("startQuickWorkout").toBool()) controller.startQuickWorkout(0); }; QTimer::singleShot(0, &controller, applyWorkoutLaunch); QTimer workoutLaunchTimer; workoutLaunchTimer.setInterval(500); QObject::connect(&workoutLaunchTimer, &QTimer::timeout, &controller, applyWorkoutLaunch); workoutLaunchTimer.start(); QObject::connect(&app, &QGuiApplication::applicationStateChanged, &controller, [&controller, applyWorkoutLaunch](Qt::ApplicationState state) { if (state == Qt::ApplicationActive) { controller.resumeWorkoutClock(); controller.refreshForCurrentDay(); QTimer::singleShot(100, &controller, applyWorkoutLaunch); QTimer::singleShot(1000, &controller, &SessionController::autoSyncHealthConnect); } else { controller.suspendWorkoutClock(); } }); QTimer::singleShot(1500, &controller, &SessionController::autoSyncHealthConnect); #else const bool useMobileUi = app.arguments().contains(QStringLiteral("--mobile")) || executableName.contains(QStringLiteral("Mobile"), Qt::CaseInsensitive); engine.loadFromModule("BodyweightBase.App", useMobileUi ? "MobileMain" : "Main"); #endif return engine.rootObjects().isEmpty() ? 1 : app.exec(); }