Skip to content

Commit

Permalink
Merge pull request #14 from Softeq/fixes-with-viewlocator
Browse files Browse the repository at this point in the history
Fixes with viewlocator
  • Loading branch information
vadimpylsky authored Dec 14, 2018
2 parents 40dd80e + 8b33423 commit 885b5ab
Show file tree
Hide file tree
Showing 35 changed files with 548 additions and 316 deletions.
14 changes: 14 additions & 0 deletions Softeq.WhiteLabel.sln
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Softeq.XToolkit.Common.iOS"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Softeq.XToolkit.Bindings.iOS", "..\XToolkit\Softeq.XToolkit.Bindings.iOS\Softeq.XToolkit.Bindings.iOS.csproj", "{2D399BA9-1878-43E2-AF05-6873EAE9151B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Softeq.XToolkit.Whitelabel.Tests", "Softeq.XToolkit.Whitelabel.Tests\Softeq.XToolkit.Whitelabel.Tests.csproj", "{B5A6100B-D12E-43A2-BBDE-027D6E2B5835}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -151,6 +153,18 @@ Global
{2B06D0B2-8BED-49EC-AC0B-154850F68190}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
{2B06D0B2-8BED-49EC-AC0B-154850F68190}.Debug|iPhone.ActiveCfg = Debug|Any CPU
{2B06D0B2-8BED-49EC-AC0B-154850F68190}.Debug|iPhone.Build.0 = Debug|Any CPU
{B5A6100B-D12E-43A2-BBDE-027D6E2B5835}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B5A6100B-D12E-43A2-BBDE-027D6E2B5835}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B5A6100B-D12E-43A2-BBDE-027D6E2B5835}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B5A6100B-D12E-43A2-BBDE-027D6E2B5835}.Release|Any CPU.Build.0 = Release|Any CPU
{B5A6100B-D12E-43A2-BBDE-027D6E2B5835}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{B5A6100B-D12E-43A2-BBDE-027D6E2B5835}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
{B5A6100B-D12E-43A2-BBDE-027D6E2B5835}.Release|iPhone.ActiveCfg = Release|Any CPU
{B5A6100B-D12E-43A2-BBDE-027D6E2B5835}.Release|iPhone.Build.0 = Release|Any CPU
{B5A6100B-D12E-43A2-BBDE-027D6E2B5835}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
{B5A6100B-D12E-43A2-BBDE-027D6E2B5835}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
{B5A6100B-D12E-43A2-BBDE-027D6E2B5835}.Debug|iPhone.ActiveCfg = Debug|Any CPU
{B5A6100B-D12E-43A2-BBDE-027D6E2B5835}.Debug|iPhone.Build.0 = Debug|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
26 changes: 11 additions & 15 deletions Softeq.XToolkit.WhiteLabel.Droid/ActivityBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ namespace Softeq.XToolkit.WhiteLabel.Droid
{
public abstract class ActivityBase : AppCompatActivity
{
private readonly Lazy<IPageNavigationService> _pageNavigationLazy;
private readonly IPageNavigationService _pageNavigation;

protected ActivityBase()
{
_pageNavigationLazy = new Lazy<IPageNavigationService>(ServiceLocator.Resolve<IPageNavigationService>);
_pageNavigation = Dependencies.PageNavigationService;
}

public List<IViewComponent<ActivityBase>> ViewComponents { get; private set; }

public override void OnBackPressed()
{
if (_pageNavigationLazy.Value.CanGoBack)
if (_pageNavigation.CanGoBack)
{
_pageNavigationLazy.Value.GoBack();
_pageNavigation.GoBack();
}
else
{
Expand All @@ -47,11 +47,7 @@ public override void OnBackPressed()
public override void OnRequestPermissionsResult(int requestCode, string[] permissions,
[GeneratedEnum] Permission[] grantResults)
{
if (ServiceLocator.IsRegistered<IPermissionRequestHandler>())
{
var permissionRequestHandler = ServiceLocator.Resolve<IPermissionRequestHandler>();
permissionRequestHandler.Handle(requestCode, permissions, grantResults);
}
Dependencies.PermissionRequestHandler?.Handle(requestCode, permissions, grantResults);

base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
Expand All @@ -65,7 +61,7 @@ protected override void OnCreate(Bundle savedInstanceState)

protected void AddViewForViewModel(ViewModelBase viewModel, int containerId)
{
var viewLocator = ServiceLocator.Resolve<ViewLocator>();
var viewLocator = Dependencies.IocContainer.Resolve<ViewLocator>();
var fragment = (Fragment)viewLocator.GetView(viewModel, ViewType.Fragment);
SupportFragmentManager
.BeginTransaction()
Expand All @@ -78,14 +74,14 @@ public abstract class ActivityBase<TViewModel> : ActivityBase
where TViewModel : ViewModelBase
{
private const string ShouldRestoreStateKey = "shouldRestore";
private readonly Lazy<IJsonSerializer> _jsonSerializerLazy;
private readonly IJsonSerializer _jsonSerializer;
private Lazy<TViewModel> _viewModel;

protected ActivityBase()
{
Bindings = new List<Binding>();
_jsonSerializerLazy = new Lazy<IJsonSerializer>(ServiceLocator.Resolve<IJsonSerializer>);
_viewModel = new Lazy<TViewModel>(() => ServiceLocator.Resolve<IBackStackManager>()
_jsonSerializer = Dependencies.JsonSerializer;
_viewModel = new Lazy<TViewModel>(() => Dependencies.IocContainer.Resolve<IBackStackManager>()
.GetExistingOrCreateViewModel<TViewModel>());
}

Expand Down Expand Up @@ -166,7 +162,7 @@ private void RestoreIfNeeded(Bundle outState)

var parametersObject = Intent.GetStringExtra(Constants.ParametersKey);
var parameters =
_jsonSerializerLazy.Value.Deserialize<IReadOnlyList<NavigationParameterModel>>(parametersObject);
_jsonSerializer.Deserialize<IReadOnlyList<NavigationParameterModel>>(parametersObject);

foreach (var parameter in parameters)
{
Expand Down Expand Up @@ -201,6 +197,6 @@ public abstract class ActivityBase<TViewModel, TInterface> : ActivityBase<TViewM
private TViewModel _viewModel;

protected override TViewModel ViewModel =>
_viewModel ?? (_viewModel = (TViewModel)ServiceLocator.Resolve<TInterface>());
_viewModel ?? (_viewModel = (TViewModel) Dependencies.IocContainer.Resolve<TInterface>());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public override void OnDismiss(IDialogInterface dialog)

protected void AddViewForViewModel(ViewModelBase viewModel, int containerId)
{
var viewLocator = ServiceLocator.Resolve<ViewLocator>();
var viewLocator = Dependencies.IocContainer.Resolve<ViewLocator>();
var fragment = (Fragment)viewLocator.GetView(viewModel, ViewType.Fragment);
ChildFragmentManager
.BeginTransaction()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ namespace Softeq.XToolkit.WhiteLabel.Droid.Dialogs
public class DroidFragmentDialogService : IDialogsService
{
private readonly IAlertBuilder _alertBuilder;
private readonly IIocContainer _iocContainer;
private readonly ViewLocator _viewLocator;

public DroidFragmentDialogService(
ViewLocator viewLocator,
IAlertBuilder alertBuilder)
IAlertBuilder alertBuilder, IIocContainer iocContainer)
{
_viewLocator = viewLocator;
_alertBuilder = alertBuilder;
_iocContainer = iocContainer;
}

public OpenDialogOptions DefaultOptions { get; } = new OpenDialogOptions();
Expand All @@ -33,15 +35,15 @@ public Task<bool> ShowDialogAsync(string title, string message, string okButtonT
public Task<TViewModel> ShowForViewModel<TViewModel>(OpenDialogOptions options = null)
where TViewModel : class, IDialogViewModel
{
var viewModel = ServiceLocator.Resolve<TViewModel>();
var viewModel = _iocContainer.Resolve<TViewModel>();
return ShowImpl<TViewModel>(viewModel as ViewModelBase);
}

public Task<TViewModel> ShowForViewModel<TViewModel, TParameter>(TParameter parameter,
OpenDialogOptions options = null)
where TViewModel : class, IDialogViewModel, IViewModelParameter<TParameter>
{
var viewModel = ServiceLocator.Resolve<TViewModel>();
var viewModel = _iocContainer.Resolve<TViewModel>();
viewModel.Parameter = parameter;
return ShowImpl<TViewModel>(viewModel as ViewModelBase);
}
Expand Down
2 changes: 1 addition & 1 deletion Softeq.XToolkit.WhiteLabel.Droid/MainApplicationBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private void StartScopeForIoc()
{
var containerBuilder = new ContainerBuilder();
ConfigureIoc(containerBuilder);
ServiceLocator.StartScope(containerBuilder);
Dependencies.IocContainer.StartScope(containerBuilder);
}

protected abstract void ConfigureIoc(ContainerBuilder containerBuilder);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Developed by Softeq Development Corporation
// http://www.softeq.com

using System;
using System.Collections.Generic;
using System.Linq;
using Android.Content;
using Plugin.CurrentActivity;
using Softeq.XToolkit.Common.Interfaces;
using Softeq.XToolkit.WhiteLabel.Mvvm;
using Softeq.XToolkit.WhiteLabel.Navigation;
using Softeq.XToolkit.WhiteLabel.Threading;

namespace Softeq.XToolkit.WhiteLabel.Droid.Navigation
{
public class ActivityPageNavigationService : IPlatformNavigationService
{
private readonly ViewLocator _viewLocator;
private readonly IJsonSerializer _jsonSerializer;
private readonly ICurrentActivity _currentActivity;

private bool _isParamsSerializationEnabled;

public ActivityPageNavigationService(ViewLocator viewLocator, IJsonSerializer jsonSerializer,
ICurrentActivity currentActivity)
{
_viewLocator = viewLocator;
_jsonSerializer = jsonSerializer;
_currentActivity = currentActivity;

_isParamsSerializationEnabled = true;
}

public bool CanGoBack => !_currentActivity.Activity.IsTaskRoot;

public void GoBack()
{
Execute.BeginOnUIThread(() =>
{
if (CanGoBack)
{
_currentActivity.Activity.Finish();
}
else
{
_currentActivity.Activity.OnBackPressed();
}
});
}

public void Initialize(object navigation){}

public ActivityPageNavigationService DisableParameterSerialization()
{
_isParamsSerializationEnabled = false;

return this;
}

public void NavigateToViewModel(ViewModelBase viewModelBase, bool clearBackStack, IReadOnlyList<NavigationParameterModel> parameters)
{
var type = _viewLocator.GetTargetType(viewModelBase.GetType(), ViewType.Activity);
StartActivityImpl(type, clearBackStack, parameters);
}

private void StartActivityImpl(Type type, bool shouldClearBackStack = false,
IReadOnlyList<NavigationParameterModel> parameters = null)
{
var intent = new Intent(CrossCurrentActivity.Current.Activity, type);
TryToSetParameters(intent, parameters);

if (shouldClearBackStack)
{
intent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);
}

_currentActivity.Activity.StartActivity(intent);
}

private void TryToSetParameters(Intent intent, IReadOnlyList<NavigationParameterModel> parameters)
{
if (_isParamsSerializationEnabled && parameters != null && parameters.Any())
{
intent.PutExtra(Constants.ParametersKey, _jsonSerializer.Serialize(parameters));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ public class FrameNavigationService : IFrameNavigationService

private readonly Stack<ViewModelBase> _backStack;
private readonly ViewLocator _viewLocator;
private readonly IIocContainer _iocContainer;
private int _containerId;

public FrameNavigationService(ViewLocator viewLocator)
public FrameNavigationService(ViewLocator viewLocator, IIocContainer iocContainer)
{
_viewLocator = viewLocator;
_iocContainer = iocContainer;
_backStack = new Stack<ViewModelBase>();
}

Expand Down Expand Up @@ -68,7 +70,7 @@ public void Initialize(object navigation)
public void NavigateToViewModel<T, TParameter>(TParameter parameter)
where T : IViewModelBase, IViewModelParameter<TParameter>
{
var viewModel = ServiceLocator.Resolve<T>();
var viewModel = _iocContainer.Resolve<T>();
_viewLocator.TryInjectParameters(viewModel, this, FrameNavigationServiceParameterName);
viewModel.Parameter = parameter;
var baseFragment = (Fragment) _viewLocator.GetView(viewModel, ViewType.Fragment);
Expand All @@ -78,7 +80,7 @@ public void NavigateToViewModel<T, TParameter>(TParameter parameter)

public void NavigateToViewModel<T>(bool clearBackStack = false) where T : IViewModelBase
{
var viewModel = ServiceLocator.Resolve<T>();
var viewModel = _iocContainer.Resolve<T>();
_viewLocator.TryInjectParameters(viewModel, this, FrameNavigationServiceParameterName);
var fragment = (Fragment) _viewLocator.GetView(viewModel, ViewType.Fragment);

Expand Down
Loading

0 comments on commit 885b5ab

Please sign in to comment.