Skip to content

Commit

Permalink
Release 0.16-patch1 (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
kekchpek authored Sep 3, 2024
2 parents 76c9c59 + 85a764e commit 0d97a13
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 73 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.kekchpek.umvvm",
"displayName": "UnityMvvm",
"version": "0.16.0",
"version": "0.16.1",
"description": "The MVVM pattern core implemented for Unity3d",
"unity": "2022.3",
"documentationUrl": "https://github.com/kekchpek/UnityMVVM",
Expand Down
Binary file not shown.
51 changes: 6 additions & 45 deletions src/UnityMVVM/DI/DiContainerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using UnityMVVM.ViewModelCore.ViewModelsFactory;
using System;
using System.Linq;
using System.Threading.Tasks;
using ModestTree;
using UnityMVVM.DI.Config;
using UnityMVVM.DI.Environment;
Expand Down Expand Up @@ -119,24 +118,7 @@ public static void InstallView<TView, TViewModel, TViewModelImpl>(this DiContain
where TViewModelImpl : class, TViewModel
{
if (viewPrefabGetter == null) throw new ArgumentNullException(nameof(viewPrefabGetter));
InstallViewInternal<TView, TViewModel, TViewModelImpl>(container, viewName, () => Task.FromResult(viewPrefabGetter()), null);
}

/// <summary>
/// Installs <see cref="IViewModelsFactory"/> for specified View-ViewModel pair.
/// </summary>
/// <param name="container">MVVM container to configure.</param>
/// <param name="viewName">View identificator for opening.</param>
/// <param name="asyncViewPrefabGetter">The async method to obtain view prefab. View should contains <typeparamref name="TView"/> component inside.</param>
/// <typeparam name="TView">The type of a view</typeparam>
/// <typeparam name="TViewModel">The type of a view model.</typeparam>
/// <typeparam name="TViewModelImpl">The type, that implements a view model.</typeparam>
public static void InstallView<TView, TViewModel, TViewModelImpl>(this DiContainer container, string viewName, Func<Task<GameObject>> asyncViewPrefabGetter)
where TView : ViewBehaviour<TViewModel>
where TViewModel : class, IViewModel
where TViewModelImpl : class, TViewModel
{
InstallViewInternal<TView, TViewModel, TViewModelImpl>(container, viewName, asyncViewPrefabGetter, null);
InstallViewInternal<TView, TViewModel, TViewModelImpl>(container, viewName, viewPrefabGetter, null);
}

/// <inheritdoc cref="InstallView{TView,TViewModel,TViewModelImpl}(Zenject.DiContainer,string,Func{UnityEngine.GameObject})"/>
Expand All @@ -157,31 +139,10 @@ public static void InstallPoolableView
InstallViewInternal<TView, TViewModel, TViewModelImpl>(
container,
viewName,
() => Task.FromResult(viewPrefabGetter()),
viewPrefabGetter,
viewPool ?? new ViewPool<TView>());
}

/// <inheritdoc cref="InstallView{TView,TViewModel,TViewModelImpl}(Zenject.DiContainer,string,Func{System.Threading.Tasks.Task{UnityEngine.GameObject}})"/>
/// <param name="viewPool">The pool for views. Uses default <see cref="ViewPool{T}"/> object if null specified.</param>
public static void InstallPoolableView
<TView, TViewModel, TViewModelImpl>
#pragma warning disable CS1573
(this DiContainer container,
string viewName,
Func<Task<GameObject>> asyncViewPrefabGetter,
#pragma warning restore CS1573
IViewPool? viewPool = null)
where TView : ViewBehaviour<TViewModel>, IPoolableView
where TViewModel : class, IViewModel
where TViewModelImpl : class, TViewModel
{
InstallViewInternal<TView, TViewModel, TViewModelImpl>(
container,
viewName,
asyncViewPrefabGetter,
viewPool ?? new ViewPool<TView>());
}


/// <inheritdoc cref="InstallView{TView,TViewModel,TViewModelImpl}(Zenject.DiContainer,string,UnityEngine.GameObject)"/>
/// <param name="viewPool">The pool for views. Uses default <see cref="ViewPool{T}"/> object if null specified.</param>
public static void InstallPoolableView
Expand All @@ -199,13 +160,13 @@ public static void InstallPoolableView
InstallViewInternal<TView, TViewModel, TViewModelImpl>(
container,
viewName,
() => Task.FromResult(viewPrefab),
() => viewPrefab,
viewPool ?? new ViewPool<TView>());
}

