Handling data on an android app - java

I'm working on a school project and I'm having trouble coming to a conclusion on how to handle the data it involves.
Basically my app lets you create recipes (cooking) that are stored on the phone.
I don't really have any usable code just yet, so far the structure I thought of is as following:
A class Recipe that represents an individual recipe for manipulation via the app (has all the properties)
A class RecipeList that has a HashMap with all the Recipe objects in it that lets you get a certain recipe by an Id RecipeList.GetRecipe("id") and save Recipes via RecipeList.SaveRecipe(Recipe), it also handles saving to the SD card
The thing is though that eventually I want to outsource saving to a web space (using PHP) which is part of my project assignment, that's why I decided on using a HashMap so I could utilize an Id.
What's the best way to go about it given my current structure? Or is there a better structure for what I'm doing?

This will tell you everything you need to know.

Related

MVP Clean Architecture - where do calculation and non data related tasks belong?

I've been doing a lot of research and trying to use MVP with Clean Architecture in my app, but still i have a lot of confusion and don't understand it completely. My biggest doubt is: where should non database related, like complex ma mathematical calculations, logic go? Every example app on the internet i found has to simply save and retrieve some data from the database.
For instance i have a screen where user need to insert 4 values and then i have code that takes this values, perform some calculations and return object that represents data to be displayed.
My question now is: where should i position the class or code responsible for producing the result in an MVP with clean architecture project, with following structure:
view <--> presenter <--> use cases <--> repositories
View sends input to the presenter, but then ? Also many examples use different "services" classes and in some of them they are connected to presenter and in other to repository.
Use cases are part of the model. In the model there are the data (the pojos to define how the data is stored in memory) and the use cases.
All your business logic as this complex mathematical calculations must be in one or many use cases.
The view will get the data and pass it to the presenter which should create a thread (in one of the many ways to run code asynchronously, I like using threadpoolexecutors for this) to run the use case which will do the maths and answer the presenter. And finally, the presenter will send the data back to the view.
Unless you have to retrieve any data (from sensors, files, databases, url responses...) or permanently store it I don't think you need a repository in this case.
You should write your complex and logical code in presenter itself
why?->
if you ever need any database values so you can get it from presenter by running thread and do further calculations.
if is there any validations you should use common classes to get the result
for ex.
view needs some result on inputs you can pass it to presenter ,
presenter will process the inputs and return the data back to view.
follow this link it will help you to understand MVP like a pro

Simplest way to save data when Serialization is no option

I am working on a game. It has a Game class where everything is created and other objects are stored, a Player class with an Inventory of Items, and then there are Rooms where the Player can go and in these rooms we have other Items and Enemys and so on. Its a fairly simple programm.
I have to implement an Option to save and load the current game state. I tried using the Serializable interface, but I encountered the problem that all of my Item classes store StringPropertys. Saving those objects obviously doesnt work scince StringProperty doesnt implement the interface.
Then I thought about writing everything in a simple text file. But loading that will be pretty elaborate.
Persisting the data with an Database would be an asolute overkill for that project. I also know there are other possibilities like XML, but I have never done that before.
What I basically want is the easiest (does not have to be the prettiest/cleanest) option to save my game state, so when someone wants to load the game, I can simply create a new Game and Player and just do something like newPlayer.setCurrentLocation(savedLocation) and so on.

Design for survey app

I'm very new to java and want to ask for a help about design. I'm going to build a simple survey app for android in java.
Here is how I see the app:
The main page (activity_main) contain LineEdit for ID and two buttons: Login and SignUp. This activity onCreate loads all registered users and questions from DB.
Sign up page (activity_signup) contains several LineEdit fields for user info as well as SignUp button. Also it provides free ID onCreate.
Profile page (activity_profile) contains some user info (as TextView), possibly and a Survey button.
Survey page (activity_survey) contains Question, Answers and two buttons: Prev and Next.
Please mention if something might be improved.
Questions:
Should I store all users and questions as private field in class MainActivity or somewhere else? (Assume that DB is so small that it perfectly fits in RAM)
How can I modify the DB with newly created user from activity_signup? It is rather general question, like how can I access private fields from other activities preserving encapsulation?
I'd like to have a filed like private static User current_user in class ProfileActivity. But I can get the user only from DB, which is private field of class MainActivity. How can I pass the user from activity_main to activity_profile (again preserving encapsulation)?
activity_survey contain answers of different types, e.g. Yes/No; single/multiple choice (+ your variant) etc. How can I handle this in java? My idea is to create an abstract class AbstractQuestion with method fillLayout and inherit several class ConcreteQuestion (here concrete should be replace with an appropriate title) from it which contains implementation of certain type of question. Store all questions in array of AbstracQuestion's. Is it doable in java or is there more right way to do this?
Thanks in advance for any help!
Don't store such objects in Activity. You have several options for data persistence - these are listed here https://developer.android.com/training/basics/data-storage/index.html - of course that does not cover any "external" sources such as databases like Realm or Firebase but let's keep to the basics.
If your data is complex and you think that it would be easy for you to retrieve it using SQLite queries then the SQLite Database is the way to go. You can access it using ContentProvider which can be queried from any place where you have Context.
You can also store it locally on your internal or external storage with simple Serializable classes. Imagine a single object let's call it Database that is Serializable and that contains all the data you need. You could load it in your App startup (like in extended Application class) and store the reference so it won't get garbage collected. Then you can access it from a Application static method you could write to get the reference. It is probably the fastest way to implement a simple storage with fairly complex structure but that is probably not the best if your data is "big". It will increase your App start time (preferably make the load and save operations asynchronous).
If your data is simple you can use SharedPreferences to store "key-value" data. This is a little like second approach but using the Android framework to do it.
The option 2 and 3 require that your data is Serializable or Parcelable. As Android says that Parcelable should not be used for persistence but rather for communication let's skip that one.
You can either make you objects Serializable or translate them to json objects with i.e. Gson library. and store those serialized json objects. Making them serializable directly may be faster approach but sometimes keeping jsons makes more sense.
You can't and you shouldn't have to.
If you wan't to keep your data in static fields move them to extended Application class (make sure you point it in AndroidManifest.xml with specific xml parameter) and access it from there. You can get access to your Application class whenever you have Context via context.getApplicationContext() that you can cast to your custom class.
To tell Android to use your custom Application class use the following in AndroidManifest.xml:
<application
android:name=".YourAppClass"
...>
...
</application>
I am not sure if I get the 4th question right. Basically if you have multiple values you need to store your results in some collection i.e. ArrayList. Your whole questionare could be represented by a Map<Question,List<Answer>> then <- arbitrary class names (these could be enums too)

