Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions src/Essentials/src/FilePicker/FilePicker.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
using System.Threading.Tasks;
using Android.Content;

namespace Microsoft.Maui.Essentials
namespace Microsoft.Maui.Essentials.Implementations
{
public static partial class FilePicker
public partial class FilePickerImplementation : IFilePicker
{
static async Task<IEnumerable<FileResult>> PlatformPickAsync(PickOptions options, bool allowMultiple = false)
public async Task<IEnumerable<FileResult>> PickAsync(PickOptions options, bool allowMultiple = false)
{
// we only need the permission when accessing the file, but it's more natural
// to ask the user first, then show the picker.
Expand Down Expand Up @@ -62,36 +62,46 @@ void OnResult(Intent intent)
return null;
}
}

public async Task<IEnumerable<FileResult>> PickMultipleAsync(PickOptions options)
{
return await PickAsync(options, true);
}
}

public partial class FilePickerFileType
public partial class FilePickerFileTypeImplementation : IFilePickerFileType
{
static FilePickerFileType PlatformImageFileType() =>
new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>>
readonly IDictionary<DevicePlatform, IEnumerable<string>> fileTypes;

public FilePickerFileTypeImplementation(IDictionary<DevicePlatform, IEnumerable<string>> fileTypes) =>
this.fileTypes = fileTypes;

public IFilePickerFileType ImageFileType() =>
new FilePickerFileTypeImplementation(new Dictionary<DevicePlatform, IEnumerable<string>>
{
{ DevicePlatform.Android, new[] { FileSystem.MimeTypes.ImagePng, FileSystem.MimeTypes.ImageJpg } }
});

static FilePickerFileType PlatformPngFileType() =>
new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>>
public IFilePickerFileType PngFileType() =>
new FilePickerFileTypeImplementation(new Dictionary<DevicePlatform, IEnumerable<string>>
{
{ DevicePlatform.Android, new[] { FileSystem.MimeTypes.ImagePng } }
});

static FilePickerFileType PlatformJpegFileType() =>
new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>>
public IFilePickerFileType JpegFileType() =>
new FilePickerFileTypeImplementation(new Dictionary<DevicePlatform, IEnumerable<string>>
{
{ DevicePlatform.Android, new[] { FileSystem.MimeTypes.ImageJpg } }
});

static FilePickerFileType PlatformVideoFileType() =>
new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>>
public IFilePickerFileType VideoFileType() =>
new FilePickerFileTypeImplementation(new Dictionary<DevicePlatform, IEnumerable<string>>
{
{ DevicePlatform.Android, new[] { FileSystem.MimeTypes.VideoAll } }
});

static FilePickerFileType PlatformPdfFileType() =>
new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>>
public IFilePickerFileType PdfFileType() =>
new FilePickerFileTypeImplementation(new Dictionary<DevicePlatform, IEnumerable<string>>
{
{ DevicePlatform.Android, new[] { FileSystem.MimeTypes.Pdf } }
});
Expand Down
38 changes: 24 additions & 14 deletions src/Essentials/src/FilePicker/FilePicker.ios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
using ObjCRuntime;
using UIKit;

namespace Microsoft.Maui.Essentials
namespace Microsoft.Maui.Essentials.Implementations
{
public static partial class FilePicker
public partial class FilePickerImplementation : IFilePicker
{
static async Task<IEnumerable<FileResult>> PlatformPickAsync(PickOptions options, bool allowMultiple = false)
public async Task<IEnumerable<FileResult>> PickAsync(PickOptions options, bool allowMultiple = false)
{
var allowedUtis = options?.FileTypes?.Value?.ToArray() ?? new string[]
{
Expand Down Expand Up @@ -46,6 +46,11 @@ static async Task<IEnumerable<FileResult>> PlatformPickAsync(PickOptions options
return await tcs.Task;
}

public async Task<IEnumerable<FileResult>> PickMultipleAsync(PickOptions options)
{
return await PickAsync(options, true);
}

static async void GetFileResults(NSUrl[] urls, TaskCompletionSource<IEnumerable<FileResult>> tcs)
{
try
Expand Down Expand Up @@ -76,34 +81,39 @@ public override void DidPickDocument(UIDocumentPickerViewController controller,

}

public partial class FilePickerFileType
public partial class FilePickerFileTypeImplementation : IFilePickerFileType
{
static FilePickerFileType PlatformImageFileType() =>
new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>>
readonly IDictionary<DevicePlatform, IEnumerable<string>> fileTypes;

public FilePickerFileTypeImplementation(IDictionary<DevicePlatform, IEnumerable<string>> fileTypes) =>
this.fileTypes = fileTypes;

public IFilePickerFileType ImageFileType() =>
new FilePickerFileTypeImplementation(new Dictionary<DevicePlatform, IEnumerable<string>>
{
{ DevicePlatform.iOS, new[] { (string)UTType.Image } }
});

static FilePickerFileType PlatformPngFileType() =>
new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>>
public IFilePickerFileType PngFileType() =>
new FilePickerFileTypeImplementation(new Dictionary<DevicePlatform, IEnumerable<string>>
{
{ DevicePlatform.iOS, new[] { (string)UTType.PNG } }
});

static FilePickerFileType PlatformJpegFileType() =>
new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>>
public IFilePickerFileType JpegFileType() =>
new FilePickerFileTypeImplementation(new Dictionary<DevicePlatform, IEnumerable<string>>
{
{ DevicePlatform.iOS, new[] { (string)UTType.JPEG } }
});

static FilePickerFileType PlatformVideoFileType() =>
new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>>
public IFilePickerFileType VideoFileType() =>
new FilePickerFileTypeImplementation(new Dictionary<DevicePlatform, IEnumerable<string>>
{
{ DevicePlatform.iOS, new string[] { UTType.MPEG4, UTType.Video, UTType.AVIMovie, UTType.AppleProtectedMPEG4Video, "mp4", "m4v", "mpg", "mpeg", "mp2", "mov", "avi", "mkv", "flv", "gifv", "qt" } }
});

static FilePickerFileType PlatformPdfFileType() =>
new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>>
public IFilePickerFileType PdfFileType() =>
new FilePickerFileTypeImplementation(new Dictionary<DevicePlatform, IEnumerable<string>>
{
{ DevicePlatform.iOS, new[] { (string)UTType.PDF } }
});
Expand Down
35 changes: 20 additions & 15 deletions src/Essentials/src/FilePicker/FilePicker.macos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
using AppKit;
using MobileCoreServices;

namespace Microsoft.Maui.Essentials
namespace Microsoft.Maui.Essentials.Implementations
{
public static partial class FilePicker
public partial class FilePickerImplementation : IFilePicker
{
static Task<IEnumerable<FileResult>> PlatformPickAsync(PickOptions options, bool allowMultiple = false)
public Task<IEnumerable<FileResult>> PickAsync(PickOptions options, bool allowMultiple = false)
{
var openPanel = new NSOpenPanel
{
Expand All @@ -33,7 +33,12 @@ static Task<IEnumerable<FileResult>> PlatformPickAsync(PickOptions options, bool
return Task.FromResult<IEnumerable<FileResult>>(resultList);
}

static void SetFileTypes(PickOptions options, NSOpenPanel panel)
public async Task<IEnumerable<FileResult>> PickMultipleAsync(PickOptions options)
{
return await PickAsync(options, true);
}

void SetFileTypes(PickOptions options, NSOpenPanel panel)
{
var allowedFileTypes = new List<string>();

Expand All @@ -49,34 +54,34 @@ static void SetFileTypes(PickOptions options, NSOpenPanel panel)
}
}

public partial class FilePickerFileType
public partial class FilePickerFileTypeImplementation : IFilePickerFileType
{
static FilePickerFileType PlatformImageFileType() =>
new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>>
static FilePickerFileTypeImplementation ImageFileType() =>
new FilePickerFileTypeImplementation(new Dictionary<DevicePlatform, IEnumerable<string>>
{
{ DevicePlatform.macOS, new string[] { UTType.PNG, UTType.JPEG, "jpeg" } }
});

static FilePickerFileType PlatformPngFileType() =>
new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>>
static FilePickerFileTypeImplementation PngFileType() =>
new FilePickerFileTypeImplementation(new Dictionary<DevicePlatform, IEnumerable<string>>
{
{ DevicePlatform.macOS, new string[] { UTType.PNG } }
});

static FilePickerFileType PlatformJpegFileType() =>
new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>>
static FilePickerFileTypeImplementation JpegFileType() =>
new FilePickerFileTypeImplementation(new Dictionary<DevicePlatform, IEnumerable<string>>
{
{ DevicePlatform.macOS, new string[] { UTType.JPEG } }
});

static FilePickerFileType PlatformVideoFileType() =>
new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>>
static FilePickerFileTypeImplementation VideoFileType() =>
new FilePickerFileTypeImplementation(new Dictionary<DevicePlatform, IEnumerable<string>>
{
{ DevicePlatform.macOS, new string[] { UTType.MPEG4, UTType.Video, UTType.AVIMovie, UTType.AppleProtectedMPEG4Video, "mp4", "m4v", "mpg", "mpeg", "mp2", "mov", "avi", "mkv", "flv", "gifv", "qt" } }
});

static FilePickerFileType PlatformPdfFileType() =>
new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>>
static FilePickerFileTypeImplementation PdfFileType() =>
new FilePickerFileTypeImplementation(new Dictionary<DevicePlatform, IEnumerable<string>>
{
{ DevicePlatform.macOS, new string[] { UTType.PDF } }
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,34 @@
using System.IO;
using System.Threading.Tasks;

namespace Microsoft.Maui.Essentials
namespace Microsoft.Maui.Essentials.Implementations
{
public partial class FilePickerImplementation : IFilePicker
/// <include file="../../docs/Microsoft.Maui.Essentials/FilePicker.xml" path="Type[@FullName='Microsoft.Maui.Essentials.FilePicker']/Docs" />
public static partial class FilePicker
{
static Task<IEnumerable<FileResult>> PlatformPickAsync(PickOptions options, bool allowMultiple = false)
public Task<IEnumerable<FileResult>> PickAsync(PickOptions options, bool allowMultiple = false)
=> throw new NotImplementedInReferenceAssemblyException();

public Task<IEnumerable<FileResult>> PickMultipleAsync(PickOptions options)
=> throw new NotImplementedInReferenceAssemblyException();
}

/// <include file="../../docs/Microsoft.Maui.Essentials/FilePickerFileType.xml" path="Type[@FullName='Microsoft.Maui.Essentials.FilePickerFileType']/Docs" />
public partial class FilePickerFileType
public partial class FilePickerFileTypeImplementation : IFilePickerFileType
{
static FilePickerFileType PlatformImageFileType()
public IFilePickerFileType ImageFileType()
=> throw new NotImplementedInReferenceAssemblyException();

static FilePickerFileType PlatformPngFileType()
public IFilePickerFileType PngFileType()
=> throw new NotImplementedInReferenceAssemblyException();

static FilePickerFileType PlatformJpegFileType()
public IFilePickerFileType JpegFileType()
=> throw new NotImplementedInReferenceAssemblyException();

static FilePickerFileType PlatformVideoFileType()
public IFilePickerFileType VideoFileType()
=> throw new NotImplementedInReferenceAssemblyException();

static FilePickerFileType PlatformPdfFileType()
public IFilePickerFileType PdfFileType()
=> throw new NotImplementedInReferenceAssemblyException();
}
}
67 changes: 59 additions & 8 deletions src/Essentials/src/FilePicker/FilePicker.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,71 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel;
using Microsoft.Maui.Essentials;
using Microsoft.Maui.Essentials.Implementations;

namespace Microsoft.Maui.Essentials
{
public interface IFilePicker
{
Task<IEnumerable<FileResult>> PickAsync(PickOptions options, bool allowMultiple);

Task<IEnumerable<FileResult>> PickMultipleAsync(PickOptions options);
}

/// <include file="../../docs/Microsoft.Maui.Essentials/FilePicker.xml" path="Type[@FullName='Microsoft.Maui.Essentials.FilePicker']/Docs" />
public static partial class FilePicker
{
/// <include file="../../docs/Microsoft.Maui.Essentials/FilePicker.xml" path="//Member[@MemberName='PickAsync']/Docs" />
public static async Task<FileResult> PickAsync(PickOptions options = null) =>
(await PlatformPickAsync(options))?.FirstOrDefault();
(await Current.PickAsync(options, false))?.FirstOrDefault();

/// <include file="../../docs/Microsoft.Maui.Essentials/FilePicker.xml" path="//Member[@MemberName='PickMultipleAsync']/Docs" />
public static Task<IEnumerable<FileResult>> PickMultipleAsync(PickOptions options = null) =>
PlatformPickAsync(options ?? PickOptions.Default, true);
Current.PickAsync(options ?? PickOptions.Default, true);

#nullable enable
static IFilePicker? currentImplementation;
#nullable disable

[EditorBrowsable(EditorBrowsableState.Never)]
public static IFilePicker Current =>
currentImplementation ??= new FilePickerImplementation();

[EditorBrowsable(EditorBrowsableState.Never)]
#nullable enable
public static void SetCurrent(IFilePicker? implementation) =>
currentImplementation = implementation;
#nullable disable
}

public interface IFilePickerFileType
{
IFilePickerFileType ImageFileType();

IFilePickerFileType PngFileType();

IFilePickerFileType JpegFileType();

IFilePickerFileType VideoFileType();

IFilePickerFileType PdfFileType();
}

/// <include file="../../docs/Microsoft.Maui.Essentials/FilePickerFileType.xml" path="Type[@FullName='Microsoft.Maui.Essentials.FilePickerFileType']/Docs" />
public partial class FilePickerFileType
{
/// <include file="../../docs/Microsoft.Maui.Essentials/FilePickerFileType.xml" path="//Member[@MemberName='Images']/Docs" />
public static readonly FilePickerFileType Images = PlatformImageFileType();
public static readonly IFilePickerFileType Images = Current.ImageFileType();
/// <include file="../../docs/Microsoft.Maui.Essentials/FilePickerFileType.xml" path="//Member[@MemberName='Png']/Docs" />
public static readonly FilePickerFileType Png = PlatformPngFileType();
public static readonly IFilePickerFileType Png = Current.PngFileType();
/// <include file="../../docs/Microsoft.Maui.Essentials/FilePickerFileType.xml" path="//Member[@MemberName='Jpeg']/Docs" />
public static readonly FilePickerFileType Jpeg = PlatformJpegFileType();
public static readonly IFilePickerFileType Jpeg = Current.JpegFileType();
/// <include file="../../docs/Microsoft.Maui.Essentials/FilePickerFileType.xml" path="//Member[@MemberName='Videos']/Docs" />
public static readonly FilePickerFileType Videos = PlatformVideoFileType();
public static readonly IFilePickerFileType Videos = Current.VideoFileType();
/// <include file="../../docs/Microsoft.Maui.Essentials/FilePickerFileType.xml" path="//Member[@MemberName='Pdf']/Docs" />
public static readonly FilePickerFileType Pdf = PlatformPdfFileType();
public static readonly IFilePickerFileType Pdf = Current.PdfFileType();

readonly IDictionary<DevicePlatform, IEnumerable<string>> fileTypes;

Expand All @@ -52,6 +89,20 @@ protected virtual IEnumerable<string> GetPlatformFileType(DevicePlatform platfor

throw new PlatformNotSupportedException("This platform does not support this file type.");
}

#nullable enable
static IFilePickerFileType? currentImplementation;
#nullable disable

[EditorBrowsable(EditorBrowsableState.Never)]
public static IFilePickerFileType Current =>
currentImplementation ??= new FilePickerFileTypeImplementation();

[EditorBrowsable(EditorBrowsableState.Never)]
#nullable enable
public static void SetCurrent(IFilePickerFileType? implementation) =>
currentImplementation = implementation;
#nullable disable
}

/// <include file="../../docs/Microsoft.Maui.Essentials/PickOptions.xml" path="Type[@FullName='Microsoft.Maui.Essentials.PickOptions']/Docs" />
Expand All @@ -75,6 +126,6 @@ public class PickOptions
public string PickerTitle { get; set; }

/// <include file="../../docs/Microsoft.Maui.Essentials/PickOptions.xml" path="//Member[@MemberName='FileTypes']/Docs" />
public FilePickerFileType FileTypes { get; set; }
public IFilePickerFileType FileTypes { get; set; }
}
}
Loading