Skip to main content
GBert.2
Senior
November 16, 2021
Question

What is the best way to update a custom component across multiple Screens?

  • November 16, 2021
  • 2 replies
  • 1883 views

I'm building a complex application with multiple screens in TouchGFX 4.16.

All those screens have the same menu bar with indications like battery energy level, time, etc.

All screens include the MenuBar custom component.

When the battery level changes, I want the model to be able to update MenuBar.

Currently, I'm doing this to update the battery:

In Model.cpp

 bool bShouldNotify = pollBatteryState();
 if (bShouldNotify)
 {
 modelListener->notifyBatteryStateChanged(m_oDeviceState.oBattery);
 }

In SomeScreen1Presenter.cpp

void SomeScreenPresenter::notifyBatteryStateChanged(oBatteryState_t const oBatteryState)
{
 view.updateBatteryState(oBatteryState);
}

In SomeScreen1View.cpp

void SomeScreenView::updateBatteryState(oBatteryState_t const oBatteryState)
{
 // Update widgets
 MenuBar.updateBattery(oBatteryState);
}

If you need to do this for all screens, this will create a lot of code.

Is there a better way to update views that all use the same component?

An event-based system maybe?

Regards,

GB

This topic has been closed for replies.

2 replies

GBert.2
GBert.2Author
Senior
November 16, 2021

Maybe we could define a general function to pass event

 In model.cpp

modelListener->notify(MY_EVENT);

In view.cpp

void MonitoringView::update(Event const event)
{
 switch (event)
 {
 case MY_EVENT: 
 data = pollTheNeededData();
 menuBar.update(data)
 break;
 }
}

But now the pollTheDataFromModel() function needs to be implemented for all presenters...

I'm not sure if it's worth it or if it respects the View-Model-Presenter architecture.

MM..1
Chief III
November 16, 2021

Maybe you can create self updated component and register to tick handler

Application::getInstance()->registerTimerWidget(this);

And create

void Component::handleTickEvent()
{
if (externalstruct.update)
 {
 // showed
 externalstruct.update = false;
 xxxx.show(externalstruct.baterylevel);
 }
}

GBert.2
GBert.2Author
Senior
November 16, 2021

That's an interesting idea.

In your example, how would your component access the`externalstruct` in the mode?

To my knowledge, inside a component, we don't have access to the Presenter Object.

void Container::handleTickEvent()
{
 externalstruct = presenter->getExternalStruct(); //< [!] not possibe
 if (externalstruct.update)
 {
 // showed
 externalstruct.update = false;
 }
}

 Also, where do you register your tick? In the constructor?

MM..1
Chief III
November 17, 2021

external struct is normal C variable included as extern C

You can share it over memory access in any thread on single core and skip MVP .