Пример #1
0
        static async Task <Location> PlatformLocationAsync(GeolocationRequest request, CancellationToken cancellationToken)
        {
            await Permissions.EnsureGrantedAsync <Permissions.LocationWhenInUse>();

            Locator service = null;
            var     gps     = Platform.GetFeatureInfo <bool>("location.gps");
            var     wps     = Platform.GetFeatureInfo <bool>("location.wps");

            if (gps)
            {
                if (wps)
                {
                    service = new Locator(LocationType.Hybrid);
                }
                else
                {
                    service = new Locator(LocationType.Gps);
                }
            }
            else
            {
                if (wps)
                {
                    service = new Locator(LocationType.Wps);
                }
                else
                {
                    service = new Locator(LocationType.Passive);
                }
            }

            var tcs = new TaskCompletionSource <bool>();

            cancellationToken = Utils.TimeoutToken(cancellationToken, request.Timeout);
            cancellationToken.Register(() =>
            {
                service?.Stop();
                tcs.TrySetResult(false);
            });

            double KmToMetersPerSecond(double km) => km * 0.277778;

            service.LocationChanged += (s, e) =>
            {
                if (e.Location != null)
                {
                    lastKnownLocation.Accuracy  = e.Location.Accuracy;
                    lastKnownLocation.Altitude  = e.Location.Altitude;
                    lastKnownLocation.Course    = e.Location.Direction;
                    lastKnownLocation.Latitude  = e.Location.Latitude;
                    lastKnownLocation.Longitude = e.Location.Longitude;
                    lastKnownLocation.Speed     = KmToMetersPerSecond(e.Location.Speed);
                    lastKnownLocation.Timestamp = e.Location.Timestamp;
                }
                service?.Stop();
                tcs.TrySetResult(true);
            };
            service.Start();

            await tcs.Task;

            return(lastKnownLocation);
        }
Пример #2
0
        static async Task <FileResult> PhotoAsync(MediaPickerOptions options, bool photo, bool pickExisting)
        {
            var sourceType = pickExisting ? UIImagePickerControllerSourceType.PhotoLibrary : UIImagePickerControllerSourceType.Camera;
            var mediaType  = photo ? UTType.Image : UTType.Movie;

            if (!UIImagePickerController.IsSourceTypeAvailable(sourceType))
            {
                throw new FeatureNotSupportedException();
            }
            if (!UIImagePickerController.AvailableMediaTypes(sourceType).Contains(mediaType))
            {
                throw new FeatureNotSupportedException();
            }

            if (!photo && !pickExisting)
            {
                await Permissions.EnsureGrantedAsync <Permissions.Microphone>();
            }

            // Check if picking existing or not and ensure permission accordingly as they can be set independently from each other
            if (pickExisting && !Platform.HasOSVersion(11, 0))
            {
                await Permissions.EnsureGrantedAsync <Permissions.Photos>();
            }

            if (!pickExisting)
            {
                await Permissions.EnsureGrantedAsync <Permissions.Camera>();
            }

            var vc = Platform.GetCurrentViewController(true);

            picker               = new UIImagePickerController();
            picker.SourceType    = sourceType;
            picker.MediaTypes    = new string[] { mediaType };
            picker.AllowsEditing = false;
            if (!photo && !pickExisting)
            {
                picker.CameraCaptureMode = UIImagePickerControllerCameraCaptureMode.Video;
            }

            if (!string.IsNullOrWhiteSpace(options?.Title))
            {
                picker.Title = options.Title;
            }

            if (DeviceInfo.Idiom == DeviceIdiom.Tablet && picker.PopoverPresentationController != null && vc.View != null)
            {
                picker.PopoverPresentationController.SourceRect = vc.View.Bounds;
            }

            var tcs = new TaskCompletionSource <FileResult>(picker);

            picker.Delegate = new PhotoPickerDelegate
            {
                CompletedHandler = async info =>
                {
                    GetFileResult(info, tcs);
                    await vc.DismissViewControllerAsync(true);
                }
            };

            if (picker.PresentationController != null)
            {
                picker.PresentationController.Delegate = new PhotoPickerPresentationControllerDelegate
                {
                    CompletedHandler = info => GetFileResult(info, tcs)
                };
            }

            await vc.PresentViewControllerAsync(picker, true);

            var result = await tcs.Task;

            picker?.Dispose();
            picker = null;

            return(result);
        }