Skip to content

Commit cd70cd0

Browse files
committed
Resolve #28. Fix ignoring converter issue when types match.
1 parent d0775f5 commit cd70cd0

File tree

14 files changed

+320
-37
lines changed

14 files changed

+320
-37
lines changed

src/UnityMvvmToolkit.Core/Interfaces/IProperty.T.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace UnityMvvmToolkit.Core.Interfaces
44
{
5-
public interface IProperty<T> : IReadOnlyProperty<T>
5+
public interface IProperty<T> : IReadOnlyProperty<T>, IProperty
66
{
77
new T Value { get; set; }
88

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace UnityMvvmToolkit.Core.Interfaces
2+
{
3+
public interface IProperty
4+
{
5+
}
6+
}

src/UnityMvvmToolkit.Core/Internal/ObjectHandlers/ObjectWrapperHandler.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public TProperty GetProperty<TProperty, TValueType>(IBindingContext context, Bin
5858
var targetType = typeof(TValueType);
5959
var sourceType = propertyType.GenericTypeArguments[0];
6060

61-
if (targetType == sourceType)
61+
if (targetType == sourceType && string.IsNullOrWhiteSpace(bindingData.ConverterName))
6262
{
6363
return (TProperty) property;
6464
}
@@ -88,7 +88,10 @@ public TProperty GetProperty<TProperty, TValueType>(IBindingContext context, Bin
8888
}
8989

9090
var args = new object[] { valueConverter };
91-
var wrapperType = typeof(PropertyWrapper<,>).MakeGenericType(sourceType, targetType);
91+
92+
var wrapperType = property is IProperty
93+
? typeof(PropertyWrapper<,>).MakeGenericType(sourceType, targetType)
94+
: typeof(ReadOnlyPropertyWrapper<,>).MakeGenericType(sourceType, targetType);
9295

9396
return (TProperty) ObjectWrapperHelper.CreatePropertyWrapper(wrapperType, args, converterId, property);
9497
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.Runtime.CompilerServices;
3+
using UnityMvvmToolkit.Core.Attributes;
4+
using UnityMvvmToolkit.Core.Interfaces;
5+
using UnityMvvmToolkit.Core.Internal.Interfaces;
6+
7+
namespace UnityMvvmToolkit.Core.Internal.ObjectWrappers
8+
{
9+
internal sealed class ReadOnlyPropertyWrapper<TSource, TValue> : IReadOnlyProperty<TValue>, IPropertyWrapper
10+
{
11+
private readonly IPropertyValueConverter<TSource, TValue> _valueConverter;
12+
13+
private int _converterId;
14+
private bool _isInitialized;
15+
16+
private TValue _value;
17+
18+
[Preserve]
19+
public ReadOnlyPropertyWrapper(IPropertyValueConverter<TSource, TValue> valueConverter)
20+
{
21+
_converterId = -1;
22+
_valueConverter = valueConverter;
23+
}
24+
25+
public int ConverterId => _converterId;
26+
27+
public TValue Value
28+
{
29+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
30+
get => _value;
31+
}
32+
33+
#pragma warning disable 67
34+
public event EventHandler<TValue> ValueChanged;
35+
#pragma warning restore 67
36+
37+
public IPropertyWrapper SetConverterId(int converterId)
38+
{
39+
if (_converterId != -1)
40+
{
41+
throw new InvalidOperationException("Can not change converter ID.");
42+
}
43+
44+
_converterId = converterId;
45+
46+
return this;
47+
}
48+
49+
public IPropertyWrapper SetProperty(object readOnlyProperty)
50+
{
51+
if (_isInitialized)
52+
{
53+
throw new InvalidOperationException(
54+
$"{nameof(ReadOnlyPropertyWrapper<TValue, TSource>)} was not reset.");
55+
}
56+
57+
_value = _valueConverter.Convert(((IReadOnlyProperty<TSource>) readOnlyProperty).Value);
58+
_isInitialized = true;
59+
60+
return this;
61+
}
62+
63+
public void Reset()
64+
{
65+
_value = default;
66+
_isInitialized = false;
67+
}
68+
}
69+
}

src/UnityMvvmToolkit.UnityPackage/Assets/Plugins/UnityMvvmToolkit/Runtime/Core/Interfaces/IProperty.T.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace UnityMvvmToolkit.Core.Interfaces
44
{
5-
public interface IProperty<T> : IReadOnlyProperty<T>
5+
public interface IProperty<T> : IReadOnlyProperty<T>, IProperty
66
{
77
new T Value { get; set; }
88

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace UnityMvvmToolkit.Core.Interfaces
2+
{
3+
public interface IProperty
4+
{
5+
}
6+
}

src/UnityMvvmToolkit.UnityPackage/Assets/Plugins/UnityMvvmToolkit/Runtime/Core/Interfaces/IProperty.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/UnityMvvmToolkit.UnityPackage/Assets/Plugins/UnityMvvmToolkit/Runtime/Core/Internal/ObjectHandlers/ObjectWrapperHandler.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public TProperty GetProperty<TProperty, TValueType>(IBindingContext context, Bin
5858
var targetType = typeof(TValueType);
5959
var sourceType = propertyType.GenericTypeArguments[0];
6060

61-
if (targetType == sourceType)
61+
if (targetType == sourceType && string.IsNullOrWhiteSpace(bindingData.ConverterName))
6262
{
6363
return (TProperty) property;
6464
}
@@ -88,7 +88,10 @@ public TProperty GetProperty<TProperty, TValueType>(IBindingContext context, Bin
8888
}
8989

9090
var args = new object[] { valueConverter };
91-
var wrapperType = typeof(PropertyWrapper<,>).MakeGenericType(sourceType, targetType);
91+
92+
var wrapperType = property is IProperty
93+
? typeof(PropertyWrapper<,>).MakeGenericType(sourceType, targetType)
94+
: typeof(ReadOnlyPropertyWrapper<,>).MakeGenericType(sourceType, targetType);
9295

9396
return (TProperty) ObjectWrapperHelper.CreatePropertyWrapper(wrapperType, args, converterId, property);
9497
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.Runtime.CompilerServices;
3+
using UnityMvvmToolkit.Core.Attributes;
4+
using UnityMvvmToolkit.Core.Interfaces;
5+
using UnityMvvmToolkit.Core.Internal.Interfaces;
6+
7+
namespace UnityMvvmToolkit.Core.Internal.ObjectWrappers
8+
{
9+
internal sealed class ReadOnlyPropertyWrapper<TSource, TValue> : IReadOnlyProperty<TValue>, IPropertyWrapper
10+
{
11+
private readonly IPropertyValueConverter<TSource, TValue> _valueConverter;
12+
13+
private int _converterId;
14+
private bool _isInitialized;
15+
16+
private TValue _value;
17+
18+
[Preserve]
19+
public ReadOnlyPropertyWrapper(IPropertyValueConverter<TSource, TValue> valueConverter)
20+
{
21+
_converterId = -1;
22+
_valueConverter = valueConverter;
23+
}
24+
25+
public int ConverterId => _converterId;
26+
27+
public TValue Value
28+
{
29+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
30+
get => _value;
31+
}
32+
33+
#pragma warning disable 67
34+
public event EventHandler<TValue> ValueChanged;
35+
#pragma warning restore 67
36+
37+
public IPropertyWrapper SetConverterId(int converterId)
38+
{
39+
if (_converterId != -1)
40+
{
41+
throw new InvalidOperationException("Can not change converter ID.");
42+
}
43+
44+
_converterId = converterId;
45+
46+
return this;
47+
}
48+
49+
public IPropertyWrapper SetProperty(object readOnlyProperty)
50+
{
51+
if (_isInitialized)
52+
{
53+
throw new InvalidOperationException(
54+
$"{nameof(ReadOnlyPropertyWrapper<TValue, TSource>)} was not reset.");
55+
}
56+
57+
_value = _valueConverter.Convert(((IReadOnlyProperty<TSource>) readOnlyProperty).Value);
58+
_isInitialized = true;
59+
60+
return this;
61+
}
62+
63+
public void Reset()
64+
{
65+
_value = default;
66+
_isInitialized = false;
67+
}
68+
}
69+
}

src/UnityMvvmToolkit.UnityPackage/Assets/Plugins/UnityMvvmToolkit/Runtime/Core/Internal/ObjectWrappers/ReadOnlyPropertyWrapper.TSource.TValue.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)