Skip to content

Commit

Permalink
Title for picker (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimonovdd authored Jul 9, 2021
1 parent cb9f22f commit fa966e4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
11 changes: 10 additions & 1 deletion MediaGallery/MediaGallery/PickMedia.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ static async Task<IEnumerable<IMediaFile>> PlatformPickAsync(MediaPickRequest re
{
token.ThrowIfCancellationRequested();
Intent intent = null;
Intent chooserIntent = null;

try
{
Expand Down Expand Up @@ -57,6 +58,12 @@ static async Task<IEnumerable<IMediaFile>> PlatformPickAsync(MediaPickRequest re
intent.PutExtra(Intent.ExtraLocalOnly, true);
intent.PutExtra(Intent.ExtraAllowMultiple, request.SelectionLimit > 1);


if (!string.IsNullOrWhiteSpace(request.Title))
intent.PutExtra(Intent.ExtraTitle, request.Title);

chooserIntent = Intent.CreateChooser(intent, request.Title ?? string.Empty);

CancelTaskIfRequested();

if (token.CanBeCanceled)
Expand All @@ -66,7 +73,7 @@ static async Task<IEnumerable<IMediaFile>> PlatformPickAsync(MediaPickRequest re
tcs?.TrySetCanceled(token);
});

Platform.AppActivity.StartActivityForResult(intent, Platform.requestCode);
Platform.AppActivity.StartActivityForResult(chooserIntent, Platform.requestCode);

CancelTaskIfRequested(false);
var result = await tcs.Task.ConfigureAwait(false);
Expand All @@ -86,6 +93,8 @@ void CancelTaskIfRequested(bool needThrow = true)
{
intent?.Dispose();
intent = null;
chooserIntent?.Dispose();
chooserIntent = null;
tcs = null;
}
}
Expand Down
11 changes: 7 additions & 4 deletions MediaGallery/MediaPickRequest/MediaPickRequest.shared.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Drawing;

using System.Linq;

namespace NativeMedia
Expand All @@ -8,8 +7,8 @@ namespace NativeMedia
public class MediaPickRequest
{
/// <summary></summary>
/// <param name="selectionLimit">Maximum count of files to pick. On Android the option just sets multiple pick allowed.</param>
/// <param name="types">Media file types available for picking</param>
/// <param name="selectionLimit"><inheritdoc cref="SelectionLimit" path="/summary"/></param>
/// <param name="types"><inheritdoc cref="Types" path="/summary"/></param>
public MediaPickRequest(int selectionLimit = 1, params MediaFileType[] types)
{
SelectionLimit = selectionLimit > 0 ? selectionLimit : 1;
Expand All @@ -18,10 +17,14 @@ public MediaPickRequest(int selectionLimit = 1, params MediaFileType[] types)
: new MediaFileType[] { MediaFileType.Image, MediaFileType.Video };
}


/// <summary>Maximum count of files to pick. On Android the option just sets multiple pick allowed.</summary>
public int SelectionLimit { get; }

/// <summary>Media file types available for picking.</summary>
public MediaFileType[] Types { get; }

/// <summary>Media file types available for picking.</summary>
public string Title { get; set; }

/// <summary>Gets or sets the source rectangle to display the Share UI from. This is only used on iPad currently.</summary>
public Rectangle PresentationSourceBounds { get; set; } = default;
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ try
{
var request = new MediaPickRequest(1, MediaFileType.Image, MediaFileType.Video)
{
PresentationSourceBounds = System.Drawing.Rectangle.Empty
PresentationSourceBounds = System.Drawing.Rectangle.Empty,
Title = "Select"
};

cts.CancelAfter(TimeSpan.FromMinutes(5));
Expand Down Expand Up @@ -127,12 +128,14 @@ protected override void OnActivityResult(int requestCode, Result resultCode, Int
```

- When using the `PickAsync` method the `selectionLimit` parameter just sets multiple pick allowed
- A request to cancel `PickAsync` method will cancel a task, but the picker UI can remain open until it is closed by the user.
- A request to cancel `PickAsync` method will cancel a task, but the picker UI can remain open until it is closed by the user
- The use of `Title` property depends on each device

## iOS

- Multi picking is supported since iOS version 14.0+ On older versions, the plugin will prompt the user to select a single file
- The `NameWithoutExtension` property on iOS versions before 14 returns a null value if the permission to access photos was not granted
- `Title` property not used

### Presentation Location

Expand Down
1 change: 1 addition & 0 deletions Sample/Sample/ViewModels/PickVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ void Pick(View view, params MediaFileType[] types)
task = MediaGallery.PickAsync(
new MediaPickRequest(SelectionLimit, types)
{
Title = $"Select {SelectionLimit} photos",
PresentationSourceBounds = view == null
? System.Drawing.Rectangle.Empty
: view.GetAbsoluteBounds().ToSystemRectangle(40)
Expand Down

0 comments on commit fa966e4

Please sign in to comment.