jamesmontemagno / PermissionsPlugin

Check and Request Permissions Plugin for Xamarin and Windows
MIT License
282 stars 85 forks source link

RequestPermissionAsync #190

Open sisaacks opened 3 years ago

sisaacks commented 3 years ago

If you are creating an issue for a BUG please fill out this information. If you are asking a question or requesting a feature you can delete the sections below.

Failure to fill out this information will result in this issue being closed. If you post a full stack trace in a bug it will be closed, please post it to http://gist.github.com and then post the link here.

Bug Information

Version Number of Plugin: Plugin.Permissions (6.0.1) Device Tested On: Ssamsung SM-G975U1 Simulator Tested On: NA Version of VS: 2019 Version of Xamarin: 4.8.0.1821 Versions of other things you are using:

Steps to reproduce the Behavior

I call to location permissions. If I do not have the permission I request the permission. If I have to request the permission, its almost like my thread is abandoned....my busy spinner stops....the method returns....and 4-5 seconds later Latitude and Altitude are filled in. If I already have the permission...and do not have to request the permission....the busy spinner shows the entire time and the values are filled in.

I check the value of IsBusy and for some reason it is set to False. Even after the permission is granted if I set IsBusy to true...when I call to get the location, IsBusy will be set to false. I have no idea why.

Expected Behavior

I would expect after requesting location permissions, while I am retrieving the location the busy spinner will show.

Actual Behavior

When I call to request permissions....the busy spinner stops. Even setting it to true again, calling for the location will again set it to false. I am not sure why

Code snippet

if(!IsBusy) { IsBusy = true;

            await Task.Delay(150);

            if(await CheckLocationServices(shouldShowCheckBox:false, overrideUser:true))
            {
                Plugin.Permissions.Abstractions.PermissionStatus permissionStatus = await PermissionCheck(Permission.Location);

                if (permissionStatus == Plugin.Permissions.Abstractions.PermissionStatus.Granted)
                {
                    Location location = await LocationUtility.GetCurrentLocation();

                    if (location != null)
                    {
                        await DoWork();
                    }
                }

public async Task PermissionCheck(Permission permission) { PermissionStatus status = PermissionStatus.Unknown;

        switch (permission)
        {
            case Permission.Location:
                status = await CrossPermissions.Current.CheckPermissionStatusAsync<LocationPermission>();
                break;

            case Permission.Storage:
                status = await CrossPermissions.Current.CheckPermissionStatusAsync<StoragePermission>();
                break;
        }

        if (status != PermissionStatus.Granted)
        {
            switch (permission)
            {
                case Permission.Location:
                    status = await CrossPermissions.Current.RequestPermissionAsync<LocationPermission>();
                    break;

                case Permission.Storage:
                    status = await CrossPermissions.Current.RequestPermissionAsync<StoragePermission>();
                    break;
            }
        }

        return status;
    }
            }

            IsBusy = false;
        }

public static async Task GetCurrentLocation() { Location location = null; string title = "Geo Location Services";

        try
        {
            GeolocationRequest request = new GeolocationRequest(GeolocationAccuracy.Best, TimeSpan.FromSeconds(15));
            location = await Geolocation.GetLocationAsync(request);

            // if altitude did not come back the first time...lets try again
            if(location != null && !location.Altitude.HasValue)
            {
                request = new GeolocationRequest(GeolocationAccuracy.Best, TimeSpan.FromSeconds(15));
                location = await Geolocation.GetLocationAsync(request);
            }
        }
        catch (FeatureNotSupportedException fnsEx)
        {
            App.logger.Error($"Location Utility GetCurrentLocation: {fnsEx.Message.ToString()}");
            Device.BeginInvokeOnMainThread(() => 
            {
                PopUpUtility.ShowPopUp(title, "Feature not supported.");
            });
        }
        catch (FeatureNotEnabledException fneEx)
        {
            App.logger.Error($"Location Utility GetCurrentLocation: {fneEx.Message.ToString()}");
            Device.BeginInvokeOnMainThread(() => { PopUpUtility.ShowPopUp("Location Services", "Location Services are required for communication via Bluetooth to exterior devices and device retrieval of altitude and latitude."); });
        }
        catch (PermissionException pEx)
        {
            App.logger.Error($"Location Utility GetCurrentLocation: {pEx.Message.ToString()}");
            Device.BeginInvokeOnMainThread(() => { PopUpUtility.ShowPopUp(title, "Feature permission not granted."); });
        }
        catch (Exception ex)
        {
            App.logger.Error($"Location Utility GetCurrentLocation: {ex.Message.ToString()}");
            Device.BeginInvokeOnMainThread(() => { PopUpUtility.ShowPopUp(title, "Feature not available."); });
        }

        return location;

    }

Screenshots

sisaacks commented 3 years ago

Are there any updates to this?