Warn the user that he is about to loose his change in Edit view when leaving to view to another in GWT - gwt-mvp

I want to prevent the user that he will loose his changes in an EditView when changing the view to another.
I use MVP4G in my project and the project is divided as mvp's structure (one package for the template another one for views ..) is there any solution to detect the EditView in the eventBus. or detect the current View shown to user
Thanks in advance

Thanks to the Navigation Event feature in mvp4g, the presenter will get control before the view changes. At this point the presenter can decide if the navigation will be done or not. This is the correct place in a mvp4g application to save your data.
First zu have to mark all events in the eventbus that will change your view with:
#Event(..., navigationEvent = true)
void goToPage1();
Next your presenters have to implement the NavigationConfirmationInterface and the requires confirm-method:
public class Presenter extends ... implements NavigationConfirmationInterface {
public void confirm(NavigationEventCommand event) {
//pseudo method to verify if the view has changed
if (isViewModified(){
//Window shouldn't be used inside a presenter
//this is just to give a simple example
if (Window.confirm("Are you sure you want to leave?")){
event.fireEvent();
}
} else {
event.fireEvent();
}
}
}
And the last thing to do, is to set the presenter of the current view to the confirmation presenter by calling:
event.fireEvent(false);
This is usually done when the presenter gets control.
You will find the documentation here:
https://github.com/FrankHossfeld/mvp4g/wiki/03.-Defining-EventBus#navigation-event

Thanks to MVP4G's team including El Hoss who gives me a hint to check the MVP4G's blog.. I've solved my problem by following this example
http://mvp4g.blogspot.com/2011/06/navigation-control.html

Related

faces-redirect=true not working while creating and rendering view

I am currently working on a JSF 2.2 application. As per requirements, I have created custom view handler (using ViewHandlerWrapper) for my application. All the methods are just passing to default view handler except renderView which I am overriding as follows -
private viewHandler viewHandlerWrapped = null;
renderView(FacesContext facesContext, UIViewRoot viewToRender) {
String viewId = viewToRender.getViewId();
if (viewId == some condition) {
/* Do calculation to derive viewId */
}
UIViewRoot viewRoot = viewHandlerWrapped.createView(facesContext,viewId+"?faces-redirect=true");
facesContext.setViewRoot(viewRoot);
//now let system render the view
viewHandlerWrapped.renderView(facesContext,viewRoot);
}
The above is working fine and rendering & navigation is happening as expected. The only issue is faces-redirect=true is not working. The URL seems to be always one behind.
I have gone through many answers given in stackoverflow or internet. But nowhere I am able to find how to solve this.
I think I am doing something wrong e.g. ?faces-redirect=true might not be the correct way while creating view. But I am not sure what can be done to correct this.
Can someone please help me out with this?
After struggling with this for more than 4 weeks, I finally found a way to get the correct URL (instead of previous one). I am updating my answer here in case any one else falls into same problem -
"It looks like we can not use the faces-redirect=true the way I was using while creating and rendering the pages. It should be suffixed with form action. So I have changed my code as follows -
1) actions are returned on click of a button e.g.
public string doAction {
----
return "action?faces-redirect=true";
}
2) Code is updated to use implicit navigation wherever possible. With this, I didn't need to build my custom viewhandler as navigation is happening implicitly. So, I have scrapped the viewhandler.
With above two simple steps, the correct URL is being displayed on the browser now.

MvvmCross: Display a BTProgressHUD spinner immediately after ViewModel creation

