Clock - use system locale settings

This commit is contained in:
MX 2022-12-19 17:54:59 +03:00
parent 1a4071a7c1
commit da32d252db
No known key found for this signature in database
GPG Key ID: 6C4C311DFD4B4AB5
6 changed files with 15 additions and 169 deletions

View File

@ -5,21 +5,8 @@ App(
entry_point="clock_app",
cdefines=["APP_CLOCK"],
requires=["gui"],
provides=["clock_settings"],
icon="A_Clock_14",
stack_size=2 * 1024,
order=81,
)
App(
appid="clock_settings",
name="Clock",
apptype=FlipperAppType.SETTINGS,
entry_point="clock_settings_app",
requires=["gui","clock"],
stack_size=1 * 1024,
order=20,
)

View File

@ -33,7 +33,7 @@ static void clock_render_callback(Canvas* const canvas, void* ctx) {
char meridian_string[MERIDIAN_LEN];
char timer_string[20];
if(state->settings.time_format == H24) {
if(state->time_format == LocaleTimeFormat24h) {
snprintf(
time_string, TIME_LEN, CLOCK_TIME_FORMAT, curr_dt.hour, curr_dt.minute, curr_dt.second);
} else {
@ -54,9 +54,12 @@ static void clock_render_callback(Canvas* const canvas, void* ctx) {
pm12 ? MERIDIAN_STRING_PM : MERIDIAN_STRING_AM);
}
if(state->settings.date_format == Iso) {
if(state->date_format == LocaleDateFormatYMD) {
snprintf(
date_string, DATE_LEN, CLOCK_ISO_DATE_FORMAT, curr_dt.year, curr_dt.month, curr_dt.day);
} else if(state->date_format == LocaleDateFormatMDY) {
snprintf(
date_string, DATE_LEN, CLOCK_RFC_DATE_FORMAT, curr_dt.month, curr_dt.day, curr_dt.year);
} else {
snprintf(
date_string, DATE_LEN, CLOCK_RFC_DATE_FORMAT, curr_dt.day, curr_dt.month, curr_dt.year);
@ -84,7 +87,7 @@ static void clock_render_callback(Canvas* const canvas, void* ctx) {
canvas_set_font(canvas, FontSecondary);
canvas_draw_str_aligned(canvas, 64, 42, AlignCenter, AlignTop, date_string);
if(state->settings.time_format == H12)
if(state->time_format == LocaleTimeFormat12h)
canvas_draw_str_aligned(canvas, 65, 12, AlignCenter, AlignCenter, meridian_string);
}
if(timer_running) {
@ -95,16 +98,12 @@ static void clock_render_callback(Canvas* const canvas, void* ctx) {
}
static void clock_state_init(ClockState* const state) {
LOAD_CLOCK_SETTINGS(&state->settings);
if(state->settings.time_format != H12 && state->settings.time_format != H24) {
state->settings.time_format = H12;
}
if(state->settings.date_format != Iso && state->settings.date_format != Rfc) {
state->settings.date_format = Iso;
}
FURI_LOG_D(TAG, "Time format: %s", state->settings.time_format == H12 ? "12h" : "24h");
FURI_LOG_D(
TAG, "Date format: %s", state->settings.date_format == Iso ? "ISO 8601" : "RFC 5322");
state->time_format = locale_get_time_format();
state->date_format = locale_get_date_format();
//FURI_LOG_D(TAG, "Time format: %s", state->settings.time_format == H12 ? "12h" : "24h");
//FURI_LOG_D(TAG, "Date format: %s", state->settings.date_format == Iso ? "ISO 8601" : "RFC 5322");
//furi_hal_rtc_get_datetime(&state->datetime);
}

View File

@ -1,7 +1,7 @@
#pragma once
#include <input/input.h>
#include "clock_settings.h"
#include <locale/locale.h>
#define TAG "Clock"
@ -28,7 +28,8 @@ typedef struct {
} PluginEvent;
typedef struct {
ClockSettings settings;
LocaleDateFormat date_format;
LocaleTimeFormat time_format;
FuriHalRtcDateTime datetime;
FuriMutex* mutex;
FuriMessageQueue* event_queue;

View File

@ -1,36 +0,0 @@
#pragma once
#include "clock_settings_filename.h"
#include <furi_hal.h>
#include <stdint.h>
#include <stdbool.h>
#include <toolbox/saved_struct.h>
#include <storage/storage.h>
#define CLOCK_SETTINGS_VER (1)
#define CLOCK_SETTINGS_PATH EXT_PATH(CLOCK_SETTINGS_FILE_NAME)
#define CLOCK_SETTINGS_MAGIC (0xC1)
#define SAVE_CLOCK_SETTINGS(x) \
saved_struct_save( \
CLOCK_SETTINGS_PATH, (x), sizeof(ClockSettings), CLOCK_SETTINGS_MAGIC, CLOCK_SETTINGS_VER)
#define LOAD_CLOCK_SETTINGS(x) \
saved_struct_load( \
CLOCK_SETTINGS_PATH, (x), sizeof(ClockSettings), CLOCK_SETTINGS_MAGIC, CLOCK_SETTINGS_VER)
typedef enum {
H12 = 1,
H24 = 2,
} TimeFormat;
typedef enum {
Iso = 1, // ISO 8601: yyyy-mm-dd
Rfc = 2, // RFC 5322: dd-mm-yyyy
} DateFormat;
typedef struct {
TimeFormat time_format;
DateFormat date_format;
} ClockSettings;

View File

@ -1,102 +0,0 @@
#include <furi.h>
#include <gui/modules/variable_item_list.h>
#include <gui/view_dispatcher.h>
#include <lib/toolbox/value_index.h>
#include "clock_settings.h"
#define TAG "Clock"
typedef struct {
ClockSettings clock_settings;
Gui* gui;
ViewDispatcher* view_dispatcher;
VariableItemList* variable_item_list;
} ClockAppSettings;
static uint32_t clock_app_settings_exit(void* context) {
UNUSED(context);
return VIEW_NONE;
}
#define TIME_FORMAT_COUNT 2
const char* const time_format_text[TIME_FORMAT_COUNT] = {
"12h",
"24h",
};
const uint32_t time_format_value[TIME_FORMAT_COUNT] = {H12, H24};
#define DATE_FORMAT_COUNT 2
const char* const date_format_text[DATE_FORMAT_COUNT] = {
"mm-dd", // ISO 8601
"dd-mm", // RFC 5322
};
const uint32_t date_format_value[DATE_FORMAT_COUNT] = {Iso, Rfc};
static void time_format_changed(VariableItem* item) {
ClockAppSettings* app = variable_item_get_context(item);
uint8_t index = variable_item_get_current_value_index(item);
variable_item_set_current_value_text(item, time_format_text[index]);
app->clock_settings.time_format = time_format_value[index];
}
static void date_format_changed(VariableItem* item) {
ClockAppSettings* app = variable_item_get_context(item);
uint8_t index = variable_item_get_current_value_index(item);
variable_item_set_current_value_text(item, date_format_text[index]);
app->clock_settings.date_format = date_format_value[index];
}
static ClockAppSettings* alloc_settings() {
ClockAppSettings* app = malloc(sizeof(ClockAppSettings));
LOAD_CLOCK_SETTINGS(&app->clock_settings);
app->gui = furi_record_open(RECORD_GUI);
app->variable_item_list = variable_item_list_alloc();
View* view = variable_item_list_get_view(app->variable_item_list);
view_set_previous_callback(view, clock_app_settings_exit);
VariableItem* item;
uint8_t value_index;
item = variable_item_list_add(
app->variable_item_list, "Clock format", TIME_FORMAT_COUNT, time_format_changed, app);
value_index = value_index_uint32(
(uint32_t)(app->clock_settings.time_format), time_format_value, TIME_FORMAT_COUNT);
//FURI_LOG_T(TAG, "Time format index: %u", value_index);
variable_item_set_current_value_index(item, value_index);
variable_item_set_current_value_text(item, time_format_text[value_index]);
item = variable_item_list_add(
app->variable_item_list, "Date format", DATE_FORMAT_COUNT, date_format_changed, app);
value_index = value_index_uint32(
(uint32_t)(app->clock_settings.date_format), date_format_value, DATE_FORMAT_COUNT);
//FURI_LOG_T(TAG, "Date format index: %u", value_index);
variable_item_set_current_value_index(item, value_index);
variable_item_set_current_value_text(item, date_format_text[value_index]);
app->view_dispatcher = view_dispatcher_alloc();
view_dispatcher_enable_queue(app->view_dispatcher);
view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
view_dispatcher_add_view(app->view_dispatcher, 0, view);
view_dispatcher_switch_to_view(app->view_dispatcher, 0);
return app;
}
static void free_settings(ClockAppSettings* app) {
view_dispatcher_remove_view(app->view_dispatcher, 0);
variable_item_list_free(app->variable_item_list);
view_dispatcher_free(app->view_dispatcher);
furi_record_close(RECORD_GUI);
SAVE_CLOCK_SETTINGS(&app->clock_settings);
free(app);
}
extern int32_t clock_settings_app(void* p) {
UNUSED(p);
ClockAppSettings* app = alloc_settings();
view_dispatcher_run(app->view_dispatcher);
free_settings(app);
return 0;
}

View File

@ -1,3 +0,0 @@
#pragma once
#define CLOCK_SETTINGS_FILE_NAME ".clock.settings"