2022-04-20 02:54 AM
I'm currently working on my second ever TouchGFX project and keep stumbling upon the same issues as in my first one.
I have a battery indicator that I would like to show on (almost) every screen. For this reason I created a container that I can easily drop on every screen and implemented a few relevant methods on this container. This battery indicator is obviously driven by hardware (a FreeRTOS message is sent from the HW task to the TGFX task, if that makes any difference). Now the Model receives these messages and notifies the ModelListener via a virtual method like `virtual void notifyBatteryVoltage(uint8_t);` or similar.
Here is where it gets interesting to me:
So far, I have always overridden the `notifyBatteryVoltage` method on (almost) all screens' ModelListeners (i.e. Presenters), and implemented another layer of `notifyBatteryVoltage` on the Views that contain the battery indicator. These Views, in turn, will notify the reused container, i.e. call `setBatteryVoltage` on it.
In this way, I wrote the same code over and over again for about ten different Views and ten related Presenters.
I feel like I am missing something fundamental here. Is there any better way to achieve something like this?
Solved! Go to Solution.
2022-04-27 06:34 AM
Yeah, whatever works, works!
2022-04-28 05:13 AM
I also have a 'status bar' container at the top of my screen, which contains a number of icons that need to change based on device status (charging, battery level, etc.). I change the icons' visibility, switch icons based on battery level, and also do fade in/outs. I maintain the status of the icons (alpha values, visibility, etc) in a static structure so that when the container is initialized each time a screen changes, the initialize() method will update everything to the current status.
I also have a handleTickEvent() for the container to perform the realtime screen updates while on any given screen, e.g. performing alpha operations (while also updating the static structure, and setting visibilities, and displaying the correct icons). I do have to have an external declaration of a structure where all of the status data is acquired and updated in a different task, but it's a small price to pay to get my status bar functionality encapsulated in a single file, StatusBar.cpp.
I also use the SIMULATOR definition to declare variables locally rather than externally, so that my status bar can somewhat function in the simulator.
I know I'm breaking some MVP rules, but you gotta do what you gotta do...