Ejemplo n.º 1
0
        public AddGeofenceViewModel(IGeofenceManager geofences,
                                    IUserDialogs dialogs,
                                    IViewModelManager viewModelMgr,
                                    IGeolocator geolocator)
        {
            this.Add = ReactiveCommand.CreateAsyncTask(
                this.WhenAny(
                    x => x.RadiusMeters,
                    x => x.Latitude,
                    x => x.Longitude,
                    x => x.Identifer,
                    (radius, lat, lng, id) =>
                    {
                        if (radius.Value < 100)
                            return false;

                        if (lat.Value < -90 || lat.Value > 90)
                            return false;

                        if (lng.Value < -180 || lng.Value > 180)
                            return false;

                        if (id.Value.IsEmpty())
                            return false;

                        return true;
                    }
                ),
                async x =>
                {
                    geofences.StartMonitoring(new GeofenceRegion
                    {
                        Identifier = this.Identifer,
                        Center = new Position(this.Latitude, this.Longitude),
                        Radius = Distance.FromMeters(this.RadiusMeters)
                    });
                    await viewModelMgr.PopNav();
                }
            );
            this.UseCurrentLocation = ReactiveCommand.CreateAsyncTask(async x =>
            {
                try
                {
                    var current = await geolocator.GetPositionAsync(5000);
                    this.Latitude = current.Latitude;
                    this.Longitude = current.Longitude;
                }
                catch
                {
                }
            });
        }