Skip to content

Commit

Permalink
Edited MvxAndroidViewPresenter and AppCompat version: If the request …
Browse files Browse the repository at this point in the history
…is a plain MvxViewModelRequest, then load the ViewModel respecting its life cycle
  • Loading branch information
nmilcoff committed Aug 20, 2017
1 parent cc27a8e commit cf9f6b0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -280,18 +280,21 @@ protected override void ShowDialogFragment(Type view,
var fragmentName = FragmentJavaName(attribute.ViewType);
var dialog = (DialogFragment)CreateFragment(attribute, fragmentName);

//TODO: Find a better way to set the ViewModel at the Fragment
IMvxViewModel viewModel;
var mvxFragmentView = (IMvxFragmentView)dialog;
// MvxNavigationService provides an already instantiated ViewModel here,
// therefore just assign it
if (request is MvxViewModelInstanceRequest instanceRequest)
viewModel = instanceRequest.ViewModelInstance;
{
mvxFragmentView.ViewModel = instanceRequest.ViewModelInstance;
}
else
{
viewModel = (IMvxViewModel)Mvx.IocConstruct(request.ViewModelType);
mvxFragmentView.LoadViewModelFrom(request, null);
}
((IMvxFragmentView)dialog).ViewModel = viewModel;

dialog.Cancelable = attribute.Cancelable;

Dialogs.Add(viewModel, dialog);
Dialogs.Add(mvxFragmentView.ViewModel, dialog);

var ft = CurrentFragmentManager.BeginTransaction();
if (attribute.SharedElements != null)
Expand Down
15 changes: 9 additions & 6 deletions MvvmCross/Droid/Droid/Views/MvxAndroidViewPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -456,18 +456,21 @@ protected virtual void ShowDialogFragment(
var fragmentName = FragmentJavaName(attribute.ViewType);
var dialog = (DialogFragment)CreateFragment(attribute, fragmentName);

//TODO: Find a better way to set the ViewModel at the Fragment
IMvxViewModel viewModel;
var mvxFragmentView = (IMvxFragmentView)dialog;
// MvxNavigationService provides an already instantiated ViewModel here,
// therefore just assign it
if (request is MvxViewModelInstanceRequest instanceRequest)
viewModel = instanceRequest.ViewModelInstance;
{
mvxFragmentView.ViewModel = instanceRequest.ViewModelInstance;
}
else
{
viewModel = (IMvxViewModel)Mvx.IocConstruct(request.ViewModelType);
mvxFragmentView.LoadViewModelFrom(request, null);
}
((IMvxFragmentView)dialog).ViewModel = viewModel;

dialog.Cancelable = attribute.Cancelable;

Dialogs.Add(viewModel, dialog);
Dialogs.Add(mvxFragmentView.ViewModel, dialog);

var ft = CurrentFragmentManager.BeginTransaction();
if (attribute.SharedElements != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ public ModalViewModel(IMvxNavigationService navigationService)
ShowNestedModalCommand = new MvxAsyncCommand(async () => await _navigationService.Navigate<NestedModalViewModel>());
}

public override System.Threading.Tasks.Task Initialize()
{
return base.Initialize();
}

public void Init()
{

}

public override void Start()
{
base.Start();
}

public IMvxAsyncCommand ShowTabsCommand { get; private set; }

public IMvxAsyncCommand CloseCommand { get; private set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ public RootViewModel(IMvxNavigationService navigationService)
{
_navigationService = navigationService;

//ShowChildCommand = new MvxAsyncCommand(async () => await _navigationService.Navigate<ChildViewModel>());

ShowChildCommand = new MvxAsyncCommand(async () => ShowViewModel<ChildViewModel>());

ShowModalCommand = new MvxAsyncCommand(async () => await _navigationService.Navigate<ModalViewModel>());
ShowModalCommand = new MvxAsyncCommand(async () => ShowViewModel<ModalViewModel>());

ShowModalNavCommand = new MvxAsyncCommand(async () => await _navigationService.Navigate<ModalNavViewModel>());

Expand Down

0 comments on commit cf9f6b0

Please sign in to comment.