Skip to content

New functionality and bug fixes. #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions samples/Unity.Mvvm.Calc/Assets/UI Toolkit/CalcView.uxml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<ui:VisualElement name="BottomPanel" class="bottom-panel">
<ui:VisualElement name="Row1" class="elements-row">
<UnityMvvmToolkit.UITK.BindableUIElements.BindableButton text="C" name="ButtonClear" command="ClearCommand" class="round-button round-button__clear-label" />
<UnityMvvmToolkit.UITK.BindableUIElements.ButtonUITK text="+/-" name="ButtonSign" enabled="false" class="round-button round-button__operation-label" />
<UnityMvvmToolkit.UITK.BindableUIElements.ButtonUITK text="%" name="ButtonPercentage" enabled="false" class="round-button round-button__operation-label" />
<UnityMvvmToolkit.UITK.BindableUIElements.BindableButton text="+/-" name="ButtonSign" enabled="false" class="round-button round-button__operation-label" />
<UnityMvvmToolkit.UITK.BindableUIElements.BindableButton text="%" name="ButtonPercentage" enabled="false" class="round-button round-button__operation-label" />
<UnityMvvmToolkit.UITK.BindableUIElements.BindableButton text="÷" name="ButtonDivision" command="OperationCommand, ÷" class="round-button round-button__operation-label" />
</ui:VisualElement>
<ui:VisualElement name="Row2" class="elements-row">
Expand All @@ -31,7 +31,7 @@
</ui:VisualElement>
<ui:VisualElement name="Row5" class="elements-row">
<UnityMvvmToolkit.UITK.BindableUIElements.BindableButton text="0" name="Button0" command="NumberCommand, 0" class="round-button round-button--wide" />
<UnityMvvmToolkit.UITK.BindableUIElements.ButtonUITK text="." name="ButtonDot" enabled="false" class="round-button" />
<UnityMvvmToolkit.UITK.BindableUIElements.BindableButton text="." name="ButtonDot" enabled="false" class="round-button" />
<UnityMvvmToolkit.UITK.BindableUIElements.BindableButton text="=" name="ButtonEqual" command="CalculateCommand" class="round-button round-button--highlighted" />
</ui:VisualElement>
</ui:VisualElement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
}

