-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindowPresenter.cs
77 lines (65 loc) · 2.04 KB
/
MainWindowPresenter.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
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.DirectoryServices;
using System.Linq;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
namespace CorporateBrowser
{
public class MainWindowPresenter: ReactiveObject
{
private readonly ReactiveList<User> users;
public ListCollectionView Users { get; set; }
public MainWindowPresenter()
{
this.users = new ReactiveList<User>();
this.Users = new ListCollectionView(users);
this.Users.GroupDescriptions.Add(new PropertyGroupDescription("Team"));
GenerateData().ObserveOn(RxApp.MainThreadScheduler).Subscribe(data => this.users.Add(data));
}
static IObservable<User> GenerateData()
{
var usersSubject = new Subject<User>();
Observable.Start(() =>
{
try
{
var searchRoot = new DirectoryEntry("LDAP://DC=fabrikam,DC=com");
var searcher = new DirectorySearcher(searchRoot)
{
Asynchronous = false,
Filter = "(&(&(objectClass=user)(objectCategory=person))(!(memberof=InvalidAccounts)))",
};
searcher.PropertiesToLoad.AddRange(PROPERTIES_TO_LOAD);
using (var users = searcher.FindAll())
{
foreach (SearchResult ldapUser in users)
{
var userName = ldapUser.SafeReadString("name");
var mail = ldapUser.SafeReadString("mail");
var phone = ldapUser.SafeReadString("mobile");
var title = ldapUser.SafeReadString("title");
var team = ldapUser.SafeReadString("department");
var picture = ldapUser.SafeReadPicture();
var newUser = new User(userName, mail, phone, title, picture, team);
usersSubject.OnNext(newUser);
}
}
usersSubject.OnCompleted();
}
catch (Exception ex)
{
usersSubject.OnError(ex);
}
},
RxApp.TaskpoolScheduler);
return usersSubject;
}
private static readonly string[] PROPERTIES_TO_LOAD = new string[] { "name", "mail", "mobile", "title", "department", "jpegPhoto" };
}
}