Example #1
0
        public MainViewModel()
        {
            var current = this.geofences.MonitoredRegions.FirstOrDefault();

            if (current != null)
            {
                this.CenterLatitude  = current.Center.Latitude;
                this.CenterLongitude = current.Center.Longitude;
                this.DistanceMeters  = current.Radius.TotalMeters;
                this.HasGeofence     = true;
            }

            this.StopGeofence = ReactiveCommand.Create(() =>
            {
                this.geofences.StopAllMonitoring();
                this.HasGeofence = false;
            },
                                                       this.WhenAny(
                                                           x => x.HasGeofence,
                                                           x => x.Value
                                                           ));

            this.SetGeofence = ReactiveCommand.Create(() =>
            {
                try
                {
                    this.geofences.StopAllMonitoring();

                    var radius = Distance.FromMeters(this.DistanceMeters.Value);
                    var center = new Plugin.Geofencing.Position(this.CenterLatitude.Value, this.CenterLongitude.Value);

                    this.geofences.StartMonitoring(new GeofenceRegion("plugintest", center, radius));
                    this.HasGeofence = true;

                    UserDialogs.Instance.Alert("Geofence set");
                }
                catch (Exception ex)
                {
                    UserDialogs.Instance.Alert(ex.ToString());
                }
            },
                                                      this.WhenAny(
                                                          x => x.CenterLatitude,
                                                          x => x.CenterLongitude,
                                                          x => x.DistanceMeters,
                                                          x => x.HasGeofence,
                                                          (latitude, longitude, dist, hasGeo) =>
                                                          latitude.Value > -90 &&
                                                          latitude.Value <90 &&
                                                                          longitude.Value> -180 &&
                                                          longitude.Value < 180 &&
                                                          dist.Value > 0 &&
                                                          dist.Value < 3000 &&
                                                          !hasGeo.Value
                                                          ));
            //this.WhenAny(
            //        x => x.CenterLatitude,
            //        x => x.CenterLongitude,
            //        x => x.DistanceMeters,
            //        x => x.HasGeofence,
            //        (latitude, longitude, dist, hasGeo) =>
            //            latitude.Value > -180 &&
            //            latitude.Value < 180 &&
            //            longitude.Value > -90 &&
            //            longitude.Value < 90 &&
            //            dist.Value > 0 &&
            //            dist.Value < 3000 &&
            //            !hasGeo.Value
            //    )
            //    .Subscribe(x =>
            //    {
            //        var msg = $"LAT: {this.CenterLatitude} - LNG: {this.CenterLongitude} - D: {this.DistanceMeters} - HG: {this.HasGeofence}";
            //        UserDialogs.Instance.Alert(msg);
            //    });


            this.RequestStatus = ReactiveCommand.CreateFromTask(async ct =>
            {
                var region = this.geofences.MonitoredRegions.First();
                var result = await this.geofences.RequestState(region, ct).ConfigureAwait(false);
                UserDialogs.Instance.Alert("Geofence Status: " + result.ToString(), "Status");
            },
                                                                this.WhenAny(
                                                                    x => x.HasGeofence,
                                                                    x => x.Value
                                                                    ));


            this.UseCurrentGps = ReactiveCommand.CreateFromTask(async ct =>
            {
                try
                {
                    var pos = await this.gps.GetPositionAsync(token: ct).ConfigureAwait(false);
                    if (pos != null)
                    {
                        Device.BeginInvokeOnMainThread(() =>
                        {
                            this.CenterLatitude  = pos.Latitude;
                            this.CenterLongitude = pos.Longitude;
                        });
                    }
                }
                catch (Exception ex)
                {
                    UserDialogs.Instance.Alert("Error getting current location - " + ex);
                }
            });
        }