I'm using the BTProgressHUD Xamarin component in an app that is built using MvvmCross. I'm currently working on the iOS version of the app. My ViewModels make several web service calls and expose an 'IsBusy' property, which the associated views are binding to, in order to show or hide the progress spinner. This is pretty much the way things are set up in the N=34 MvvmCross sample (https://github.com/MvvmCross/NPlus1DaysOfMvvmCross/tree/master/N-34-Progress) as well.
The problem is that in some cases a ViewModel must automatically call a service as soon as it is created. I tried to make the call in the Start() function of the ViewModel, but I noticed that the BTProgressHUD spinner does not show up on top of the view. I suspect that the problem is that BTProgressHUD must be displayed only after a view has been made visible, and probably this is not the case when ViewModel.Start() runs.
Has anyone encountered this before? Is there a simple way to run code in the ViewModel after the view has been made visible?
Thanks.
Is there a simple way to run code in the ViewModel after the view has been made visible?
The N=42 video - http://slodge.blogspot.co.uk/2013/11/n42-is-my-viewmodel-visible-can-i-kill.html - introduces an IVisible interface that you can add to your ViewModel - it's your job to call this from your View - but this is easy to do on each platform. For example,, on iOS it is done using ViewDidAppear/ViewDidDisappear in https://github.com/MvvmCross/NPlus1DaysOfMvvmCross/blob/master/N-42-Lifecycles/Lifecycle.Touch/Views/FirstView.cs
protected IVisible VisibleViewModel
{
get { return base.ViewModel as IVisible; }
}
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
VisibleViewModel.IsVisible(true);
}
public override void ViewDidDisappear(bool animated)
{
base.ViewDidDisappear(animated);
VisibleViewModel.IsVisible(false);
}

MVVMCross changing selected tab bar item from within nested view controller

We're using MVVMCross within our application and I've come up against something that I'm not sure I've solved in the best way possible.
One of our ViewModels contains 3 other view models - a dashboard and 2 lists. In iOS this is presented using a MvxTabBarViewController which works great. Android and WP present this view in a similar manner. An example of the object model is below:
public class ProjectViewModel : MvxViewModel
{
public DashboardViewModel Dashboard {get;set;}
public FirstListViewModel FirstList {get;set;}
public SecondListViewModel SecondList {get;set;}
}
We're now in the situation where if a certain action happens within the DashboardViewModel we would like to instruct the navigation to change the tab in iOS and the same thing to happen on the other platforms.
The only way I've been able to get the tab to change on iOS is to use this.SelectedIndex = 1; from within the iOS ProjectView.
At the moment also the only way I've managed to trigger this change is to fire an event from the DashboardViewModel and then the ProjectViewModel subscribes to this and fires another event which is subscribed to by the ProjectView to instruct it to change the tab in whatever device specific way it needs to. I can't help but think there is a better way to do this.
I've tried taking a look at a custom ViewPresenter for iOS and calling ShowViewModel FirstListViewModel from within the DashboardViewModel but the presenter doesn't appear to be getting used so we just transition normally. My idea was I could get in the middle, cancel the navigation request and then flip the active tab on the ProjectView.
Any suggestions would be appreciated on how we could do this in a better cross platform way using MVVMCross to handle the change if at all possible.
You should be able to do this in any of several ways:
using a custom presenter with overridden Show as you suggest
using a custom presenter with overridden ChangePresentation - and using a custom hint
using a custom binding or a binding to a property within the ProjectView to drive the transition
using a custom IMvxInteraction property
using a custom event from VM to View
using a messenger to send a message from the ViewModels to the Views.
Ultimately lots of these could work and which of these I might choose would depend on which one worked and which one the team are happy with - shipping the working app is always the ultimate goal.
Given where I am with MvvmCross experience, I'd probably opt today for trying the approach of trying a custom IMvxInteraction property. But this might not be for everyone... it certainly might be overkill for this sample...
However, to do this, I would try:
add a public enum Display { Dash, First, Second } to the Core project
add a ProjectViewModel property:
private MvxInteraction<Display> _display = new MvxInteraction< Display >();
public IMvxInteraction<Display> DisplayChange { get { return _display; } }
whenever this ViewModel wants to fire the change it can fire it using e.g. _display.Raise(Display.First)
the ProjectView could then bind Display to its own property which might be implemented like:
private IDisposable _subscription;
private IMvxInteraction<Display> _displayInteraction;
public IMvxInteraction<Display> ChangeDisplay
{
get { return _displayInteraction; }
set
{
if (_subscription != null)
{
_subscription.Dispose();
_subscription = null;
}
_displayInteraction = value;
if (_displayInteraction != null)
{
_subscription = _displayInteraction.WeakSubscribe(DoDisplayChange);
}
}
}
private void DoDisplayChange(Display which)
{
// change the tab display here
}
the binding would be added in ViewDidLoad like:
set.Bind(this).For(v => v.ChangeDisplay).To(vm => vm.DisplayChange);