.check-box__tick--animation {
transition-property: width height;
transition-property: width, height;
transition-duration: 150ms, 150ms;
transition-timing-function: ease-out-back, ease-out-back;
}
2 changes: 1 addition & 1 deletion src/UnityMvvmToolkit.Core/.config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"isRoot": true,
"tools": {
"dotnet-stryker": {
"version": "3.7.1",
"version": "3.9.0",
"commands": [
"dotnet-stryker"
]
Expand Down
7 changes: 1 addition & 6 deletions src/UnityMvvmToolkit.Core/Attributes/ObservableAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
using System;

namespace System.Runtime.CompilerServices
{
internal static class IsExternalInit {}
}

namespace UnityMvvmToolkit.Core.Attributes
{
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public sealed class ObservableAttribute : Attribute
{
/// <summary>
Expand Down
6 changes: 2 additions & 4 deletions src/UnityMvvmToolkit.Core/BindingContextObjectProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ public TCommand GetCommand<TCommand>(IBindingContext context, string propertyNam
{
EnsureIsNotNullOrWhiteSpace(propertyName, nameof(propertyName));

if (TryGetContextMemberInfo(context.GetType(), propertyName, out var memberInfo) == false ||
memberInfo.MemberType != MemberTypes.Property)
if (TryGetContextMemberInfo(context.GetType(), propertyName, out var memberInfo) == false)
{
throw new InvalidOperationException($"Command '{propertyName}' not found.");
}
Expand All @@ -131,8 +130,7 @@ public IBaseCommand RentCommandWrapper(IBindingContext context, CommandBindingDa
EnsureIsNotNullOrWhiteSpace(bindingData.ParameterValue,
$"Command '{bindingData.PropertyName}' has no parameter. Use {nameof(GetCommand)} instead.");

if (TryGetContextMemberInfo(context.GetType(), bindingData.PropertyName, out var memberInfo) == false ||
memberInfo.MemberType != MemberTypes.Property)
if (TryGetContextMemberInfo(context.GetType(), bindingData.PropertyName, out var memberInfo) == false)
{
throw new InvalidOperationException($"Command '{bindingData.PropertyName}' not found.");
}
Expand Down
35 changes: 21 additions & 14 deletions src/UnityMvvmToolkit.Core/Internal/BindingContextMemberProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,32 +58,39 @@ private static bool TryGetFieldHashCode(Type contextType, FieldInfo fieldInfo, o
return false;
}

if (fieldInfo.IsPublic)
if (HasObservableAttribute(fieldInfo, out var propertyName))
{
return TryGetHashCode(contextType, fieldInfo.Name, fieldInfo.FieldType, out hashCode);
return string.IsNullOrWhiteSpace(propertyName)
? TryGetHashCode(contextType, GetBindableName(fieldInfo.Name), fieldInfo.FieldType, out hashCode)
: TryGetHashCode(contextType, propertyName, fieldInfo.FieldType, out hashCode);
}

if (HasObservableAttribute(fieldInfo, out var propertyName) == false)
if (fieldInfo.IsPublic)
{
hashCode = default;
return false;
return TryGetHashCode(contextType, fieldInfo.Name, fieldInfo.FieldType, out hashCode);
}

return string.IsNullOrWhiteSpace(propertyName)
? TryGetHashCode(contextType, GetFieldName(fieldInfo.Name), fieldInfo.FieldType, out hashCode)
: TryGetHashCode(contextType, propertyName, fieldInfo.FieldType, out hashCode);
hashCode = default;
return false;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool TryGetPropertyHashCode(Type contextType, PropertyInfo propertyInfo, out int hashCode)
{
if (propertyInfo.GetMethod.IsPrivate)
if (HasObservableAttribute(propertyInfo, out var propertyName))
{
hashCode = default;
return false;
return string.IsNullOrWhiteSpace(propertyName)
? TryGetHashCode(contextType, GetBindableName(propertyInfo.Name), propertyInfo.PropertyType, out hashCode)
: TryGetHashCode(contextType, propertyName, propertyInfo.PropertyType, out hashCode);
}

return TryGetHashCode(contextType, propertyInfo.Name, propertyInfo.PropertyType, out hashCode);
if (propertyInfo.GetMethod.IsPublic)
{
return TryGetHashCode(contextType, propertyInfo.Name, propertyInfo.PropertyType, out hashCode);
}

hashCode = default;
return false;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down Expand Up @@ -116,9 +123,9 @@ private static bool HasObservableAttribute(MemberInfo fieldInfo, out string prop
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static string GetFieldName(string fieldName)
private static string GetBindableName(string memberName)
{
var resultName = fieldName;
var resultName = memberName;

if (resultName.Length > 1)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.ComponentModel;

// ReSharper disable CheckNamespace
// ReSharper disable UnusedType.Global

namespace System.Runtime.CompilerServices
{
[EditorBrowsable(EditorBrowsableState.Never)]
internal static class IsExternalInit
{
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using UnityMvvmToolkit.Core.Interfaces;

namespace UnityMvvmToolkit.Common.Interfaces
{
public interface IBindableCollection : IBindableElement
{
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using UnityMvvmToolkit.Core.Interfaces;

namespace UnityMvvmToolkit.Common.Interfaces
{
public interface IBindingContextProvider
{
bool IsValid { get; }
IBindingContext BindingContext { get;}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ private void Awake()
private void OnDestroy()
{
ResetBindingContext();
OnDispose();
}

public void SetBindingContext(IBindingContext context, IObjectProvider objectProvider)
{
if (_bindingContext != null)
if (_bindingContext is not null)
{
throw new InvalidOperationException(
$"{GetType().Name} - binding context was not reset. Reset the binding context first.");
Expand All @@ -53,11 +54,12 @@ public void ResetBindingContext(IObjectProvider objectProvider)
}

protected abstract void OnInit();
protected abstract IBindableElement[] GetBindableElements();
protected abstract void OnDispose();
protected abstract IReadOnlyList<IBindableElement> GetBindableElements();

protected virtual TBindingContext GetBindingContext()
{
if (typeof(TBindingContext).GetConstructor(Type.EmptyTypes) == null)
if (typeof(TBindingContext).GetConstructor(Type.EmptyTypes) is null)
{
throw new InvalidOperationException(
$"Cannot create an instance of the type parameter {typeof(TBindingContext)} because it does not have a parameterless constructor.");
Expand Down Expand Up @@ -109,13 +111,13 @@ private void ResetBindingContext()
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void SetBindingContext(ReadOnlySpan<IBindableElement> bindableElements, IBindingContext context,
private void SetBindingContext(IReadOnlyList<IBindableElement> bindableElements, IBindingContext context,
IObjectProvider objectProvider, bool initialize)
{
_bindingContext = (TBindingContext) context;
_objectProvider = objectProvider;

for (var i = 0; i < bindableElements.Length; i++)
for (var i = 0; i < bindableElements.Count; i++)
{
var bindableElement = bindableElements[i];

Expand All @@ -129,10 +131,10 @@ private void SetBindingContext(ReadOnlySpan<IBindableElement> bindableElements,
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void ResetBindingContext(ReadOnlySpan<IBindableElement> bindableElements,
private void ResetBindingContext(IReadOnlyList<IBindableElement> bindableElements,
IObjectProvider objectProvider, bool dispose)
{
for (var i = 0; i < bindableElements.Length; i++)
for (var i = 0; i < bindableElements.Count; i++)
{
var bindableElement = bindableElements[i];

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"name": "UnityMvvmToolkit.Common",
"rootNamespace": "",
"references": [
"GUID:5c4878a33f8e8b04cb2f76d30bb7b0b0"
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
using System;

namespace System.Runtime.CompilerServices
{
internal static class IsExternalInit {}
}

namespace UnityMvvmToolkit.Core.Attributes
{
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public sealed class ObservableAttribute : Attribute
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ public TCommand GetCommand<TCommand>(IBindingContext context, string propertyNam
{
EnsureIsNotNullOrWhiteSpace(propertyName, nameof(propertyName));

if (TryGetContextMemberInfo(context.GetType(), propertyName, out var memberInfo) == false ||
memberInfo.MemberType != MemberTypes.Property)
if (TryGetContextMemberInfo(context.GetType(), propertyName, out var memberInfo) == false)
{
throw new InvalidOperationException($"Command '{propertyName}' not found.");
}
Expand All @@ -131,8 +130,7 @@ public IBaseCommand RentCommandWrapper(IBindingContext context, CommandBindingDa
EnsureIsNotNullOrWhiteSpace(bindingData.ParameterValue,
$"Command '{bindingData.PropertyName}' has no parameter. Use {nameof(GetCommand)} instead.");

if (TryGetContextMemberInfo(context.GetType(), bindingData.PropertyName, out var memberInfo) == false ||
memberInfo.MemberType != MemberTypes.Property)
if (TryGetContextMemberInfo(context.GetType(), bindingData.PropertyName, out var memberInfo) == false)
{
throw new InvalidOperationException($"Command '{bindingData.PropertyName}' not found.");
}
Expand Down
Loading