What type of adapter should be used in this case? Singleton or shared preferences method?

I am developing android application. I did not plan my project that much before. So when I thought that I finished my task then I realized that I am not done at all. I have some issues that I figured it out now. I want to go again from the beginning of the project.
I want to get the list of training that is in external database and data will be populated manually using SQLite browser (only training table, columns are _id, title, description, date and time, location.). And onclick of that item in list, it should start new activity with holding the training session all along the application activity. [On Training, people will fill their personal information (Now, person table), where the data of person should be stored in the particular training session].
Now, my questions are;
1. What type of Adapter should I use in this case and why?
2. Should I use shared preferences or Singleton method to keep the session of training all activities of application?
Both of those question are determined by your requirements for this software. You said it yourself that you 'did not plan my project that much before'. Sounds like you're at a point where you need to do some hard thinking about your requirements. Requirements will, in turn, determine the technologies/ methodologies you need to use.
With the information you've provided, it would be impossible for anyone to provide advice that would be anything better than a guess.

Designing a program with statistics

I am about to start my newest project, it is basicly an application that gets some data from a database and then display that data a graph!
now even though that this may seem simple it is important to me that this is done in a very correct way when it comes to object orientated programming.
Now my idea was the following:
I wanted to create the following four classes:
Adapter:
The class that connects the application to the database and recives the data
CallQueue:
This is an object that differences depending on what type of data is recived from the database and what type of data you wish to show on your graph. An example of this would be Cheese and fruits. both of them are food but they are very different types of food.
Statistics
This would be a tool class used to calculate the information recived from the database (for example changeing it to percentage instead of raw data)
Graph
This would be the class that gets the information from the statistic class and turns the numbers into a graph
GUI
This is ofcourse the GUI class where i will post the graph on!
Now i want to make the project as object orientated as possible. But my problem is that the information from the database is not always the same. For example if get the data from a day to day basis it will be different than from month to month. This means that the information is always going to change depending on what the user need.
How would i make this program object orientated ? and what type of connections should my classes have to eachother to make it most accessible. Do i have to create subclasses in order to simplify it?
should i add all information from the datbase directly into the CallQueue Class or should that object be created later on?
Update - Elaboration
The name callQueue is not a streaming implementation it is marely an object that should contain values of the data recived from the database (note that this is still in the idea phase and nothing is implemented). The idea is that a user opens the program and then chooses a day from and then a day to for instance: 04/11/2012 to 10/11/2012. The reason the objects value changes is when the day changes for instance to the following: 04/11/2012 - 04/12/2012 then a new graph will be created new information from the database will be calculated ect ect.
One thing that i am confused about aswell is the following:
When you have an object that is created from the database (adapater Note this could be optimized if you guys have a better idea) then how would you calculate statistics from that? would it be better that the statistic class called the adapter for data then worked with the data and then created the objects contain the calculated data?
Then the Graph class would need to get the objects data and insert into the graph.
From experience designing large systems and even smaller, the best approach is to think in terms of components rather than classes. This will allow you to break down your problems into smaller (and mostly independent) pieces.
So for example, you will have a component which sole responsibility will be to bring the data to your application for processing. That component will need to be able to deal with the multiple data sources, etc... That then becomes a sub-system that you can design independently from the rest of the application and deals with a specific problem, smaller than the whole.
If the sub-problems are still larger than they should, keep breaking them down into sub-compoennts, until the implementation of the components becomes almost trivial. At that point, you can start bringing in the notion of classes because you have enough visibility on the protagonists in your system.
In short, I put a lot of emphasis on separation of concerns. By isolating sub-problems into sub-components, you also isolate the solutions which makes it easier to correct your design mistakes or replace implementations without impacting the entire system.
Just my two cents...

Categories