Swap RootViewController with MVVMCross

I need to implement a login/logout using MVVMCross, iOS only to start. After the user logs in, I want to close the view and make the "real" first view the root controller. For logout, I want to do the same in reverse. Whenever the LoginViewModel is requested, clear the root and replace it.
This Remove ViewController from stack indicates there is a ClearTop parameter, but it looks like it is gone in v3?
I then found this What is the best way to handle GoBack for the different MvvmCross (v3) platforms and I implemented this Presenter:
public override void Close(IMvxViewModel toClose)
{
if (toClose is LoginViewModel)
{
ClearBackStack();
Show(new MvxViewModelRequest() { ViewModelType = typeof(FirstViewModel)});
return;
}
base.Close(toClose);
}
public override void Show(MvxViewModelRequest request)
{
if (request.ViewModelType == typeof (LoginViewModel))
{
ClearBackStack();
}
base.Show(request);
}
Is this the correct way to handle this? Is there an easier mechanism (pre-v3 like)? Should I be overriding ChangePresentation instead?
Also, is there a mechanism to call ShowViewModel from a View? Do I need to resolve the IMvxViewDispatcher or is there a more straight forward method?
Yes, if you want to do custom presentation techniques then the easiest way is to implement your own view presenter.
For an introduction and some links on this, see How can I implement SplitView in another view in MvvmCross?
You are free to write code directly in your views, including navigation logic using resolved IoC objects. However, mvvmCross tries to encourage you to put this logic in the viewmodels - especially so that the 'logic' is more easily shared between platforms.

Hiding master UISplitViewController for only certain views

I have a UISplitViewController which has a UINavigationController in the master and a UIViewController in the detail. When the device is orientated into landscape mode I want the normal behaviour to be preserved. I.e. The master gets shown in landscape and hidden in portrait.
However depending what the user clicks in the master depends on which UIViewController is loaded into the detail part of the UISplitViewController. What I would like is for the master to be hidden in landscape mode when a user clicks on a button in the detail UIViewController. The problem is I can't get this to work.
My delegate looks like this (have removed some lines for simpler viewing):
public class SplitControllerDelegate : UISplitViewControllerDelegate {
SplitViewController incomingController;
private bool hideMaster = false;
public override bool ShouldHideViewController (UISplitViewController svc,
UIViewController viewController,
UIInterfaceOrientation inOrientation) {
return hideMaster;
}
public void SetHideMaster(bool value) {
hideMaster = value;
}
}
I then call it from the detail UIViewController like
splitControllerDelegate.SetHideMaster(value);
However nothing changes. I'm unsure of how to make it perform the change? Should the master disappear immediately? What causes the WillHideViewController to fire?
Thanks
Mike
What you're trying to do cannot be done officially. ShouldHideViewController() is called only upon device rotation. So unless you rotate forth and back, your controller won't disappear.
You have various options:
Don't use UIListViewController but some other third party replacement
Use UIViewController containment feature of iOS5 and implement your own split view
Apply a hack to UISplitViewController
About the last point. You should be able to force ShouldHideViewController() being called if you set the Delegate property to NULL and then assign a new delegate. Afterwards, call the WillRotate() method of the split view controller using the current orientation.
I'd go for the 2nd option.
By design you cannot do much with the standard UISplitView, try that third party control :
https://github.com/mattgemmell/MGSplitViewController

Resources