diff --git a/Samples/Samples/ViewModel/ShareViewModel.cs b/Samples/Samples/ViewModel/ShareViewModel.cs new file mode 100644 index 000000000..dd63df134 --- /dev/null +++ b/Samples/Samples/ViewModel/ShareViewModel.cs @@ -0,0 +1,112 @@ +using System.IO; +using System.Windows.Input; +using Xamarin.Essentials; +using Xamarin.Forms; + +namespace Samples.ViewModel +{ + class ShareViewModel : BaseViewModel + { + bool shareText = true; + bool shareUri; + string text; + string uri; + string subject; + string title; + string shareFileAttachmentContents; + string shareFileAttachmentName; + string shareFileTitle; + + public ICommand RequestCommand { get; } + + public ICommand RequestFileCommand { get; } + + public ShareViewModel() + { + RequestCommand = new Command(OnRequest); + RequestFileCommand = new Command(OnFileRequest); + } + + public bool ShareText + { + get => shareText; + set => SetProperty(ref shareText, value); + } + + public bool ShareUri + { + get => shareUri; + set => SetProperty(ref shareUri, value); + } + + public string Text + { + get => text; + set => SetProperty(ref text, value); + } + + public string Uri + { + get => uri; + set => SetProperty(ref uri, value); + } + + public string Subject + { + get => subject; + set => SetProperty(ref subject, value); + } + + public string Title + { + get => title; + set => SetProperty(ref title, value); + } + + public string ShareFileTitle + { + get => shareFileTitle; + set => SetProperty(ref shareFileTitle, value); + } + + public string ShareFileAttachmentContents + { + get => shareFileAttachmentContents; + set => SetProperty(ref shareFileAttachmentContents, value); + } + + public string ShareFileAttachmentName + { + get => shareFileAttachmentName; + set => SetProperty(ref shareFileAttachmentName, value); + } + + async void OnRequest() + { + await Share.RequestAsync(new ShareTextRequest + { + Subject = Subject, + Text = ShareText ? Text : null, + Uri = ShareUri ? Uri : null, + Title = Title + }); + } + + async void OnFileRequest() + { + if (!string.IsNullOrWhiteSpace(ShareFileAttachmentContents)) + { + // create a temprary file + var fn = string.IsNullOrWhiteSpace(ShareFileAttachmentName) ? "Attachment.txt" : ShareFileAttachmentName.Trim(); + var file = Path.Combine(FileSystem.CacheDirectory, fn); + File.WriteAllText(file, ShareFileAttachmentContents); + + await Share.RequestAsync(new ShareFileRequest + { + Title = Title, + File = new ShareFile(file) + }); + } + } + } +} diff --git a/Xamarin.Essentials/Share/Share.shared.cs b/Xamarin.Essentials/Share/Share.shared.cs new file mode 100644 index 000000000..cfe27631f --- /dev/null +++ b/Xamarin.Essentials/Share/Share.shared.cs @@ -0,0 +1,126 @@ +using System; +using System.IO; +using System.Threading.Tasks; + +namespace Xamarin.Essentials +{ + public static partial class Share + { + public static Task RequestAsync(string text) => + RequestAsync(new ShareTextRequest(text)); + + public static Task RequestAsync(string text, string title) => + RequestAsync(new ShareTextRequest(text, title)); + + public static Task RequestAsync(ShareTextRequest request) => + PlatformRequestAsync(request); + + public static Task RequestAsync(ShareFileRequest request) + { + ExperimentalFeatures.VerifyEnabled(ExperimentalFeatures.ShareFileRequest); + + return PlatformRequestAsync(request); + } + } + + public class ShareTextRequest + { + public ShareTextRequest() + { + } + + public ShareTextRequest(string text) => Text = text; + + public ShareTextRequest(string text, string title) + : this(text) => Title = title; + + public string Title { get; set; } + + public string Subject { get; set; } + + public string Text { get; set; } + + public string Uri { get; set; } + } + + public class ShareFileRequest + { + public ShareFileRequest() + { + ExperimentalFeatures.VerifyEnabled(ExperimentalFeatures.ShareFileRequest); + } + + public ShareFileRequest(string title, ShareFile file) + { + ExperimentalFeatures.VerifyEnabled(ExperimentalFeatures.ShareFileRequest); + Title = title; + File = file; + } + + public ShareFileRequest(string title, FileBase file) + { + ExperimentalFeatures.VerifyEnabled(ExperimentalFeatures.ShareFileRequest); + Title = title; + File = new ShareFile(file); + } + + public ShareFileRequest(ShareFile file) + { + ExperimentalFeatures.VerifyEnabled(ExperimentalFeatures.ShareFileRequest); + File = file; + } + + public ShareFileRequest(FileBase file) + { + ExperimentalFeatures.VerifyEnabled(ExperimentalFeatures.ShareFileRequest); + File = new ShareFile(file); + } + + public string Title { get; set; } + + public ShareFile File { get; set; } + } + + public class ShareFile : FileBase + { + public ShareFile(string fullPath) + : base(fullPath) + { + ExperimentalFeatures.VerifyEnabled(ExperimentalFeatures.ShareFileRequest); + } + + public ShareFile(string fullPath, string contentType) + : base(fullPath, contentType) + { + ExperimentalFeatures.VerifyEnabled(ExperimentalFeatures.ShareFileRequest); + } + + public ShareFile(FileBase file) + : base(file) + { + ExperimentalFeatures.VerifyEnabled(ExperimentalFeatures.ShareFileRequest); + } + + string attachmentName; + + public string AttachmentName + { + get => GetAttachmentName(); + set => attachmentName = value; + } + + internal string GetAttachmentName() + { + // try the provided file name + if (!string.IsNullOrWhiteSpace(attachmentName)) + return attachmentName; + + // try get from the path + if (!string.IsNullOrWhiteSpace(FullPath)) + return Path.GetFileName(FullPath); + + // this should never happen as the path is validated in the constructor + throw new InvalidOperationException($"Unable to determine the attachment file name from '{FullPath}'."); + } + } +}