Refactor app access (#205)

- Use `tt::app::` functions to start/stop apps and get current app(context) instead of using loader everywhere
- Removed `tt_service_loader.*` from TactilityC
- Created `tt_app_stop()` for TactilityC
- Bumped version to 0.3.0 to prepare for upcoming release
This commit is contained in:
Ken Van Hoeylandt 2025-02-05 21:57:27 +01:00 committed by GitHub
parent 68e34ca740
commit 6337458992
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
21 changed files with 58 additions and 145 deletions

View File

@ -52,9 +52,8 @@ class AlertDialogApp : public App {
private: private:
static void onButtonClickedCallback(lv_event_t* e) { static void onButtonClickedCallback(lv_event_t* e) {
auto appContext = service::loader::getCurrentAppContext(); auto app = std::static_pointer_cast<AlertDialogApp>(getCurrentApp());
assert(appContext != nullptr); assert(app != nullptr);
auto app = std::static_pointer_cast<AlertDialogApp>(appContext->getApp());
app->onButtonClicked(e); app->onButtonClicked(e);
} }

View File

@ -80,7 +80,7 @@ lv_obj_t* GpioApp::createGpioRowWrapper(lv_obj_t* parent) {
// region Task // region Task
void GpioApp::onTimer(TT_UNUSED std::shared_ptr<void> context) { void GpioApp::onTimer(TT_UNUSED std::shared_ptr<void> context) {
auto appContext = service::loader::getCurrentAppContext(); auto appContext = getCurrentAppContext();
if (appContext->getManifest().id == manifest.id) { if (appContext->getManifest().id == manifest.id) {
auto app = std::static_pointer_cast<GpioApp>(appContext->getApp()); auto app = std::static_pointer_cast<GpioApp>(appContext->getApp());
if (app != nullptr) { if (app != nullptr) {

View File

@ -72,7 +72,7 @@ public:
/** Returns the app data if the app is active. Note that this could clash if the same app is started twice and a background thread is slow. */ /** Returns the app data if the app is active. Note that this could clash if the same app is started twice and a background thread is slow. */
std::shared_ptr<I2cScannerApp> _Nullable optApp() { std::shared_ptr<I2cScannerApp> _Nullable optApp() {
auto appContext = service::loader::getCurrentAppContext(); auto appContext = getCurrentAppContext();
if (appContext != nullptr && appContext->getManifest().id == manifest.id) { if (appContext != nullptr && appContext->getManifest().id == manifest.id) {
return std::static_pointer_cast<I2cScannerApp>(appContext->getApp()); return std::static_pointer_cast<I2cScannerApp>(appContext->getApp());
} else { } else {

View File

@ -58,9 +58,8 @@ private:
} }
static void onButtonClickedCallback(lv_event_t* e) { static void onButtonClickedCallback(lv_event_t* e) {
auto appContext = service::loader::getCurrentAppContext(); auto app = std::static_pointer_cast<InputDialogApp>(getCurrentApp());
assert(appContext != nullptr); assert(app != nullptr);
auto app = std::static_pointer_cast<InputDialogApp>(appContext->getApp());
app->onButtonClicked(e); app->onButtonClicked(e);
} }

View File

@ -21,7 +21,7 @@ class PowerApp;
/** Returns the app data if the app is active. Note that this could clash if the same app is started twice and a background thread is slow. */ /** Returns the app data if the app is active. Note that this could clash if the same app is started twice and a background thread is slow. */
std::shared_ptr<PowerApp> _Nullable optApp() { std::shared_ptr<PowerApp> _Nullable optApp() {
auto appContext = service::loader::getCurrentAppContext(); auto appContext = getCurrentAppContext();
if (appContext != nullptr && appContext->getManifest().id == manifest.id) { if (appContext != nullptr && appContext->getManifest().id == manifest.id) {
return std::static_pointer_cast<PowerApp>(appContext->getApp()); return std::static_pointer_cast<PowerApp>(appContext->getApp());
} else { } else {

View File

@ -21,7 +21,7 @@ namespace tt::app::screenshot {
extern const AppManifest manifest; extern const AppManifest manifest;
class ScreenshotApp : public App { class ScreenshotApp final : public App {
lv_obj_t* modeDropdown = nullptr; lv_obj_t* modeDropdown = nullptr;
lv_obj_t* pathTextArea = nullptr; lv_obj_t* pathTextArea = nullptr;
@ -39,7 +39,7 @@ class ScreenshotApp : public App {
public: public:
ScreenshotApp(); ScreenshotApp();
~ScreenshotApp(); ~ScreenshotApp() final;
void onShow(AppContext& app, lv_obj_t* parent) override; void onShow(AppContext& app, lv_obj_t* parent) override;
void onStartPressed(); void onStartPressed();
@ -50,7 +50,7 @@ public:
/** Returns the app data if the app is active. Note that this could clash if the same app is started twice and a background thread is slow. */ /** Returns the app data if the app is active. Note that this could clash if the same app is started twice and a background thread is slow. */
std::shared_ptr<ScreenshotApp> _Nullable optApp() { std::shared_ptr<ScreenshotApp> _Nullable optApp() {
auto appContext = service::loader::getCurrentAppContext(); auto appContext = getCurrentAppContext();
if (appContext != nullptr && appContext->getManifest().id == manifest.id) { if (appContext != nullptr && appContext->getManifest().id == manifest.id) {
return std::static_pointer_cast<ScreenshotApp>(appContext->getApp()); return std::static_pointer_cast<ScreenshotApp>(appContext->getApp());
} else { } else {

View File

@ -49,9 +49,8 @@ class SelectionDialogApp : public App {
private: private:
static void onListItemSelectedCallback(lv_event_t* e) { static void onListItemSelectedCallback(lv_event_t* e) {
auto appContext = service::loader::getCurrentAppContext(); auto app = std::static_pointer_cast<SelectionDialogApp>(getCurrentApp());
assert(appContext != nullptr); assert(app != nullptr);
auto app = std::static_pointer_cast<SelectionDialogApp>(appContext->getApp());
app->onListItemSelected(e); app->onListItemSelected(e);
} }

View File

@ -93,12 +93,10 @@ private:
static void onListItemSelectedCallback(lv_event_t* e) { static void onListItemSelectedCallback(lv_event_t* e) {
auto index = reinterpret_cast<std::size_t>(lv_event_get_user_data(e)); auto index = reinterpret_cast<std::size_t>(lv_event_get_user_data(e));
auto appContext = service::loader::getCurrentAppContext(); auto app = std::static_pointer_cast<TimeZoneApp>(getCurrentApp());
if (appContext != nullptr && appContext->getManifest().id == manifest.id) { assert(app != nullptr);
auto app = std::static_pointer_cast<TimeZoneApp>(appContext->getApp());
app->onListItemSelected(index); app->onListItemSelected(index);
} }
}
void onListItemSelected(std::size_t index) { void onListItemSelected(std::size_t index) {
TT_LOG_I(TAG, "Selected item at index %zu", index); TT_LOG_I(TAG, "Selected item at index %zu", index);
@ -120,7 +118,7 @@ private:
} }
static void updateTimerCallback(std::shared_ptr<void> context) { static void updateTimerCallback(std::shared_ptr<void> context) {
auto appContext = service::loader::getCurrentAppContext(); auto appContext = getCurrentAppContext();
if (appContext != nullptr && appContext->getManifest().id == manifest.id) { if (appContext != nullptr && appContext->getManifest().id == manifest.id) {
auto app = std::static_pointer_cast<TimeZoneApp>(appContext->getApp()); auto app = std::static_pointer_cast<TimeZoneApp>(appContext->getApp());
app->updateList(); app->updateList();

View File

@ -1,10 +1,11 @@
#include "Tactility/app/wifiapsettings/WifiApSettings.h" #include "Tactility/app/wifiapsettings/WifiApSettings.h"
#include "Tactility/app/App.h"
#include "Tactility/app/AppContext.h" #include "Tactility/app/AppContext.h"
#include "Tactility/app/AppManifest.h"
#include "Tactility/app/alertdialog/AlertDialog.h" #include "Tactility/app/alertdialog/AlertDialog.h"
#include "Tactility/lvgl/Style.h" #include "Tactility/lvgl/Style.h"
#include "Tactility/lvgl/Toolbar.h" #include "Tactility/lvgl/Toolbar.h"
#include "Tactility/service/loader/Loader.h"
#include <Tactility/TactilityCore.h> #include <Tactility/TactilityCore.h>
#include <Tactility/service/wifi/WifiSettings.h> #include <Tactility/service/wifi/WifiSettings.h>
@ -17,20 +18,10 @@ namespace tt::app::wifiapsettings {
extern const AppManifest manifest; extern const AppManifest manifest;
/** Returns the app data if the app is active. Note that this could clash if the same app is started twice and a background thread is slow. */
const std::shared_ptr<AppContext> _Nullable optWifiApSettingsApp() {
auto app = service::loader::getCurrentAppContext();
if (app != nullptr && app->getManifest().id == manifest.id) {
return app;
} else {
return nullptr;
}
}
void start(const std::string& ssid) { void start(const std::string& ssid) {
auto bundle = std::make_shared<Bundle>(); auto bundle = std::make_shared<Bundle>();
bundle->putString("ssid", ssid); bundle->putString("ssid", ssid);
service::loader::startApp(manifest.id, bundle); app::start(manifest.id, bundle);
} }
static void onPressForget(TT_UNUSED lv_event_t* event) { static void onPressForget(TT_UNUSED lv_event_t* event) {
@ -44,11 +35,7 @@ static void onPressForget(TT_UNUSED lv_event_t* event) {
static void onToggleAutoConnect(lv_event_t* event) { static void onToggleAutoConnect(lv_event_t* event) {
lv_event_code_t code = lv_event_get_code(event); lv_event_code_t code = lv_event_get_code(event);
auto app = optWifiApSettingsApp(); auto app = getCurrentAppContext();
if (app == nullptr) {
return;
}
auto parameters = app->getParameters(); auto parameters = app->getParameters();
tt_check(parameters != nullptr, "Parameters missing"); tt_check(parameters != nullptr, "Parameters missing");
@ -125,14 +112,10 @@ class WifiApSettings : public App {
} }
void onResult(TT_UNUSED AppContext& appContext, TT_UNUSED Result result, std::unique_ptr<Bundle> bundle) override { void onResult(TT_UNUSED AppContext& appContext, TT_UNUSED Result result, std::unique_ptr<Bundle> bundle) override {
if (result == Result::Ok && bundle != nullptr) {
auto index = alertdialog::getResultIndex(*bundle); auto index = alertdialog::getResultIndex(*bundle);
if (index == 0) { // Yes if (index == 0) { // Yes
auto app = optWifiApSettingsApp(); auto parameters = appContext.getParameters();
if (app == nullptr) {
return;
}
auto parameters = app->getParameters();
tt_check(parameters != nullptr, "Parameters missing"); tt_check(parameters != nullptr, "Parameters missing");
std::string ssid = parameters->getString("ssid"); std::string ssid = parameters->getString("ssid");
@ -147,12 +130,13 @@ class WifiApSettings : public App {
} }
// Stop self // Stop self
service::loader::stopApp(); app::stop();
} else { } else {
TT_LOG_E(TAG, "Failed to remove SSID"); TT_LOG_E(TAG, "Failed to remove SSID");
} }
} }
} }
}
}; };
extern const AppManifest manifest = { extern const AppManifest manifest = {

View File

@ -25,7 +25,7 @@ void View::resetErrors() {
} }
static void onConnect(TT_UNUSED lv_event_t* event) { static void onConnect(TT_UNUSED lv_event_t* event) {
auto wifi = optWifiConnect(); auto wifi = std::static_pointer_cast<WifiConnect>(getCurrentApp());
auto& view = wifi->getView(); auto& view = wifi->getView();
wifi->getState().setConnectionError(false); wifi->getState().setConnectionError(false);

View File

@ -13,16 +13,6 @@ namespace tt::app::wificonnect {
extern const AppManifest manifest; extern const AppManifest manifest;
/** Returns the app data if the app is active. Note that this could clash if the same app is started twice and a background thread is slow. */
std::shared_ptr<WifiConnect> _Nullable optWifiConnect() {
auto appContext = service::loader::getCurrentAppContext();
if (appContext != nullptr && appContext->getManifest().id == manifest.id) {
return std::static_pointer_cast<WifiConnect>(appContext->getApp());
} else {
return nullptr;
}
}
static void eventCallback(const void* message, void* context) { static void eventCallback(const void* message, void* context) {
auto* event = static_cast<const service::wifi::Event*>(message); auto* event = static_cast<const service::wifi::Event*>(message);
auto* wifi = static_cast<WifiConnect*>(context); auto* wifi = static_cast<WifiConnect*>(context);

View File

@ -36,7 +36,7 @@ static void on_enable_switch_changed(lv_event_t* event) {
if (code == LV_EVENT_VALUE_CHANGED) { if (code == LV_EVENT_VALUE_CHANGED) {
bool is_on = lv_obj_has_state(enable_switch, LV_STATE_CHECKED); bool is_on = lv_obj_has_state(enable_switch, LV_STATE_CHECKED);
auto wifi = optWifiManage(); auto wifi = std::static_pointer_cast<WifiManage>(getCurrentApp());
auto bindings = wifi->getBindings(); auto bindings = wifi->getBindings();
bindings.onWifiToggled(is_on); bindings.onWifiToggled(is_on);

View File

@ -14,16 +14,6 @@ namespace tt::app::wifimanage {
extern const AppManifest manifest; extern const AppManifest manifest;
/** Returns the app data if the app is active. Note that this could clash if the same app is started twice and a background thread is slow. */
std::shared_ptr<WifiManage> _Nullable optWifiManage() {
auto appContext = service::loader::getCurrentAppContext();
if (appContext != nullptr && appContext->getManifest().id == manifest.id) {
return std::static_pointer_cast<WifiManage>(appContext->getApp());
} else {
return nullptr;
}
}
static void onConnect(const char* ssid) { static void onConnect(const char* ssid) {
service::wifi::settings::WifiApSettings settings; service::wifi::settings::WifiApSettings settings;
if (service::wifi::settings::load(ssid, &settings)) { if (service::wifi::settings::load(ssid, &settings)) {

View File

@ -66,7 +66,7 @@ static TickType_t getNextUpdateTime() {
time_t now = ::time(nullptr); time_t now = ::time(nullptr);
struct tm* tm_struct = localtime(&now); struct tm* tm_struct = localtime(&now);
uint32_t seconds_to_wait = 60U - tm_struct->tm_sec; uint32_t seconds_to_wait = 60U - tm_struct->tm_sec;
TT_LOG_I(TAG, "Update in %lu s", seconds_to_wait); TT_LOG_D(TAG, "Update in %lu s", seconds_to_wait);
return pdMS_TO_TICKS(seconds_to_wait * 1000U); return pdMS_TO_TICKS(seconds_to_wait * 1000U);
} }
@ -109,7 +109,7 @@ static const lv_obj_class_t statusbar_class = {
}; };
static void statusbar_pubsub_event(TT_UNUSED const void* message, void* obj) { static void statusbar_pubsub_event(TT_UNUSED const void* message, void* obj) {
TT_LOG_I(TAG, "event"); TT_LOG_D(TAG, "event");
auto* statusbar = static_cast<Statusbar*>(obj); auto* statusbar = static_cast<Statusbar*>(obj);
if (lock(kernel::millisToTicks(100))) { if (lock(kernel::millisToTicks(100))) {
update_main(statusbar); update_main(statusbar);
@ -167,7 +167,7 @@ lv_obj_t* statusbar_create(lv_obj_t* parent) {
lv_obj_set_width(obj, LV_PCT(100)); lv_obj_set_width(obj, LV_PCT(100));
lv_obj_set_height(obj, STATUSBAR_HEIGHT); lv_obj_set_height(obj, STATUSBAR_HEIGHT);
obj_set_style_no_padding(obj); lv_obj_set_style_pad_all(obj, 0, 0);
lv_obj_center(obj); lv_obj_center(obj);
lv_obj_set_flex_flow(obj, LV_FLEX_FLOW_ROW); lv_obj_set_flex_flow(obj, LV_FLEX_FLOW_ROW);
lv_obj_set_flex_align(obj, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER); lv_obj_set_flex_align(obj, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
@ -186,7 +186,7 @@ lv_obj_t* statusbar_create(lv_obj_t* parent) {
for (int i = 0; i < STATUSBAR_ICON_LIMIT; ++i) { for (int i = 0; i < STATUSBAR_ICON_LIMIT; ++i) {
auto* image = lv_image_create(obj); auto* image = lv_image_create(obj);
lv_obj_set_size(image, STATUSBAR_ICON_SIZE, STATUSBAR_ICON_SIZE); lv_obj_set_size(image, STATUSBAR_ICON_SIZE, STATUSBAR_ICON_SIZE);
obj_set_style_no_padding(image); lv_obj_set_style_pad_all(image, 0, 0);
obj_set_style_bg_blacken(image); obj_set_style_bg_blacken(image);
statusbar->icons[i] = image; statusbar->icons[i] = image;
@ -242,7 +242,7 @@ int8_t statusbar_icon_add(const std::string& image) {
statusbar_data.icons[i].visible = !image.empty(); statusbar_data.icons[i].visible = !image.empty();
statusbar_data.icons[i].image = image; statusbar_data.icons[i].image = image;
result = i; result = i;
TT_LOG_I(TAG, "id %d: added", i); TT_LOG_D(TAG, "id %d: added", i);
break; break;
} }
} }
@ -256,7 +256,7 @@ int8_t statusbar_icon_add() {
} }
void statusbar_icon_remove(int8_t id) { void statusbar_icon_remove(int8_t id) {
TT_LOG_I(TAG, "id %d: remove", id); TT_LOG_D(TAG, "id %d: remove", id);
tt_check(id >= 0 && id < STATUSBAR_ICON_LIMIT); tt_check(id >= 0 && id < STATUSBAR_ICON_LIMIT);
statusbar_lock(); statusbar_lock();
StatusbarIcon* icon = &statusbar_data.icons[id]; StatusbarIcon* icon = &statusbar_data.icons[id];
@ -268,7 +268,7 @@ void statusbar_icon_remove(int8_t id) {
} }
void statusbar_icon_set_image(int8_t id, const std::string& image) { void statusbar_icon_set_image(int8_t id, const std::string& image) {
TT_LOG_I(TAG, "id %d: set image %s", id, image.empty() ? "(none)" : image.c_str()); TT_LOG_D(TAG, "id %d: set image %s", id, image.empty() ? "(none)" : image.c_str());
tt_check(id >= 0 && id < STATUSBAR_ICON_LIMIT); tt_check(id >= 0 && id < STATUSBAR_ICON_LIMIT);
if (statusbar_lock(50 / portTICK_PERIOD_MS)) { if (statusbar_lock(50 / portTICK_PERIOD_MS)) {
StatusbarIcon* icon = &statusbar_data.icons[id]; StatusbarIcon* icon = &statusbar_data.icons[id];
@ -280,7 +280,7 @@ void statusbar_icon_set_image(int8_t id, const std::string& image) {
} }
void statusbar_icon_set_visibility(int8_t id, bool visible) { void statusbar_icon_set_visibility(int8_t id, bool visible) {
TT_LOG_I(TAG, "id %d: set visibility %d", id, visible); TT_LOG_D(TAG, "id %d: set visibility %d", id, visible);
tt_check(id >= 0 && id < STATUSBAR_ICON_LIMIT); tt_check(id >= 0 && id < STATUSBAR_ICON_LIMIT);
if (statusbar_lock(50 / portTICK_PERIOD_MS)) { if (statusbar_lock(50 / portTICK_PERIOD_MS)) {
StatusbarIcon* icon = &statusbar_data.icons[id]; StatusbarIcon* icon = &statusbar_data.icons[id];

View File

@ -20,7 +20,7 @@ Gui* gui = nullptr;
void onLoaderMessage(const void* message, TT_UNUSED void* context) { void onLoaderMessage(const void* message, TT_UNUSED void* context) {
auto* event = static_cast<const loader::LoaderEvent*>(message); auto* event = static_cast<const loader::LoaderEvent*>(message);
if (event->type == loader::LoaderEventTypeApplicationShowing) { if (event->type == loader::LoaderEventTypeApplicationShowing) {
auto app_instance = service::loader::getCurrentAppContext(); auto app_instance = app::getCurrentAppContext();
showApp(app_instance); showApp(app_instance);
} else if (event->type == loader::LoaderEventTypeApplicationHiding) { } else if (event->type == loader::LoaderEventTypeApplicationHiding) {
hideApp(); hideApp();

View File

@ -87,7 +87,7 @@ void ScreenshotTask::taskMain() {
} }
} }
} else if (work.type == TASK_WORK_TYPE_APPS) { } else if (work.type == TASK_WORK_TYPE_APPS) {
auto appContext = loader::getCurrentAppContext(); auto appContext = app::getCurrentAppContext();
if (appContext != nullptr) { if (appContext != nullptr) {
const app::AppManifest& manifest = appContext->getManifest(); const app::AppManifest& manifest = appContext->getManifest();
if (manifest.id != last_app_id) { if (manifest.id != last_app_id) {

View File

@ -29,6 +29,9 @@ bool tt_app_has_result(AppHandle handle);
*/ */
void tt_app_start(const char* appId); void tt_app_start(const char* appId);
/** Stop the currently running app */
void tt_app_stop();
/** /**
* Start an app by id and bundle. * Start an app by id and bundle.
* @param[in] appId the app manifest id * @param[in] appId the app manifest id

View File

@ -1,33 +0,0 @@
#pragma once
#include "tt_app.h"
#include "tt_bundle.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* Start an application providing a manifest id and an optional bundle.
* Execution is always deferred.
* This function generally returns immediately unless the scheduler is blocked.
* @param[in] id application manifest id
* @param[in] bundle an allocated bundle (or NULL) of which the memory ownership is handed over to this function
*/
void tt_service_loader_start_app(const char* id, BundleHandle _Nullable bundle);
/**
* Stop the currently active app.
* Execution is always deferred.
* This function generally returns immediately unless the scheduler is blocked.
*/
void tt_service_loader_stop_app();
/**
* Get the context handle of the app that is currently shown on the screen.
*/
AppHandle tt_service_loader_get_current_app();
#ifdef __cplusplus
}
#endif

View File

@ -27,4 +27,8 @@ void tt_app_start_with_bundle(const char* appId, BundleHandle parameters) {
tt::app::start(appId, std::shared_ptr<tt::Bundle>((tt::Bundle*)parameters)); tt::app::start(appId, std::shared_ptr<tt::Bundle>((tt::Bundle*)parameters));
} }
void tt_app_stop() {
tt::app::stop();
}
} }

View File

@ -1,20 +0,0 @@
#include "tt_service_loader.h"
#include <Tactility/Bundle.h>
#include <Tactility/service/loader/Loader.h>
extern "C" {
void tt_service_loader_start_app(const char* id, BundleHandle _Nullable bundle) {
auto shared_bundle = std::shared_ptr<tt::Bundle>((tt::Bundle*)bundle);
tt::service::loader::startApp(id, std::move(shared_bundle));
}
void tt_service_loader_stop_app() {
tt::service::loader::stopApp();
}
AppHandle tt_service_loader_get_current_app() {
return tt::service::loader::getCurrentAppContext().get();
}
}

View File

@ -1 +1 @@
0.2.0 0.3.0