Skip to content

Commit

Permalink
Improvements for Intent.CreateChooser (#61)
Browse files Browse the repository at this point in the history
* Fixed the PickAsync method for devices with Android 11

* added NeedUseCreateChooser

* added NeedUseCreateChooser to readme

* added intentDisposable & renamed parameter

* added a description of UseCreateChooser for iOS

* updated sample

* Update README.md

Co-authored-by: Dima Dimov <dimovde@yandex.ru>

* Update PickMedia.android.cs

Co-authored-by: Dima Dimov <dimovde@yandex.ru>

* Update PickMedia.android.cs

Co-authored-by: Dima Dimov <dimovde@yandex.ru>

* Update MediaPickRequest.shared.cs

Co-authored-by: Dima Dimov <dimovde@yandex.ru>

* upd Version

Co-authored-by: Dima Dimov <dimovde@yandex.ru>
  • Loading branch information
Andrey Kopylov and dimonovdd committed Sep 15, 2021
1 parent 46e1af5 commit 9432429
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 10 deletions.
8 changes: 4 additions & 4 deletions MediaGallery/MediaGallery.csproj
Expand Up @@ -10,10 +10,10 @@
<Title>Xamarin.MediaGallery</Title>
<Description>This plugin is designed for picking and saving photos and video files from the native gallery of Android and iOS devices</Description>
<Product>$(AssemblyName) ($(TargetFramework))</Product>
<AssemblyVersion>1.0.2.1</AssemblyVersion>
<AssemblyFileVersion>1.0.2.1</AssemblyFileVersion>
<Version>1.0.2.1</Version>
<PackageVersion>1.0.2.1</PackageVersion>
<AssemblyVersion>1.0.2.2</AssemblyVersion>
<AssemblyFileVersion>1.0.2.2</AssemblyFileVersion>
<Version>1.0.2.2</Version>
<PackageVersion>1.0.2.2-preview1</PackageVersion>
<Authors>dimonovdd</Authors>
<Owners>dimonovdd</Owners>
<PackageProjectUrl>https://github.com/dimonovdd/Xamarin.MediaGallery</PackageProjectUrl>
Expand Down
14 changes: 9 additions & 5 deletions MediaGallery/MediaGallery/PickMedia.android.cs
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
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
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
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
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
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

0 comments on commit 9432429

Please sign in to comment.