-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainViewModel.cs
103 lines (87 loc) · 3.07 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
using LocalIM.Model;
using LocalIM.ViewModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace LocalIM
{
public class MainViewModel : INotifyPropertyChanged
{
public MainViewModel()
{
ContactViewModels = new ObservableCollection<ContactViewModel>();
ContactViewModels.CollectionChanged += Contacts_CollectionChanged;
}
void Contacts_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("StatusText"));
}
public ObservableCollection<ContactViewModel> ContactViewModels { get; private set; }
public string UserName { get; set; }
static object _lockObject = new object();
ContactViewModel FindContactViewModel(string username, string address)
{
return ContactViewModels.FirstOrDefault(x => x.Contact.Username == username && x.Contact.Address == address);
}
public void CheckNewContact(string username, string address)
{
lock (_lockObject)
{
var contact = FindContactViewModel(username, address);
if (contact == null)
{
contact = new ContactViewModel(
new Contact
{
Address = address,
Username = username
})
{
LastAction = DateTime.Now
};
ContactViewModels.Add(contact);
}
else
contact.LastAction = DateTime.Now;
}
}
public void SendMessage(Contact contact, OutgoingMessage msg)
{
}
public string StatusText
{
get
{
return string.Format("{0} Contacts", ContactViewModels.Count);
}
}
public void GotMessage(string username, string address, string message,Guid guid)
{
var contactViewModel = FindContactViewModel(username, address);
if (contactViewModel != null)
{
if (!contactViewModel.Contact.Messages.Any(x => x.Guid == guid))
{
var m = new IncomingMessage
{
Guid = guid,
Text = message,
TimeStamp = DateTime.Now
};
contactViewModel.AddMessage(m);
// todo : send confirm
}
}
}
public void GotMessageConfirm(Guid guid)
{
}
public event PropertyChangedEventHandler PropertyChanged;
}
}