public static Guide GetCompleteGuide(string guideID, iFixitDataContext dc) { Guide g = null; //get the node g = dc.GuidesTable.FirstOrDefault(c => c.GuideID == guideID); if (g == null) return null; //get its child steps g.Steps = dc.StepsTable.Where(s => s.parentName == g.GuideID).Distinct().ToList(); List<Step> completeSteps = new List<Step>(); for (int i = 0; i < g.Steps.Count; ++i) { string parentGuideId = g.Steps.ElementAt(i).parentName; string stepIndex = g.Steps.ElementAt(i).StepIndex; completeSteps.Add(GetCompleteStep(parentGuideId, stepIndex, dc)); } g.Steps = completeSteps.OrderBy(s => int.Parse(s.StepIndex)).ToList(); return g; }
private void DeleteCached_Tap(object sender, EventArgs e) { //go through all guides/steps/lines and topics and dump them using (iFixitDataContext db = new iFixitDataContext(App.DBConnectionString)) { var cachedGuides = from g in db.GuidesTable where g.Populated == true select g; foreach (Guide g in cachedGuides) { var cachedSteps = from s in db.StepsTable where s.parentName == g.GuideID select s; foreach (Step s in cachedSteps) { var cachedLines = from l in db.LinesTable where l.parentName == g.GuideID select l; //Debug.WriteLine("got " + cachedLines.Count() + " lines"); db.LinesTable.DeleteAllOnSubmit(cachedLines); } //Debug.WriteLine("got " + cachedSteps.Count() + " steps"); db.StepsTable.DeleteAllOnSubmit(cachedSteps); } //Debug.WriteLine("got " + cachedGuides.Count() + " guides"); db.GuidesTable.DeleteAllOnSubmit(cachedGuides); var cachedTopics = from top in db.TopicsTable where top.Populated == true select top; db.TopicsTable.DeleteAllOnSubmit(cachedTopics); db.SubmitChanges(); } //nuke all cached images using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication()) { //iterate through all files if (isoStore.DirectoryExists(ImgCache.BASE_PATH)) { foreach (string file in isoStore.GetFileNames(ImgCache.BASE_PATH + "\\*")) { //Debug.WriteLine(file); isoStore.DeleteFile(file); } } } UpdateBinding(); }
/* * FIXME add methods simillar to above for Topic, Guide, Step! */ public static Topic GetCompleteTopic(string name, iFixitDataContext dc) { Topic t = null; //get the node t = dc.TopicsTable.FirstOrDefault(c => c.Name == name); if (t == null) return null; //get its child guides t.Guides = dc.GuidesTable.Where(g => g.parentName == name).Distinct().OrderBy(g => g.Title).ToList(); return t; }
public static Step GetCompleteStep(string parentGuideId, string stepNumber, iFixitDataContext dc) { Step s = null; //get the node s = dc.StepsTable.FirstOrDefault(c => c.parentName == parentGuideId && c.StepIndex == stepNumber); if (s == null) return null; //get its child lines s.Lines = dc.LinesTable.Where(l => l.parentName == s.parentName + stepNumber).Distinct().ToList(); return s; }
// Code to execute when the application is launching (eg, from Start) // This code will not execute when the application is reactivated private void Application_Launching(object sender, LaunchingEventArgs e) { Debug.WriteLine("LAUNCHING"); using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) { if (myIsolatedStorage.FileExists(SerialStore)) { myIsolatedStorage.DeleteFile(SerialStore); } } //this has asynchroneous components that will run while the UI loads and whatnot NetworkInterface.GetIsNetworkAvailable() if (DeviceNetworkInformation.IsNetworkAvailable && !rootHasBeenSavedInISO) { getAreas(); //in the future //JSONInterface2.populateCollectionView(fillCollection); } else { // clear loading screen PhoneApplicationService.Current.State[App.InitializeWithLoadingScreen] = false; } // Create the database if it does not exist. //the using statement guarntees that setup and teardown will always happen, and properly using (iFixitDataContext tDB = new iFixitDataContext(DBConnectionString)) { // Create the local database. if (!tDB.DatabaseExists()) { tDB.CreateDatabase(); // Save categories to the database. tDB.SubmitChanges(); } } }
private void UpdateBinding() { using (iFixitDataContext db = new iFixitDataContext(App.DBConnectionString)) { // this.CachedList.ItemsSource = DBHelpers.GetCompleteCategory("root", db).Categories; IQueryable<Topic> queryCached = from top in db.TopicsTable where top.Populated == true select top; this.CachedList.ItemsSource = queryCached; } }
/* * Iterate through all steps and their lines, inserting them into the DB */ private void InsertStepsAndLines(Guide parentGuide, GHStep[] steps, iFixitDataContext db) { //loop through all steps and attempt to insert them foreach (GHStep newStep in steps) { //see if it exists //Step sOld = db.StepsTable.FirstOrDefault(s => s.Title == newStep.title); Step sOld = DBHelpers.GetCompleteStep(parentGuide.GuideID, newStep.number, db); if (sOld == null) { //insert it sOld = new Step(newStep); sOld.parentName = parentGuide.GuideID; db.StepsTable.InsertOnSubmit(sOld); } else { //update it sOld.FillFields(newStep); } //go through all lines foreach (GHStepLines newLine in newStep.lines) { Lines oldLine = db.LinesTable.FirstOrDefault(l => l.parentName == sOld.parentName + sOld.StepIndex); if (oldLine == null) { //insert it Lines l = new Lines(newLine); l.parentName = sOld.parentName + sOld.StepIndex; db.LinesTable.InsertOnSubmit(l); } else { //add it oldLine.FillFields(newLine); oldLine.parentName = sOld.parentName + sOld.StepIndex; } } } }
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { Debug.WriteLine("A Guide has been navigated to. We are going to make API calls and do SCIENCE"); this.guideID = this.NavigationContext.QueryString["GuideID"]; Debug.WriteLine("\tgot guide id = " + guideID); if (tapped != null) tapped.Foreground = App.Current.Resources["PhoneForegroundBrush"] as Brush; //if the VM is already populated, don't reload it if (vm != null) return; int numCols = -1; string key = guideID; //if online, do an api call. The callback will be fired to populate the view if (DeviceNetworkInformation.IsNetworkAvailable && !this.State.ContainsKey(key)) { Debug.WriteLine("online. Querying for new guide content"); vm = new GuideViewModel(SourceGuide); this.DataContext = vm; new JSONInterface2().populateGuideView(this.guideID, insertGuideIntoDB); } //if not online, populate view from DB else { Debug.WriteLine("offline. Using DB for guide content"); using (iFixitDataContext db = new iFixitDataContext(App.DBConnectionString)) { //get the guide, if it already exists //SourceGuide = db.GuidesTable.SingleOrDefault(g => g.GuideID == this.guideID); SourceGuide = DBHelpers.GetCompleteGuide(this.guideID, db); vm = new GuideViewModel(SourceGuide); //force view model to update //vm.UpdateContentFromGuide(SourceGuide); this.DataContext = vm; //hide the loading bar LoadingBarInfo.Visibility = System.Windows.Visibility.Collapsed; } } if (SourceGuide != null) { numCols = SourceGuide.Steps.Count + 1; } // if there is a saved index, re-navigate to it if (key != null && this.State.ContainsKey(key)) { int selectedTabIndex = (int)this.State[key]; if (0 <= selectedTabIndex && selectedTabIndex < numCols) { GuidePivot.SelectedIndex = selectedTabIndex; } } }
public bool insertGuideIntoDB(GuideHolder guide) { //convert the GuideHolder we got into a DB object Debug.WriteLine("inserting guide " + guide.guide.title + " into db from net"); using (iFixitDataContext db = new iFixitDataContext(App.DBConnectionString)) { //SourceGuide = db.GuidesTable.SingleOrDefault(g => g.GuideID == this.guideID); SourceGuide = DBHelpers.GetCompleteGuide(this.guideID, db); if (SourceGuide == null) { Debug.WriteLine("\tdid not find old"); SourceGuide = new Guide(guide); db.GuidesTable.InsertOnSubmit(SourceGuide); } else { Debug.WriteLine("\tfound old"); SourceGuide.FillFields(guide); } InsertStepsAndLines(SourceGuide, guide.guide.steps, db); //insert it into the DB SourceGuide.Populated = true; db.SubmitChanges(); } //force view model to update vm.UpdateContentFromGuide(SourceGuide); this.gTitle.Text = vm.GuideTitle; this.gTopic.Text = vm.GuideTopic; //PivotItem infoPivot = buildInfoPivotItem(); //GuidePivot.Items.Add(infoPivot); this.DataContext = vm; //GuidePivot.ItemsSource = temp; //hide the loading bar LoadingBarInfo.Visibility = System.Windows.Visibility.Collapsed; return true; }
/* * force this view model to update all its internal data */ public void UpdateData() { Debug.WriteLine("Updating view model..."); //run queries Topic top = null; using (iFixitDataContext db = new iFixitDataContext(App.DBConnectionString)) { top = DBHelpers.GetCompleteTopic(Name, db); } if (top == null) { Debug.WriteLine(">>couldnt find DeviceInfo topic in DB to display it"); return; } //fill in internal collections this.Name = top.Name; this.Description = top.Description; this.Contents = top.Contents; this.ImageURL = top.ImageURL; this.BaseURL = "http://www.ifixit.com/Device/" + this.Name; //fill in guides GuideList.Clear(); foreach (Guide g in top.Guides) { Debug.WriteLine("\tinside updating view model. Found guide: " + g.Title); GuideList.Add(g); } }
/* * Insert the data from the JSON parser into the database */ private bool insertDevInfoIntoDB(DeviceInfoHolder devInfo) { //something is wrong and the device was not found. Bail if (devInfo == null) { Debug.WriteLine("something went terribly wrong with a DeviceInfo. Bailing"); NavigationService.GoBack(); return false; } Topic top = null; Debug.WriteLine("putting device info into DB..."); //try to get a topic of this name from the DB //if it fails, make a new one. if it works, update the old using (iFixitDataContext db = new iFixitDataContext(App.DBConnectionString)) { bool gotTopicFromDB = true; top = DBHelpers.GetCompleteTopic(devInfo.topic_info.name, db); if (top == null) { top = new Topic(); gotTopicFromDB = false; } //translate devInfo in a Topic() //name is already the same top.Name = devInfo.topic_info.name; top.parentName = devInfo.title; top.Contents = devInfo.description; top.ImageURL = devInfo.image.text + ".medium"; top.Populated = true; //TODO inject metatdata here like # answers top.Description = ""; top.Description += prepHTML(devInfo.contents, devInfo); //now do the same for all attached guides foreach (DIGuides g in devInfo.guides) { Debug.WriteLine("\tguide " + g.title); //search if the guide already exists, and get or update it Guide gOld = new Guide(); gOld.FillFieldsFromDeviceInfo(navTopicName, g); gOld = db.GuidesTable.FirstOrDefault(other => other.GuideID == gOld.GuideID); if (gOld == null) { gOld = new Guide(); db.GuidesTable.InsertOnSubmit(gOld); } gOld.FillFieldsFromDeviceInfo(navTopicName, g); // hang it below the topic, it its collection of guides top.AddGuide(gOld); //FIXME do we need to specifically add this to the guide table? is that magic? db.SubmitChanges(); } if (!gotTopicFromDB) { db.TopicsTable.InsertOnSubmit(top); } //update the Topic() into the database db.SubmitChanges(); //force the view model to update infoVM.UpdateData(); updateInfoBrowser(); //force the views to update this.InfoStack.DataContext = infoVM; this.GuidesStack.DataContext = infoVM; //disable the loading bars this.LoadingBarInfo.Visibility = System.Windows.Visibility.Collapsed; this.LoadingBarGuides.Visibility = System.Windows.Visibility.Collapsed; } return true; }