Add date/time input module

ofw pr 4261 by aaronjamt
This commit is contained in:
MX
2025-12-01 06:06:30 +03:00
parent a7561bee98
commit 6abd2b0e9f
13 changed files with 908 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
#include "example_date_time_input_scene.h"
// Generate scene on_enter handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_enter,
void (*const example_date_time_input_on_enter_handlers[])(void*) = {
#include "example_date_time_input_scene_config.h"
};
#undef ADD_SCENE
// Generate scene on_event handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_event,
bool (*const example_date_time_input_on_event_handlers[])(void* context, SceneManagerEvent event) =
{
#include "example_date_time_input_scene_config.h"
};
#undef ADD_SCENE
// Generate scene on_exit handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_exit,
void (*const example_date_time_input_on_exit_handlers[])(void* context) = {
#include "example_date_time_input_scene_config.h"
};
#undef ADD_SCENE
// Initialize scene handlers configuration structure
const SceneManagerHandlers example_date_time_input_scene_handlers = {
.on_enter_handlers = example_date_time_input_on_enter_handlers,
.on_event_handlers = example_date_time_input_on_event_handlers,
.on_exit_handlers = example_date_time_input_on_exit_handlers,
.scene_num = ExampleDateTimeInputSceneNum,
};

View File

@@ -0,0 +1,29 @@
#pragma once
#include <gui/scene_manager.h>
// Generate scene id and total number
#define ADD_SCENE(prefix, name, id) ExampleDateTimeInputScene##id,
typedef enum {
#include "example_date_time_input_scene_config.h"
ExampleDateTimeInputSceneNum,
} ExampleDateTimeInputScene;
#undef ADD_SCENE
extern const SceneManagerHandlers example_date_time_input_scene_handlers;
// Generate scene on_enter handlers declaration
#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_enter(void*);
#include "example_date_time_input_scene_config.h"
#undef ADD_SCENE
// Generate scene on_event handlers declaration
#define ADD_SCENE(prefix, name, id) \
bool prefix##_scene_##name##_on_event(void* context, SceneManagerEvent event);
#include "example_date_time_input_scene_config.h"
#undef ADD_SCENE
// Generate scene on_exit handlers declaration
#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_exit(void* context);
#include "example_date_time_input_scene_config.h"
#undef ADD_SCENE

View File

@@ -0,0 +1,2 @@
ADD_SCENE(example_date_time_input, input_date_time, InputDateTime)
ADD_SCENE(example_date_time_input, show_date_time, ShowDateTime)

View File

@@ -0,0 +1,47 @@
#include "../example_date_time_input.h"
void example_date_time_input_scene_input_date_time_callback(void* context) {
ExampleDateTimeInput* app = context;
view_dispatcher_send_custom_event(app->view_dispatcher, 0);
}
void example_date_time_input_scene_input_date_time_on_enter(void* context) {
furi_assert(context);
ExampleDateTimeInput* app = context;
DateTimeInput* date_time_input = app->date_time_input;
date_time_input_set_result_callback(
date_time_input,
NULL,
example_date_time_input_scene_input_date_time_callback,
context,
&app->date_time);
date_time_input_set_editable_fields(
date_time_input,
app->edit_date,
app->edit_date,
app->edit_date,
app->edit_time,
app->edit_time,
app->edit_time);
view_dispatcher_switch_to_view(app->view_dispatcher, ExampleDateTimeInputViewIdDateTimeInput);
}
bool example_date_time_input_scene_input_date_time_on_event(void* context, SceneManagerEvent event) {
ExampleDateTimeInput* app = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) { //Back button pressed
scene_manager_previous_scene(app->scene_manager);
return true;
}
return consumed;
}
void example_date_time_input_scene_input_date_time_on_exit(void* context) {
UNUSED(context);
}

View File

@@ -0,0 +1,94 @@
#include "../example_date_time_input.h"
static void
example_date_time_input_scene_confirm_dialog_callback(DialogExResult result, void* context) {
ExampleDateTimeInput* app = context;
view_dispatcher_send_custom_event(app->view_dispatcher, result);
}
static void example_date_time_input_scene_update_view(void* context) {
ExampleDateTimeInput* app = context;
DialogEx* dialog_ex = app->dialog_ex;
dialog_ex_set_header(dialog_ex, "The date and time are", 64, 0, AlignCenter, AlignTop);
uint8_t hour = app->date_time.hour;
char label_hour[4] = "";
if(furi_hal_rtc_get_locale_timeformat() == FuriHalRtcLocaleTimeFormat12h) {
if(hour < 12) {
snprintf(label_hour, sizeof(label_hour), " AM");
} else {
snprintf(label_hour, sizeof(label_hour), " PM");
}
hour %= 12;
if(hour == 0) hour = 12;
}
char buffer[29] = {};
snprintf(
buffer,
sizeof(buffer),
"%04d-%02d-%02d\n%02d:%02d:%02d%s",
app->date_time.year,
app->date_time.month,
app->date_time.day,
hour,
app->date_time.minute,
app->date_time.second,
label_hour);
dialog_ex_set_text(dialog_ex, buffer, 64, 29, AlignCenter, AlignCenter);
dialog_ex_set_left_button_text(dialog_ex, "Date");
dialog_ex_set_right_button_text(dialog_ex, "Time");
dialog_ex_set_center_button_text(dialog_ex, "Both");
dialog_ex_set_result_callback(
dialog_ex, example_date_time_input_scene_confirm_dialog_callback);
dialog_ex_set_context(dialog_ex, app);
}
void example_date_time_input_scene_show_date_time_on_enter(void* context) {
furi_assert(context);
ExampleDateTimeInput* app = context;
example_date_time_input_scene_update_view(app);
view_dispatcher_switch_to_view(app->view_dispatcher, ExampleDateTimeInputViewIdShowDateTime);
}
bool example_date_time_input_scene_show_date_time_on_event(void* context, SceneManagerEvent event) {
ExampleDateTimeInput* app = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
switch(event.event) {
case DialogExResultCenter:
app->edit_date = true;
app->edit_time = true;
scene_manager_next_scene(app->scene_manager, ExampleDateTimeInputSceneInputDateTime);
consumed = true;
break;
case DialogExResultLeft:
app->edit_date = true;
app->edit_time = false;
scene_manager_next_scene(app->scene_manager, ExampleDateTimeInputSceneInputDateTime);
consumed = true;
break;
case DialogExResultRight:
app->edit_date = false;
app->edit_time = true;
scene_manager_next_scene(app->scene_manager, ExampleDateTimeInputSceneInputDateTime);
consumed = true;
break;
default:
break;
}
}
return consumed;
}
void example_date_time_input_scene_show_date_time_on_exit(void* context) {
UNUSED(context);
}