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:
static void onButtonClickedCallback(lv_event_t* e) {
auto appContext = service::loader::getCurrentAppContext();
assert(appContext != nullptr);
auto app = std::static_pointer_cast<AlertDialogApp>(appContext->getApp());
auto app = std::static_pointer_cast<AlertDialogApp>(getCurrentApp());
assert(app != nullptr);
app->onButtonClicked(e);
}

View File

@ -80,7 +80,7 @@ lv_obj_t* GpioApp::createGpioRowWrapper(lv_obj_t* parent) {
// region Task
void GpioApp::onTimer(TT_UNUSED std::shared_ptr<void> context) {
auto appContext = service::loader::getCurrentAppContext();
auto appContext = getCurrentAppContext();
if (appContext->getManifest().id == manifest.id) {
auto app = std::static_pointer_cast<GpioApp>(appContext->getApp());
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. */
std::shared_ptr<I2cScannerApp> _Nullable optApp() {
auto appContext = service::loader::getCurrentAppContext();
auto appContext = getCurrentAppContext();
if (appContext != nullptr && appContext->getManifest().id == manifest.id) {
return std::static_pointer_cast<I2cScannerApp>(appContext->getApp());
} else {

View File

@ -58,9 +58,8 @@ private:
}
static void onButtonClickedCallback(lv_event_t* e) {
auto appContext = service::loader::getCurrentAppContext();
assert(appContext != nullptr);
auto app = std::static_pointer_cast<InputDialogApp>(appContext->getApp());
auto app = std::static_pointer_cast<InputDialogApp>(getCurrentApp());
assert(app != nullptr);
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. */
std::shared_ptr<PowerApp> _Nullable optApp() {
auto appContext = service::loader::getCurrentAppContext();
auto appContext = getCurrentAppContext();
if (appContext != nullptr && appContext->getManifest().id == manifest.id) {
return std::static_pointer_cast<PowerApp>(appContext->getApp());
} else {

View File

@ -21,7 +21,7 @@ namespace tt::app::screenshot {
extern const AppManifest manifest;
class ScreenshotApp : public App {
class ScreenshotApp final : public App {
lv_obj_t* modeDropdown = nullptr;
lv_obj_t* pathTextArea = nullptr;
@ -39,7 +39,7 @@ class ScreenshotApp : public App {
public:
ScreenshotApp();
~ScreenshotApp();
~ScreenshotApp() final;
void onShow(AppContext& app, lv_obj_t* parent) override;
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. */
std::shared_ptr<ScreenshotApp> _Nullable optApp() {
auto appContext = service::loader::getCurrentAppContext();
auto appContext = getCurrentAppContext();
if (appContext != nullptr && appContext->getManifest().id == manifest.id) {
return std::static_pointer_cast<ScreenshotApp>(appContext->getApp());
} else {

View File

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

View File

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

View File

@ -1,10 +1,11 @@
#include "Tactility/app/wifiapsettings/WifiApSettings.h"
#include "Tactility/app/App.h"
#include "Tactility/app/AppContext.h"
#include "Tactility/app/AppManifest.h"
#include "Tactility/app/alertdialog/AlertDialog.h"
#include "Tactility/lvgl/Style.h"
#include "Tactility/lvgl/Toolbar.h"
#include "Tactility/service/loader/Loader.h"
#include <Tactility/TactilityCore.h>
#include <Tactility/service/wifi/WifiSettings.h>
@ -17,20 +18,10 @@ namespace tt::app::wifiapsettings {
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) {
auto bundle = std::make_shared<Bundle>();
bundle->putString("ssid", ssid);
service::loader::startApp(manifest.id, bundle);
app::start(manifest.id, bundle);
}
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) {
lv_event_code_t code = lv_event_get_code(event);
auto app = optWifiApSettingsApp();
if (app == nullptr) {
return;
}
auto app = getCurrentAppContext();
auto parameters = app->getParameters();
tt_check(parameters != nullptr, "Parameters missing");
@ -125,31 +112,28 @@ class WifiApSettings : public App {
}
void onResult(TT_UNUSED AppContext& appContext, TT_UNUSED Result result, std::unique_ptr<Bundle> bundle) override {
auto index = alertdialog::getResultIndex(*bundle);
if (index == 0) { // Yes
auto app = optWifiApSettingsApp();
if (app == nullptr) {
return;
}
if (result == Result::Ok && bundle != nullptr) {
auto index = alertdialog::getResultIndex(*bundle);
if (index == 0) { // Yes
auto parameters = appContext.getParameters();
tt_check(parameters != nullptr, "Parameters missing");
auto parameters = app->getParameters();
tt_check(parameters != nullptr, "Parameters missing");
std::string ssid = parameters->getString("ssid");
if (service::wifi::settings::remove(ssid.c_str())) {
TT_LOG_I(TAG, "Removed SSID");
std::string ssid = parameters->getString("ssid");
if (service::wifi::settings::remove(ssid.c_str())) {
TT_LOG_I(TAG, "Removed SSID");
if (
service::wifi::getRadioState() == service::wifi::RadioState::ConnectionActive &&
service::wifi::getConnectionTarget() == ssid
) {
service::wifi::disconnect();
}
if (
service::wifi::getRadioState() == service::wifi::RadioState::ConnectionActive &&
service::wifi::getConnectionTarget() == ssid
) {
service::wifi::disconnect();
// Stop self
app::stop();
} else {
TT_LOG_E(TAG, "Failed to remove SSID");
}
// Stop self
service::loader::stopApp();
} else {
TT_LOG_E(TAG, "Failed to remove SSID");
}
}
}

View File

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

View File

@ -13,16 +13,6 @@ namespace tt::app::wificonnect {
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) {
auto* event = static_cast<const service::wifi::Event*>(message);
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) {
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();
bindings.onWifiToggled(is_on);

View File

@ -14,16 +14,6 @@ namespace tt::app::wifimanage {
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) {
service::wifi::settings::WifiApSettings settings;
if (service::wifi::settings::load(ssid, &settings)) {

View File

@ -66,7 +66,7 @@ static TickType_t getNextUpdateTime() {
time_t now = ::time(nullptr);
struct tm* tm_struct = localtime(&now);
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);
}
@ -109,7 +109,7 @@ static const lv_obj_class_t statusbar_class = {
};
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);
if (lock(kernel::millisToTicks(100))) {
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_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_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);
@ -186,7 +186,7 @@ lv_obj_t* statusbar_create(lv_obj_t* parent) {
for (int i = 0; i < STATUSBAR_ICON_LIMIT; ++i) {
auto* image = lv_image_create(obj);
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);
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].image = image;
result = i;
TT_LOG_I(TAG, "id %d: added", i);
TT_LOG_D(TAG, "id %d: added", i);
break;
}
}
@ -256,7 +256,7 @@ int8_t statusbar_icon_add() {
}
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);
statusbar_lock();
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) {
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);
if (statusbar_lock(50 / portTICK_PERIOD_MS)) {
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) {
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);
if (statusbar_lock(50 / portTICK_PERIOD_MS)) {
StatusbarIcon* icon = &statusbar_data.icons[id];

View File

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

View File

@ -87,7 +87,7 @@ void ScreenshotTask::taskMain() {
}
}
} else if (work.type == TASK_WORK_TYPE_APPS) {
auto appContext = loader::getCurrentAppContext();
auto appContext = app::getCurrentAppContext();
if (appContext != nullptr) {
const app::AppManifest& manifest = appContext->getManifest();
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);
/** Stop the currently running app */
void tt_app_stop();
/**
* Start an app by id and bundle.
* @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));
}
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