/
MainViewModel.cs
189 lines (169 loc) · 5.85 KB
/
MainViewModel.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
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using News.Resources;
using System.Collections.Generic;
using News.Models;
using System.Diagnostics;
using System.Threading.Tasks;
namespace News.ViewModels
{
public class MainViewModel : INotifyPropertyChanged
{
public MainViewModel(AppSettings settings)
{
this.settings = settings;
this.Items = new ObservableCollection<ItemViewModel>();
this.ActiveItems = new ObservableCollection<ItemViewModel>();
this.Folders = new ObservableCollection<FolderViewModel>();
if (this.settings.UrlSetting.StartsWith("http"))
{
this.server = new Server(this.settings.UrlSetting, this.settings.UsernameSetting, this.settings.PasswordSetting);
}
}
public bool IsConfigured
{
get
{
return this.server != null;
}
}
private Server server;
private AppSettings settings;
/// <summary>
/// A collection for ItemViewModel objects.
/// </summary>
public ObservableCollection<ItemViewModel> Items { get; private set; }
public ObservableCollection<FolderViewModel> Folders { get; private set; }
public ObservableCollection<ItemViewModel> ActiveItems { get; set; }
private string _sampleProperty = "Sample Runtime Property Value";
/// <summary>
/// Sample ViewModel property; this property is used in the view to display its value using a Binding
/// </summary>
/// <returns></returns>
public string SampleProperty
{
get
{
return _sampleProperty;
}
set
{
if (value != _sampleProperty)
{
_sampleProperty = value;
NotifyPropertyChanged("SampleProperty");
}
}
}
public ItemViewModel ActiveItem { get; set; }
public FolderViewModel ActiveFolder { get; set; }
/// <summary>
/// Sample property that returns a localized string
/// </summary>
public string LocalizedSampleProperty
{
get
{
return AppResources.SampleProperty;
}
}
public bool IsDataLoaded
{
get;
private set;
}
public bool IsLoading { get; set; }
/// <summary>
/// Creates and adds a few ItemViewModel objects into the Items collection.
/// </summary>
async public Task<bool> LoadData()
{
if (!this.IsConfigured)
{
return false;
}
IsLoading = true;
this.Folders.Clear();
NotifyPropertyChanged("IsLoading");
try
{
Folder[] folders = await server.getFoldersAsync();
FolderViewModel folderView;
FolderViewModel all = new FolderViewModel(new Folder() { name = "All", id = -1 }, server);
this.Folders.Add(all);
foreach (Folder folder in folders)
{
folderView = new FolderViewModel(folder, server);
this.Folders.Add(folderView);
}
IsLoading = false;
this.IsDataLoaded = true;
NotifyPropertyChanged("IsLoading");
return true;
}
catch (Exception e)
{
Debug.WriteLine("loading folders failed");
IsLoading = false;
NotifyPropertyChanged("IsLoading");
this.server = null;
return false;
}
}
public void applySettings(AppSettings settings)
{
this.settings = settings;
this.IsDataLoaded = false;
if (this.settings.UrlSetting.StartsWith("http"))
{
this.server = new Server(this.settings.UrlSetting, this.settings.UsernameSetting, this.settings.PasswordSetting);
}
else
{
this.server = null;
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
async public void loadFolder(int i, int offset)
{
try
{
IsLoading = true;
NotifyPropertyChanged("IsLoading");
ActiveFolder = this.Folders[i];
Folder folder = this.Folders[i].folder;
Item[] items;
if (folder.id >= 0)
{
items = await server.getFolderItemsAsync(folder, offset);
}
else
{
items = await server.getAllItemsAsync(offset);
}
ItemViewModel itemView;
foreach (Item item in items)
{
itemView = new ItemViewModel(item, server);
this.Folders[i].Items.Add(itemView);
}
IsLoading = false;
NotifyPropertyChanged("IsLoading");
}
catch (Exception e)
{
IsLoading = false;
NotifyPropertyChanged("IsLoading");
}
}
}
}