-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Share.shared.cs
394 lines (343 loc) · 15.3 KB
/
Share.shared.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Storage;
namespace Microsoft.Maui.ApplicationModel.DataTransfer
{
/// <summary>
/// The Share API enables an application to share data such as text and web links to other applications on the device.
/// </summary>
public interface IShare
{
/// <summary>
/// Show the operating systems user interface to share text.
/// </summary>
/// <param name="request">A <see cref="ShareTextRequest"/> object containing the details of the data to share.</param>
/// <returns>A <see cref="Task"/> object with the current status of the asynchronous operation.</returns>
Task RequestAsync(ShareTextRequest request);
/// <summary>
/// Show the operating system's user interface to share a single file.
/// </summary>
/// <param name="request">A <see cref="ShareFileRequest"/> object containing the details of the data to share.</param>
/// <returns>A <see cref="Task"/> object with the current status of the asynchronous operation.</returns>
Task RequestAsync(ShareFileRequest request);
/// <summary>
/// Show the operating system's user interface to share multiple files.
/// </summary>
/// <param name="request">A <see cref="ShareMultipleFilesRequest"/> object containing the details of the data to share.</param>
/// <returns>A <see cref="Task"/> object with the current status of the asynchronous operation.</returns>
Task RequestAsync(ShareMultipleFilesRequest request);
}
/// <summary>
/// The Share API enables an application to share data such as text and web links to other applications on the device.
/// </summary>
public static partial class Share
{
/// <summary>
/// Show the operating system's user interface to share text.
/// </summary>
/// <param name="text">The text to share.</param>
/// <returns>A <see cref="Task"/> object with the current status of the asynchronous operation.</returns>
public static Task RequestAsync(string text) =>
Current.RequestAsync(text);
/// <summary>
/// Show the operating system's user interface to share text.
/// </summary>
/// <param name="text">The text to share.</param>
/// <param name="title">The title to display on the operating system share dialog.</param>
/// <returns>A <see cref="Task"/> object with the current status of the asynchronous operation.</returns>
public static Task RequestAsync(string text, string title) =>
Current.RequestAsync(text, title);
/// <summary>
/// Show the operating system's user interface to share text.
/// </summary>
/// <param name="request">A <see cref="ShareTextRequest"/> object containing the details of the data to share.</param>
/// <returns>A <see cref="Task"/> object with the current status of the asynchronous operation.</returns>
public static Task RequestAsync(ShareTextRequest request) =>
Current.RequestAsync(request);
/// <summary>
/// Show the operating system's user interface to share a single file.
/// </summary>
/// <param name="request">A <see cref="ShareFileRequest"/> object containing the details of the data to share.</param>
/// <returns>A <see cref="Task"/> object with the current status of the asynchronous operation.</returns>
public static Task RequestAsync(ShareFileRequest request) =>
Current.RequestAsync(request);
/// <summary>
/// Show the operating system's user interface to share multiple files.
/// </summary>
/// <param name="request">A <see cref="ShareMultipleFilesRequest"/> object containing the details of the data to share.</param>
/// <returns>A <see cref="Task"/> object with the current status of the asynchronous operation.</returns>
public static Task RequestAsync(ShareMultipleFilesRequest request) =>
Current.RequestAsync(request);
static IShare Current => ApplicationModel.DataTransfer.Share.Default;
static IShare? defaultImplementation;
/// <summary>
/// Provides the default implementation for static usage of this API.
/// </summary>
public static IShare Default =>
defaultImplementation ??= new ShareImplementation();
internal static void SetDefault(IShare? implementation) =>
defaultImplementation = implementation;
}
/// <summary>
/// Concrete implementation of the <see cref="IShare"/> APIs.
/// </summary>
partial class ShareImplementation : IShare
{
/// <inheritdoc cref="IShare.RequestAsync(ShareTextRequest)"/>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="request"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">Thrown when <see cref="ShareTextRequest.Text"/> and <see cref="ShareTextRequest.Uri"/> are both <see langword="null"/> or empty.</exception>
public Task RequestAsync(ShareTextRequest request)
{
if (request == null)
throw new ArgumentNullException(nameof(request));
if (string.IsNullOrEmpty(request.Text) && string.IsNullOrEmpty(request.Uri))
throw new ArgumentException($"Both the {nameof(request.Text)} and {nameof(request.Uri)} are invalid. Make sure to include at least one of them in the request.");
return PlatformRequestAsync(request);
}
/// <inheritdoc cref="IShare.RequestAsync(ShareFileRequest)"/>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="request"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">Thrown when <see cref="ShareFileRequest.File"/> is <see langword="null"/>.</exception>
public Task RequestAsync(ShareFileRequest request)
{
if (request == null)
throw new ArgumentNullException(nameof(request));
if (request.File == null)
throw new ArgumentException(FileNullException(nameof(request.File)));
return PlatformRequestAsync(request);
}
/// <inheritdoc cref="IShare.RequestAsync(ShareMultipleFilesRequest)"/>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="request"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">Thrown when <see cref="ShareMultipleFilesRequest.Files"/> is <see langword="null"/> or empty or one of the entries in <see cref="ShareMultipleFilesRequest.Files"/> is <see langword="null"/>.</exception>
public Task RequestAsync(ShareMultipleFilesRequest request)
{
if (request == null)
throw new ArgumentNullException(nameof(request));
if (!(request.Files?.Count > 0))
throw new ArgumentException(FileNullException(nameof(request.Files)));
if (request.Files.Any(file => file == null))
throw new ArgumentException(FileNullException(nameof(request.Files)));
return PlatformRequestAsync(request);
}
static string FileNullException(string file)
=> $"The {file} parameter in the request files is invalid";
}
/// <summary>
/// Static class with extension methods for the <see cref="IShare"/> APIs.
/// </summary>
public static class ShareExtensions
{
/// <summary>
/// Show the operating system's user interface to share text.
/// </summary>
/// <param name="share">The object this method is invoked on.</param>
/// <param name="text">The text to share.</param>
/// <returns>A <see cref="Task"/> object with the current status of the asynchronous operation.</returns>
public static Task RequestAsync(this IShare share, string text) =>
share.RequestAsync(new ShareTextRequest(text));
/// <summary>
/// Show the operating systems user interface to share text.
/// </summary>
/// <param name="share">The object this method is invoked on.</param>
/// <param name="text">The text to share.</param>
/// <param name="title">The title to display on the operating system share dialog.</param>
/// <returns>A <see cref="Task"/> object with the current status of the asynchronous operation.</returns>
public static Task RequestAsync(this IShare share, string text, string title) =>
share.RequestAsync(new ShareTextRequest(text, title));
}
/// <summary>
/// Base class for the different share requests that can be used with the <see cref="IShare"/> API.
/// </summary>
public abstract class ShareRequestBase
{
/// <summary>
/// Gets or sets the title to use on the operating system's share user interface.
/// </summary>
public string? Title { get; set; }
/// <summary>
/// Gets or sets the source rectangle to display the iOS share user interface from.
/// </summary>
/// <remarks>This functionality is only available on iOS on an iPad.</remarks>
public Rect PresentationSourceBounds { get; set; } = Rect.Zero;
}
/// <summary>
/// Represents a request for sharing text with other apps on the device.
/// </summary>
public class ShareTextRequest : ShareRequestBase
{
/// <summary>
/// Initializes a new instance of the <see cref="ShareTextRequest"/> class.
/// </summary>
public ShareTextRequest()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ShareTextRequest"/> class with the given text.
/// </summary>
/// <param name="text">The text to share.</param>
public ShareTextRequest(string text) =>
Text = text;
/// <summary>
/// Initializes a new instance of the <see cref="ShareTextRequest"/> class with the given text and title.
/// </summary>
/// <param name="text">The text to share.</param>
/// <param name="title">The title to display on the operating systems share dialog.</param>
public ShareTextRequest(string text, string title)
: this(text) => Title = title;
/// <summary>
/// Gets or sets the main text or message to share.
/// </summary>
public string? Text { get; set; }
/// <summary>
/// Gets or sets the subject that is sometimes used for applications such as mail clients.
/// </summary>
/// <remarks>The subject is only applicable on Android and for certain apps.</remarks>
public string? Subject { get; set; }
/// <summary>
/// Gets or sets a valid URI to share.
/// </summary>
/// <remarks>Needs to be a valid URI, else an exception may be thrown when sharing.</remarks>
public string? Uri { get; set; }
}
/// <summary>
/// Represents a request for sharing a single file with other apps on the device.
/// </summary>
public class ShareFileRequest : ShareRequestBase
{
/// <summary>
/// Initializes a new instance of the <see cref="ShareFileRequest"/> class.
/// </summary>
public ShareFileRequest()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ShareFileRequest"/> class with the given title and file.
/// </summary>
/// <param name="title">The title to display on the operating systems share dialog.</param>
/// <param name="file">The file to share.</param>
public ShareFileRequest(string title, ShareFile file)
{
Title = title;
File = file;
}
/// <summary>
/// Initializes a new instance of the <see cref="ShareFileRequest"/> class with the given title and file.
/// </summary>
/// <param name="title">The title to display on the operating systems share dialog.</param>
/// <param name="file">The file to share.</param>
public ShareFileRequest(string title, FileBase file)
{
Title = title;
File = new ShareFile(file);
}
/// <summary>
/// Initializes a new instance of the <see cref="ShareFileRequest"/> class with the given file.
/// </summary>
/// <param name="file">The file to share.</param>
public ShareFileRequest(ShareFile file)
=> File = file;
/// <summary>
/// Initializes a new instance of the <see cref="ShareFileRequest"/> class with the given file.
/// </summary>
/// <param name="file">The file to share.</param>
public ShareFileRequest(FileBase file)
=> File = new ShareFile(file);
/// <summary>
/// Gets or sets the file to share.
/// </summary>
public ShareFile? File { get; set; }
}
/// <summary>
/// Represents a request for sharing multiple files with other apps on the device.
/// </summary>
public class ShareMultipleFilesRequest : ShareRequestBase
{
/// <summary>
/// Initializes a new instance of the <see cref="ShareMultipleFilesRequest"/> class.
/// </summary>
public ShareMultipleFilesRequest()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ShareMultipleFilesRequest"/> class with the given files.
/// </summary>
/// <param name="files">A collection of files to share.</param>
public ShareMultipleFilesRequest(IEnumerable<ShareFile> files) =>
Files = files.ToList();
/// <summary>
/// Initializes a new instance of the <see cref="ShareMultipleFilesRequest"/> class with the given files.
/// </summary>
/// <param name="files">A collection of files to share.</param>
public ShareMultipleFilesRequest(IEnumerable<FileBase> files)
: this(ConvertList(files))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ShareMultipleFilesRequest"/> class with the given title and files.
/// </summary>
/// <param name="title">The title to display on the operating systems share dialog.</param>
/// <param name="files">A collection of files to share.</param>
public ShareMultipleFilesRequest(string title, IEnumerable<ShareFile> files)
: this(files) => Title = title;
/// <summary>
/// Initializes a new instance of the <see cref="ShareMultipleFilesRequest"/> class with the given title and files.
/// </summary>
/// <param name="title">The title to display on the operating systems share dialog.</param>
/// <param name="files">A collection of files to share.</param>
public ShareMultipleFilesRequest(string title, IEnumerable<FileBase> files)
: this(title, ConvertList(files))
{
}
/// <summary>
/// Gets or sets the files to share.
/// </summary>
public List<ShareFile>? Files { get; set; } = new List<ShareFile>();
/// <summary>
/// Convert a single file share request into a multi-file share request.
/// </summary>
/// <param name="request">The <see cref="ShareFileRequest"/> object to convert into a <see cref="ShareMultipleFilesRequest"/> object.</param>
public static explicit operator ShareMultipleFilesRequest(ShareFileRequest request) =>
new ShareMultipleFilesRequest
{
Title = request.Title,
Files = new List<ShareFile> { request.File! },
PresentationSourceBounds = request.PresentationSourceBounds
};
static IEnumerable<ShareFile> ConvertList(IEnumerable<FileBase> files)
=> files?.Select(file => new ShareFile(file)) ?? Array.Empty<ShareFile>();
}
/// <summary>
/// A representation of a file that can be used with the <see cref="IShare"/> API.
/// </summary>
public class ShareFile : FileBase
{
/// <summary>
/// Initializes a new instance of the <see cref="ShareFile"/> class with the given file path.
/// </summary>
/// <param name="fullPath">The full path to the file represented by this object.</param>
public ShareFile(string fullPath)
: base(fullPath)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ShareFile"/> class with the given file path and content type.
/// </summary>
/// <param name="fullPath">The full path to the file represented by this object.</param>
/// <param name="contentType">Explicit content type (MIME type) of the file (eg: <c>image/png</c>).</param>
public ShareFile(string fullPath, string contentType)
: base(fullPath, contentType)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ShareFile"/> class from the given <see cref="FileBase"/> object.
/// </summary>
/// <param name="file">A <see cref="FileBase"/> object that will be wrapped in a <see cref="ShareFile"/> object.</param>
public ShareFile(FileBase file)
: base(file)
{
}
}
}