/
BoardListViewModel.cs
237 lines (207 loc) · 7.69 KB
/
BoardListViewModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using Caliburn.Micro;
using JetBrains.Annotations;
using LinqToVisualTree;
using Microsoft.Phone.Shell;
using trello.Assets;
using trello.Interactions;
using trello.Services.Handlers;
using trello.Views.Boards;
using trellow.api;
using trellow.api.Lists;
namespace trello.ViewModels.Boards
{
[UsedImplicitly]
public class BoardListViewModel : PivotItemViewModel,
IConfigureTheAppBar,
IHandle<CardCreated>,
IHandle<CardDeleted>,
IHandle<CardMovingFromList>
{
private readonly ITrello _api;
private readonly INavigationService _navigation;
private readonly IEventAggregator _events;
private readonly IWindowManager _windows;
private readonly Func<CardViewModel> _cardFactory;
private string _name;
private string _id;
private string _previousId;
private string _nextId;
private string _boardId;
private InteractionManager _interactionManager;
[UsedImplicitly]
public string Id
{
get { return _id; }
set
{
if (value == _id) return;
_id = value;
NotifyOfPropertyChange(() => Id);
}
}
[UsedImplicitly]
public string PreviousId
{
get { return _previousId; }
set
{
if (value == _previousId) return;
_previousId = value;
NotifyOfPropertyChange(() => PreviousId);
}
}
[UsedImplicitly]
public string NextId
{
get { return _nextId; }
set
{
if (value == _nextId) return;
_nextId = value;
NotifyOfPropertyChange(() => NextId);
}
}
[UsedImplicitly]
public string BoardId
{
get { return _boardId; }
set
{
if (value == _boardId) return;
_boardId = value;
NotifyOfPropertyChange(() => BoardId);
}
}
[UsedImplicitly]
public string Name
{
get { return _name; }
set
{
if (value == _name) return;
_name = value;
NotifyOfPropertyChange(() => Name);
DisplayName = _name;
}
}
[UsedImplicitly]
public IObservableCollection<CardViewModel> Cards { get; set; }
public BoardListViewModel(ITrello api,
INavigationService navigation,
IEventAggregator events,
IWindowManager windows,
Func<CardViewModel> cardFactory)
{
_api = api;
_navigation = navigation;
_events = events;
_windows = windows;
_cardFactory = cardFactory;
_events.Subscribe(this);
Cards = new BindableCollection<CardViewModel>();
}
protected override void OnInitialize()
{
RefreshLists();
_interactionManager = new InteractionManager();
var view = GetView() as BoardListView;
if (view == null) return;
var scrollViewer = view.Cards.Descendants<ScrollViewer>().Cast<ScrollViewer>().SingleOrDefault();
_interactionManager = new HoldCardInteraction(view.DragImage, view.Cards, scrollViewer);
_interactionManager.AddInteraction(new DragVerticalInteraction(view.DragImage,
view.Cards,
scrollViewer,
_events));
_interactionManager.AddInteraction(new DragHorizontalInteraction(view.Cards,
_events,
PreviousId,
NextId));
}
private async void RefreshLists()
{
var cards = await _api.Cards.ForList(new ListId(Id));
var vms = cards.Select(card =>
{
var vm = _cardFactory()
.InitializeWith(card)
.EnableInteractions(_interactionManager);
return vm;
});
Cards.Clear();
Cards.AddRange(vms);
}
public BoardListViewModel InitializeWith(IEnumerable<List> allLists, List list)
{
Id = list.Id;
BoardId = list.IdBoard;
Name = list.Name;
// we're going to cheat and use the BCL for a circular list
var ll = new LinkedList<List>(allLists);
var node = ll.Find(list);
if (node != null) PreviousId = (node.Previous ?? ll.Last).Value.Id;
if (node != null) NextId = (node.Next ?? ll.First).Value.Id;
return this;
}
public ApplicationBar Configure(ApplicationBar existing)
{
var addButton = new ApplicationBarIconButton(new AssetUri("Icons/dark/appbar.add.rest.png"))
{Text = "add card"};
addButton.Click += (sender, args) => AddCard();
existing.Buttons.Add(addButton);
return existing;
}
private void AddCard()
{
var model = new AddCardViewModel(GetView(), _events)
{
BoardId = BoardId,
ListId = Id
};
_windows.ShowDialog(model);
}
public void Handle(CardCreated message)
{
var vm = _cardFactory()
.InitializeWith(message.Card)
.EnableInteractions(_interactionManager);
Cards.Add(vm);
_navigation.UriFor<CardDetailPivotViewModel>()
.WithParam(x => x.Id, vm.Id)
.Navigate();
}
public void Handle(CardDeleted message)
{
// note: make sure this is idempotent since it will be called for each list on an opened board
var found = Cards.Where(card => card.Id == message.CardId).ToArray();
foreach (var card in found)
Cards.Remove(card);
}
public void Handle(CardMovingFromList message)
{
if (message.DestinationListId == Id)
{
// this list has a new card assigned to it from a
// previous list
var vm = _cardFactory()
.InitializeWith(message.Card)
.EnableInteractions(_interactionManager);
Cards.Insert(0, vm);
Cards.Refresh();
}
else if (message.SourceListId == Id)
{
// this list has lost a card assigned to another
// list
var vm = Cards
.Single(c => c.Id == message.Card.Id)
.DisableInteractions();
//Cards.Remove(vm);
//Cards.Refresh();
}
}
}
}