/* * adds needed stuff to the HTML to be displayed in the browser */ private string prepHTML(string baseHTML, DeviceInfoHolder devInfo) { string o = ""; o += "<html><head>"; //prevent zooming o += "<meta name='viewport' content='width=320,user-scalable=no'/>"; //inject the theme o += "<style type='text/css'>" + "body {{font-size:1.0em;background-color:" + FetchBackgroundColor() + ";" + "color:" + FetchFontColor() + ";}} " + "</style>"; //inject the script to pass link taps out of the browser o += "<script type='text/javascript'>"; o += @"function getLinks(){ a = document.getElementsByTagName('a'); for(var i=0; i < a.length; i++){ var msg = a[i].href; a[i].onclick = function() {notify(msg); }; } } function notify(msg) { window.external.Notify(msg); event.returnValue=false; return false; }"; //inject the script to find height o += @"function Scroll() { var elem = document.getElementById('content'); window.external.Notify(elem.scrollHeight + ''); } "; //remove all anchors while (baseHTML.Contains("<a class=\"anchor\"")) { int start = baseHTML.IndexOf("<a class=\"anchor\""); int end = baseHTML.IndexOf("</h2>", start); baseHTML = baseHTML.Remove(start, end - start); } //FIXME remove this when we fix the webbrowser //remove all links while (baseHTML.Contains("<a href")) { //remove most of the link int start = baseHTML.IndexOf("<a href"); int end = baseHTML.IndexOf(">", start); baseHTML = baseHTML.Remove(start, end + 1 - start); //remove end tag start = baseHTML.IndexOf("</a>", start); baseHTML = baseHTML.Remove(start, "</a>".Length); } o += @"window.onload = function() { Scroll(); getLinks(); }"; o += "</script>"; o += "</head>"; o += "<body><div id='content'>"; //o += "<img src='" + infoVM.ImageURL + "'>"; o += "<h2>" + devInfo.guides.Length + " Guides</h2>"; o += "<h2>" + devInfo.solutions.count + " Solutions</h2>"; o += baseHTML.Trim(); o += "</div></body>"; o += "</html>"; return o; }
/* * Take in the results from the API query to get additional info about categories (IE thumb) and * add it to the DB entries */ bool storeJSONCategoryInDB(DeviceInfoHolder di) { Category c = root.Categories.Single(cat => cat.Name == di.title); Debug.WriteLine("Category: " + c.Name + "| " + c.parentName + "| "); Deployment.Current.Dispatcher.BeginInvoke(() => { c.Thumbnail = di.image.text + ".standard"; --thumbCount; //manually force the cache to save the image //ImgCache.RetrieveAndCacheByURL(di.image.text); if (thumbCount == 0) { MainPage temp = RootFrame.Content as MainPage; if (temp != null) { temp.initDataBinding(); temp.StopLoadingIndication(); PhoneApplicationService.Current.State[LoadingScreenShown] = true; } } }); return true; }
/* * 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; }