unoplatform / uno.toolkit.ui

A set of custom controls for the WinUI and the Uno Platform not offered out of the box by WinUI, such as Card, TabBar, NavigationBar, etc.
https://platform.uno/
MIT License
82 stars 27 forks source link

Spec: `MediaGallery` #1147

Open MartinZikmund opened 3 months ago

MartinZikmund commented 3 months ago

What would you like to be added:

The MediaGallery class is a static class designed for interaction with the device's media gallery on iOS and Android platforms. It provides functionality to check user permissions for accessing the gallery and saving media files to the gallery.

Why is this needed:

User requirement to efficiently save files into storage.

Public API

Uno.Toolkit.UI.MediaFileType enum

Represents a media file type.

Members

Uno.Toolkit.UI.MediaGallery class

Allows interaction with the device's media gallery.

This class is available only on Android and iOS targets, where media gallery requires a specific API.

CheckAccessAsync method

/// <summary>
/// Checks the user's permission to access the device's gallery.
/// Will trigger the permission request if not already granted.
/// </summary>
/// <returns>A Task that represents a boolean indicating whether the user has access.</returns>
public static async Task<bool> CheckAccessAsync()

SaveAsync (stream)

/// <summary>
/// Saves a media file to the device's gallery.
/// </summary>
/// <param name="type">The type of the media file.</param>
/// <param name="stream">A stream representing the media file.</param>
/// <param name="targetFileName">The name to save the file as.</param>
/// <returns>A Task that represents the progress of the save operation.</returns>
public static async Task SaveAsync(MediaFileType type, Stream stream, string targetFileName)

SaveAsync (byte array)

/// <summary>
/// Saves a media file to the device's gallery.
/// </summary>
/// <param name="type">The type of the media file.</param>
/// <param name="data">A byte array representing the media file.</param>
/// <param name="targetFileName">The name to save the file as.</param>
/// <returns>A Task that represents the progress of the save operation.</returns>
public static async Task SaveAsync(MediaFileType type, byte[] data, string targetFileName)

Usage

// Check for gallery access
bool hasAccess = await MediaGallery.CheckAccessAsync();

if (hasAccess)
{
    // Save a video to the gallery using stream
    using Stream videoStream = ...; // Video stream
    await MediaGallery.SaveAsync(MediaFileType.Video, videoStream, "MyVideo.mp4");
}
francoistanguay commented 3 months ago

So there's no WinUI/WinRT-equivalent API for this? even at lower level (Storage/...)?

If there's really nothing, shouldnt this be in a Toolkit (non-UI) package vs a Toolkit.UI has it's more of a WinRT api and not a XAML/View component?

kazo0 commented 3 months ago

I'll let @MartinZikmund expand on it but I believe it's in .UI because of the need for launching the Android/iOS native UI. The non-UI Toolkit lib is just a netstandard2.0 library

jeromelaban commented 3 months ago

So there's no WinUI/WinRT-equivalent API for this? even at lower level (Storage/...)?

It would be StorageFile, but its API is too large for what we need to support. We could end up adding support for it in the future, if that becomes necessary.

francoistanguay commented 3 months ago

Dont we already have StorageFile support? Can we just add what is needed?

Maybe easier to discuss.

MartinZikmund commented 3 months ago

@francoistanguay unfortunately not easily possible - for example on iOS one can save the file, but not retrieve it back (unless another permission is added). The API is just so specific, that mapping it onto StorageFile is close to impossible. If we could get it to work, it would be very specific and most operations would just result in NotSupportedException. In this case having separate API feels more user friendly.

For why Uno.Toolkit.UI and not non UI - I needed access to Uno.UI. I could move it to Uno.Toolkit, but then we would have to add Uno.UI reference to Uno.Toolkit as well (it does not have it currently)