/// <summary> /// Save this route as the last route. /// </summary> /// <param name="route">The route to save</param> /// <returns>Returns a async task</returns> public async Task saveRoute(Route route) { if (!isInitialized) { await initDataConnector(); } using (Stream saveStream = await dataPath.OpenStreamForWriteAsync(lastRouteFileName, CreationCollisionOption.ReplaceExisting)) { XDocument data = new XDocument(); string rootPath = Path.Combine(routePath.Path, route.Name); List<XElement> waypoints = new List<XElement>(); foreach (Waypoint waypoint in route.Waypoints) { if (waypoint is PointOfInterest) { PointOfInterest pointOfIntrest = (PointOfInterest)waypoint; XElement informationElement = new XElement("information", pointOfIntrest.Information); informationElement.SetAttributeValue("lang", languageManager.Language); XElement soundElement = new XElement("sound", pointOfIntrest.Sound.Substring(rootPath.Length + ((rootPath.Length == pointOfIntrest.Sound.Length) ? 0 : 1))); soundElement.SetAttributeValue("lang", languageManager.Language); waypoints.Add(new XElement("waypoint", new object[] { new XElement("latitude", waypoint.Latitude), new XElement("longitude", waypoint.Longitude), new XElement("visited", waypoint.IsVisited), new XElement("name", pointOfIntrest.Name), new XElement("informations", new object[] { informationElement }), new XElement("image", pointOfIntrest.Image.Substring(rootPath.Length + ((rootPath.Length == pointOfIntrest.Image.Length) ? 0 : 1))), new XElement("sounds", new object[] { soundElement }) } )); } else { waypoints.Add(new XElement("waypoint", new object[] { new XElement("latitude", waypoint.Latitude), new XElement("longitude", waypoint.Longitude), new XElement("visited", waypoint.IsVisited) } )); } } XElement routeElement = new XElement("route", new object[] { new XElement("name", route.Name), new XElement("systemSound", route.SystemSound), new XElement("routeIcon", route.ImagePad.Substring(rootPath.Length + ((rootPath.Length == route.ImagePad.Length) ? 0 : 1))), new XElement("waypoints", waypoints.ToArray()), new XElement("routeIndex", route.RouteIndex), }); data.Add(routeElement); //data.create data.Save(saveStream); } }
/// <summary> /// Change the information data to the current language /// </summary> /// <param name="route">The route to change</param> /// <returns>The same route with the new infomation data</returns> public async Task<Route> ChangeRouteLanguage(Route route) { //Get the new route with the new language. Route newRoute = await getRoute(route.Name); //basic checks if everything is ok if (route.Waypoints.Count != newRoute.Waypoints.Count) { //Nog the same waypoint count. I can not change the language return route; } for (int iIndex = 0; iIndex < route.Waypoints.Count; iIndex++) { if (route.Waypoints[iIndex].GetType() != newRoute.Waypoints[iIndex].GetType()) { return route; } } for (int iIndex = 0; iIndex < route.Waypoints.Count; iIndex++) { if (route.Waypoints[iIndex] is PointOfInterest) { ((PointOfInterest)route.Waypoints[iIndex]).Information = ((PointOfInterest)newRoute.Waypoints[iIndex]).Information; ((PointOfInterest)route.Waypoints[iIndex]).Sound = ((PointOfInterest)newRoute.Waypoints[iIndex]).Sound; } } return route; }
/// <summary> /// Get the last used route /// </summary> /// <returns>The last route or null if no route is found</returns> public async Task<Route> getlastRoute() { if (!isInitialized) await initDataConnector(); try { StorageFile routeInfoFile = await dataPath.GetFileAsync(lastRouteFileName); XDocument data = XDocument.Load(routeInfoFile.Path); XElement routeElm = data.Element("route"); Route route = new Route { Name = (string)routeElm.Element("name"), SystemSound = (string)routeElm.Element("systemSound"), RouteIndex = (int)routeElm.Element("routeIndex"), }; route.ImagePad = Path.Combine(routePath.Path, route.Name, (string)routeElm.Element("routeIcon")); route.Waypoints = getWaypoints(route.Name, routeElm); return route; } catch(FileNotFoundException) { //No route found. Return null route return null; } }
/// <summary> /// Get the route /// </summary> /// <param name="routename">The name of the route to get</param> /// <returns>The requested route</returns> public async Task<Route> getRoute(string routename) { try { XElement routeElm = await getRouteElement(routename); Route route = new Route { Name = (string)routeElm.Element("name"), SystemSound = (string)routeElm.Element("systemSound"), ImagePad = Path.Combine(routePath.Path, routename, (string) routeElm.Element("routeIcon")) }; route.Waypoints = getWaypoints(routename, routeElm); return route; } catch(FileNotFoundException) { throw new ArgumentException("Route doesn't exists"); } }
/// <summary> /// Get all the waypoints by a route /// </summary> /// <param name="route">Route to get the waypoints</param> /// <returns>A list of waypoints</returns> public async Task<List<Waypoint>> getWaypoints(Route route) { try { XElement routeElement = await getRouteElement(route); return getWaypoints(route.Name, routeElement); } catch(ArgumentException ex) { throw ex; } }
/// <summary> /// Get the route element in the xmlFile /// </summary> /// <param name="route">Route to get the route element</param> /// <returns>The route element</returns> private async Task<XElement> getRouteElement(Route route) { try { return await getRouteElement(route.Name); } catch(ArgumentException ex) { throw ex; } }
/// <summary> /// Change the current route /// </summary> /// <param name="selectedRoute">Change the route to this one</param> public void setRoute(Route selectedRoute) { currentRoute = selectedRoute; }