private static void InstallViewInternal<TView, TViewModel, TViewModelImpl>(
DiContainer container, string viewName,
Func<Task<GameObject>> viewPrefabGetter, IViewPool? viewPool)
Func<GameObject> viewPrefabGetter, IViewPool? viewPool)
where TView : ViewBehaviour<TViewModel>
where TViewModel : class, IViewModel
where TViewModelImpl : class, TViewModel
Expand All @@ -223,7 +184,7 @@ private static void InstallViewInternal<TView, TViewModel, TViewModelImpl>(
.AsTransient()
.WithArgumentsExplicit(new []
{
new TypeValuePair(typeof(Func<Task<GameObject>>), viewPrefabGetter),
new TypeValuePair(typeof(Func<GameObject>), viewPrefabGetter),
new TypeValuePair(typeof(IViewPool), viewPool),
});
env.Mapper.Map<TView, TViewModelImpl>();
Expand Down
2 changes: 1 addition & 1 deletion src/UnityMVVM/ViewManager/IViewManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public interface IViewManager
/// <param name="container">The container to instantiate the view to.</param>
/// <param name="payload">View model payload.</param>
/// <returns>Returns created view model.</returns>
public Task<IViewModel> Create(IViewModel parent, string viewName, Transform container, IPayload? payload = null);
public IViewModel Create(IViewModel parent, string viewName, Transform container, IPayload? payload = null);

/// <summary>
/// Creates view model and corresponding view. Closes all views on layers above specified.
Expand Down
13 changes: 6 additions & 7 deletions src/UnityMVVM/ViewManager/ViewManagerImpl.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using AsyncReactAwait.Promises;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AsyncReactAwait.Bindable;
using UnityEngine;
using UnityEngine.Scripting;
Expand Down Expand Up @@ -83,7 +82,7 @@ public async IPromise OpenExact(string viewLayerId, string viewName, IPayload? p
var layer = GetLayer(viewLayerId);

await layer.Clear();
await CreateViewOnLayer(viewName, layer, payload);
CreateViewOnLayer(viewName, layer, payload);
}

/// <inheritdoc cref="IViewManager.CloseExact(string)"/>
Expand Down Expand Up @@ -145,9 +144,9 @@ public IReadOnlyList<string> GetLayerIds()
return _layerIds;
}

public async Task<IViewModel> Create(IViewModel parent, string viewName, Transform container, IPayload? payload = null)
public IViewModel Create(IViewModel parent, string viewName, Transform container, IPayload? payload = null)
{
return await _viewsContainer.ResolveViewFactory(viewName).Create(parent.Layer, parent, container, payload);
return _viewsContainer.ResolveViewFactory(viewName).Create(parent.Layer, parent, container, payload);
}

/// <inheritdoc cref="IViewManager.Open(string, string, IPayload)"/>
Expand Down Expand Up @@ -175,7 +174,7 @@ public async Task<IViewModel> Create(IViewModel parent, string viewName, Transfo
// open required view
if (_layers[i].Id == viewLayerId)
{
var viewModel = await CreateViewOnLayer(viewName, _layers[i], payload);
var viewModel = CreateViewOnLayer(viewName, _layers[i], payload);
return viewModel;
}
}
Expand All @@ -187,9 +186,9 @@ public async Task<IViewModel> Create(IViewModel parent, string viewName, Transfo
}
}

