public ReleaseCheckoutProfileManagerViewModel() { AddNew = new DelegateCommand(parameter => { ReleaseCheckoutProfileEditor editor = null; ReleaseCheckoutProfile profile = new ReleaseCheckoutProfile(); while (true) { editor = new ReleaseCheckoutProfileEditor() { DataContext = new ReleaseCheckoutProfileEditViewModel() { Model = profile, CheckoutProfiles = CheckoutProfiles, MaxTasksCount = MaxTasksCount - GetCurentTasksCount() } }; if (editor.ShowDialog() ?? false) { m_model.Add(profile); break; } else { break; } } }); Edit = new DelegateCommand(parameter => { ReleaseCheckoutProfileEditor editor = null; ReleaseCheckoutProfile originalProfile = SelectedProfiles[0].Model; ReleaseCheckoutProfile profile = new ReleaseCheckoutProfile(originalProfile); while (true) { editor = new ReleaseCheckoutProfileEditor() { DataContext = new ReleaseCheckoutProfileEditViewModel() { Model = profile, CheckoutProfiles = CheckoutProfiles, MaxTasksCount = MaxTasksCount - GetCurentTasksCount() + originalProfile.TasksCount } }; if (editor.ShowDialog() ?? false) { m_model[m_model.IndexOf(originalProfile)] = profile; break; } else { break; } } }, parameter => SelectedProfiles.Count == 1); Duplicate = new DelegateCommand(parameter => { ReleaseCheckoutProfileEditor editor = null; ReleaseCheckoutProfile profile = new ReleaseCheckoutProfile(SelectedProfiles[0].Model); while (true) { editor = new ReleaseCheckoutProfileEditor() { DataContext = new ReleaseCheckoutProfileEditViewModel() { Model = profile, CheckoutProfiles = CheckoutProfiles, MaxTasksCount = MaxTasksCount - GetCurentTasksCount() } }; if (editor.ShowDialog() ?? false) { m_model.Add(profile); break; } else { break; } } }, parameter => SelectedProfiles.Count == 1); Delete = new DelegateCommand(parameter => { foreach (ReleaseCheckoutProfileRecordViewModel profile in SelectedProfiles.ToList()) { m_model.Remove(profile.Model); } }, parameter => SelectedProfiles.Count > 0); }
public CheckoutProfileManagerViewModel() { AddNew = new DelegateCommand(parameter => { CheckoutProfileEditor editor = null; CheckoutProfile profile = new CheckoutProfile(); while (true) { editor = new CheckoutProfileEditor() { DataContext = new CheckoutProfileEditViewModel() { Model = profile } }; if (editor.ShowDialog() ?? false) { if (m_model.Where(p => p.Name == profile.Name).ToList().Count == 0) { m_model.Add(profile); break; } MessageBox.Show($"Checkout profile name {profile.Name} is busy. Choose another name, please.", "Duplication", MessageBoxButton.OK, MessageBoxImage.Warning); } else { break; } } }); Edit = new DelegateCommand(parameter => { CheckoutProfile originalProfile = SelectedProfiles[0].Model; CheckoutProfile profile = new CheckoutProfile(originalProfile); while (true) { CheckoutProfileEditor editor = new CheckoutProfileEditor() { DataContext = new CheckoutProfileEditViewModel() { Model = profile } }; if (editor.ShowDialog() ?? false) { List <CheckoutProfile> dupList = m_model.Where(p => p.Name == profile.Name).ToList(); if (dupList.Count == 0 || dupList[0] == originalProfile) { profile.CopyTo(originalProfile); break; } MessageBox.Show( $"Checkout profile name {profile.Name} is busy. Choose another name, please.", "Duplication", MessageBoxButton.OK, MessageBoxImage.Warning); } else { break; } } }, parameter => SelectedProfiles.Count == 1); Duplicate = new DelegateCommand(parameter => { CheckoutProfileEditor editor = null; CheckoutProfile profile = new CheckoutProfile(SelectedProfiles[0].Model); while (true) { editor = new CheckoutProfileEditor() { DataContext = new CheckoutProfileEditViewModel() { Model = profile } }; if (editor.ShowDialog() ?? false) { if (m_model.Where(p => p.Name == profile.Name).ToList().Count == 0) { m_model.Add(profile); break; } MessageBox.Show($"Checkout profile name {profile.Name} is busy. Choose another name, please.", "Duplication", MessageBoxButton.OK, MessageBoxImage.Warning); } else { break; } } }, parameter => SelectedProfiles.Count == 1); Delete = new DelegateCommand(parameter => { foreach (CheckoutProfileRecordViewModel profile in SelectedProfiles.ToList()) { m_model.Remove(profile.Model); } }, parameter => SelectedProfiles.Count > 0); Import = new DelegateCommand(parameter => { OpenFileDialog openFIleDialog = new OpenFileDialog() { DefaultExt = ".csv", Filter = "CSV files (.csv)|*.csv" }; if (openFIleDialog.ShowDialog() ?? false) { using (StreamReader reader = new StreamReader(openFIleDialog.FileName)) { int lineCount = 0; bool isOk = true; while (!reader.EndOfStream) { string[] profileParameters = reader.ReadLine().Split(new char[] { ';' }); CheckoutProfile profile = null; try { profile = new CheckoutProfile() { Name = profileParameters[0], Email = profileParameters[1], BillingAddress = new Address() { FirstName = profileParameters[2], SecondName = profileParameters[3], StreetAddress1 = profileParameters[4], StreetAddressLine2 = profileParameters[5], PostalCode = profileParameters[6], City = profileParameters[7], State = CountriesCollection.Countries[(CountryCode)int.Parse(profileParameters[9])].States.ElementAtOrDefault(int.Parse(profileParameters[8])), Country = CountriesCollection.Countries[(CountryCode)int.Parse(profileParameters[9])], PhoneNumber = profileParameters[25] }, ShippingAddress = new Address() { FirstName = profileParameters[10], SecondName = profileParameters[11], StreetAddress1 = profileParameters[12], StreetAddressLine2 = profileParameters[13], PostalCode = profileParameters[14], City = profileParameters[15], State = CountriesCollection.Countries[(CountryCode)int.Parse(profileParameters[17])].States.ElementAtOrDefault(int.Parse(profileParameters[16])), Country = CountriesCollection.Countries[(CountryCode)int.Parse(profileParameters[17])], PhoneNumber = profileParameters[26] }, IsShippingAsBilling = bool.Parse(profileParameters[18]), PayCard = new PayCard() { Number = profileParameters[19], Holder = profileParameters[20], ExpirationDate = DateTime.ParseExact(profileParameters[21], "O", CultureInfo.InvariantCulture), CVS = profileParameters[22], Type = PayCardTypeCollection.TypesDictionary[(PayCardTypeCode)int.Parse(profileParameters[23])] }, BuyLimit = int.Parse(profileParameters[24]) }; } catch (Exception e) { isOk = false; break; } if (m_model.Where(p => p.Name == profile.Name).ToList().Count == 0) { m_model.Add(profile); } lineCount++; } if (!isOk) { MessageBox.Show($"Wrong format at line {lineCount + 1}"); } reader.Close(); } } }); Export = new DelegateCommand(parameter => { SaveFileDialog saveFileDialog = new SaveFileDialog { DefaultExt = ".csv", Filter = "CSV files (.csv)|*.csv" }; if (saveFileDialog.ShowDialog() ?? false) { using (StreamWriter writer = new StreamWriter(saveFileDialog.FileName)) { foreach (CheckoutProfileRecordViewModel profileVM in Profiles) { CheckoutProfile profile = profileVM.Model; writer.WriteLine($"{profile.Name};{profile.Email};{profile.BillingAddress.FirstName};" + $"{profile.BillingAddress.SecondName};{profile.BillingAddress.StreetAddress1};" + $"{profile.BillingAddress.StreetAddressLine2};{profile.BillingAddress.PostalCode};" + $"{profile.BillingAddress.City};{profile.BillingAddress.Country.States.IndexOf(profile.BillingAddress.State)};" + $"{(int)profile.BillingAddress.Country.Code};" + $"{profile.ShippingAddress.FirstName};" + $"{profile.ShippingAddress.SecondName};{profile.ShippingAddress.StreetAddress1};" + $"{profile.ShippingAddress.StreetAddressLine2};{profile.ShippingAddress.PostalCode};" + $"{profile.ShippingAddress.City};{profile.ShippingAddress.Country.States.IndexOf(profile.ShippingAddress.State)};" + $"{(int)profile.ShippingAddress.Country.Code};" + $"{profile.IsShippingAsBilling};{profile.PayCard.Number};{profile.PayCard.Holder};" + $"{profile.PayCard.ExpirationDate.Value.ToString("O")};{profile.PayCard.CVS};{(int)PayCardTypeCollection.TypesDictionary.First(p => p.Value == profile.PayCard.Type).Key};" + $"{profile.BuyLimit};{profile.BillingAddress.PhoneNumber};{profile.ShippingAddress.PhoneNumber};"); } writer.Close(); } } }, parameter => Profiles.Count > 0); }