Skip to content

Commit 49cfe89

Browse files
committed
Refactor samples.
1 parent eddad92 commit 49cfe89

33 files changed

Lines changed: 247 additions & 223 deletions

samples/Unity.Mvvm.Calc/Assets/Scripts/AppContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ private void RegisterInstance<T>(T instance)
3232
_registeredTypes.Add(typeof(T), instance);
3333
}
3434

35-
private IReadOnlyDictionary<char, IMathOperation> GetMathOperations()
35+
private static IReadOnlyDictionary<char, IMathOperation> GetMathOperations()
3636
{
3737
return new Dictionary<char, IMathOperation>
3838
{

samples/Unity.Mvvm.Calc/Assets/Scripts/ViewModels/CalcViewModel.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
using UnityMvvmToolkit.Core;
44
using UnityMvvmToolkit.Core.Interfaces;
55

6+
// ReSharper disable UnusedMember.Global
7+
// ReSharper disable UnusedAutoPropertyAccessor.Global
8+
69
namespace ViewModels
710
{
811
public class CalcViewModel : IBindingContext

samples/Unity.Mvvm.Counter/Assets/Scripts/AppContext.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
using Interfaces.Services;
55
using Services;
66
using UnityEngine;
7-
using UnityMvvmToolkit.Core;
87
using UnityMvvmToolkit.Core.Converters.PropertyValueConverters;
9-
using UnityMvvmToolkit.Core.Enums;
108
using UnityMvvmToolkit.Core.Interfaces;
119
using ValueConverters;
1210
using ViewModels;
@@ -24,7 +22,7 @@ public void Construct()
2422
RegisterInstance<IThemeService>(_themeService);
2523
RegisterInstance(new CounterViewModel());
2624
RegisterInstance<IDataStoreService>(new DataStoreService(this));
27-
RegisterInstance(GetObjectProvider());
25+
RegisterInstance(GetValueConverters());
2826
}
2927

3028
public T Resolve<T>()
@@ -37,14 +35,6 @@ private void RegisterInstance<T>(T instance)
3735
_registeredTypes.Add(typeof(T), instance);
3836
}
3937

40-
private static IObjectProvider GetObjectProvider()
41-
{
42-
return new BindingContextObjectProvider(GetValueConverters())
43-
.WarmupViewModel<CounterViewModel>()
44-
.WarmupValueConverter<IntToStrConverter>(1)
45-
.WarmupValueConverter<ThemeModeToBoolConverter>(1, WarmupType.OnlyByName);
46-
}
47-
4838
private static IValueConverter[] GetValueConverters()
4939
{
5040
return new IValueConverter[]

samples/Unity.Mvvm.Counter/Assets/Scripts/BindableUIElements/BindableContentPage.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ public void ResetBindingContext(IObjectProvider objectProvider)
2828
return;
2929
}
3030

31+
_themeModeProperty.ValueChanged -= OnPropertyValueChanged;
32+
3133
objectProvider.ReturnReadOnlyProperty(_themeModeProperty);
3234

33-
_themeModeProperty.ValueChanged -= OnPropertyValueChanged;
3435
_themeModeProperty = null;
3536
}
3637

samples/Unity.Mvvm.Counter/Assets/Scripts/BindableUIElements/BindableCountLabel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public BindableCountLabel()
1919
protected override void UpdateControlText(string newText)
2020
{
2121
base.UpdateControlText(newText);
22+
2223
_scaleAnimation?.PlayAsync().Forget();
2324
}
2425

samples/Unity.Mvvm.Counter/Assets/Scripts/BindableUIElements/BindableCounterSlider.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ public void SetBindingContext(IBindingContext context, IObjectProvider objectPro
2020

2121
public void ResetBindingContext(IObjectProvider objectProvider)
2222
{
23-
_incrementCommand = null;
24-
_decrementCommand = null;
25-
2623
Increment -= OnIncrement;
2724
Decrement -= OnDecrement;
25+
26+
_incrementCommand = null;
27+
_decrementCommand = null;
2828
}
2929

3030
private void OnIncrement(object sender, EventArgs e)

samples/Unity.Mvvm.Counter/Assets/Scripts/BindableUIElements/BindableThemeSwitcher.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public void SetBindingContext(IBindingContext context, IObjectProvider objectPro
1818
_valueProperty.ValueChanged += OnPropertyValueChanged;
1919

2020
UpdateControlValue(_valueProperty.Value);
21+
22+
ValueChanged += OnControlValueChanged;
2123
}
2224

2325
public void ResetBindingContext(IObjectProvider objectProvider)
@@ -27,13 +29,18 @@ public void ResetBindingContext(IObjectProvider objectProvider)
2729
return;
2830
}
2931

32+
_valueProperty.ValueChanged -= OnPropertyValueChanged;
33+
3034
objectProvider.ReturnProperty(_valueProperty);
3135

32-
_valueProperty.ValueChanged -= OnPropertyValueChanged;
3336
_valueProperty = null;
37+
38+
ValueChanged -= OnControlValueChanged;
39+
40+
UpdateControlValue(false);
3441
}
3542

36-
protected override void OnControlValueChanged(bool value)
43+
private void OnControlValueChanged(object sender, bool value)
3744
{
3845
_valueProperty.Value = value;
3946
}

samples/Unity.Mvvm.Counter/Assets/Scripts/Services/ThemeService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class ThemeService : MonoBehaviour, IThemeService
1414
public void SetThemeMode(ThemeMode mode)
1515
{
1616
var theme = mode == ThemeMode.Light ? _lightTheme : _darkTheme;
17+
1718
if (_panelSettings.themeStyleSheet != theme)
1819
{
1920
_panelSettings.themeStyleSheet = theme;

samples/Unity.Mvvm.Counter/Assets/Scripts/UIElements/ThemeSwitcher.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,13 @@ public bool IsDarkMode
3838
set => SetValue(value);
3939
}
4040

41+
public event EventHandler<bool> ValueChanged;
42+
4143
public void SetValueWithoutNotify(bool value)
4244
{
4345
SetValue(value, false);
4446
}
4547

46-
protected virtual void OnControlValueChanged(bool value)
47-
{
48-
throw new NotImplementedException();
49-
}
50-
5148
private void CreateLabelContainer(string containerName, string labelText, string labelClassNameModifier)
5249
{
5350
var labelContainer = new VisualElement();
@@ -108,7 +105,7 @@ private void SetValue(bool value, bool notify = true)
108105

109106
if (notify)
110107
{
111-
OnControlValueChanged(value);
108+
ValueChanged?.Invoke(this, value);
112109
}
113110
}
114111

samples/Unity.Mvvm.Counter/Assets/Scripts/ViewModels/CounterViewModel.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using UnityMvvmToolkit.Core;
33
using UnityMvvmToolkit.Core.Interfaces;
44

5+
// ReSharper disable UnusedAutoPropertyAccessor.Global
6+
57
namespace ViewModels
68
{
79
public class CounterViewModel : IBindingContext

0 commit comments

Comments
 (0)