private async Task<IViewModel> CreateViewOnLayer(string viewName, IViewLayer layer, IPayload? payload)
private IViewModel CreateViewOnLayer(string viewName, IViewLayer layer, IPayload? payload)
{
var viewModel = await _viewsContainer.ResolveViewFactory(viewName).Create(layer, null, layer.Container, payload);
var viewModel = _viewsContainer.ResolveViewFactory(viewName).Create(layer, null, layer.Container, payload);
_createdViewsNames.Add(viewModel, viewName);
viewModel.Destroyed += OnViewModelDestroyed;
layer.Set(viewModel);
Expand Down
23 changes: 11 additions & 12 deletions src/UnityMVVM/ViewModelCore/ViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using UnityEngine;
using UnityMVVM.ViewManager;
using UnityMVVM.ViewManager.ViewLayer;
Expand Down Expand Up @@ -72,46 +71,46 @@ protected T CreateSubView<T>(string viewName, IPayload? payload = null) where T
{
return CreateSubView<T>(viewName, _layer.Container, payload);
}

/// <summary>
/// Creates a child view and view model.
/// </summary>
/// <param name="viewName">The view identifier to open.</param>
/// <param name="payload">The view model payload.</param>
/// <returns>Created view model.</returns>
protected async Task<IViewModel> CreateSubView(string viewName, IPayload? payload = null)
protected IViewModel CreateSubView(string viewName, IPayload? payload = null)
{
return await CreateSubView(viewName, _layer.Container, payload);
return CreateSubView(viewName, _layer.Container, payload);
}

/// <inheritdoc cref="CreateSubView(string,UnityMVVM.ViewModelCore.IPayload?)"/>
/// <inheritdoc cref="CreateSubView{T}(string,UnityMVVM.ViewModelCore.IPayload?)"/>
/// <param name="container">The container to instantiate view to.</param>
protected async Task<IViewModel> CreateSubView(
protected T CreateSubView<T>(
#pragma warning disable CS1573
string viewName,
#pragma warning restore CS1573
Transform container,
#pragma warning disable CS1573
IPayload? payload = null)
IPayload? payload = null)
#pragma warning restore CS1573
where T : class, IViewModel
{
var viewModel = await _viewManager.Create(this, viewName, container, payload);
var viewModel = _viewManager.Create<T>(this, viewName, container, payload);
return viewModel;
}

/// <inheritdoc cref="CreateSubView{T}(string,UnityMVVM.ViewModelCore.IPayload?)"/>
/// <param name="container">The container to instantiate view to.</param>
protected T CreateSubView<T>(
protected IViewModel CreateSubView(
#pragma warning disable CS1573
string viewName,
#pragma warning restore CS1573
Transform container,
#pragma warning disable CS1573
IPayload? payload = null)
#pragma warning restore CS1573
where T : class, IViewModel
{
var viewModel = _viewManager.Create<T>(this, viewName, container, payload);
var viewModel = _viewManager.Create(this, viewName, container, payload);
return viewModel;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine;
using UnityMVVM.ViewManager.ViewLayer;

namespace UnityMVVM.ViewModelCore.ViewModelsFactory
Expand All @@ -18,7 +17,7 @@ internal interface IViewModelsFactory
/// <param name="parentTransform">The transform to instantiate the view to.</param>
/// <param name="payload">View model payload.</param>
/// <returns>Returns created view model to control the view.</returns>
Task<IViewModel> Create(IViewLayer viewLayer,
IViewModel Create(IViewLayer viewLayer,
IViewModel? parent,
Transform parentTransform,
IPayload? payload = null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal class ViewModelsFactory<TView> : IViewModelsFactory
private readonly IInstantiator _instantiator;
private readonly IViewToViewModelMapper _viewToViewModelMapper;
private readonly IViewFactory _viewFactory;
private readonly Func<Task<GameObject>> _viewPrefabGetter;
private readonly Func<GameObject> _viewPrefabGetter;
private readonly IViewPool? _viewPool;

/// <summary>
Expand All @@ -33,7 +33,7 @@ internal class ViewModelsFactory<TView> : IViewModelsFactory
/// <param name="viewPool">The pool for views(if presented)</param>
[Preserve]
public ViewModelsFactory(
Func<Task<GameObject>> viewPrefabGetter,
Func<GameObject> viewPrefabGetter,
IInstantiator instantiator,
IViewToViewModelMapper viewToViewModelMapper,
IViewFactory viewFactory,
Expand All @@ -47,12 +47,12 @@ public ViewModelsFactory(
}

/// <inheritdoc cref="IViewModelsFactory.Create(IViewLayer, IViewModel, Transform, IPayload)"/>
public async Task<IViewModel> Create(IViewLayer viewLayer,
public IViewModel Create(IViewLayer viewLayer,
IViewModel? parent,
Transform transform,
IPayload? payload = null)
{
var view = _viewFactory.Instantiate<TView>(await _viewPrefabGetter.Invoke(), transform, _viewPool);
var view = _viewFactory.Instantiate<TView>(_viewPrefabGetter.Invoke(), transform, _viewPool);

if (view is not Component c)
throw new Exception("View should be a Component");
Expand Down

0 comments on commit 0d97a13

Please sign in to comment.