vendredi 17 juillet 2015

Best design pattern for data model in restrofit rest android app which json data

I have learned from https://www.youtube.com/watch?v=xHXn3Kg2IQE which is best approach for flow in application (use service, content provider etc) but I don't know what is best practice (design pattern) to work with data retried and data received. All data is in JSON.

So I want to develop simple app. The app make request to server to login. In response I get token and JSON which describe a voting. After that I have to sent this complex JSON base on user input. This is kind of form to fill up. A voting system.

{
  "data" : {
   "name": "Name of kind of voitng",
   "general_data" : [ {"name": "n1", "value":1}, {"name": "n1", "value":1}],
   "voits": [{"name": "n1", "value":1}, {"name": "n1", "value":1}],
} 
}

So I have classes which is mapped to this JSON. But I need also represent these data on the activity. So now I have two classes.

    class GeneralDataElementJSON { 
String name; 
int value;
}
    class GeneralDataElementModel { 
String name; 
int value; 
boolean isVoited = false;
}
    class GeneralDataElementView extends View {}

Now in my approach I map the retrieved data to instances of *JSON classes after that I convert instances of *JSON classes into instances of *Model classes. The instances of *View classes are adapter for instances of *Model for display.

After user input I convert instances of *Model classes into instances of *JSON classes and instances of *JSON classes (using retrofit) are converted to JSON and sent back to server.

So it is complicated. I think that there should be better approach to do that. But how?

Aucun commentaire:

Enregistrer un commentaire