| 18 | return &LittleFS; // always get back to safety |
| 19 | } |
| 20 | bool BruceTheme::openThemeFile(FS *fs, String filepath, bool overwriteConfigSettings) { |
| 21 | |
| 22 | if (fs == nullptr) return true; |
| 23 | if (!fs->exists(filepath)) return false; |
| 24 | File file; |
| 25 | file = fs->open(filepath, FILE_READ); |
| 26 | if (!file) { |
| 27 | log_e("THEME: %s. Using default theme", "Theme file not found"); |
| 28 | removeTheme(); |
| 29 | return false; |
| 30 | } |
| 31 | |
| 32 | // Deserialize the JSON document |
| 33 | JsonDocument jsonDoc; |
| 34 | if (deserializeJson(jsonDoc, file)) { |
| 35 | displayError("5", true); |
| 36 | log_e("THEME: %s. Using default theme", "Failed reading theme file"); |
| 37 | removeTheme(); |
| 38 | return false; |
| 39 | } |
| 40 | themePath = filepath; |
| 41 | String baseThemePath = themePath.substring(0, themePath.lastIndexOf('/')) + "/"; |
| 42 | |
| 43 | ThemeEntry entries[] = { |
| 44 | {"wifi", &theme.wifi, theme.paths.wifi }, |
| 45 | {"ble", &theme.ble, theme.paths.ble }, |
| 46 | {"ethernet", &theme.ethernet, theme.paths.ethernet }, |
| 47 | {"rf", &theme.rf, theme.paths.rf }, |
| 48 | {"rfid", &theme.rfid, theme.paths.rfid }, |
| 49 | {"fm", &theme.fm, theme.paths.fm }, |
| 50 | {"ir", &theme.ir, theme.paths.ir }, |
| 51 | {"files", &theme.files, theme.paths.files }, |
| 52 | {"gps", &theme.gps, theme.paths.gps }, |
| 53 | {"nrf", &theme.nrf, theme.paths.nrf }, |
| 54 | {"interpreter", &theme.interpreter, theme.paths.interpreter}, |
| 55 | {"clock", &theme.clock, theme.paths.clock }, |
| 56 | {"others", &theme.others, theme.paths.others }, |
| 57 | {"connect", &theme.connect, theme.paths.connect }, |
| 58 | {"config", &theme.config, theme.paths.config }, |
| 59 | {"boot_img", &theme.boot_img, theme.paths.boot_img }, |
| 60 | {"boot_sound", &theme.boot_sound, theme.paths.boot_sound }, |
| 61 | {"lora", &theme.lora, theme.paths.lora } |
| 62 | }; |
| 63 | |
| 64 | JsonObject _th = jsonDoc.as<JsonObject>(); |
| 65 | for (auto &entry : entries) { |
| 66 | if (!_th[entry.key].isNull()) { |
| 67 | String path = baseThemePath + _th[entry.key].as<String>(); |
| 68 | if (fs->exists(path)) { |
| 69 | *entry.flag = true; |
| 70 | entry.path = _th[entry.key].as<String>(); |
| 71 | // Pre-cache PNGs into BIN files to avoid runtime decoding and allocations |
| 72 | if (path.endsWith(".png") || path.endsWith(".PNG")) { preparePngBin(*fs, path); } |
| 73 | } else { |
| 74 | log_w("THEME: file not found: %s", entry.key); |
| 75 | } |
| 76 | } |
| 77 | } |
no test coverage detected