Skip to content

Commit

Permalink
CancellationToken for PickAsync (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimonovdd authored May 30, 2021
1 parent 9073051 commit 91e12ae
Show file tree
Hide file tree
Showing 10 changed files with 327 additions and 914 deletions.
2 changes: 1 addition & 1 deletion MediaGallery/MediaGallery.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<AssemblyFileVersion>1.0.0.0</AssemblyFileVersion>
<Version>1.0.0.0</Version>
<PackageVersion>1.0.0.0-alpha005</PackageVersion>
<PackageVersion>1.0.0</PackageVersion>
<Authors>dimonovdd</Authors>
<Owners>dimonovdd</Owners>
<PackageProjectUrl>https://github.com/dimonovdd/Xamarin.MediaGallery</PackageProjectUrl>
Expand Down
3 changes: 2 additions & 1 deletion MediaGallery/MediaGallery/MediaGallery.netstandard.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace NativeMedia
{
public static partial class MediaGallery
{
static Task<IEnumerable<IMediaFile>> PlatformPickAsync(MediaPickRequest request)
static Task<IEnumerable<IMediaFile>> PlatformPickAsync(MediaPickRequest request, CancellationToken token)
=> Task.FromResult<IEnumerable<IMediaFile>>(null);

static Task PlatformSaveAsync(MediaFileType type, byte[] data, string fileName)
Expand Down
7 changes: 4 additions & 3 deletions MediaGallery/MediaGallery/MediaGallery.shared.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace NativeMedia
Expand All @@ -11,17 +12,17 @@ public static partial class MediaGallery
/// <returns>Media files selected by a user.</returns>
/// <inheritdoc cref = "MediaPickRequest(int, MediaFileType[])" path="/param"/>
public static Task<MediaPickResult> PickAsync(int selectionLimit = 1, params MediaFileType[] types)
=> PickAsync(new MediaPickRequest(selectionLimit, types));
=> PickAsync(new MediaPickRequest(selectionLimit, types), default);

/// <param name="request">Media file request to pick.</param>
/// <inheritdoc cref = "PickAsync(int, MediaFileType[])" path="//*[not(self::param)]"/>
public static async Task<MediaPickResult> PickAsync(MediaPickRequest request)
public static async Task<MediaPickResult> PickAsync(MediaPickRequest request, CancellationToken token = default)
{
ExeptionHelper.CheckSupport();
if (request == null)
throw new ArgumentNullException(nameof(request));

return new MediaPickResult(await PlatformPickAsync(request));
return new MediaPickResult(await PlatformPickAsync(request, token));
}

/// <summary>Saves a media file with metadata </summary>
Expand Down
89 changes: 61 additions & 28 deletions MediaGallery/MediaGallery/PickMedia.android.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Uri = Android.Net.Uri;
using Android.Provider;
using System.Threading;
#if MONOANDROID11_0
using MediaColumns = Android.Provider.MediaStore.IMediaColumns;
#else
Expand All @@ -21,40 +22,72 @@ public static partial class MediaGallery
const string videoType = "video/*";
static TaskCompletionSource<Intent> tcs;

static async Task<IEnumerable<IMediaFile>> PlatformPickAsync(MediaPickRequest request)
static async Task<IEnumerable<IMediaFile>> PlatformPickAsync(MediaPickRequest request, CancellationToken token)
{
var isImage = request.Types.Contains(MediaFileType.Image);
tcs = new TaskCompletionSource<Intent>();
token.ThrowIfCancellationRequested();
Intent intent = null;

Intent intent;

// https://github.com/dimonovdd/Xamarin.MediaGallery/pull/5
if (isImage && request.Types.Length == 1)
{
intent = new Intent(Intent.ActionPick, MediaStore.Images.Media.ExternalContentUri);
intent.SetType(imageType);
intent.PutExtra(Intent.ExtraMimeTypes, new string[] { imageType });
}
else
try
{
intent = new Intent(Intent.ActionGetContent);

intent.SetType(isImage ? $"{imageType}, {videoType}" : videoType);
intent.PutExtra(
Intent.ExtraMimeTypes,
isImage
? new string[] { imageType, videoType }
: new string[] { videoType });
}
var isImage = request.Types.Contains(MediaFileType.Image);
tcs = new TaskCompletionSource<Intent>();

CancelTaskIfRequested(false);

// https://github.com/dimonovdd/Xamarin.MediaGallery/pull/5
if (isImage && request.Types.Length == 1)
{
intent = new Intent(Intent.ActionPick, MediaStore.Images.Media.ExternalContentUri);
intent.SetType(imageType);
intent.PutExtra(Intent.ExtraMimeTypes, new string[] { imageType });
}
else
{
intent = new Intent(Intent.ActionGetContent);

intent.SetType(isImage ? $"{imageType}, {videoType}" : videoType);
intent.PutExtra(
Intent.ExtraMimeTypes,
isImage
? new string[] { imageType, videoType }
: new string[] { videoType });
intent.AddCategory(Intent.CategoryOpenable);
}

intent.PutExtra(Intent.ExtraLocalOnly, true);
intent.PutExtra(Intent.ExtraAllowMultiple, request.SelectionLimit > 1);

intent.PutExtra(Intent.ExtraLocalOnly, false);
intent.PutExtra(Intent.ExtraAllowMultiple, request.SelectionLimit > 1);
CancelTaskIfRequested();

Platform.AppActivity.StartActivityForResult(intent, Platform.requestCode);
if (token.CanBeCanceled)
token.Register(() =>
{
Platform.AppActivity.FinishActivity(Platform.requestCode);
tcs?.TrySetCanceled(token);
});

var result = await tcs.Task;
Platform.AppActivity.StartActivityForResult(intent, Platform.requestCode);

return GetFilesFromIntent(result);
CancelTaskIfRequested(false);
var result = await tcs.Task;
return GetFilesFromIntent(result);

void CancelTaskIfRequested(bool needThrow = true)
{
if (token.IsCancellationRequested)
{
tcs?.TrySetCanceled(token);
if (needThrow)
token.ThrowIfCancellationRequested();
}
}
}
finally
{
intent?.Dispose();
intent = null;
tcs = null;
}
}

internal static void OnActivityResult(int requestCode, Result resultCode, Intent intent)
Expand Down
Loading

0 comments on commit 91e12ae

Please sign in to comment.