Open TheOnePerson opened 1 year ago
Converted this to draft again: I feel like I should simplify the data structure for holding face’s settings details a little bit, thus saving 4 bytes per entry. Will take care of it over the next couple of days.
I had to make a small change to the preferences face. When the alarm face is present, too, I may happen that the preferences face restores movement's settings after the alarm face has restored its face settings, resulting in blanking out the alarm indicator erroneously. Tbh, the whole construct of persisting movement settings from the preferences face feels a bit dodgy. The cleaner approach would be to move the logic for saving and restoring the data right into movement.c. But I hesitated to change this very central piece of code without feedback from @joeycastillo.
In this PR, I have put together a solution for persisting data of a watch face trying to keep the logic for storing and reading the data files as encapsulated as possible:
file_settings.c
&file_settings.h
implementing two functionsface_settings_init()
andface_settings_save()
.face_settings_init()
function to initialize the details about the data structure belonging to your particular watch face. The function takes an identifying string for your watch face (must be unique) and a pointer to the data structure that will be saved to flash storage. You can also specify two callback functions (see below). It also checks whether a data file for your watch face already exists and if so, it loads it into the data structure.erase_file
variable tofalse
).data_[hash of identifier string]_[data scheme version]
. The file's content is a binary copy of the data structure.face_settings_save()
. The current settings will only be saved when it’s contents have changed compared to the last call offace_settings_save()
. This comparison is done by calculating a (hopefully) unique 4-byte hash value over the content of the data structure.The implementation of persistent storage for the alarm face looks like this:
#include "face_settings.h"
, of coursealarm_face_setup()
there is only one line added:face_data_init("alarm_face", 0, *context_ptr, sizeof(alarm_state_t), _face_save_data, NULL);
_face_save_data()
is the function dealing with 'cleaning up' the data structure before saving it. It sets some properties to their defaults and looks like this:alarm_face_wants_background_task()
like so:if (now.unit.minute == 0 && (now.unit.hour % 4) == 0) face_data_save(state);
face_data_save(state)
into your*_face_resign()
function, which would lead to immediate saving when the watch face resigns. But since the alarm face had a background-task function anyway, I chose the alternative approach.This PR also extends the following faces, so that they persist their settings:
Any comments welcome! Let me know if you consider this a feasible approach.
P.S.: I had to heavily alter
filesystem.c
, too, so that it now uses the browser's localStorage to save and load files. This is due to easier debugging and to get any persistence at all in the emulator.