private void RefreshMessageState(RefreshMessageStateTemplate refrmsT) { ContactUI contact = null; foreach (ContactUI i in mainPageViewModel.ContactsTab.Contacts) { if (i.ChatId == refrmsT.ChatId) { contact = i; break; } } if (contact != null) { foreach (MessageUI m in contact.ContactMessages) { if (m.MessageId == refrmsT.messageId) { Application.Current.Dispatcher.Invoke(new Action(() => { m.MessageState = refrmsT.MessageState; if (m.MessageState == MessageState.Readed) { if (m.SenderId != mainPageViewModel.MainViewModel.UserInfo.Info.Id) { contact.MessageCounter--; } } })); break; } } } }
void NotifyHandling() { BinaryFormatter formatter = new BinaryFormatter(); using (MemoryStream memoryStream = new MemoryStream()) { while (true) { try { memoryStream.Flush(); memoryStream.Position = 0; do { byte[] buffer = new byte[256]; int bytes; bytes = clientStream.Read(buffer, 0, buffer.Length); memoryStream.Write(buffer, 0, bytes); buffer = new byte[256]; }while (clientStream.DataAvailable); memoryStream.Position = 0; RRTemplate serverResponse = formatter.Deserialize(memoryStream) as RRTemplate; if (serverResponse != null) { switch (serverResponse.RRType) { case RRType.AddContact: AddRemoveContactTemplate aContT = serverResponse.RRObject as AddRemoveContactTemplate; if (aContT != null) { AddContact(aContT); } break; case RRType.RemoveContact: AddRemoveContactTemplate rContT = serverResponse.RRObject as AddRemoveContactTemplate; if (rContT != null) { RemoveContact(rContT); } break; case RRType.RefreshContacts: RefreshContactsTemplate refrC = serverResponse.RRObject as RefreshContactsTemplate; if (refrC != null) { RefreshContacts(refrC); } break; case RRType.SearchOutContacts: SearchOutContactsTemplate searchOutC = serverResponse.RRObject as SearchOutContactsTemplate; if (searchOutC != null) { SearchOutContacts(searchOutC); } break; case RRType.SendMessage: SendMessageTemplate sMessT = serverResponse.RRObject as SendMessageTemplate; if (sMessT != null) { SendMessageReceive(sMessT); } break; case RRType.RefreshContactStatus: RefreshContactStatusTemplate refrContStT = serverResponse.RRObject as RefreshContactStatusTemplate; if (refrContStT != null) { RefreshContactStatus(refrContStT); } break; case RRType.AddContactTarget: AddRemoveContactTargetTemplate aconttT = serverResponse.RRObject as AddRemoveContactTargetTemplate; if (aconttT != null) { AddContactTarget(aconttT); } break; case RRType.RemoveContactTarget: AddRemoveContactTargetTemplate rconttT = serverResponse.RRObject as AddRemoveContactTargetTemplate; if (rconttT != null) { RemoveContactTarget(rconttT); } break; case RRType.RefreshMessageState: RefreshMessageStateTemplate refrmsT = serverResponse.RRObject as RefreshMessageStateTemplate; if (refrmsT != null) { RefreshMessageState(refrmsT); } break; case RRType.CreateConv: CreateConvTemplate crconvT = serverResponse.RRObject as CreateConvTemplate; if (crconvT != null) { AddConv(crconvT); } break; case RRType.RemoveConv: RemoveConvTemplate rconvT = serverResponse.RRObject as RemoveConvTemplate; if (rconvT != null) { RemoveConv(rconvT); } break; case RRType.RemoveConvTarget: RemoveConvTemplate rconvtT = serverResponse.RRObject as RemoveConvTemplate; if (rconvtT != null) { RemoveConvTarget(rconvtT); } break; case RRType.BlockUnblockUserTarget: BlockUnblockUserTemplate bunbUT = serverResponse.RRObject as BlockUnblockUserTemplate; if (bunbUT != null) { ChangeBlockUserStatusTarget(bunbUT); } break; default: if (client.Connected) { mainPageViewModel.MainViewModel.ErrorService(new ErrorReportTemplate(ErrorType.Other, new Exception("Unknown problem"))); } else { return; } break; } } else { if (client.Connected) { mainPageViewModel.MainViewModel.ErrorService(new ErrorReportTemplate(ErrorType.UnCorrectServerResponse, new Exception("Uncorrect server response"))); } else { return; } } } catch (SocketException ex) { if (client.Connected) { mainPageViewModel.MainViewModel.ErrorService(new ErrorReportTemplate(ErrorType.NoConnectionWithServer, ex)); } else { return; } } catch (Exception ex) { if (client.Connected) { mainPageViewModel.MainViewModel.ErrorService(new ErrorReportTemplate(ErrorType.Other, ex)); } else { return; } } } } }