Skip to content

Commit 119c9be

Browse files
committed
Change task item template binding logic.
1 parent 2703f38 commit 119c9be

3 files changed

Lines changed: 23 additions & 13 deletions

File tree

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@ public void Construct()
2222
{
2323
_disposables = new List<IDisposable>();
2424
_registeredTypes = new Dictionary<Type, object>();
25-
25+
2626
RegisterInstance(_addTaskView);
27-
27+
2828
RegisterInstance(new TaskBroker());
2929
RegisterInstance<IDialogsService>(new DialogsService(this));
3030

31-
RegisterInstance(new MainViewModel(this, _taskItemAsset));
31+
RegisterInstance(new MainViewModel(this));
3232
RegisterInstance(new AddTaskDialogViewModel(this));
33-
33+
3434
RegisterInstance<IDataStoreService>(new DataStoreService(this));
35-
// RegisterInstance<IBindableElementsFactory>(new ToDoListBindableElementsFactory(_taskItemAsset));
3635
RegisterInstance(GetValueConverters());
36+
RegisterInstance(GetCollectionItemTemplates());
3737
}
3838

3939
public T Resolve<T>()
@@ -71,4 +71,12 @@ private IValueConverter[] GetValueConverters()
7171
new IntToStrConverter()
7272
};
7373
}
74+
75+
private IReadOnlyDictionary<Type, object> GetCollectionItemTemplates()
76+
{
77+
return new Dictionary<Type, object>
78+
{
79+
{ typeof(TaskItemViewModel), _taskItemAsset }
80+
};
81+
}
7482
}

samples/Unity.Mvvm.ToDoList/Assets/Scripts/ViewModels/MainViewModel.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
using Extensions;
1212
using Interfaces;
1313
using Interfaces.Services;
14-
using UnityEngine.UIElements;
1514
using UnityMvvmToolkit.Common.Interfaces;
1615
using UnityMvvmToolkit.Core;
1716
using UnityMvvmToolkit.Core.Interfaces;
@@ -32,7 +31,7 @@ public class MainViewModel : IBindingContext, IDisposable
3231

3332
private readonly IReadOnlyProperty<ObservableCollection<TaskItemViewModel>> _taskItems;
3433

35-
public MainViewModel(IAppContext appContext, VisualTreeAsset taskItemTemplate)
34+
public MainViewModel(IAppContext appContext)
3635
{
3736
_dialogsService = appContext.Resolve<IDialogsService>();
3837

@@ -48,8 +47,6 @@ public MainViewModel(IAppContext appContext, VisualTreeAsset taskItemTemplate)
4847

4948
_isAddTaskDialogActive = new ObservableProperty<bool>();
5049

51-
TaskItemTemplate = new ObservableProperty<VisualTreeAsset>(taskItemTemplate);
52-
5350
SubscribeOnTaskAddMessage(appContext.Resolve<TaskBroker>()).Forget();
5451
}
5552

@@ -58,8 +55,6 @@ public MainViewModel(IAppContext appContext, VisualTreeAsset taskItemTemplate)
5855
public int CompletedTasks => _completedTasks.Value;
5956
public IReadOnlyCollection<TaskItemViewModel> TaskItems => _taskItems.Value;
6057

61-
public IReadOnlyProperty<VisualTreeAsset> TaskItemTemplate { get; }
62-
6358
public bool IsAddTaskDialogActive
6459
{
6560
get => _isAddTaskDialogActive.Value;

samples/Unity.Mvvm.ToDoList/Assets/Scripts/Views/BaseView.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using Interfaces;
1+
using System;
2+
using System.Collections.Generic;
3+
using Interfaces;
24
using UnityEngine;
35
using UnityMvvmToolkit.Core.Interfaces;
46
using UnityMvvmToolkit.UITK;
@@ -11,7 +13,7 @@ public class BaseView<TBindingContext> : DocumentView<TBindingContext>
1113
[SerializeField] private AppContext _appContext;
1214

1315
protected IAppContext AppContext => _appContext;
14-
16+
1517
protected override TBindingContext GetBindingContext()
1618
{
1719
return _appContext.Resolve<TBindingContext>();
@@ -21,5 +23,10 @@ protected override IValueConverter[] GetValueConverters()
2123
{
2224
return _appContext.Resolve<IValueConverter[]>();
2325
}
26+
27+
protected override IReadOnlyDictionary<Type, object> GetCollectionItemTemplates()
28+
{
29+
return _appContext.Resolve<IReadOnlyDictionary<Type, object>>();
30+
}
2431
}
2532
}

0 commit comments

Comments
 (0)