Exemplo n.º 1
0
        public GpsViewModel(IGpsManager manager, IUserDialogs dialogs)
        {
            this.manager    = manager;
            this.IsUpdating = this.manager.IsListening;

            this.WhenAnyValue(x => x.UseBackground)
            .Subscribe(x => this.Access = this.manager.GetCurrentStatus(this.UseBackground).ToString());

            this.WhenAnyValue(x => x.IsUpdating)
            .Select(x => x ? "Stop Listening" : "Start Updating")
            .ToPropertyEx(this, x => x.ListenerText);

            this.GetCurrentPosition = ReactiveCommand.CreateFromTask(async _ =>
            {
                var result = await dialogs.RequestAccess(() => this.manager.RequestAccess(true));
                if (!result)
                {
                    return;
                }

                var reading = await this.manager.GetLastReading();
                if (reading == null)
                {
                    await dialogs.Alert("Could not getting GPS coordinates");
                }
                else
                {
                    this.SetValues(reading);
                }
            });
            this.BindBusyCommand(this.GetCurrentPosition);

            this.SelectPriority = ReactiveCommand.Create(() => dialogs.ActionSheet(
                                                             new ActionSheetConfig()
                                                             .SetTitle("Select Priority/Desired Accuracy")
                                                             .Add("Highest", () => this.Priority = GpsPriority.Highest)
                                                             .Add("Normal", () => this.Priority  = GpsPriority.Normal)
                                                             .Add("Low", () => this.Priority     = GpsPriority.Low)
                                                             .AddCancel()
                                                             ));

            this.ToggleUpdates = ReactiveCommand.CreateFromTask(
                async() =>
            {
                if (this.manager.IsListening)
                {
                    await this.manager.StopListener();
                    this.gpsListener?.Dispose();
                }
                else
                {
                    var result = await dialogs.RequestAccess(() => this.manager.RequestAccess(this.UseBackground));
                    if (!result)
                    {
                        await dialogs.Alert("Insufficient permissions");
                        return;
                    }

                    var request = new GpsRequest
                    {
                        UseBackground = this.UseBackground,
                        Priority      = this.Priority,
                    };
                    if (IsInterval(this.DesiredInterval))
                    {
                        request.Interval = ToInterval(this.DesiredInterval);
                    }

                    if (IsInterval(this.ThrottledInterval))
                    {
                        request.ThrottledInterval = ToInterval(this.ThrottledInterval);
                    }

                    await this.manager.StartListener(request);
                }
                this.IsUpdating = this.manager.IsListening;
            },
                this.WhenAny(
                    x => x.IsUpdating,
                    x => x.DesiredInterval,
                    x => x.ThrottledInterval,
                    (u, i, t) =>
            {
                if (u.GetValue())
                {
                    return(true);
                }

                var isdesired   = IsInterval(i.GetValue());
                var isthrottled = IsInterval(t.GetValue());

                if (isdesired && isthrottled)
                {
                    var desired  = ToInterval(i.GetValue());
                    var throttle = ToInterval(t.GetValue());
                    if (throttle.TotalSeconds >= desired.TotalSeconds)
                    {
                        return(false);
                    }
                }
                return(true);
            }
                    )
                );

            this.UseRealtime = ReactiveCommand.Create(() =>
            {
                var rt = GpsRequest.Realtime(false);
                this.ThrottledInterval = String.Empty;
                this.DesiredInterval   = rt.Interval.TotalSeconds.ToString();
                this.Priority          = rt.Priority;
            });

            this.RequestAccess = ReactiveCommand.CreateFromTask(async() =>
            {
                var access  = await this.manager.RequestAccess(this.UseBackground);
                this.Access = access.ToString();
            });
            this.BindBusyCommand(this.RequestAccess);
        }
Exemplo n.º 2
0
        public GpsViewModel(IGpsManager manager, IUserDialogs dialogs)
        {
            this.manager    = manager;
            this.IsUpdating = this.manager.IsListening;

            this.WhenAnyValue(x => x.UseBackground)
            .Subscribe(x => this.Access = this.manager.GetCurrentStatus(this.UseBackground).ToString());

            this.WhenAnyValue(x => x.IsUpdating)
            .Select(x => x ? "Stop Listening" : "Start Updating")
            .ToPropertyEx(this, x => x.ListenerText);

            this.GetCurrentPosition = ReactiveCommand.CreateFromTask(async _ =>
            {
                var result = await dialogs.RequestAccess(() => this.manager.RequestAccess(true));
                if (!result)
                {
                    return;
                }

                var reading = await this.manager.GetLastReading();
                if (reading == null)
                {
                    dialogs.Alert("Could not getting GPS coordinates");
                }
                else
                {
                    this.SetValues(reading);
                }
            });
            this.BindBusyCommand(this.GetCurrentPosition);

            this.SelectPriority = ReactiveCommand.Create(() => dialogs.ActionSheet(
                                                             new ActionSheetConfig()
                                                             .SetTitle("Select Priority/Desired Accuracy")
                                                             .Add("Highest", () => this.Priority = GpsPriority.Highest)
                                                             .Add("Normal", () => this.Priority  = GpsPriority.Normal)
                                                             .Add("Low", () => this.Priority     = GpsPriority.Low)
                                                             .SetCancel()
                                                             ));

            this.ToggleUpdates = ReactiveCommand.CreateFromTask(
                async() =>
            {
                if (this.manager.IsListening)
                {
                    await this.manager.StopListener();
                    this.gpsListener?.Dispose();
                }
                else
                {
                    var result = await dialogs.RequestAccess(() => this.manager.RequestAccess(this.UseBackground));
                    if (!result)
                    {
                        dialogs.Alert("Insufficient permissions");
                        return;
                    }

                    var request = new GpsRequest
                    {
                        UseBackground = this.UseBackground,
                        Priority      = this.Priority
                    };
                    var meters = ToDeferred(this.DeferredMeters);
                    if (meters > 0)
                    {
                        request.DeferredDistance = Distance.FromMeters(meters);
                    }

                    var secs = ToDeferred(this.DeferredSeconds);
                    if (secs > 0)
                    {
                        request.DeferredTime = TimeSpan.FromSeconds(secs);
                    }

                    await this.manager.StartListener(request);
                }
                this.IsUpdating = this.manager.IsListening;
            },
                this.WhenAny(
                    x => x.IsUpdating,
                    x => x.DeferredMeters,
                    x => x.DeferredSeconds,
                    (u, m, s) =>
                    u.GetValue() ||
                    (
                        ToDeferred(m.GetValue()) >= 0 &&
                        ToDeferred(s.GetValue()) >= 0
                    )
                    )
                );

            this.RequestAccess = ReactiveCommand.CreateFromTask(async() =>
            {
                var access  = await this.manager.RequestAccess(this.UseBackground);
                this.Access = access.ToString();
            });
            this.BindBusyCommand(this.RequestAccess);
        }