Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements for Intent.CreateChooser #61

Merged
merged 15 commits into from
Sep 15, 2021
14 changes: 9 additions & 5 deletions MediaGallery/MediaGallery/PickMedia.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ static async Task<IEnumerable<IMediaFile>> PlatformPickAsync(MediaPickRequest re
{
token.ThrowIfCancellationRequested();
Intent intent = null;
Intent chooserIntent = null;
IDisposable intentDisposable = null;

try
{
Expand Down Expand Up @@ -62,7 +62,11 @@ static async Task<IEnumerable<IMediaFile>> PlatformPickAsync(MediaPickRequest re
if (!string.IsNullOrWhiteSpace(request.Title))
intent.PutExtra(Intent.ExtraTitle, request.Title);

chooserIntent = Intent.CreateChooser(intent, request.Title ?? string.Empty);
if (request.UseCreateChooser)
{
intentDisposable = intent;
intent = Intent.CreateChooser(intent, request.Title ?? string.Empty);
}

CancelTaskIfRequested();

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

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

CancelTaskIfRequested(false);
var result = await tcs.Task.ConfigureAwait(false);
Expand All @@ -91,10 +95,10 @@ void CancelTaskIfRequested(bool needThrow = true)
}
finally
{
intentDisposable?.Dispose();
intentDisposable = null;
intent?.Dispose();
intent = null;
chooserIntent?.Dispose();
chooserIntent = null;
tcs = null;
}
}
Expand Down
3 changes: 3 additions & 0 deletions MediaGallery/MediaPickRequest/MediaPickRequest.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,8 @@ public MediaPickRequest(int selectionLimit = 1, params MediaFileType[] types)

/// <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;

/// <summary>Gets or sets whether to use Intent.CreateChooser. Currently used only for Android.</summary>
public bool UseCreateChooser { get; set; } = true;
}
}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ try
var request = new MediaPickRequest(1, MediaFileType.Image, MediaFileType.Video)
{
PresentationSourceBounds = System.Drawing.Rectangle.Empty,
UseCreateChooser = true,
Title = "Select"
};

Expand Down Expand Up @@ -130,12 +131,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
- The use of `Title` property depends on each device
- `UseCreateChooser` specifies whether to use `Intent.CreateChooser`

## 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
- `UseCreateChooser` property not used

### Presentation Location

Expand Down
4 changes: 4 additions & 0 deletions Sample/Sample/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
<Style TargetType="Label">
<Setter Property="Margin" Value="0" />
</Style>
<Style TargetType="Switch">
<Setter Property="OnColor" Value="LightGray" />
<Setter Property="ThumbColor" Value="#990000ff" />
</Style>
</ResourceDictionary>
</Application.Resources>
</Application>
5 changes: 4 additions & 1 deletion Sample/Sample/ViewModels/PickVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public int DelayMilliseconds

public string OperationInfo { get; set; }

public bool UseCreateChooser { get; set; } = true;

public IEnumerable<IMediaFile> SelectedItems { get; set; }

public ICommand PickAnyCommand { get; }
Expand Down Expand Up @@ -73,7 +75,8 @@ void Pick(View view, params MediaFileType[] types)
Title = $"Select {SelectionLimit} photos",
PresentationSourceBounds = view == null
? System.Drawing.Rectangle.Empty
: view.GetAbsoluteBounds().ToSystemRectangle(40)
: view.GetAbsoluteBounds().ToSystemRectangle(40),
UseCreateChooser = UseCreateChooser
},
cts.Token);
}
Expand Down
5 changes: 5 additions & 0 deletions Sample/Sample/Views/PickPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
<Label Text="Cancel after:"/>
<Entry Text="{Binding DelayMilliseconds}" Placeholder="Milliseconds" Keyboard="Numeric"/>

<Grid ColumnDefinitions="auto,auto">
<Label Grid.Column="0" Text="Need use Intent.CreateChooser:"/>
<Switch Grid.Column="1" IsToggled="{Binding UseCreateChooser}"/>
</Grid>

<Button Text="Any" Command="{Binding PickAnyCommand, Mode=OneTime}"/>
<Button Text="Image" Command="{Binding PickImageCommand, Mode=OneTime}"
CommandParameter="{Binding Source={RelativeSource Self}}"/>
Expand Down