/// <summary> /// Load categories /// </summary> /// <returns>All categories</returns> public ObservableCollection<Category> LoadCategories() { var results = new ObservableCollection<Category>(); using (ItemDataContext context = new ItemDataContext(ConnectionString)) { // For development use only ////context.DeleteDatabase(); if (!context.DatabaseExists()) { // Create database if it doesn't exist context.CreateDatabase(); } if (context.Categories.Count() == 0) { // Initialization XDocument data = XDocument.Load(XMLSource); var dataCategories = from query in data.Descendants("Category") select new Category((string)query.Element("Name")); foreach (var item in dataCategories) { // Add the new category to the context Category cat = new Category(); cat.Name = item.Name; context.Categories.InsertOnSubmit(cat); } // Save changes to the database context.SubmitChanges(); } var contextCategories = from i in context.Categories select i; foreach (Category cat in contextCategories) { results.Add(cat); } } return results; }
/// <summary> /// Load data method. /// </summary> /// <returns>The items</returns> public ObservableCollection<Item> LoadData() { var results = new ObservableCollection<Item>(); using (ItemDataContext context = new ItemDataContext(ConnectionString)) { // For development use only ////context.DeleteDatabase(); // Create database if it doesn't exist if (!context.DatabaseExists()) { context.CreateDatabase(); } // Initialization if (context.Items.Count() == 0 || context.Categories.Count() == 0) { XDocument data = new XDocument(); // For the different languages CultureInfo currentCulture = CultureInfo.CurrentCulture; // XML uri string dataUri = string.Empty; // Language selection switch (currentCulture.TwoLetterISOLanguageName) { // Disable French for the first release ////case "fr": //// dataUri = "Data/dataExample.fr-FR.xml"; //// break; case "en": // Configuration: XML source. dataUri = "Data/dataExample.xml"; break; } // By default it is in English if (string.IsNullOrEmpty(dataUri)) { // Configuration: XML source. dataUri = "Data/dataExample.xml"; } data = XDocument.Load(dataUri); var dataCategories = from query in data.Descendants("Category") select new Category((string)query.Element("Name")); var dataItems = from query in data.Descendants("Item") select new Item((string)query.Element("Name"), (int?)query.Element("CategoryId"), false, Visibility.Visible); foreach (var item in dataCategories) { // Add the new category to the context Category cat = new Category(); cat.Name = item.Name; context.Categories.InsertOnSubmit(cat); } foreach (var item in dataItems) { // Add the new item to the context Item it = new Item(); it.Id = item.Id; it.Name = item.Name; it.CategoryId = item.CategoryId; it.IsSelected = item.IsSelected; context.Items.InsertOnSubmit(it); } // Save changes to the database context.SubmitChanges(); } var contextItems = from i in context.Items select i; foreach (Item it in contextItems) { results.Add(it); } } return results; }
/// <summary> /// Add an item. /// </summary> public void AddItem() { string name = this.Name; Category category = this.SelectedCategory; if (string.IsNullOrWhiteSpace(name)) { this.Name = string.Empty; return; } // Force the default category if (category == null) { category = this.defaultCategory; } // 30 characters maximum if (name.Length > 30 || category == null) { return; } // We keep the selected category if (category != null) { this.defaultCategory = category; } using (ItemDataContext context = new ItemDataContext(ConnectionString)) { Item itemToCreate = new Item(name, category.Id, false, Visibility.Visible); if (context.Items.Where(i => i.Name == itemToCreate.Name && i.CategoryId == itemToCreate.CategoryId) .FirstOrDefault() == null) { context.Items.InsertOnSubmit(itemToCreate); context.SubmitChanges(); } } INavigationService navigationService = this.GetService<INavigationService>(); if (navigationService == null) { return; } Dictionary<string, string> parameters = new Dictionary<string, string>(); parameters.Add("Reload", "true"); navigationService.Navigate("/Pages/MainPage.xaml", parameters); }
/// <summary> /// Save data method /// </summary> public void SaveDataContext() { using (ItemDataContext context = new ItemDataContext(ConnectionString)) { ////foreach (Item it in this.SwimChecklist.Where(i => i.ToRemove == true)) ////{ //// context.Items.DeleteOnSubmit(context.Items.Single(i => i.Id == it.Id)); //// context.SubmitChanges(); ////} ////this.SwimChecklist = new ObservableCollection<Item>(this.swimChecklist //// .Where(i => i.ToRemove == false) //// .OrderBy(i => i.Id) //// .Select(i => new Item(i.Id, i.Name, i.CategoryId, i.IsSelected, i.Visibility))); ////foreach (Item it in this.SwimChecklist.Where(i => i.HasChanged == true && i.ToRemove == false)) ////{ //// Item itemToModify = context.Items.Single(i => i.Id == it.Id); //// itemToModify.IsSelected = it.IsSelected; //// context.SubmitChanges(); //// this.SwimChecklist.Single(s => s.Id == it.Id).HasChanged = false; ////} foreach (Item it in this.SwimChecklist.Where(i => i.HasChanged == true)) { if (it.ToRemove == true) { context.Items.DeleteOnSubmit(context.Items.Single(i => i.Id == it.Id)); } else { Item itemToModify = context.Items.Single(i => i.Id == it.Id); itemToModify.IsSelected = it.IsSelected; } context.SubmitChanges(); } foreach (Item it in this.BikeChecklist.Where(i => i.HasChanged == true)) { if (it.ToRemove == true) { context.Items.DeleteOnSubmit(context.Items.Single(i => i.Id == it.Id)); } else { Item itemToModify = context.Items.Single(i => i.Id == it.Id); itemToModify.IsSelected = it.IsSelected; } context.SubmitChanges(); } foreach (Item it in this.RunChecklist.Where(i => i.HasChanged == true)) { if (it.ToRemove == true) { context.Items.DeleteOnSubmit(context.Items.Single(i => i.Id == it.Id)); } else { Item itemToModify = context.Items.Single(i => i.Id == it.Id); itemToModify.IsSelected = it.IsSelected; } context.SubmitChanges(); } foreach (Item it in this.MiscChecklist.Where(i => i.HasChanged == true)) { if (it.ToRemove == true) { context.Items.DeleteOnSubmit(context.Items.Single(i => i.Id == it.Id)); } else { Item itemToModify = context.Items.Single(i => i.Id == it.Id); itemToModify.IsSelected = it.IsSelected; } context.SubmitChanges(); } } this.IsDataSaved = true; }