-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPicture.cs
130 lines (107 loc) · 4.11 KB
/
Picture.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Popups;
using System.Diagnostics;
namespace PhotoLibraryApp
{
public class Picture
{
// File to store application's data
private const string TEXT_FILE_NAME = "PictureLibrary.txt";
// Global collection of pictures
public static ObservableCollection<Picture> Collection = new ObservableCollection<Picture>();
public static StorageFile mainStorageFile;
// Path of the picture file
public string Path { get; set; }
// BitmapImage to be used as the souce of the image control
public BitmapImage ImageSource { get; set; }
/// <summary>
/// Adds pictures to the library and updates storage file
/// </summary>
/// <param name="picture"></param>
public static async Task AddPictures(IReadOnlyList<StorageFile> storageFiles)
{
foreach (var storageFile in storageFiles)
{
mainStorageFile = storageFile;
if (Collection.Any(p => p.Path == storageFile.Path) == false)
{
// Add picture to the global collection
await AddPictureToCollection(storageFile.Path);
// Save picture file path in storage data file
FileHelper.WriteTextFileAsync(TEXT_FILE_NAME, storageFile.Path);
}
else
{
var dialog = new MessageDialog($"The file '{storageFile.Path}' already exists in the collection.");
await dialog.ShowAsync();
}
}
}
/// <summary>
/// Loads all pictures from the library data file
/// </summary>
/// <returns>A list of pictures</returns>
public async static Task LoadAllPicturesAsync()
{
var content = await FileHelper.ReadTextFileAsync(TEXT_FILE_NAME);
if (!string.IsNullOrWhiteSpace(content))
{
var fileList = content.Split("\r\n", StringSplitOptions.RemoveEmptyEntries);
foreach (var file in fileList)
{
await AddPictureToCollection(file);
}
}
}
private static async Task AddPictureToCollection(string filePath)
{
// Create a bitmap
StorageFile storageFile = null;
try
{
storageFile = await StorageFile.GetFileFromPathAsync(filePath);
}
catch (UnauthorizedAccessException)
{
throw new Exception("Access denied to the folder");
}
BitmapImage bitmapImage = new BitmapImage();
var stream = await storageFile.OpenAsync(Windows.Storage.FileAccessMode.Read);
bitmapImage.SetSource(stream);
// Create Picture object
var pic = new Picture();
pic.Path = storageFile.Path;
pic.ImageSource = bitmapImage;
// Add Picture object to the global observable collection
Collection.Add(pic);
}
//Delete Photos Method:
public static async Task DeletePhotoFromCollection(string photoPath)
{
string currFile = ApplicationData.Current.LocalFolder.Path + "\\" + TEXT_FILE_NAME;
string tempFile = currFile + ".temp";
Debug.WriteLine(currFile);
Debug.WriteLine(tempFile);
using (var sr = new StreamReader(currFile))
using (var sw = new StreamWriter(tempFile))
{
string line;
while ((line = sr.ReadLine()) != null)
{
if (line != photoPath)
sw.WriteLine(line);
}
}
File.Delete(currFile);
File.Move(tempFile, currFile);
}
}
}