/// <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> /// 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> /// Get all the categories /// </summary> public void GetCategories() { Observable.Start(() => this.LoadCategories()) .ObserveOnDispatcher() .Subscribe(list => { foreach (Category cat in list) { this.Categories.Add(cat); // Configuration: default category if (cat.Name == "Swim") { this.defaultCategory = cat; } } this.IsDataLoaded = true; }); }
/// <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; }