Implemented app result and SelectionDialog (#96)
This commit is contained in:
committed by
GitHub
parent
4744565b0e
commit
6094b9c3f2
@@ -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;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user