dimonovdd / Xamarin.MediaGallery

This plugin is designed to picking and save images and video files from native gallery of Android and iOS devices and capture photos
MIT License
154 stars 19 forks source link

Added method overloading for Xamarin.Android #43

Closed dimonovdd closed 3 years ago

dimonovdd commented 3 years ago

Description

Added methods overloading for Xamarin.Android with activity parameter

Related to issue

API Changes

Added:

public static Task<IEnumerable<IMediaFile>> PickAsync(Activity activity, MediaPickRequest request, CancellationToken token = default);

public static async Task SaveAsync(Activity activity, MediaFileType type, byte[] data, string fileName);

public static async Task SaveAsync(Activity activity, MediaFileType type, string filePath);

public static async Task SaveAsync(Activity activity, MediaFileType type, Stream fileStream, string fileName);

PR Checklist

dimonovdd commented 3 years ago

@pictos @ahoefling I'm thinking of another way:

public static partial class Platform
{
    static Func<Activity> getCurrentActivity;

    public static void Init(Func<Activity> getCurrentActivity, Bundle bundle)
         => getCurrentActivity = getCurrentActivity;

    internal static Activity AppActivity
        => getCurrentActivity?.Invoke()
            ?? throw ExceptionHelper.ActivityNotDetected;
}
pictos commented 3 years ago

@pictos @ahoefling I'm thinking of another way:

public static partial class Platform
{
    static Func<Activity> getCurrentActivity;

    public static void Init(Func<Activity> getCurrentActivity, Bundle bundle)
         => getCurrentActivity = getCurrentActivity;

    internal static Activity AppActivity
        => getCurrentActivity?.Invoke()
            ?? throw ExceptionHelper.ActivityNotDetected;
}

Not sure if you need to go with a fancy Func for that... In Forms apps, the Activity will be always the same

pictos commented 3 years ago

Also, you have a XE dependency, so you don't need to capture the Activity in your Platform class

dimonovdd commented 3 years ago

@pictos This issue is mainly related to xamarin.Android apps

pictos commented 3 years ago

@dimonovdd

@pictos This issue is mainly related to xamarin.Android apps

I see, in that case, I believe that you can use the XE to grab the current activity since that implements all Activity's lifecycle

dimonovdd commented 3 years ago

I don't quite understand what you mean

pictos commented 3 years ago

@dimonovdd your lib has a reference to Xamarin.Essentials. So instead of collecting the activity let the XE handle that for you... I did this on your code and worked.

// on your lib
public static void Init(Activity activity, Bundle bundle) { }
 internal static Activity AppActivity => Xamarin.Essentials.Platform.CurrentActivity;

// Sample
base.OnCreate(savedInstanceState);
//NativeMedia.Platform.Init(this, savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
dimonovdd commented 3 years ago

@pictos Thank you very much. I completely forgot about the existence of Xamarin.Essentials.Platform.CurrentActivity property.