If you want to implement an MVP app without any framework, who would deal with navigation between views?
Right now, I have this navigator interface/class and the presenter has a reference to its implementation. To navigate from login to home, I have the LoginNavigator with a method navigate().
This method is called in one of the presenter methods. However, the next view, the home view, needs data to be displayed and the data is passed to the view in a service class.
So: navigation = presenter, data passing = from service class
The reason I have a separate navigator thing going on is because I didnt want to put Swing-specific code (the switching of panels) into the presenter.
Any recommendations on how to implement this nicely when not using some framework?
the best idea is View should have responsibility for navigation action and the presenter would trigger that. but you can even do the action and trigger both in view which is not so clean.
the navigation interface is a great idea because in mvp, the concept says the more the view is silly the better MVP implementation would be.
the navigation mathods is better to be in every view contract separately. for example if you have a register page just a method called gotToVerifyView() is enough to handle the navigation. for every view you should have some methods like this to get the handle of navigation to the presenter. this way the presenter is specifying the road map some how.
I dont know whats really wrong with your case of receiving data from a service. explain more so i can edit my answer for better.
Related
I'm developing a desktop app in java for a school project and I want to know how should I be designing the code of the GUI, specially because I will later have to run JUnit and functional tests on the app. I know it kinda sounds like an opinion-based question, but all I'm not asking for the "best" method, I just want to be pointed to a specific design pattern that can fit the need of my app.
To elaborate a little more on the type of application, it's "stage based", one button leads to a different section of the app. One main window, only error or warning popups (only one frame).
My current approach so far:
So far I've designed 3 different JPanel classes using eclipse's WindowBuilder (main menu, admin menu and user menu) and a AppGUI class that creates and empty frame with a CardLayout layout and fills it with one of the 3 panels (starts with main menu). Then the buttons on such panels point to the next panel. (Example, main menu -> user menu -> create profile)
The panels need to communicate with the AppGUI instance in order to execute the panel changes and later on, the functionality that the other options will have. To do this, I've been passing the AppGUI instance as a parameter to the panels on initialization and storing it inside each panel (I first thought of having a singleton AppGUI and have static reference to it, but I later on read that it's bad practice, specially for later mock testing).
Also I'd like to have the logic for the application on different classes other than the GUI classes and I'm not sure if passing the AppGUI as parameter to every method out there is good practice or not
In conclusion, it would be great if someone could point me to a specific design pattern for this kind of GUI, thanks in advance!
I would suggest the Model View Controller (MVC) pattern.
The idea of MVC is that the models (your app logic), the views (UI elements), and the controllers all work together. The models and the views are completely independent of each other i.e. you don't pass a model class object to a view class object or vice versa. The models and the views should talk to the controllers and the controllers makes all the decisions.
Controllers
They contain your app's UI logic, like how to layout the views. They are notified by the model once in a while (e.g. some data have changed!). They are also notified by the views (e.g. the user pressed a button!). And controllers need to respond to these notifications. For example, when the user pressed a button, the controller might tell another view to show, or tell the model to do some calculations or some other stuff. In your case, your controller is most likely to have all the ActionListener for the buttons and text boxes on the UI.
Models
They contain your app's logic. Try to design your model to be UI independent as much as possible. If designed correctly, your model should still work without the UI i.e. you should be able to make a command line version of your app without changing the model. For example, if you are making a calculator, the model should do the actual calculation.
Views
Most of the time, you don't need to worry about this part because swing already provides you with lots of view classes, such as JButton, JPanel, JTextArea etc. If you happen to be creating your own views, remember that it should not contain anything related to the model i.e. the view should work fine with a different model.
You can always search for more info on the web.
I need some advice for those who are experienced making Android applications. What I would really like to have, for my application's appearance: at the top, a title-bar which is a ImageView (content is a png), and at the bottom a series of custom buttons which make up a tab-bar like thing. In between the title and the tab-bar is the Content, which may be anything... (most likely buttons)
I have been doing this by making a RelativeLayout which specifies LeftMargin and UpperMargin for x,y coordinates--
Currently all of my activities are inheriting a custom MyActivity class, which rebuilds the title and the tab-bar at the time of onCreate. This seems bad to me!
PART1)
---A solution to Persistent data
Since the "tab-bar" and the title are persistent no matter what screen you're on during this application's run-time, it makes the most sense to store them somewhere... How should I do this? Make a singleton object that the Activity's ask for?
I thought a little about the singleton object, and I'm not even sure what I would store, since the Views that are on displayed during Activity A have activity A as context, and not Activity B.
PART2)
---Animation Aesthetics
I would really like to have the "Content" (the view in the middle between title and tabbar) slide out to the left, and the new content slide in from the right. I.e, I'd like the tab-bar and the title to remain fixed while the "activities" change. is this at all possible? What should I do to achieve it?
one idea I had, was to make all of the program in one activity! I would create an animation for the Custom View in the middle, and I would override the "back" button to navigate correctly to the previous Custom View. Is that a horrible idea?
Anyone have any advice?
Read http://developer.android.com/design. Most of the design principles can be applied to apps that run on legacy releases; it's not just limited to Honeycomb and Ice Cream Sandwich. Do consider the Action Bar and Dashboard design patterns.
I don't really recommend using just one Activity -- generally, an Activity should be a separate, encapsulated, pretty well defined chunk of functionality that can execute independently of other Activities.
To avoid duplication of your UI, consider reusing XML layouts.
To avoid duplication of your logic, consider using Fragments. You should be able to mix and match them in your activities.
To achieve the animation you describe, consider implementing a ViewPager.
Using the ActionBarCompat sample app and Android Support Library, you can enjoy modern goodies like Action Bar, fragments, tabs, and horizontal sliding transitions on devices running Android all the way back to Donut (1.6).
Have a question about using mvp on complicated views.
Suppose, we have some widget with boilerplate design (ListWidget) like this:
Control buttons provide functionality for switching between ContentPanel (just regular CellTable with data) and DetailsPanel (here we can edit particular entry, this is not Modal Dialog)
DetailsPanel, in turn, have this structure
Button1, Button2, Button3 should deal only with DetailsPanel and change ActionPanel (ActionPanel some kind of wizard, so its content should be tottaly changed after Button1, Button2 or Button3 clicked), but Buttons Save and Cancel should navigate us back onto ListWidget.
What approach should I use to implement described functionality?
Should I use 2 different ActivityManagers with one of them in master context or not?
How to manage and broadcast messages to dependant panels on ListWidget? (Is it ok to put Handlers in Views and just push event into inner eventBus on some actions?
For instance, when I save item in DetailsPanel, PreviewPanel should change and this item should be focused in CellTable; etc)
If I should use pattern with two different ActivityManagers, how exactly should I handle activity change behavior?
Thanks, hope somebody helps me.
I am answering your question, first with a brief explanation and then point by point taking your questions.
At framework level, you could have a class which implements ValueChangeHandler and set that on History using History.addValueChangeHandler(controller); assuming controller is implementing the value change handler
Any newItem you would put in history with History.newItem("NameOfAction") would invoke implementation of ValueChangeHandler which in this case would be controller
Now within implementation of ValueChangeHandler you can
String token = event.getValue();
if (token.equals("NameOfAction")) {
Call Appropriate presenter
}
Now let's talk about the second part of framework, presenter and view implementation. I am assuming that RootPanel is the container of all your UI Widgets. You can create individual presenters for each action
presenter = new ActionPresenter(rpc, eventbus, new ActionView())
Notice that I am creating a view and passing on to the presenter, presenter gets data and build the view with data. You could later pass on the container to the presenter to show the whole thing on UI.
Now about your specific questions
MVP would be your best bet here The article on MVP here is pretty good
I don't see a reason to use 2 activity managers as long as your container of those widgets is same
EventBus would be ideal. If you have to pass data with events, session variable like static fields in main class would server your purpose
I would need more details on exactly what you want to put in which manager
Hope this helps!
I'm new to GWT, and I've been reviewing the MVP implementation which uses the rpcService and the eventBus. I was wondering how a tab panel can be implemented such that each tab has its own sub-view. I have been waffling between making a custom widget that derives from a panel, or to figuring out how to make a presenter use another presenter, or to make a compound presenter class which handles it all for me.
Does anyone have advice on how to separate the functionality for each tab as opposed to keeping the implementation within one view/presenter pair?
I was in the same situation, but decided to change my implementation to simulate a TabPanel. If your views and presenters don't need to interact with each other (e.g. dragging something from one tab to another) then I think it'll be easier to separate functionality by loading your View into a shared SimplePanel. You can simulate the tabbed portion of the TabPanel with a widget that listens for PlaceChangeEvents (to change the highlighted tab) and sends goTo commands to the PlaceController your app is using (to handle clicks on the different headers).
It took a couple of hours to implement this, and the resulting code is much cleaner. My initial attempt involved listening for PlaceChangeEvents and then calling the appropriate tabPanel.selectTab() function, but trying to figure out how to start and stop the presenters for the different tabs was too jumbled up - like you suggest, you'd have to implement your own compound view model.
I solved this without faking a main tab, but using the one provided with GWT's basic SDK. I did this by:
Add an ArrayList of Presenters to the MainTabPresenter
Constructed each tab's present plus view within the 'go' method of the MainTabPresenter
Called 'go(null)' on each of the tab presenters.
Handled the null situation on each.
Implemented a method in the MainTabView to add the tabs to the DecoratedTabPanel
It all works like a charm. The MainTabPresenter so very thin, and allows for complete implementations of View/Presenters to be written into their own files.
My application has a custom view which contains other custom views of a different type. The subviews have their own click listeners (which I can't change, as these are in 3rd party libraries). How can I intercept a user's click at the level of my view to do some processing, and then pass the click on to the proper subview?
Justin, you can play with dispatchTouchEvent() or onInterceptTouchEvent().
I'm not entirely sure about this, but in Java what I would do is define various subview objects in my main view and simply send those to the draw/paint function. That way I'd only have ONE click listener which would be in the view anyways.
Can you do something like that in Android?