public GpsViewModel(IGpsManager manager, IDialogs dialogs) { this.manager = manager; this.IsUpdating = this.manager.IsListening; this.WhenAnyValue(x => x.UseBackground) .Subscribe(x => this.Access = this.manager.GetCurrentStatus( new GpsRequest { UseBackground = 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(new GpsRequest())); 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); ReactiveCommand.Create(() => dialogs.ActionSheet( "Select Priority", false, ("Highest", () => this.Priority = GpsPriority.Highest), ("Normal", () => this.Priority = GpsPriority.Normal), ("Low", () => this.Priority = GpsPriority.Low) )); 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(new GpsRequest { UseBackground = 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(new GpsRequest { UseBackground = this.UseBackground }); this.Access = access.ToString(); }); this.BindBusyCommand(this.RequestAccess); }
public GpsViewModel(IGpsManager manager, IDialogs dialogs) { this.manager = manager; var l = this.manager.CurrentListener; this.IsUpdating = l != null; this.UseBackground = l?.UseBackground ?? true; this.Priority = l?.Priority ?? GpsPriority.Normal; this.DesiredInterval = l?.Interval.TotalSeconds.ToString() ?? String.Empty; this.ThrottledInterval = l?.ThrottledInterval?.TotalSeconds.ToString() ?? String.Empty; this.MinimumDistanceMeters = l?.MinimumDistance?.TotalMeters.ToString() ?? String.Empty; this.NotificationTitle = manager.Title; this.NotificationMessage = manager.Message; this.WhenAnyValue(x => x.IsUpdating) .Select(x => x ? "Stop Listening" : "Start Updating") .ToPropertyEx(this, x => x.ListenerText); this.WhenAnyValue(x => x.NotificationTitle) .Skip(1) .Subscribe(x => this.manager.Title = x) .DisposedBy(this.DestroyWith); this.WhenAnyValue(x => x.NotificationMessage) .Skip(1) .Subscribe(x => this.manager.Message = x) .DisposedBy(this.DestroyWith); this.GetCurrentPosition = this.CreateOneReading(dialogs, LocationRetrieve.Current); this.GetLastReading = this.CreateOneReading(dialogs, LocationRetrieve.Last); this.GetLastOrCurrent = this.CreateOneReading(dialogs, LocationRetrieve.LastOrCurrent); ReactiveCommand.Create(() => dialogs.ActionSheet( "Select Priority", false, ("Highest", () => this.Priority = GpsPriority.Highest), ("Normal", () => this.Priority = GpsPriority.Normal), ("Low", () => this.Priority = GpsPriority.Low) )); this.ToggleUpdates = ReactiveCommand.CreateFromTask( async() => { if (this.manager.CurrentListener != null) { await this.manager.StopListener(); this.gpsListener?.Dispose(); } else { var result = await dialogs.RequestAccess(async() => { var access = await this.manager.RequestAccess(new GpsRequest { UseBackground = this.UseBackground }); this.Access = access.ToString(); return(access); }); if (!result) { await dialogs.Alert("Insufficient permissions"); return; } var request = new GpsRequest { UseBackground = this.UseBackground, Priority = this.Priority, }; if (IsNumeric(this.DesiredInterval)) { request.Interval = ToInterval(this.DesiredInterval); } if (IsNumeric(this.ThrottledInterval)) { request.ThrottledInterval = ToInterval(this.ThrottledInterval); } if (IsNumeric(this.MinimumDistanceMeters)) { request.MinimumDistance = Distance.FromMeters(Int32.Parse(this.MinimumDistanceMeters)); } await this.manager.StartListener(request); } this.IsUpdating = this.manager.CurrentListener != null; }, this.WhenAny( x => x.IsUpdating, x => x.DesiredInterval, x => x.ThrottledInterval, x => x.MinimumDistanceMeters, (u, i, t, d) => { if (u.GetValue()) { return(true); } var isdesired = IsNumeric(i.GetValue()); var isthrottled = IsNumeric(t.GetValue()); var ismindist = IsNumeric(d.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(new GpsRequest { UseBackground = this.UseBackground }); this.Access = access.ToString(); }); this.BindBusyCommand(this.RequestAccess); }