Avalonia icon indicating copy to clipboard operation
Avalonia copied to clipboard

Add Sharing Api

Open emmauss opened this issue 1 year ago • 8 comments

What does the pull request do?

Add sharing api and support for the following platforms; Web and Android

public interface IShare
    {
        /// <summary>
        /// Shares text using platform's sharing feature
        /// </summary>
        /// <param name="text">The text string to be shared</param>
        void Share(string text);

        /// <summary>
        /// Shares a file using platform's sharing feature
        /// </summary>
        /// <param name="file">The storage file <see cref="IStorageFile"/> to be shared</param>
        void Share(IStorageFile file);

        /// <summary>
        /// Shares a list of files using platform's sharing feature
        /// </summary>
        /// <param name="files">The list of storage files <see cref="IStorageFile"/> to be shared</param>
        void Share(IList<IStorageFile> files);
    }

The api is accessed using the TopLevel.

public void Share(TopLevel parent)
        {
            parent.Share?.Share("Hello");
        }

What is the current behavior?

What is the updated/expected behavior with this PR?

How was the solution implemented (if it's not obvious)?

Checklist

  • [ ] Added unit tests (if possible)?
  • [x] Added XML documentation to any related classes?
  • [ ] Consider submitting a PR to https://github.com/AvaloniaUI/Documentation with user documentation

Breaking changes

Obsoletions / Deprecations

Fixed issues

emmauss avatar Oct 21 '22 07:10 emmauss

You can test this PR using the following package version. 11.0.999-cibuild0025158-beta. (feed url: https://nuget.avaloniaui.net/repository/avalonia-all/index.json) [PRBUILDID]

avaloniaui-team avatar Oct 21 '22 09:10 avaloniaui-team

  1. I believe the operation should be async
  2. Do we need the entire IStorageFile interface for sharing purposes? I suspect that only name, contents and may be MIME type are required

kekekeks avatar Oct 21 '22 17:10 kekekeks

@maxkatz6 do we want a separate interface or should the feature be exposed by IStorageProvider?

kekekeks avatar Oct 21 '22 18:10 kekekeks

  1. I believe the operation should be async

  2. Do we need the entire IStorageFile interface for sharing purposes? I suspect that only name, contents and may be MIME type are required

  1. There may be an issue on Web, if the api is async. Navigator only allows sharing if the action is spawned by a user gesture. Moreover, the api on respective platforms do not expect a response to client, and is non blocking.
  2. For android, the uri is needed. For web, the original file handle provided by the file dialog is required. We can't use just the file path on web.

emmauss avatar Oct 22 '22 12:10 emmauss

There may be an issue on Web, if the api is async. Navigator only allows sharing if the action is spawned by a user gesture.

Yes, but you still can have async API. Operation needs to be initiated on a user gesture. There is no difference if somewhere else it is awaited.

@maxkatz6 do we want a separate interface or should the feature be exposed by IStorageProvider

I don't have a strong opinion here. If can be in separated interface. But probably naming should be adjusted to be consistent. Like "IShareProvider" I guess.

Do we need the entire IStorageFile interface for sharing purposes? I suspect that only name, contents and may be MIME type are required For android, the uri is needed. For web, the original file handle provided by the file dialog is required. We can't use just the file path on web.

It is limiting as only allows to share physically existing file. I am pretty sure these platforms support sharing a stream of data (needs to be double checked).

Use case, app generates a 2GB data stream of a videl and user wants to share it to another application. Saving to a physical file and then sharing would be consuming (and problematic on web).

maxkatz6 avatar Oct 23 '22 08:10 maxkatz6

Stream based sharing API also aligns with our plans to have a fallback solution, when "save file picker" is not available on system (iOS and non-Chrome Browser). See Task Export(FilePickerOptions options, Func<Stream, FilePickerFileType, Task> writer); part: https://github.com/AvaloniaUI/Avalonia/issues/7234

maxkatz6 avatar Oct 23 '22 08:10 maxkatz6

You can test this PR using the following package version. 11.0.999-cibuild0025246-beta. (feed url: https://nuget.avaloniaui.net/repository/avalonia-all/index.json) [PRBUILDID]

avaloniaui-team avatar Oct 23 '22 19:10 avaloniaui-team

You can test this PR using the following package version. 11.0.999-cibuild0025290-beta. (feed url: https://nuget.avaloniaui.net/repository/avalonia-all/index.json) [PRBUILDID]

avaloniaui-team avatar Oct 24 '22 11:10 avaloniaui-team