Implemented app result and SelectionDialog (#96)

This commit is contained in:
Ken Van Hoeylandt
2024-11-27 21:13:07 +01:00
committed by GitHub
parent 4744565b0e
commit 6094b9c3f2
13 changed files with 345 additions and 12 deletions
+2
View File
@@ -44,6 +44,7 @@ namespace app {
namespace display { extern const Manifest manifest; }
namespace i2csettings { extern const Manifest manifest; }
namespace power { extern const Manifest manifest; }
namespace selectiondialog { extern const Manifest manifest; }
namespace systeminfo { extern const Manifest manifest; }
namespace textviewer { extern const Manifest manifest; }
namespace wificonnect { extern const Manifest manifest; }
@@ -62,6 +63,7 @@ static const std::vector<const app::Manifest*> system_apps = {
&app::i2csettings::manifest,
&app::imageviewer::manifest,
&app::settings::manifest,
&app::selectiondialog::manifest,
&app::systeminfo::manifest,
&app::textviewer::manifest,
&app::wificonnect::manifest,
+1
View File
@@ -24,6 +24,7 @@ public:
virtual void setData(void* data) = 0;
virtual const Bundle& getParameters() const = 0;
virtual Flags getFlags() const = 0;
virtual void setResult(Result result, const Bundle& bundle = Bundle()) = 0;
};
}
+5
View File
@@ -57,4 +57,9 @@ const Bundle& AppInstance::getParameters() const {
return parameters;
}
void AppInstance::setResult(Result result, const Bundle& bundle) {
std::unique_ptr<ResultHolder> new_holder(new ResultHolder(result, bundle));
resultHolder = std::move(new_holder);
}
} // namespace
+14 -2
View File
@@ -1,6 +1,7 @@
#pragma once
#include <string>
#include <Bundle.h>
#include "CoreDefines.h"
// Forward declarations
@@ -23,11 +24,17 @@ typedef enum {
TypeUser
} Type;
typedef enum {
ResultOk,
ResultCancelled,
ResultError
} Result;
typedef void (*AppOnStart)(App& app);
typedef void (*AppOnStop)(App& app);
typedef void (*AppOnShow)(App& app, lv_obj_t* parent);
typedef void (*AppOnHide)(App& app);
typedef void (*AppOnResult)(App& app, Result result, const Bundle& resultData);
typedef struct Manifest {
/**
@@ -43,7 +50,7 @@ typedef struct Manifest {
/**
* Optional icon.
*/
std::string icon = {};
std::string icon;
/**
* App type affects launch behaviour.
@@ -69,7 +76,12 @@ typedef struct Manifest {
* Non-blocking method, called before gui is destroyed
*/
const AppOnHide _Nullable onHide = nullptr;
} AppManifest;
/**
* Handle the result for apps that are launched
*/
const AppOnResult _Nullable onResult = nullptr;
} Manifest;
struct {
bool operator()(const Manifest* left, const Manifest* right) const { return left->name < right->name; }
@@ -0,0 +1,109 @@
#include "SelectionDialog.h"
#include "Log.h"
#include "lvgl.h"
#include "lvgl/Toolbar.h"
#include "service/loader/Loader.h"
#include <StringUtils.h>
namespace tt::app::selectiondialog {
#define PARAMETER_BUNDLE_KEY_TITLE "title"
#define PARAMETER_BUNDLE_KEY_ITEMS "items"
#define RESULT_BUNDLE_KEY_INDEX "index"
#define PARAMETER_ITEM_CONCATENATION_TOKEN ";;"
#define DEFAULT_TITLE "Select..."
#define TAG "selection_dialog"
void setItemsParameter(Bundle& bundle, const std::vector<std::string>& items) {
std::string result = string_join(items, PARAMETER_ITEM_CONCATENATION_TOKEN);
bundle.putString(PARAMETER_BUNDLE_KEY_ITEMS, result);
}
int32_t getResultIndex(const Bundle& bundle) {
int32_t index = -1;
bundle.optInt32(RESULT_BUNDLE_KEY_INDEX, index);
return index;
}
void setResultIndex(Bundle& bundle, int32_t index) {
bundle.putInt32(RESULT_BUNDLE_KEY_INDEX, index);
}
void setTitleParameter(Bundle& bundle, const std::string& title) {
bundle.putString(PARAMETER_BUNDLE_KEY_TITLE, title);
}
static std::string getTitleParameter(const Bundle& bundle) {
std::string result;
if (bundle.optString(PARAMETER_BUNDLE_KEY_TITLE, result)) {
return result;
} else {
return DEFAULT_TITLE;
}
}
static void onListItemSelected(lv_event_t* e) {
lv_event_code_t code = lv_event_get_code(e);
if (code == LV_EVENT_CLICKED) {
size_t index = (size_t)(e->user_data);
TT_LOG_I(TAG, "Selected item at index %d", index);
tt::app::App* app = service::loader::get_current_app();
Bundle bundle;
setResultIndex(bundle, (int32_t)index);
app->setResult(app::ResultOk, bundle);
service::loader::stop_app();
}
}
static void createChoiceItem(void* parent, const std::string& title, size_t index) {
auto* list = static_cast<lv_obj_t*>(parent);
lv_obj_t* btn = lv_list_add_button(list, nullptr, title.c_str());
lv_obj_add_event_cb(btn, &onListItemSelected, LV_EVENT_CLICKED, (void*)index);
}
static void onShow(App& app, lv_obj_t* parent) {
lv_obj_set_flex_flow(parent, LV_FLEX_FLOW_COLUMN);
std::string title = getTitleParameter(app.getParameters());
lvgl::toolbar_create(parent, title);
lv_obj_t* list = lv_list_create(parent);
lv_obj_set_width(list, LV_PCT(100));
lv_obj_set_flex_grow(list, 1);
const Bundle& parameters = app.getParameters();
std::string items_concatenated;
if (parameters.optString(PARAMETER_BUNDLE_KEY_ITEMS, items_concatenated)) {
std::vector<std::string> items = string_split(items_concatenated, PARAMETER_ITEM_CONCATENATION_TOKEN);
if (items.empty() || items.front().empty()) {
TT_LOG_E(TAG, "No items provided");
app.setResult(ResultError);
service::loader::stop_app();
} else if (items.size() == 1) {
Bundle result_bundle;
setResultIndex(result_bundle, 0);
app.setResult(ResultOk, result_bundle);
service::loader::stop_app();
TT_LOG_W(TAG, "Auto-selecting single item");
} else {
size_t index = 0;
for (const auto& item: items) {
createChoiceItem(list, item, index++);
}
}
} else {
TT_LOG_E(TAG, "No items provided");
app.setResult(ResultError);
service::loader::stop_app();
}
}
extern const Manifest manifest = {
.id = "SelectionDialog",
.name = "Selection Dialog",
.type = TypeHidden,
.onShow = &onShow
};
}
@@ -0,0 +1,25 @@
#pragma once
#include <string>
#include <vector>
#include "Bundle.h"
/**
* Start the app by its ID and provide:
* - an optional title
* - 2 or more items
*
* If you provide 0 items, the app will auto-close.
* If you provide 1 item, the app will auto-close with result index 0
*/
namespace tt::app::selectiondialog {
/** App startup parameters */
void setTitleParameter(Bundle& bundle, const std::string& title);
void setItemsParameter(Bundle& bundle, const std::vector<std::string>& items);
/** App result data */
int32_t getResultIndex(const Bundle& bundle);
}
+3 -3
View File
@@ -83,13 +83,13 @@ lv_obj_t* toolbar_create(lv_obj_t* parent, const std::string& title) {
lv_obj_set_style_pad_all(toolbar->action_container, 0, 0);
lv_obj_set_style_border_width(toolbar->action_container, 0, 0);
toolbar_set_nav_action(obj, LV_SYMBOL_CLOSE, &stop_app, nullptr);
return obj;
}
lv_obj_t* toolbar_create(lv_obj_t* parent, const app::App& app) {
lv_obj_t* toolbar = toolbar_create(parent, app.getManifest().name);
toolbar_set_nav_action(toolbar, LV_SYMBOL_CLOSE, &stop_app, nullptr);
return toolbar;
return toolbar_create(parent, app.getManifest().name);
}
void toolbar_set_title(lv_obj_t* obj, const std::string& title) {
+32 -4
View File
@@ -66,6 +66,7 @@ static void loader_unlock() {
}
LoaderStatus start_app(const std::string& id, bool blocking, const Bundle& bundle) {
TT_LOG_I(TAG, "Start app %s", id.c_str());
tt_assert(loader_singleton);
LoaderMessageLoaderStatusResult result = {
@@ -94,6 +95,7 @@ LoaderStatus start_app(const std::string& id, bool blocking, const Bundle& bundl
}
void stop_app() {
TT_LOG_I(TAG, "Stop app");
tt_check(loader_singleton);
LoaderMessage message(LoaderMessageTypeAppStop);
loader_singleton->queue.put(&message, TtWaitForever);
@@ -257,6 +259,8 @@ static void do_stop_app() {
// Stop current app
app::AppInstance* app_to_stop = loader_singleton->app_stack.top();
std::unique_ptr<app::ResultHolder> result_holder = std::move(app_to_stop->getResult());
const app::Manifest& manifest = app_to_stop->getManifest();
app_transition_to_state(*app_to_stop, app::StateHiding);
app_transition_to_state(*app_to_stop, app::StateStopped);
@@ -268,14 +272,38 @@ static void do_stop_app() {
TT_LOG_I(TAG, "Free heap: %zu", heap_caps_get_free_size(MALLOC_CAP_INTERNAL));
#endif
// Resume previous app
if (original_stack_size > 1) {
}
app::AppInstance* app_to_resume = loader_singleton->app_stack.top();
tt_assert(app_to_resume);
app_transition_to_state(*app_to_resume, app::StateShowing);
auto on_result = app_to_resume->getManifest().onResult;
if (on_result != nullptr) {
if (result_holder != nullptr) {
Bundle* result_bundle = result_holder->resultData;
if (result_bundle != nullptr) {
on_result(
*app_to_resume,
result_holder->result,
*result_bundle
);
} else {
const Bundle empty_bundle;
on_result(
*app_to_resume,
result_holder->result,
empty_bundle
);
}
} else {
const Bundle empty_bundle;
on_result(
*app_to_resume,
app::ResultCancelled,
empty_bundle
);
}
}
loader_unlock();
LoaderEventInternal event_internal = {.type = LoaderEventTypeApplicationStopped};