-
Notifications
You must be signed in to change notification settings - Fork 451
/
AppImage.cs
119 lines (105 loc) · 3.68 KB
/
AppImage.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
namespace UniversalDemo.ViewModel
{
using System;
using System.IO;
using System.Threading.Tasks;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
/// <summary>
/// The view model for an image in the user's Dropbox.
/// </summary>
public class AppImage : ViewModelBase
{
/// <summary>
/// Initializes a new instance of the <see cref="AppImage"/> class.
/// </summary>
/// <param name="name">The file name of the image.</param>
/// <param name="rev">The revision of the image file.</param>
public AppImage(string name, string rev)
{
this.Name = name;
this.Rev = rev;
}
/// <summary>
/// Gets the file name of this image.
/// </summary>
public string Name { get; private set; }
/// <summary>
/// Gets the current revision of this image.
/// </summary>
public string Rev { get; private set; }
/// <summary>
/// Gets a value indicating whether this image is loading.
/// </summary>
/// <value>
/// <c>true</c> if this instance is loading; otherwise, <c>false</c>.
/// </value>
public bool IsLoading { get; private set; }
/// <summary>
/// Gets this file as an image.
/// </summary>
public ImageSource Image { get; private set; }
/// <summary>
/// Fetches the image from Dropbox if necessary.
/// </summary>
/// <param name="replaceImage">If set to <c>true</c> then replace an existing image.</param>
/// <returns>
/// An asynchronous task.
/// </returns>
public async Task Update(bool replaceImage = false)
{
if (this.IsLoading ||
(!replaceImage && this.Image != null))
{
return;
}
var client = this.App.DropboxClient;
if (client == null)
{
return;
}
this.IsLoading = true;
this.NotifyPropertyChanged("IsLoading");
try
{
using (var download = await client.Files.DownloadAsync("/" + this.Name))
{
var stream = await download.GetContentAsStreamAsync();
// Convert the stream to the memory stream, because a memory stream supports seeking.
var memStream = new MemoryStream();
await stream.CopyToAsync(memStream);
memStream.Position = 0;
var bitmap = new BitmapImage();
await bitmap.SetSourceAsync(memStream.AsRandomAccessStream());
this.Image = bitmap;
this.NotifyPropertyChanged("Image");
if (download.Response.Rev != this.Rev)
{
this.Rev = download.Response.Rev;
this.NotifyPropertyChanged("Rev");
}
}
}
finally
{
this.IsLoading = false;
this.NotifyPropertyChanged("IsLoading");
}
}
/// <summary>
/// Updates the revision of this image. If the revision has changed,
/// then this will cause an image update.
/// </summary>
/// <param name="rev">The rev.</param>
public void UpdateRev(string rev)
{
if (this.Rev == rev)
{
return;
}
this.Rev = rev;
this.NotifyPropertyChanged("Rev");
var task = this.Update(replaceImage: true);
}
}
}