Merge develop into main (#305)

## New features

- Implement translations for apps
- Created `tt::settings::setLanguage` and `::getLanguage()`
- External app errors are now reported to the user via an AlertDialog
- Store system settings in `/data/settings.properties`
- Created a "Region & Language" app and moved the timezone setting there.

## Other changes

- Change `/data` and `/system` filesystem sector size from 4096 to 512 bytes to allow for more small files (60+ files of 4kB were over the limit of 256kB for the filesystem)
- Increased size of `/data` and `/system`
- Moved `tt::time::*` to `tt::settings`
- Removed the timezone setting from the "Time & Date" setting app
- Reverse encoder direction of Lilygo T-Lora Pager
- Improved partability of `Time.cpp` (removed separate set of functions for PC/sim)
This commit is contained in:
Ken Van Hoeylandt
2025-08-28 21:50:29 +02:00
committed by GitHub
parent ee5a5a7181
commit 8c8ccd8783
73 changed files with 1114 additions and 219 deletions
+65
View File
@@ -0,0 +1,65 @@
#include <Tactility/Log.h>
#include <Tactility/settings/Language.h>
#include <utility>
#include <Tactility/settings/SettingsProperties.h>
namespace tt::settings {
constexpr auto* TAG = "Language";
void setLanguage(Language newLanguage) {
SettingsProperties properties;
if (!loadSettingsProperties(properties)) {
return;
}
properties.language = newLanguage;
saveSettingsProperties(properties);
}
Language getLanguage() {
SettingsProperties properties;
if (!loadSettingsProperties(properties)) {
return Language::en_US;
} else {
return properties.language;
}
}
std::string toString(Language language) {
switch (language) {
case Language::en_GB:
return "en-GB";
case Language::en_US:
return "en-US";
case Language::fr_FR:
return "fr-FR";
case Language::nl_BE:
return "nl-BE";
case Language::nl_NL:
return "nl-NL";
default:
TT_LOG_E(TAG, "Missing serialization for language %d", static_cast<int>(language));
std::unreachable();
}
}
bool fromString(const std::string& text, Language& language) {
if (text == "en-GB") {
language = Language::en_GB;
} else if (text == "en-US") {
language = Language::en_US;
} else if (text == "fr-FR") {
language = Language::fr_FR;
} else if (text == "nl-BE") {
language = Language::nl_BE;
} else if (text == "nl-NL") {
language = Language::nl_NL;
} else {
return false;
}
return true;
}
}
@@ -0,0 +1,77 @@
#include <Tactility/Mutex.h>
#include <Tactility/file/FileLock.h>
#include <Tactility/file/PropertiesFile.h>
#include <Tactility/settings/Language.h>
#include <Tactility/settings/SettingsProperties.h>
namespace tt::settings {
constexpr auto* TAG = "SettingsProperties";
constexpr auto* FILE_PATH = "/data/settings.properties";
static Mutex mutex = Mutex();
static bool cached = false;
static SettingsProperties cachedProperties;
static bool loadSettingsPropertiesFromFile(SettingsProperties& properties) {
std::map<std::string, std::string> map;
if (!file::withLock<bool>(FILE_PATH, [&map] {
return file::loadPropertiesFile(FILE_PATH, map);
})) {
TT_LOG_E(TAG, "Failed to load %s", FILE_PATH);
return false;
}
auto language_entry = map.find("language");
if (language_entry != map.end()) {
if (!fromString(language_entry->second, properties.language)) {
TT_LOG_W(TAG, "Unknown language \"%s\" in %s", language_entry->second.c_str(), FILE_PATH);
properties.language = Language::en_US;
}
} else {
properties.language = Language::en_US;
}
auto time_format_entry = map.find("timeFormat24h");
bool time_format_24h = time_format_entry == map.end() ? true : (time_format_entry->second == "true");
properties.timeFormat24h = time_format_24h;
return true;
}
bool loadSettingsProperties(SettingsProperties& properties) {
auto scoped_lock = mutex.asScopedLock();
scoped_lock.lock();
if (!cached) {
if (!loadSettingsPropertiesFromFile(cachedProperties)) {
return false;
}
cached = true;
}
properties = cachedProperties;
return true;
}
bool saveSettingsProperties(const SettingsProperties& properties) {
auto scoped_lock = mutex.asScopedLock();
scoped_lock.lock();
return file::withLock<bool>(FILE_PATH, [&properties] {
std::map<std::string, std::string> map;
map["language"] = toString(properties.language);
map["timeFormat24h"] = properties.timeFormat24h ? "true" : "false";
if (!file::savePropertiesFile(FILE_PATH, map)) {
TT_LOG_E(TAG, "Failed to save %s", FILE_PATH);
return false;
}
cachedProperties = properties;
cached = true;
return true;
});
}
}
+81
View File
@@ -0,0 +1,81 @@
#include <Tactility/settings/Time.h>
#include <Tactility/kernel/SystemEvents.h>
#include <Tactility/Preferences.h>
#include <Tactility/settings/SettingsProperties.h>
#ifdef ESP_PLATFORM
#include <ctime>
#endif
namespace tt::settings {
constexpr auto* TIME_SETTINGS_NAMESPACE = "time";
constexpr auto* TIMEZONE_PREFERENCES_KEY_NAME = "tz_name";
constexpr auto* TIMEZONE_PREFERENCES_KEY_CODE = "tz_code";
constexpr auto* TIMEZONE_PREFERENCES_KEY_TIME24 = "tz_time24";
void initTimeZone() {
#ifdef ESP_PLATFORM
auto code= getTimeZoneCode();
if (!code.empty()) {
setenv("TZ", code.c_str(), 1);
tzset();
}
#endif
}
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);
#ifdef ESP_PLATFORM
setenv("TZ", code.c_str(), 1);
tzset();
#endif
kernel::publishSystemEvent(kernel::SystemEvent::Time);
}
std::string getTimeZoneName() {
Preferences preferences(TIME_SETTINGS_NAMESPACE);
std::string result;
if (preferences.optString(TIMEZONE_PREFERENCES_KEY_NAME, result)) {
return result;
} else {
return {};
}
}
std::string getTimeZoneCode() {
Preferences preferences(TIME_SETTINGS_NAMESPACE);
std::string result;
if (preferences.optString(TIMEZONE_PREFERENCES_KEY_CODE, result)) {
return result;
} else {
return {};
}
}
bool isTimeFormat24Hour() {
SettingsProperties properties;
if (!loadSettingsProperties(properties)) {
return true;
} else {
return properties.timeFormat24h;
}
}
void setTimeFormat24Hour(bool show24Hour) {
SettingsProperties properties;
if (!loadSettingsProperties(properties)) {
return;
}
properties.timeFormat24h = show24Hour;
saveSettingsProperties(properties);
}
}