Ejemplo n.º 1
0
        public GeofenceEditPage(Geofence geofence)
        {
            data   = geofence;
            Title  = geofence.Name;
            layout = new StackLayout()
            {
                Spacing = 15
            };
            layout.Padding = new Thickness(20, 20);
            entryLayout    = new StackLayout()
            {
                Orientation = StackOrientation.Vertical, HorizontalOptions = LayoutOptions.FillAndExpand
            };
            lblLayout = new StackLayout()
            {
                Orientation = StackOrientation.Vertical, VerticalOptions = LayoutOptions.FillAndExpand
            };
            namelbl = new Label()
            {
                Text = AppResources.NomLabel, VerticalTextAlignment = TextAlignment.Center
            };
            nameEntry = new Entry()
            {
                Placeholder = AppResources.NomPlaceHolder, HorizontalOptions = LayoutOptions.FillAndExpand
            };
            nameEntry.TextChanged += (object sender, TextChangedEventArgs e) =>
            {
                if (nameEntry.Text.Length > 50)
                {
                    nameEntry.Text = nameEntry.Text.Substring(0, 50);
                }
                else if (geofence.Name != nameEntry.Text && nameEntry.Text.Length < 50)
                {
                    geofence.Name = nameEntry.Text;
                    Title         = geofence.Name;
                    didChange     = true;
                }
            };
            lblLayout.Children.Add(namelbl);
            entryLayout.Children.Add(nameEntry);
            latitudelbl = new Label()
            {
                Text = AppResources.LatitudeLabel, VerticalTextAlignment = TextAlignment.Center
            };
            latitudeEntry = new Entry()
            {
                Text = geofence.Latitude + "", Placeholder = AppResources.LatitudePlaceHolder, HorizontalOptions = LayoutOptions.FillAndExpand
            };
            latitudeEntry.Unfocused += (sender, e) =>
            {
                double value = 0;
                if (double.TryParse(latitudeEntry.Text, out value) || string.IsNullOrEmpty(latitudeEntry.Text))
                {
                    if (geofence.Latitude > value || geofence.Latitude < value)
                    {
                        geofence.Latitude = value;
                        didChange         = true;
                    }
                }
            };
            lblLayout.Children.Add(latitudelbl);
            entryLayout.Children.Add(latitudeEntry);


            longitudelbl = new Label()
            {
                Text = AppResources.LongitudeLabel, VerticalTextAlignment = TextAlignment.Center
            };
            longitudeEntry = new Entry()
            {
                Text = geofence.Longitude + "", Placeholder = AppResources.LongitudePlaceHolder, HorizontalOptions = LayoutOptions.FillAndExpand
            };
            longitudeEntry.Unfocused += (sender, e) =>
            {
                double value = 0;
                if (double.TryParse(longitudeEntry.Text, out value) || string.IsNullOrEmpty(longitudeEntry.Text))
                {
                    if (geofence.Longitude > value || geofence.Longitude < value)
                    {
                        geofence.Longitude = value;
                        didChange          = true;
                    }
                }
            };
            lblLayout.Children.Add(longitudelbl);
            entryLayout.Children.Add(longitudeEntry);

            radiuslbl = new Label()
            {
                Text = AppResources.RayonLabel, VerticalTextAlignment = TextAlignment.Center
            };
            radiusPicker = new Picker()
            {
                HorizontalOptions = LayoutOptions.FillAndExpand
            };
            foreach (var rad in GeofenceManager.GeofenceRadius)
            {
                radiusPicker.Items.Add(rad.ToString());
            }
            radiusPicker.SelectedItem          = geofence.Radius.ToString();
            radiusPicker.SelectedIndexChanged += (sender, e) =>
            {
                double val = geofence.Radius;
                double.TryParse(radiusPicker.SelectedItem.ToString(), out val);
                val = val <= 0 ? geofence.Radius : val > ApplicationConst.GeofenceMaxRadius ? ApplicationConst.GeofenceMaxRadius : val;
                radiusPicker.SelectedItem = val.ToString();
                if (geofence.Radius > val || geofence.Radius < val)
                {
                    geofence.Radius = val;
                    didChange       = true;
                }
            };

            entryLayout.Children.Add(radiusPicker);
            lblLayout.Children.Add(radiuslbl);
            formLayout = new StackLayout()
            {
                Orientation = StackOrientation.Horizontal
            };
            formLayout.Children.Add(lblLayout);
            formLayout.Children.Add(entryLayout);

            typeLayout = new StackLayout()
            {
                Orientation = StackOrientation.Horizontal
            };
            typeEnterLayout = new StackLayout();
            typeEnterLayout.Children.Add(new Label()
            {
                Text = AppResources.ActionEntreeLabel, VerticalTextAlignment = TextAlignment.Center
            });

            EnterTypePicker = new Picker()
            {
                HorizontalOptions = LayoutOptions.FillAndExpand
            };
            var EnterSoustypePicker = new Picker()
            {
                HorizontalOptions = LayoutOptions.FillAndExpand
            };

            foreach (var t in ActionManager.GetActionTypes())
            {
                EnterTypePicker.Items.Add(t);
            }

            EnterTypePicker.SelectedIndexChanged += (sender, e) =>
            {
                string value = EnterTypePicker.Items[EnterTypePicker.SelectedIndex];

                EnterSoustypePicker.Items.Clear();
                foreach (var t in ActionManager.GetActionList().Where((Action a) => a.Category == value))
                {
                    EnterSoustypePicker.Items.Add(t.Description);
                }
                EnterSoustypePicker.IsEnabled = EnterSoustypePicker.Items.Count != 0;
                if (EnterSoustypePicker.IsEnabled)
                {
                    if (!string.IsNullOrEmpty(geofence.EnterActionName))
                    {
                        var list = ActionManager.GetActionList();
                        EnterSoustypePicker.SelectedIndex = EnterSoustypePicker.Items.IndexOf(list.First(a => a.Name == geofence.EnterActionName).Description);
                    }
                    else
                    {
                        EnterSoustypePicker.SelectedIndex = 0;
                    }
                }
            };


            EnterSoustypePicker.SelectedIndexChanged += (sender, e) =>
            {
                string value = EnterSoustypePicker.SelectedIndex >= 0 ? EnterSoustypePicker.Items[EnterSoustypePicker.SelectedIndex] : "";
                if (value != "")
                {
                    var action = ActionManager.GetActionList().FirstOrDefault((a) => a.Description == value);
                    if (geofence.EnterActionName != action.Name)
                    {
                        geofence.EnterActionName = action.Name;
                        didChange = true;
                    }
                }
                else
                {
                    geofence.EnterActionName = "";
                }
            };

            typeExitLayout = new StackLayout();
            typeExitLayout.Children.Add(new Label()
            {
                Text = AppResources.ActionSortieLabel, VerticalTextAlignment = TextAlignment.Center
            });

            var ExitTypePicker = new Picker()
            {
                HorizontalOptions = LayoutOptions.FillAndExpand
            };
            var ExitSoustypePicker = new Picker()
            {
                HorizontalOptions = LayoutOptions.FillAndExpand
            };

            foreach (var t in ActionManager.GetActionTypes())
            {
                ExitTypePicker.Items.Add(t);
            }
            ExitTypePicker.SelectedIndexChanged += (sender, e) =>
            {
                string value = ExitTypePicker.Items[ExitTypePicker.SelectedIndex];
                ExitSoustypePicker.Items.Clear();

                foreach (var t in ActionManager.GetActionList().Where(a => a.Category == value))
                {
                    ExitSoustypePicker.Items.Add(t.Description);
                }
                ExitSoustypePicker.IsEnabled = ExitSoustypePicker.Items.Count != 0;
                if (ExitSoustypePicker.IsEnabled)
                {
                    if (!string.IsNullOrEmpty(geofence.ExitActionName))
                    {
                        ExitSoustypePicker.SelectedIndex = ExitSoustypePicker.Items.IndexOf(ActionManager.GetActionList().First(a => a.Name == geofence.ExitActionName).Description);
                    }
                    else
                    {
                        ExitSoustypePicker.SelectedIndex = 0;
                    }
                }
            };


            ExitSoustypePicker.SelectedIndexChanged += (sender, e) =>
            {
                string value = ExitSoustypePicker.SelectedIndex >= 0 ? ExitSoustypePicker.Items[ExitSoustypePicker.SelectedIndex] : "";
                if (value != "")
                {
                    var action = ActionManager.GetActionList().FirstOrDefault((a) => a.Description == value);
                    if (geofence.ExitActionName != action.Name)
                    {
                        geofence.ExitActionName = action.Name;
                        didChange = true;
                    }
                }
                else
                {
                    geofence.ExitActionName = "";
                }
            };

            string category = ActionManager.Null;

            if (!string.IsNullOrEmpty(geofence.EnterActionName))
            {
                if (ActionManager.GetActionList().Any(a => geofence.EnterActionName == a.Name))
                {
                    var enterAction = ActionManager.GetActionList().First(a => geofence.EnterActionName == a.Name);
                    category = enterAction.Category;
                }
                else
                {
                    geofence.EnterActionName = string.Empty;
                    category = ActionManager.Null;
                }
            }
            EnterTypePicker.SelectedItem = category;


            category = ActionManager.Null;

            if (!string.IsNullOrEmpty(geofence.ExitActionName))
            {
                var list = (ActionManager.GetActionList());
                if (list.Any(a => geofence.ExitActionName == a.Name))
                {
                    var exitAction = list.First(a => geofence.ExitActionName == a.Name);
                    category = exitAction.Category;
                }
                else
                {
                    geofence.ExitActionName = string.Empty;
                    category = ActionManager.Null;
                }
            }
            ExitTypePicker.SelectedItem = category;

            typeEnterLayout.Children.Add(EnterTypePicker);
            typeEnterLayout.Children.Add(EnterSoustypePicker);

            typeExitLayout.Children.Add(ExitTypePicker);
            typeExitLayout.Children.Add(ExitSoustypePicker);

            typeLayout.Children.Add(typeEnterLayout);
            typeLayout.Children.Add(typeExitLayout);
            delaylbl = new Label()
            {
                Text = AppResources.DelaiNotificationLabel, VerticalTextAlignment = TextAlignment.Center
            };
            delayLayout = new StackLayout();
            delayLayout.Children.Add(delaylbl);

            delayPicker = new HMSTimePicker {
                HorizontalOptions = LayoutOptions.FillAndExpand
            };
            delayPicker.Time       = new TimeSpan(0, 0, (int)geofence.NotificationDelay);
            delayPicker.Unfocused += (sender, e) =>
            {
                var time = (uint)delayPicker.Time.TotalSeconds;
                if (time > uint.MaxValue)
                {
                    time = uint.MaxValue;
                }
                if (time != geofence.NotificationDelay)
                {
                    geofence.NotificationDelay = time;
                    didChange = true;
                }
            };
            delayLayout.Children.Add(delayPicker);



            map = new Button()
            {
                WidthRequest = App.Width / 2, Text = AppResources.CarteButton
            };
            map.Clicked += (sender, e) =>
            {
                var loc = new Location()
                {
                    Latitude = geofence.Latitude, Longitude = geofence.Longitude
                };
                App.Instance.PushPage(new MapPage(loc));
            };

            layout.Children.Add(formLayout);
            layout.Children.Add(map);
            layout.Children.Add(typeLayout);
            layout.Children.Add(delayLayout);


            if (geofence.PublicFlag == 0)
            {
                accesSwitch = new ChoiceSwitch()
                {
                    LeftOption = AppResources.AccesGeofencePrivate, RightOption = AppResources.AccesGeofencePublic
                };

                accesSwitch.SelectedIndexChanged += (sender, e) =>
                {
                    geofence.PublicFlag = accesSwitch.SelectedIndex;
                };
                layout.Children.Add(accesSwitch);
            }
            ScrollView scrollview = new ScrollView();

            scrollview.Content = layout;
            Content            = scrollview;
            didChange          = false;
        }
        /// <summary>
        /// Initialize this instance.
        /// </summary>
        private void Initialize()
        {
            if (geofence == null)
            {
                var data = App.GeofenceManager.GetPendingGeofenceRequest();
                geofence = data ?? new Geofence()
                {
                    Latitude = 0, Longitude = 0
                };
            }

            InitializeComponent();
            switchNotification.IsToggled = true;
            labelLongitude.Text          = AppResources.LongitudeLabel;
            entryLongitude.Placeholder   = AppResources.LongitudePlaceHolder;
            labelLatitude.Text           = AppResources.LatitudeLabel;
            entryLatitude.Placeholder    = AppResources.LatitudePlaceHolder;
            btnMap.Text            = AppResources.CarteButton;
            nameEntry.Placeholder  = AppResources.NomLieuPlaceHolder;
            notificationLabel.Text = AppResources.ActiverNotificationLabel;
            enterActionLabel.Text  = AppResources.ActionEntreeLabel;
            exitActionLabel.Text   = AppResources.ActionSortieLabel;
            btnSave.Text           = AppResources.EnregistrerBouton;
            btnCancel.Text         = AppResources.AnnulerBouton;


            if (geofence != null)
            {
                entryLatitude.Text  = geofence.Latitude.ToString();
                entryLongitude.Text = geofence.Longitude.ToString();

                btnMap.Clicked += (sender, e) =>
                {
                    var loc = new Location()
                    {
                        Latitude = geofence.Latitude, Longitude = geofence.Longitude
                    };
                    var map = new MapPage(loc);
                    Navigation.PushAsync(map);
                };


                btnSave.Clicked += (sender, e) =>
                {
                    if (!string.IsNullOrEmpty(nameEntry.Text))
                    {
                        geofence.Name = nameEntry.Text;
                        geofence.NotificationEnabled = switchNotification.IsToggled;
                        geofence.Radius      = ApplicationConst.DefaultGeofenceRadius;
                        geofence.User        = App.UserNoseq;
                        geofence.ServerNoseq = App.ServerInfoList.First((arg) => App.ServerInfoNoseq == arg.Noseq).Domain;
                        App.GeofenceManager.AddGeofence(geofence);
                        Navigation.PopAsync();
                    }
                    else
                    {
                        nameEntry.PlaceholderColor = Color.Red;
                    }
                };

                btnCancel.Clicked += (sender, e) =>
                {
                    Navigation.PopAsync();
                };

                nameEntry.Focused += (sender, e) =>
                {
                    nameEntry.PlaceholderColor = Color.Gray;
                };

                nameEntry.Unfocused += (sender, e) =>
                {
                    if (nameEntry.Text.Length > ApplicationConst.GeofenceNameMaxSize)
                    {
                        nameEntry.Text = nameEntry.Text.Substring(0, ApplicationConst.GeofenceNameMaxSize);
                    }
                    else if (string.IsNullOrEmpty(nameEntry.Text))
                    {
                        nameEntry.PlaceholderColor = Color.Red;
                    }
                };
                entryLatitude.Unfocused += (sender, e) =>
                {
                    double lat = 0;
                    if (double.TryParse(entryLatitude.Text, out lat))
                    {
                        geofence.Latitude = lat;
                    }
                    else
                    {
                        entryLatitude.Text = geofence.Latitude.ToString();
                    }
                };

                entryLongitude.Unfocused += (sender, e) =>
                {
                    double lon = 0;
                    if (double.TryParse(entryLongitude.Text, out lon))
                    {
                        geofence.Longitude = lon;
                    }
                    else
                    {
                        entryLongitude.Text = geofence.Longitude.ToString();
                    }
                };



                //Type Picker
                foreach (var t in ActionManager.GetActionTypes())
                {
                    EnterTypePicker.Items.Add(t);
                }

                EnterTypePicker.SelectedIndexChanged += (sender, e) =>
                {
                    string value = EnterTypePicker.Items[EnterTypePicker.SelectedIndex];

                    EnterSoustypePicker.Items.Clear();
                    foreach (var t in ActionManager.GetActionList().Where((Action a) => a.Category == value))
                    {
                        EnterSoustypePicker.Items.Add(t.Description);
                    }
                    EnterSoustypePicker.IsEnabled = EnterSoustypePicker.Items.Count != 0;
                    if (EnterSoustypePicker.IsEnabled)
                    {
                        if (!string.IsNullOrEmpty(geofence.EnterActionName))
                        {
                            EnterSoustypePicker.SelectedIndex = EnterSoustypePicker.Items.IndexOf(ActionManager.GetActionList().First(a => a.Name == geofence.EnterActionName).Description);
                        }
                        else
                        {
                            EnterSoustypePicker.SelectedIndex = 0;
                        }
                    }
                };


                EnterSoustypePicker.SelectedIndexChanged += (sender, e) =>
                {
                    string value = EnterSoustypePicker.SelectedIndex >= 0 ? EnterSoustypePicker.Items[EnterSoustypePicker.SelectedIndex] : "";
                    if (value != "")
                    {
                        var action = ActionManager.GetActionList().FirstOrDefault((a) => a.Description == value);
                        geofence.EnterActionName = action.Name;
                    }
                    else
                    {
                        geofence.EnterActionName = "";
                    }
                };

                foreach (var t in ActionManager.GetActionTypes())
                {
                    ExitTypePicker.Items.Add(t);
                }
                ExitTypePicker.SelectedIndexChanged += (sender, e) =>
                {
                    string value = ExitTypePicker.Items[ExitTypePicker.SelectedIndex];
                    ExitSoustypePicker.Items.Clear();

                    foreach (var t in ActionManager.GetActionList().Where(a => a.Category == value))
                    {
                        ExitSoustypePicker.Items.Add(t.Description);
                    }
                    ExitSoustypePicker.IsEnabled = ExitSoustypePicker.Items.Count != 0;
                    if (ExitSoustypePicker.IsEnabled)
                    {
                        if (!string.IsNullOrEmpty(geofence.ExitActionName))
                        {
                            ExitSoustypePicker.SelectedIndex = ExitSoustypePicker.Items.IndexOf(ActionManager.GetActionList().First(a => a.Name == geofence.ExitActionName).Description);
                        }
                        else
                        {
                            ExitSoustypePicker.SelectedIndex = 0;
                        }
                    }
                };


                ExitSoustypePicker.SelectedIndexChanged += (sender, e) =>
                {
                    string value = ExitSoustypePicker.SelectedIndex >= 0 ? ExitSoustypePicker.Items[ExitSoustypePicker.SelectedIndex] : "";
                    if (value != "")
                    {
                        var action = ActionManager.GetActionList().FirstOrDefault((a) => a.Description == value);
                        geofence.ExitActionName = action.Name;
                    }
                    else
                    {
                        geofence.ExitActionName = "";
                    }
                };

                string category = ActionManager.Null;

                if (!string.IsNullOrEmpty(geofence.EnterActionName))
                {
                    var enterAction = ActionManager.GetActionList().First(a => geofence.EnterActionName == a.Name);
                    category = enterAction.Category;
                }
                EnterTypePicker.SelectedItem = category;

                category = ActionManager.Null;

                if (!string.IsNullOrEmpty(geofence.ExitActionName))
                {
                    var exitAction = ActionManager.GetActionList().First(a => geofence.EnterActionName == a.Name);
                    category = exitAction.Category;
                }
                ExitTypePicker.SelectedItem = category;


                delayLabel = new Label()
                {
                    Text = AppResources.DelaiNotificationLabel, VerticalTextAlignment = TextAlignment.Center
                };
                delayLayout.Children.Add(delayLabel);
                delayPicker = new HMSTimePicker {
                    HorizontalOptions = LayoutOptions.FillAndExpand
                };
                try
                {
                    delayPicker.Time = new TimeSpan(0, ApplicationConst.DefaultGeofenceTriggerTime / 60, ApplicationConst.DefaultGeofenceTriggerTime % 60);
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e.Message);
                }
                delayPicker.Unfocused += (sender, e) =>
                {
                    var time = (uint)delayPicker.Time.TotalSeconds;
                    if (time > uint.MaxValue)
                    {
                        time = uint.MaxValue;
                    }
                    if (time != geofence.NotificationDelay)
                    {
                        geofence.NotificationDelay = time;
                    }
                };
                delayLayout.Children.Add(delayPicker);

                labelRadius.Text = AppResources.RayonLabel;
                foreach (var rad in GeofenceManager.GeofenceRadius)
                {
                    radiusPicker.Items.Add(rad.ToString());
                }
                radiusPicker.SelectedIndexChanged += (sender, e) => CheckEntryRadius();
                radiusPicker.SelectedItem          = ApplicationConst.DefaultGeofenceRadius.ToString();

                ChoiceSwitch accesSwitch = new ChoiceSwitch()
                {
                    LeftOption = AppResources.AccesGeofencePrivate, RightOption = AppResources.AccesGeofencePublic
                };

                accesSwitch.SelectedIndexChanged += (sender, e) =>
                {
                    geofence.PublicFlag = accesSwitch.SelectedIndex;
                };
                AccesLayout.Children.Add(accesSwitch);
            }
        }