cancel
Showing results for 
Search instead for 
Did you mean: 

Determining the current view

Chandler
Associate II

Suppose there are two views: "Screen1" and "Screen2". Depending on which screen is active, we want to do something (like enable or disable an interrupt). Within the Model, what is the proper way to determine which screen is active? Is there an API that returns a string or number, and can someone point me to a simple example of the syntax? Thanks

1 ACCEPTED SOLUTION

Accepted Solutions

Hello Chandler,

Thank you very much for these precisions. This is now clearer to me.

Since the Model needs the modelListener to interact with Screens, there's no direct way inside TouchGFX to know in the model the current Screen.

However, there's a simple solution to your problem and it is to use the MVP concept.

In other words, in the Model you declare a variable (can be an instance of a struct, an enum or a simple uint8_t for example) that will indicate which screen is currently active. Then, in every screenView, in the initialize() function, you set the variable in the model to the value corresponding to the current screen.

Finally, you just need to check this variable in your Model::tick() to suspend or enable your tasks.

Hope this answers your question.

/Alexandre

View solution in original post

6 REPLIES 6
Alexandre RENOUX
Principal

Hello Chandler,

The Model has a pointer to your currently active Presenter. The type of this pointer is an interface (ModelListener) which you can modify to reflect the application-specific events that are appropriate.

So the flow of things is Model -> ModelListener -> Prsenter1 or Presenter2 (the active one) -> Screen1 or Screen2 (the active one)

Here is a code example :

Model.cpp

if(modelListener != 0)
 {
         modelListener->setNewValue(val);
  }

ModelListener.hpp

virtual void setNewValue(uint8_t value) {}

Now if you implement setNewValue in Prsenter1 and Presenter2, only the active presenter's function will be called. From the presenter, it is easy to access the ScreenView.

Have a look at the following articles :

https://support.touchgfx.com/docs/development/ui-development/software-architecture/model-view-presenter-design-pattern

https://support.touchgfx.com/docs/development/ui-development/touchgfx-engine-features/backend-communication/#propagating-data-to-ui

When your question is answered, please close this topic by choosing Select as Best.

/Alexandre

Thank you, Alexandre. I am familiar with the process you have outlined, and I have used it. Let me restate my question like this: Suppose a certain screen is active. How do I determine which screen it is? For instance, what call can I make from within the Model to know whether Screen1 or Screen2 is the current view? Is there an API that returns this information, and if so, can you show me the syntax? Thanks again.

Not that I know of.

But if you use the ModelListener interface, there's no need to know the active screen by calling an API since the modelListener will always link to the active presenter. Sorry but I don't understand your use case.

/Alexandre

Alexandre - On each call of the Model click, I want to check if the screen has changed from the previous time, and if so, to place a messgae in a general mailbox to be read by tasks running in main(). Those tasks need to be selectively enabled or suspended, depending on which screen happens to be active. So: is there a way to return the name of the currently active screen when the Model tick function is called? That is all I need to know. Thanks

Hello Chandler,

Thank you very much for these precisions. This is now clearer to me.

Since the Model needs the modelListener to interact with Screens, there's no direct way inside TouchGFX to know in the model the current Screen.

However, there's a simple solution to your problem and it is to use the MVP concept.

In other words, in the Model you declare a variable (can be an instance of a struct, an enum or a simple uint8_t for example) that will indicate which screen is currently active. Then, in every screenView, in the initialize() function, you set the variable in the model to the value corresponding to the current screen.

Finally, you just need to check this variable in your Model::tick() to suspend or enable your tasks.

Hope this answers your question.

/Alexandre

Very good, that confirms what I suspected. I actually started down this path at first and thought, "surely there must be an API for this!" I will go back to that method, confident that this is how it should be done. Please take it as a suggestion for improvement to the ST team that they consider adding a way of getting the current screen, as this is a very practical consideration when we deal with any code directly outside the "MVP" graphics stack, i.e., anything running in a task in main() or another source file handling motors, sensors, etc. Thanks