public LoginViewModel(ISettingsService settingsService, IWCFClientService clientService, IDialogService dialogService) { this.settingsService = settingsService; this.clientService = clientService; this.dialogService = dialogService; ClientName = settingsService.Name; ServerIp = settingsService.ServerIp; ServerPort = settingsService.ServerPort; AddValidationRule(() => ClientName, () => { if (string.IsNullOrEmpty(ClientName)) { return("Client name cannot be empty"); } if (ClientName.Length < 3) { return("Client name must be longer than 3 characters"); } if (ClientName.Length > 15) { return("Client name must be no longer than 15 characters"); } return(null); }); AddValidationRule(() => ServerPort, () => { if (string.IsNullOrEmpty(ServerPort)) { return("Server port cannot be empty"); } if (!int.TryParse(ServerPort, out int port)) { return("Server port must be number"); } if (port < 0 || port > 65535) { return("Server port must be greater than 0 and less than 65535"); } return(null); }); var ipPattern = "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"; AddValidationRule(() => ServerIp, () => { if (string.IsNullOrEmpty(ServerIp)) { return("Server ip cannot be empty"); } if (!Regex.IsMatch(ServerIp, ipPattern, RegexOptions.Compiled)) { return("Server address is incorrect"); } return(null); }); Validate(); }
public ClientViewModel(IWCFClientService clientService, ISettingsService settingsService, IDialogService dialogService) { this.clientService = clientService; this.settingsService = settingsService; this.dialogService = dialogService; ServerIp = settingsService.ServerIp; serverPort = settingsService.ServerPort; clientService.Disconnected += (s, e) => { DispatcherHelper.CheckBeginInvokeOnUI(() => { DisconnectClient(); }); }; clientService.MessageReceived += (s, e) => { DispatcherHelper.CheckBeginInvokeOnUI(() => { Messages.Add(new Model.Message(e.Message)); }); }; Messenger.Default.Register <NotificationMessage>(this, (m) => { switch (m.Notification) { case "ClientWindowClosed": DisconnectClient(); break; } }); if (clientService.IsConnected) { Client = clientService.Client; ClientConnected = true; DispatcherHelper.RunAsync(RequestRecentMessages); } else { Client = new Client(settingsService.Name); ConnectClient(); } }