I have an application which contains TextViews and ImageButtons organized in various layouts.
This application receives data from a server and this data is used to fill in TextViews and change ImageButtons icons.
The screen shows the data of a single agent and the system must support multiple agents (let’s say two for simplicity, foreground and background).
I have to provide a menu to choose of which agent to display data on the Views (when foreground and background agents are switched, I need to show the latest values I received for the ex-background agent).
Only the data of a single agent can be displayed at any given time on the screen and I cannot use multiple activities.
This problem may have many solutions, but I’m looking for a smart design which can allow me to save some time during development.
A base attempt:
- Do not duplicate Views, only duplicate content (icon status, strings…)
- Every time an update for an agent is received, save it in the data structure of that agent (do not miss “background” agent data)
- Only update the views for the current “foreground” agent
- When the user requests an agent switch (display data of the background agent), update all the views with the content of the new “foreground” agent by calling a function which calls all the functions I use to update the views when a new value is received
A desired attemp:
- Have a single Layout of the GUI which contains all the views for an agent, for each agent
- Hide/Show this maxi layout when agent switch is required
However I do not know exactly how to implement this: is this feasible? Do you have any suggestions?
This is how I decided to design the software.
With two classes I made a clean design which is very easy to use, with a small compromise on the maintenance side, but still highly usable.
The first class (
classA) stores, for each agent, the value to display for each view of the GUI.So, if I know the current foreground agent (state variable of the application), I can access a
map<agentId, classA>and get the value for each view.Similarly, when a new value is received, I just store it into the
classAobject of the related agent.The second class (
classB) is in charge of updating each GUI element, so:map<agentId, classA>agentIdparameter to access the rightclassAobject from where the right value is retrievedupdateAllmethod which calls every update method (used when an agent switch is required)Usage example
When a new data for an agent is received i just do the following (pseudocode):
Design advantages:
Design disadvantages:
classBmembers, update method, update method call inupdateAllmethodComments, critics, suggestions are highly appreciated.