Where should I perform Retrofit operations? [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm new to MVP, Retrofit and Dagger, so I've decided to make a simple app based on Riot Games API, that just shows some game's info in a list. Everything works fine, but it made me wonder: "Is presenter a good place to do such things, like Retrofit operations? I couldn't find anything about it. There are some simple examples how to use Retrofit, but they are sometimes shown even inside an activity class, which doesn't seem right to me.
So, here is my question: Where should I perform such operations to make the code as clean as possible?
My app repository: https://github.com/Mallorax/Rito_Api_Test

If the Model is what Retrofit returns, you can't put the call there.
The View is intended to separated from the Presenter, and subscribes via Callback interfaces, which Retrofit provides out of the box.
Therefore, your only option without involving some Service layer is to put Retrofit in the Presenter.

I suggest you to place some network call to plain class e.g. Interactors for example LoginInteractor which contains loginMethod, than when it finishes pass the result to the presenter class, which will handle the data and will convert (or not) it for some kind of model which is ready to be presented on UI.
Activity/fragment contains Presenter and Presenter contains Interactor(s)

Related

How can i keep variables state across all activities? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
How can i presist data across all my activities so when I change a variable on activity A, that variable will be changed on all activities to which it belongs.
So to play safe and easier I would like to know the right way to acomplish this.
I read some ppl sayng to use static variable but I have like 45-70 variables and 21 classes and doenst feels right to me to just put everything static...
I have also been using intents to pass the data between activities but I have to update it manually all the time wich can lead to inconsistencies sometimes.
My goal is to have a class that can be shared between all activities automatically and replicate any changes across all activities.
What you're looking for here is a model object. Android has a ViewModel object that would work well for this. The ViewModel object should be able to outlive the lifecycle of any activities or fragments that use it. I would recommend checking out that link as it will likely explain how to use a ViewModel more thoroughly than I ever could.
Edit: I misunderstood some parts of your question. Using a static class like you mentioned is definitely an option (and in my opinion, not an anti-pattern). If you would like to persist that data across app restarts, you could also utilize Room.
On a side note, I would also recommend using LiveData inside your model. LiveData is a wrapper of sorts that makes practically any object observable. You'll be able to observe your data in your model object and notify activities that need to know accordingly.

Better put code in same class or in different class? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I'm newbie programming in java.
I'm doing some forms and in one of them I put some fields, a button and a JTextArea. The idea is when I click in button makes a connection to external database and in JTextArea shows me the connection return (if is okay or if is failed and the error).
So I've created a class with the UI and a class(connection name) with the attributes database, port, username, password ... that make connection to external database.
I don't know if these organization mode is the best way to make it. I should include class connection in the same class of the UI,? How should I pass values of the form to the class connection, by a method?
Could you suggest me how to make it?
As lealceldeiro commented, this is opinion based, but there are some structures that it is good for these types of applications. What I've been taught and have been using is MVC(Model, View, Controller) structure.
The model is responsible for managing the data of the application. It receives user input from the controller.
The view means presentation of the model in a particular format.
The controller responds to the user input and performs interactions on the data model objects. The controller receives the input, optionally validates it and then passes the input to the model.
I usually make packages/modules called controllers, views and models. So I think you're on the right track here.
Let's say you have one class for your GUI, where the button onClickListener is. That would be your View.
Your models would be the class that you use to connect to the database, and maybe some classes/models in which you store the data from your database queries.
For you I would suggest to make another class that will be your controller. This class would contain your database object, and you can use this to make queries to the database and update your models. One of these could be connectToDatabase(), and you could call this method from your GUI when the button is clicked.
I hope I explained this well enough, and good luck!

What's the purpose of objects in Java? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Today I had an interview for test automation in one of the MNC.
They asked me "why do we need to create an object?"
I explained about OOPs concepts with example of individual bank account holders. But he is not convinced. He just need a definition.
What could be a suitable answer for that question?
You require an object to represent state.
At the most simple definition, a class defines behaviour and an instance of a class (an object) represents state.
Of course there are other things like static contexts which can also maintain state, which you can mention also, but above is the clearest answer which I believe they were looking for.
It also always helps to give an example. You could talk about, for example, an Employee class. You would need an object to represent John and another to represent Jane.
I think that this question is kind of generic and does not give much value to an interview. But some generic question should have a generic answer, and here is mine:
We need to create objects in java so we can get instances that have a state inside our application. This allows us to have persistent encapsulated elements that contain any required information, and methods that operate with it.
Just plain basic OOP theory.
There are many reasons why we create a object apart from basic oops
1) To bring up persistent state data to transactional state to perform action (curd and other) and persist back to data storage.(EJB, POJO,etc )
2) Creating handler to serve service and send fluid data across wire like web-service.
3)Stuctural behavior in action.for example you designed a class for a workflow and to make in action state we create a object and serve the behavior example validation , authorization , etc class
All in all to make design time architecture to response based live system

Middleware in Play framework (Java) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have experience web programing using express js framework
and this is my first time to learn playframework in java
how to make middleware in play framework
as ussualy i use express js just add middleware in front of controller in routes
example like this
router.get('/all/:key' , user_mid.login, ctrl_post.all)
user_mid.login = is my middleware
ctrl_post.all = is my controller method to handle the request
so how to make middleware in play framework
Play does not have a concept called middleware.
In Play Java, Results are returned by classes extending the Action (or Controller) abstract class. Why am I telling you this? Well, because you can compose actions. One action can forward a request to the next action, then to the next, and the other way around with the response. Play has good support for this concept: Action composition .
This goes in three steps:
Define an action by extending (usually) play.mvc.Action.Simple .
Annotate the method in your controller with #With, and your newly crated class. That way you composed two actions. They are called in the order in which the #With annotations appear, if you have multiple.
(Optionally) Define your own annotation for an action.

Making the user follow specific steps [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Am not sure if the title describes what I want. Am developing an App on Android where kids start a new empty drawing project and finish with a full drawing at the end by dragging different shapes from a list of shapes that I created for the App. My problem is that I dont know how am going to follow the kid steps.
The kid is supposed to read a message in each step and try to apply it and then click on a next button to view the next step.
I want to make sure that he do exactly what the message is saying in order to view the next step.
Theoretically, how is this applied in Java ? If for example, there would be 20 steps how can I make the app follow up with him and make sure he is doing the right thing
You are looking to create a wizard. In Java use a MVC libray Spring-MVC or if its android you can use similar approach. Basically you want steps and views while saving the state - typical MVC pattern. Have a model object that is passed along to the views. Each view will have portions of the model that it can read/write to.
Android -refer to WizarDroid.
Spring - AbstractWizardFormController will help

Categories