예제 #1
0
        private async void SendArmedTrap()
        {
            try
            {
                progressDialog = ProgressDialog.Show(Activity, Resources.GetString(MyTrap.Droid.Resource.String.loading), Resources.GetString(MyTrap.Droid.Resource.String.arming_trap));

                ArmedTrapApiRequest armedTrap = new ArmedTrapApiRequest();

                armedTrap.NameKey   = selectedTrap.NameKey;
                armedTrap.Latitude  = (float)lastLocation.Latitude;
                armedTrap.Longitude = (float)lastLocation.Longitude;

                var response = await TrapApiService.Arm(armedTrap);

                if (ResponseValidator.Validate(response, Activity))
                {
                    ShowAlertArmedTrapSuccessfuly();
                }

                progressDialog.Cancel();
            }
            catch (Exception exception)
            {
                InsightsUtils.LogException(exception);
            }
        }
        private async void btnPlaceTrap_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            var dialog = new MessageDialog("You want to place the trap at the current position?");

            dialog.Title = "Place Trap";

            dialog.Commands.Add(new UICommand {
                Label = "Yes", Id = 0
            });
            dialog.Commands.Add(new UICommand {
                Label = "No", Id = 1
            });

            var res = await dialog.ShowAsync();

            if ((int)res.Id == 0)
            {
                ArmedTrapApiRequest armedTrap = new ArmedTrapApiRequest();

                armedTrap.NameKey   = TRAP_NAMEKEY_SELECTED;
                armedTrap.Latitude  = LAST_LATITUDE;
                armedTrap.Longitude = LAST_LONGITUDE;

                var response = await TrapApiService.Arm(armedTrap);

                if (ResponseValidator.Validate(response))
                {
                    var dialogSuccess = new MessageDialog("Trap was placed. You will be notified when someone is gaught!");

                    dialogSuccess.Title = "Placed Trap!";

                    dialogSuccess.Commands.Add(new UICommand {
                        Label = "Ok", Id = 0
                    });

                    res = await dialogSuccess.ShowAsync();

                    AppShell.Current.AppFrame.Navigate(typeof(HomePage));
                }
            }
        }
        private async void LoadUserTraps()
        {
            try
            {
                List <ArmedTrapApiResult> response = await TrapApiService.ListArmed();

                if (response != null && response.Count > 0)
                {
                    List <BasicGeoposition> basicPositions = new List <BasicGeoposition>();

                    foreach (var trapPlanted in response)
                    {
                        RandomAccessStreamReference mapIconStreamReference = null;

                        if (trapPlanted.NameKey == ETrap.BEAR.ToString())
                        {
                            mapIconStreamReference = RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx:///Assets/trap_bear_trap_selected_icon.png"));
                        }
                        else if (trapPlanted.NameKey == ETrap.MINE.ToString())
                        {
                            mapIconStreamReference = RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx:///Assets/trap_mine_trap_selected_icon.png"));
                        }
                        else if (trapPlanted.NameKey == ETrap.PIT.ToString())
                        {
                            mapIconStreamReference = RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx:///Assets/trap_pit_trap_selected_icon.png"));
                        }
                        else if (trapPlanted.NameKey == ETrap.DOGS.ToString())
                        {
                            mapIconStreamReference = RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx:///Assets/trap_dogs_trap_selected_icon.png"));
                        }
                        else
                        {
                            continue;
                        }

                        MapIcon mapIcon = new MapIcon();

                        mapIcon.Location = new Geopoint(new BasicGeoposition()
                        {
                            Latitude = trapPlanted.Latitude, Longitude = trapPlanted.Longitude
                        });
                        mapIcon.NormalizedAnchorPoint = new Point(0.5, 1.0);
                        mapIcon.Title  = trapPlanted.Date.ToLocalTime().ToString();
                        mapIcon.Image  = mapIconStreamReference;
                        mapIcon.ZIndex = 0;

                        map.MapElements.Add(mapIcon);

                        basicPositions.Add(new BasicGeoposition()
                        {
                            Latitude = trapPlanted.Latitude, Longitude = trapPlanted.Longitude
                        });
                    }

                    if (response.Count > 1)
                    {
                        await map.TrySetViewBoundsAsync(GeoboundingBox.TryCompute(basicPositions), null, MapAnimationKind.Default);
                    }
                    else
                    {
                        map.Center = new Geopoint(new BasicGeoposition()
                        {
                            Latitude  = response[0].Latitude,
                            Longitude = response[0].Longitude
                        });
                    }
                }
                else
                {
                    var dialog = new MessageDialog("You didn't put traps :(");

                    dialog.Title = "Warning";

                    dialog.Commands.Add(new UICommand {
                        Label = "Ok", Id = 0
                    });

                    var res = await dialog.ShowAsync();

                    if ((int)res.Id == 0)
                    {
                        AppShell.Current.AppFrame.Navigate(typeof(HomePage));
                    }
                }
            }
            catch (Exception exception)
            {
            }
        }
        private async void LoadUserTraps()
        {
            try
            {
                List <ArmedTrapApiResult> response = await TrapApiService.ListArmed();

                if (response != null && response.Count > 0)
                {
                    var boundsBuilders = new LatLngBounds.Builder();

                    foreach (var trapPlanted in response)
                    {
                        var idIcon = 0;

                        if (trapPlanted.NameKey == ETrap.BEAR.ToString())
                        {
                            idIcon = MyTrap.Droid.Resource.Drawable.trap_bear_trap_selected_icon;
                        }
                        else if (trapPlanted.NameKey == ETrap.MINE.ToString())
                        {
                            idIcon = MyTrap.Droid.Resource.Drawable.trap_mine_trap_selected_icon;
                        }
                        else if (trapPlanted.NameKey == ETrap.PIT.ToString())
                        {
                            idIcon = MyTrap.Droid.Resource.Drawable.trap_pit_trap_selected_icon;
                        }
                        else if (trapPlanted.NameKey == ETrap.DOGS.ToString())
                        {
                            idIcon = MyTrap.Droid.Resource.Drawable.trap_dogs_trap_selected_icon;
                        }
                        //else if (trapPlanted.NameKey == ETrap.LOOP.ToString())
                        else
                        {
                            //idIcon = MyTrap.Droid.Resource.Drawable.trap_loop_trap_icon;
                            continue;
                        }

                        var position = new LatLng(trapPlanted.Latitude, trapPlanted.Longitude);

                        googleMap.AddMarker(new MarkerOptions()
                                            .SetPosition(position)
                                            .SetTitle(trapPlanted.Date.ToLocalTime().ToString(Resources.GetString(MyTrap.Droid.Resource.String.format_date_hour_minute)))
                                            .SetIcon(BitmapDescriptorFactory.FromResource(idIcon))
                                            );

                        int fillColor = Int32.Parse("80f15f4b", System.Globalization.NumberStyles.HexNumber);

                        CircleOptions circleOptions = new CircleOptions();

                        circleOptions.InvokeCenter(position);
                        circleOptions.InvokeFillColor(fillColor);
                        circleOptions.InvokeStrokeWidth(3);

                        googleMap.AddCircle(circleOptions);

                        boundsBuilders.Include(new LatLng(trapPlanted.Latitude, trapPlanted.Longitude));
                    }

                    if (response.Count > 1)
                    {
                        var bounds = boundsBuilders.Build();

                        googleMap.MoveCamera(CameraUpdateFactory.NewLatLngBounds(bounds, 100));
                    }
                    else
                    {
                        googleMap.MoveCamera(CameraUpdateFactory.NewLatLngZoom(new LatLng(response[0].Latitude, response[0].Longitude), 16));
                    }
                }
                else
                {
                    HomeActivity homeActivity = (HomeActivity)Activity;

                    new AlertDialog.Builder(Activity)
                    .SetIcon(Android.Resource.Drawable.IcDialogAlert)
                    .SetTitle(MyTrap.Droid.Resource.String.alert_title_error)
                    .SetMessage(MyTrap.Droid.Resource.String.no_traps_armed)
                    .SetPositiveButton(MyTrap.Droid.Resource.String.ok, new MyDialogClickListener(homeActivity))
                    .SetCancelable(false)
                    .Show();
                }

                progressDialog.Cancel();
            }
            catch (Exception exception)
            {
                InsightsUtils.LogException(exception);
            }
        }