public void GotANewFriend(User newFriend) { ListViewItem item = _displayFriends.ToList().Find(x => (x.Content as DisplayUser).Username == newFriend.Username); DisplayUser user = item.Content as DisplayUser; user.UserType = UserType.Friends; user.IsOnline = newFriend.IsOnline; if (user.IsOnline == true) { item.ContentTemplate = (DataTemplate)this.FindResource("OnlineTemplate"); } else { item.ContentTemplate = (DataTemplate)this.FindResource("OfflineTemplate"); } ICollectionView view = CollectionViewSource.GetDefaultView(lvFriends.ItemsSource); view.Refresh(); if (pnReceiveRequest.Visibility == Visibility.Visible || pnSentRequest.Visibility == Visibility.Visible) { CollapseAllPanels(); pnChat.Visibility = Visibility.Visible; pnChat.Visibility = Visibility.Visible; txtblNameCurFriend.Text = newFriend.Name; txtblUsernameCurFriend.Text = newFriend.Username; AvatarHelper.LoadAvatarFromLocal(imgCurFriend, newFriend.Username); lvChatMessages.ItemsSource = _displayMessages[newFriend.Username]; txtblIsWritting.Text = ""; } NotificationHelper.NotifyInfo(newFriend.Name + " is your friend now!"); }
private void TestTemplate() { List <ListViewItem> friends = new List <ListViewItem>(); for (int i = 0; i < 10; i++) { DisplayUser user = new DisplayUser() { AvatarUrl = Environment.CurrentDirectory + @"\Resources\img\avatar-default.png", Name = "Tuan", Username = "******" }; if (i % 2 == 0) { user.IsOnline = true; } else { user.IsOnline = false; } ListViewItem item = new ListViewItem(); item.Content = user; if (user.IsOnline) { item.ContentTemplate = (DataTemplate)this.FindResource("OnlineTemplate"); } else { item.ContentTemplate = (DataTemplate)this.FindResource("OfflineTemplate"); } friends.Add(item); } lvFriends.ItemsSource = friends; List <ListViewItem> messages = new List <ListViewItem>(); for (int i = 0; i < 20; i++) { DisplayMessage message = new DisplayMessage() { AvatarUrl = Environment.CurrentDirectory + @"\Resources\img\avatar-default.png", MessageContent = "Hello" }; ListViewItem item = new ListViewItem(); item.Content = message; if (i % 2 == 0) { item.ContentTemplate = (DataTemplate)this.FindResource("MyMessageTemplate"); } else { item.ContentTemplate = (DataTemplate)this.FindResource("FriendMessageTemplate"); } messages.Add(item); } lvChatMessages.ItemsSource = messages; }
private void FindFriendAvatar_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { string username = e.Result.ToString(); DisplayUser user = _displayFriends.ToList().Find(x => (x.Content as DisplayUser).Username == username).Content as DisplayUser; user.AvatarUrl = null; string avatarPath = AvatarHelper.GetAvatarPath(username); if (avatarPath == null) { avatarPath = AvatarHelper.DefaultAvatarPath; } user.AvatarUrl = avatarPath; }
public bool ReceiveMessage(DataMessage message) { DisplayMessage displayMessage = new DisplayMessage() { AvatarUrl = AvatarHelper.GetAvatarPath(message.Sender.Username), MessageContent = message.Message }; if (displayMessage.AvatarUrl == null) { displayMessage.AvatarUrl = AvatarHelper.DefaultAvatarPath; } ListViewItem item = new ListViewItem { Content = displayMessage, ContentTemplate = (DataTemplate)this.FindResource("FriendMessageTemplate") }; _displayMessages[message.Sender.Username].Add(item); lvChatMessages.ScrollIntoView(item); txtblIsWritting.Text = ""; DisplayUser user = _displayFriends.ToList().Find(x => (x.Content as DisplayUser).Username == message.Sender.Username).Content as DisplayUser; //MessageBox.Show("Here"); if (pnChat.Visibility == Visibility.Visible && txtblUsernameCurFriend.Text == message.Sender.Username) { user.UnreadMessage = null; //MessageBox.Show("null"); } else { if (user.UnreadMessage == null) { user.UnreadMessage = 1; } else { user.UnreadMessage++; } NotificationHelper.NotifyGotAMessage(message.Sender.Name, message.Message); //MessageBox.Show("unread message = " + user.UnreadMessage); } return(true); }
public void SentMakeFriendRequest(User stranger) { DisplayUser displayStranger = new DisplayUser() { AvatarUrl = AvatarHelper.DefaultAvatarPath, IsOnline = stranger.IsOnline, Name = stranger.Name, Username = stranger.Username, UserType = UserType.Pendings }; ListViewItem item = new ListViewItem() { Content = displayStranger, ContentTemplate = (DataTemplate)this.FindResource("OfflineTemplate") }; _displayFriends.Add(item); _friends.Add(stranger); _displayMessages.Add(stranger.Username, new ObservableCollection <ListViewItem>()); InitAFriendAvatar(stranger.Username); }
private void GetMessageWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { List <ZolaService.DataMessage> messages = e.Result as List <ZolaService.DataMessage>; if (messages.Count == 0) { return; } string curUserAvatarUrl = AvatarHelper.GetAvatarPath(_curUser.Username); messages.Sort(new MessageTimeCompare()); for (int i = 0; i < messages.Count; i++) { ZolaService.DataMessage message = messages[i]; string friendAvatarUrl = AvatarHelper.GetAvatarPath(message.Sender.Username); if (friendAvatarUrl == null) { friendAvatarUrl = AvatarHelper.DefaultAvatarPath; } DisplayMessage displayMessage = new DisplayMessage() { AvatarUrl = friendAvatarUrl, MessageContent = message.Message }; ListViewItem item = new ListViewItem(); displayMessage.AvatarUrl = friendAvatarUrl; item.Content = displayMessage; item.ContentTemplate = (DataTemplate)this.FindResource("FriendMessageTemplate"); _displayMessages[message.Sender.Username].Add(item); DisplayUser displayUser = _displayFriends.ToList().Find(x => (x.Content as DisplayUser).Username == message.Sender.Username).Content as DisplayUser; if (displayUser.UnreadMessage == null) { displayUser.UnreadMessage = 1; } else { displayUser.UnreadMessage++; } } }
/// <summary> /// Select cur chat user /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void lvFriends_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { ListViewItem item = (ListViewItem)(sender as ListView).SelectedItem; if (item != null) { DisplayUser user = item.Content as DisplayUser; switch (user.UserType) { case UserType.Friends: if (pnChat.Visibility == Visibility.Collapsed) { CollapseAllPanels(); pnChat.Visibility = Visibility.Visible; } else { if (user.Username == txtblUsernameCurFriend.Text) { break; } } txtblNameCurFriend.Text = user.Name; txtblUsernameCurFriend.Text = user.Username; AvatarHelper.LoadAvatarFromLocal(imgCurFriend, user.Username); lvChatMessages.ItemsSource = _displayMessages[user.Username]; txtblIsWritting.Text = ""; DisplayUser displayUser = _displayFriends.ToList().Find(x => (x.Content as DisplayUser).Username == user.Username).Content as DisplayUser; displayUser.UnreadMessage = null; txtMessageToSend.Text = ""; txtMessageToSend.Focus(); break; case UserType.Pendings: if (pnSentRequest.Visibility == Visibility.Collapsed) { CollapseAllPanels(); pnSentRequest.Visibility = Visibility.Visible; } break; case UserType.Requests: if (pnReceiveRequest.Visibility == Visibility.Collapsed) { CollapseAllPanels(); pnReceiveRequest.Visibility = Visibility.Visible; } AvatarHelper.LoadAvatarFromLocal(imgRequestFriend, user.Username); txtblRequestName.Text = user.Name; txtblRequestUsername.Text = user.Username; bool?gender = _friends.Find(x => x.Username == user.Username).IsMale; if (gender == true) { txtblRequestGender.Text = "Male"; } if (gender == false) { txtblRequestGender.Text = "Female"; } if (gender == null) { txtblRequestGender.Text = "Unknow"; } break; } } }
/// <summary> /// Get Friend list, sent request list, reveiced request list /// </summary> private void InitFriends() { _displayFriends = new ObservableCollection <ListViewItem>(); _friends.Clear(); List <ZolaService.User> tmp; //Get Friend List #region Get List Friend tmp = App.Proxy.GetFriends(_curUser.Username); foreach (ZolaService.User friend in tmp) { DisplayUser user = new DisplayUser() { AvatarUrl = AvatarHelper.DefaultAvatarPath, Name = friend.Name, Username = friend.Username, IsOnline = friend.IsOnline, UserType = UserType.Friends }; ListViewItem item = new ListViewItem(); item.Content = user; if (user.IsOnline) { item.ContentTemplate = (DataTemplate)this.FindResource("OnlineTemplate"); } else { item.ContentTemplate = (DataTemplate)this.FindResource("OfflineTemplate"); } _displayFriends.Add(item); } _friends.AddRange(tmp); #endregion //Get Received Friend request List #region get received friend request list tmp = App.Proxy.GetPendingFriendRequests(_curUser); foreach (ZolaService.User friend in tmp) { DisplayUser user = new DisplayUser() { AvatarUrl = AvatarHelper.DefaultAvatarPath, Name = friend.Name, Username = friend.Username, IsOnline = friend.IsOnline, UserType = UserType.Requests }; ListViewItem item = new ListViewItem(); item.Content = user; if (user.IsOnline) { item.ContentTemplate = (DataTemplate)this.FindResource("OnlineTemplate"); } else { item.ContentTemplate = (DataTemplate)this.FindResource("OfflineTemplate"); } _displayFriends.Add(item); } _friends.AddRange(tmp); #endregion //Get Pending Friend Request #region Get Pending Friend Request tmp = App.Proxy.GetSentFriendRequest(_curUser); foreach (ZolaService.User friend in tmp) { DisplayUser user = new DisplayUser() { AvatarUrl = AvatarHelper.DefaultAvatarPath, Name = friend.Name, Username = friend.Username, IsOnline = friend.IsOnline, UserType = UserType.Pendings }; ListViewItem item = new ListViewItem(); item.Content = user; if (user.IsOnline) { item.ContentTemplate = (DataTemplate)this.FindResource("OnlineTemplate"); } else { item.ContentTemplate = (DataTemplate)this.FindResource("OfflineTemplate"); } _displayFriends.Add(item); } _friends.AddRange(tmp); #endregion lvFriends.ItemsSource = _displayFriends; CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lvFriends.ItemsSource); PropertyGroupDescription groupDescription = new PropertyGroupDescription("Content.UserType"); view.GroupDescriptions.Add(groupDescription); view.Filter = FriendNameFilter; txtFilter.Text = ""; }