async Task SaveToJson() { try { // read list of photos from JSON file FileStream stream1 = new FileStream(JsonFilePath, FileMode.Open); StreamReader reader = new StreamReader(stream1); string fileContents = reader.ReadToEnd(); reader.Close(); PhotoList = JsonConvert.DeserializeObject <List <PhotoItem> >(fileContents); // query to see if photo object already in file List <PhotoItem> otherObjects = (from photoItemObject in PhotoList where photoItemObject.PhotoFilePath != PhotoItemObject.PhotoFilePath select photoItemObject).ToList <PhotoItem>(); // photo object already exists; update it if (otherObjects.Count != PhotoList.Count) { otherObjects.Add(PhotoItemObject); PhotoList = otherObjects; AllPhotos = PhotoList; } // add new photo item to list else { PhotoList.Add(PhotoItemObject); AllPhotos.Add(PhotoItemObject); } AllPhotos = (from photo in AllPhotos orderby photo.PhotoTime select photo).ToList(); PhotoSource = new ObservableCollection <PhotoItem>(AllPhotos); // write updated photo list to JSON file StreamWriter writer = new StreamWriter(JsonFilePath, false); writer.WriteLine(JsonConvert.SerializeObject(PhotoList)); writer.Close(); // update locations Location currentLocation = await GetCurrentDistance(); for (int i = 0; i < AllPhotos.Count; i++) { AllPhotos[i].DistanceToCurrent = Math.Round(Location.CalculateDistance(currentLocation, AllPhotos[i].PhotoLocation, DistanceUnits.Miles), 4); } } catch (Exception e) { Console.WriteLine(e.Message); } }
private static readonly string[] extensions = { ".jpg", ".png", ".gif", ".bmp" }; // the extensions to be monitored /// <summary> /// Initializes a new instance of the <see cref="Photos"/> class. /// </summary> public Photos() { this.AllPhotos = new List <string>(); this.OutputDirPath = new Config().OutputDirPath; this.ThumbnailsDirPath = Path.Combine(OutputDirPath, "Thumbnails"); string[] allPhotos = Directory.GetFiles(OutputDirPath, "*", SearchOption.AllDirectories); foreach (string photo in allPhotos) { if (!photo.StartsWith(ThumbnailsDirPath) && extensions.Contains(Path.GetExtension(photo))) { AllPhotos.Add(photo); } } }
async Task SortBy(string sortType) { Func <PhotoItem, Object> orderByFunc = null; if (sortType == "Location") { // sort by closest to furthest Location currentLocation = await GetCurrentDistance(); for (int i = 0; i < AllPhotos.Count; i++) { AllPhotos[i].DistanceToCurrent = Math.Round(Location.CalculateDistance(currentLocation, AllPhotos[i].PhotoLocation, DistanceUnits.Miles), 4); } orderByFunc = photoItem => photoItem.DistanceToCurrent; PhotoSource = new ObservableCollection <PhotoItem>(AllPhotos.OrderBy(orderByFunc)); } else if (sortType == "Rating") { // get top 5 rated photos orderByFunc = photoItem => photoItem.Rating; PhotoSource = new ObservableCollection <PhotoItem>(AllPhotos.OrderByDescending(orderByFunc).Take(5)); } else if (sortType == "Time") { // sort by most recent photo orderByFunc = photoItem => photoItem.PhotoTime; PhotoSource = new ObservableCollection <PhotoItem>(AllPhotos.OrderBy(orderByFunc)); } else { // sort by category var categorizedPhotos = from photo in AllPhotos where photo.Categories.IndexOf(sortType) != -1 select photo; PhotoSource = new ObservableCollection <PhotoItem>(categorizedPhotos); } }