Refactor app loading and window management (#609)
This commit is contained in:
committed by
GitHub
parent
dc3f6104b8
commit
37c507544b
@@ -1,8 +1,9 @@
|
||||
#include <Tactility/settings/Time.h>
|
||||
|
||||
#include <Tactility/Preferences.h>
|
||||
#include <Tactility/settings/SystemSettings.h>
|
||||
|
||||
#include <tactility/paths.h>
|
||||
#include <tactility/preferences.h>
|
||||
#include <tactility/system_event.h>
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
@@ -17,6 +18,21 @@ constexpr auto* TIMEZONE_PREFERENCES_KEY_NAME = "tz_name";
|
||||
constexpr auto* TIMEZONE_PREFERENCES_KEY_CODE = "tz_code";
|
||||
constexpr auto* TIMEZONE_PREFERENCES_KEY_TIME24 = "tz_time24";
|
||||
|
||||
namespace {
|
||||
|
||||
// Same "time" namespace/file that Ntp.cpp's storeTimeInNvs()/setTimeFromNvs() use for
|
||||
// "syncTime" - matches the shared NVS namespace this used to be.
|
||||
bool getPreferencesPath(std::string& outPath) {
|
||||
char root[128];
|
||||
if (paths_get_user_data_path(root, sizeof(root)) != ERROR_NONE) {
|
||||
return false;
|
||||
}
|
||||
outPath = std::string(root) + "/" + TIME_SETTINGS_NAMESPACE + ".properties";
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void initTimeZone() {
|
||||
#ifdef ESP_PLATFORM
|
||||
auto code= getTimeZoneCode();
|
||||
@@ -28,9 +44,15 @@ void initTimeZone() {
|
||||
}
|
||||
|
||||
void setTimeZone(const std::string& name, const std::string& code) {
|
||||
Preferences preferences(TIME_SETTINGS_NAMESPACE);
|
||||
preferences.putString(TIMEZONE_PREFERENCES_KEY_NAME, name);
|
||||
preferences.putString(TIMEZONE_PREFERENCES_KEY_CODE, code);
|
||||
std::string path;
|
||||
if (getPreferencesPath(path)) {
|
||||
Preferences* preferences = preferences_open(path.c_str());
|
||||
if (preferences != nullptr) {
|
||||
preferences_put_string(preferences, TIMEZONE_PREFERENCES_KEY_NAME, name.c_str());
|
||||
preferences_put_string(preferences, TIMEZONE_PREFERENCES_KEY_CODE, code.c_str());
|
||||
preferences_close(preferences);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
setenv("TZ", code.c_str(), 1);
|
||||
@@ -41,32 +63,49 @@ void setTimeZone(const std::string& name, const std::string& code) {
|
||||
}
|
||||
|
||||
std::string getTimeZoneName() {
|
||||
Preferences preferences(TIME_SETTINGS_NAMESPACE);
|
||||
std::string result;
|
||||
if (preferences.optString(TIMEZONE_PREFERENCES_KEY_NAME, result)) {
|
||||
return result;
|
||||
} else {
|
||||
return "Europe/Amsterdam";
|
||||
std::string path;
|
||||
if (getPreferencesPath(path)) {
|
||||
Preferences* preferences = preferences_open(path.c_str());
|
||||
if (preferences != nullptr) {
|
||||
char buffer[64];
|
||||
error_t error = preferences_opt_string(preferences, TIMEZONE_PREFERENCES_KEY_NAME, buffer, sizeof(buffer));
|
||||
preferences_close(preferences);
|
||||
if (error == ERROR_NONE) {
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
}
|
||||
return "Europe/Amsterdam";
|
||||
}
|
||||
|
||||
bool hasTimeZone() {
|
||||
Preferences preferences(TIME_SETTINGS_NAMESPACE);
|
||||
std::string timezone;
|
||||
if (!preferences.optString(TIMEZONE_PREFERENCES_KEY_NAME, timezone)) {
|
||||
std::string path;
|
||||
if (!getPreferencesPath(path)) {
|
||||
return false;
|
||||
}
|
||||
return !timezone.empty();
|
||||
Preferences* preferences = preferences_open(path.c_str());
|
||||
if (preferences == nullptr) {
|
||||
return false;
|
||||
}
|
||||
bool has = preferences_has_string(preferences, TIMEZONE_PREFERENCES_KEY_NAME);
|
||||
preferences_close(preferences);
|
||||
return has;
|
||||
}
|
||||
|
||||
std::string getTimeZoneCode() {
|
||||
Preferences preferences(TIME_SETTINGS_NAMESPACE);
|
||||
std::string result;
|
||||
if (preferences.optString(TIMEZONE_PREFERENCES_KEY_CODE, result)) {
|
||||
return result;
|
||||
} else {
|
||||
return "CET-1CEST,M3.5.0,M10.5.0/3"; // Default: Europe/Amsterdam
|
||||
std::string path;
|
||||
if (getPreferencesPath(path)) {
|
||||
Preferences* preferences = preferences_open(path.c_str());
|
||||
if (preferences != nullptr) {
|
||||
char buffer[64];
|
||||
error_t error = preferences_opt_string(preferences, TIMEZONE_PREFERENCES_KEY_CODE, buffer, sizeof(buffer));
|
||||
preferences_close(preferences);
|
||||
if (error == ERROR_NONE) {
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
}
|
||||
return "CET-1CEST,M3.5.0,M10.5.0/3"; // Default: Europe/Amsterdam
|
||||
}
|
||||
|
||||
bool isTimeFormat24Hour() {
|
||||
|
||||
Reference in New Issue
Block a user