Beispiel #1
0
        /// <summary>
        /// Initialized an instance of add form with a num pad.
        /// </summary>
        ///
        /// <returns></returns>
        public AddView(Action <Item> localUnplacedEvent, Action <Item> baseUnplacedEvent,
                       string storageName = "", bool limited = true, Grid partialUnplacedGrid = null)
        {
            BackgroundColor = ContentManager.ThemeColor;
            ContentManager.AddOnBackgroundChangeListener(c => BackgroundColor = c);

            item = new Item().SetItem(-1, -1, -1, 1, "product", ContentManager.addIcon);
            Grid currentGrid = new Grid();

            Vector2D <int> selectGridIndex = new Vector2D <int>(0, 5);

            presetSelectGrid            = GridManager.InitializeGrid("Partial Preset Grid", 2, 1, formIconWidthHeight, formIconWidthHeight);
            presetSelectGrid.RowSpacing = form_icon_margin; presetSelectGrid.ColumnSpacing = form_icon_margin;
            presetScrollView            = new ScrollView()
            {
                Content = presetSelectGrid, Orientation = ScrollOrientation.Horizontal, Margin = new Thickness(0, form_grid_spacing)
            };
            defaultSelectGrid            = GridManager.InitializeGrid("Partial Default Grid", 2, 1, formIconWidthHeight, formIconWidthHeight);
            defaultSelectGrid.RowSpacing = form_icon_margin; defaultSelectGrid.ColumnSpacing = form_icon_margin;
            GridManager.AddGridItem(defaultSelectGrid, ContentManager.GeneralIcons, true, GridOrganizer.OrganizeMode.VerticalLeft);
            defaultScrollView = new ScrollView()
            {
                Content = defaultSelectGrid, Orientation = ScrollOrientation.Horizontal, Margin = new Thickness(0, form_grid_spacing)
            };

            foreach (IconLayout icon in ContentManager.GeneralIcons)
            {
                icon.OnClickIconAction = (imageButton) =>
                {
                    toggleIconSelect(imageButton, defaultSelectGrid); item.Icon = icon.GetImageSource();
                };
            }

            autoDetectLabel = new Label()
            {
                FontSize = 15, TextColor = Color.Black, BackgroundColor = Color.White, IsVisible = false, AnchorX = 0
            };

            Grid form = new Grid()
            {
                BackgroundColor = ContentManager.ThemeColor,
                RowSpacing      = form_grid_spacing,
                ColumnSpacing   = form_grid_spacing,
                RowDefinitions  =
                {
                    new RowDefinition()
                    {
                        Height = GridLength.Star
                    },
                    new RowDefinition()
                    {
                        Height = GridLength.Star
                    },
                    new RowDefinition()
                    {
                        Height = GridLength.Star
                    },
                    new RowDefinition()
                    {
                        Height = GridLength.Star
                    },
                    new RowDefinition()
                    {
                        Height = GridLength.Star
                    }
                },
                ColumnDefinitions =
                {
                    new ColumnDefinition()
                    {
                        Width = 120
                    },
                    new ColumnDefinition()
                }
            };

            ContentManager.AddOnBackgroundChangeListener(c => form.BackgroundColor = c);

            void ClearText(Button button)
            {
                button.Text = "";
                toggleSelect(selectorIndex, formSelector, selectedColor, unselectedColor);
            }

            nameInput = new Entry()
            {
                HeightRequest   = form_height_proportional * ContentManager.screenHeight / form_grid_row_count, Placeholder = "Product",
                BackgroundColor = unselectedColor, PlaceholderColor = Color.Black, Margin = new Thickness(0, form_label_horizontal_margin, form_label_horizontal_margin, 0)
            };
            nameInput.Unfocused += (obj, args) => { nameInput.BackgroundColor = unselectedColor; item.Name = nameInput.Text; autoDetectExpiration(nameInput.Text); if (imageSelectorIndex == 0)
                                                    {
                                                        changeSelectedIcon();
                                                    }
            };
            nameInput.Focused   += (obj, args) => { nameInput.BackgroundColor = selectedColor; };
            nameInput.Completed += (obj, args) => { item.Name = nameInput.Text; if (imageSelectorIndex == 0)
                                                    {
                                                        changeSelectedIcon();
                                                    }
            };
            var nameLabel = new Label()
            {
                Text = "Name: ", FontSize = form_label_font_size, TextColor = Color.Black, Margin = new Thickness(form_label_horizontal_margin, 0), VerticalTextAlignment = TextAlignment.Center
            };
            var dateLabel = new Label()
            {
                Text = "Exp. Date: ", FontSize = form_label_font_size, TextColor = Color.Black, Margin = new Thickness(form_label_horizontal_margin, 0), VerticalTextAlignment = TextAlignment.Center
            };
            var amountLabel = new Label()
            {
                Text = "Amount: ", FontSize = form_label_font_size, TextColor = Color.Black, Margin = new Thickness(form_label_horizontal_margin, 0), VerticalTextAlignment = TextAlignment.Center
            };
            var iconLabel = new Label()
            {
                Text = "Icon: ", FontSize = form_label_font_size, TextColor = Color.Black, HeightRequest = formGridRowHeight, VerticalTextAlignment = TextAlignment.Center
            };
            var dateMonth = new Button()
            {
                BorderColor = Color.Black, BorderWidth = form_input_border_width, BackgroundColor = Color.Transparent, TextColor = Color.Black
            };

            dateMonth.Clicked += (obj, arg) => { selectorIndex = 0; ClearText(dateMonth); };
            var dateDay = new Button()
            {
                BorderColor = Color.Black, BorderWidth = form_input_border_width, BackgroundColor = Color.Transparent, TextColor = Color.Black
            };

            dateDay.Clicked += (obj, arg) => { selectorIndex = 1; ClearText(dateDay); };
            var dateYear = new Button()
            {
                BorderColor = Color.Black, BorderWidth = form_input_border_width, TextColor = Color.Black
            };

            dateYear.Clicked += (obj, arg) => { selectorIndex = 2; ClearText(dateYear); };
            var amountInput = new Button()
            {
                BorderColor = Color.Black, BorderWidth = form_input_border_width, BackgroundColor = Color.Transparent,
                Margin      = new Thickness(0, 0, form_label_horizontal_margin, 0), TextColor = Color.Black
            };

            amountInput.Clicked += (obj, arg) => { selectorIndex = 3; ClearText(amountInput); };
            Grid expGrid = GridManager.InitializeGrid(1, 3, GridLength.Star, GridLength.Star);

            expGrid.ColumnSpacing = form_grid_spacing;
            expGrid.Margin        = new Thickness(0, 0, form_label_horizontal_margin, 0);
            GridManager.AddGridItem(expGrid, new List <View>()
            {
                dateMonth, dateDay, dateYear
            }, true);

            var iconSelect1 = new Button()
            {
                HeightRequest = formGridRowHeight, Text = "Preset", TextColor = Color.Black, CornerRadius = form_icon_select_border_radius,
                BorderColor   = Color.Black, BorderWidth = 2
            };

            iconSelect1.Clicked += (obj, arg) =>
            {
                imageSelectorIndex = 0; toggleSelect(0, imageSelector, selectedColor, unselectedColor);

                defaultScrollView.IsVisible = false;
                presetScrollView.IsVisible  = true;
                changeSelectedIcon();
            };
            var iconSelect2 = new Button()
            {
                HeightRequest = formGridRowHeight, Text = "General", TextColor = Color.Black, CornerRadius = form_icon_select_border_radius,
                BorderColor   = Color.Black, BorderWidth = 2
            };

            iconSelect2.Clicked += (obj, arg) =>
            {
                imageSelectorIndex          = 1; toggleSelect(1, imageSelector, selectedColor, unselectedColor);
                defaultScrollView.IsVisible = true;
                presetScrollView.IsVisible  = false;
                Console.WriteLine("AddView 144 default select grid children length " + defaultSelectGrid.Children.Count + " " + ((Grid)defaultScrollView.Content).Children.Count);
            };

            Grid iconLabelGrid = GridManager.InitializeGrid(3, 1, GridLength.Star, GridLength.Star);

            GridManager.AddGridItem(iconLabelGrid, new List <View>()
            {
                iconLabel, iconSelect1, iconSelect2
            }, false);
            iconLabelGrid.Margin = new Thickness(form_label_horizontal_margin, form_grid_spacing);

            void toggleSelect(int index, List <Button> buttonList, Color colorTo, Color normal)
            {
                foreach (var button in buttonList)
                {
                    button.BackgroundColor = normal;
                }
                buttonList[index].BackgroundColor = colorTo;
            }

            formSelector.Add(dateMonth);
            formSelector.Add(dateDay);
            formSelector.Add(dateYear);
            formSelector.Add(amountInput);
            imageSelector.Add(iconSelect1);
            imageSelector.Add(iconSelect2);

            form.Children.Add(nameLabel, 0, 0);
            form.Children.Add(nameInput, 1, 0);
            form.Children.Add(dateLabel, 0, 1);
            form.Children.Add(expGrid, 1, 1);
            form.Children.Add(amountLabel, 0, 2);
            form.Children.Add(amountInput, 1, 2);
            form.Children.Add(presetScrollView, 1, 3);
            form.Children.Add(defaultScrollView, 1, 3);
            form.Children.Add(iconLabelGrid, 0, 3);
            Grid.SetRowSpan(presetScrollView, 2);
            Grid.SetRowSpan(iconLabelGrid, 2);
            Grid.SetRowSpan(defaultScrollView, 2);

            void setItem(int newText, int index)
            {
                switch (selectorIndex)
                {
                case 0: item.expMonth = newText; break;

                case 1: item.expDay = newText; break;

                case 2: item.expYear = newText; break;

                case 3: item.Amount = newText; break;
                }
            }

            void changeText(string addedText)
            {
                // limiting digits: month / day: 2 digits. Year: 4 digits. Amount : 3 digits.
                int    limit   = selectorIndex == 2 ? 4 : selectorIndex == 3 ? 3 : 2;
                string oldText = formSelector[selectorIndex].Text ?? "";
                string newText = oldText;

                if (oldText.Length < limit)
                {
                    toggleSelect(selectorIndex, formSelector, selectedColor, unselectedColor);
                    newText = oldText + addedText;
                    formSelector[selectorIndex].Text = newText;
                    if (oldText.Length == limit - 1 && selectorIndex < formSelector.Count - 1)
                    {
                        var inputInt = int.Parse(newText);
                        switch (selectorIndex)
                        {
                        case 0:
                            if (inputInt > 12)
                            {
                                newText = "12";
                            }
                            if (int.Parse(newText.ToCharArray()[0].ToString()) == 0)
                            {
                                newText.Remove(0, 1);
                            }
                            if (inputInt <= 0)
                            {
                                newText = "1";
                            }
                            break;

                        case 1:
                            var text = formSelector[0].Text ?? "1";
                            if (int.Parse(text.ToCharArray()[0].ToString()) == 0)
                            {
                                text.Remove(0, 1);
                            }
                            var maxDay = DateCalculator.GetMonthList()[int.Parse(text) - 1];
                            if (inputInt > maxDay)
                            {
                                newText = maxDay.ToString();
                            }
                            if (inputInt <= 0)
                            {
                                newText = "1";
                            }
                            break;

                        case 3:
                            if (inputInt < 0)
                            {
                                newText = "1";
                            }
                            break;
                        }
                        formSelector[selectorIndex].Text = newText;
                        setItem(int.Parse(newText), selectorIndex);
                        selectorIndex++;
                        toggleSelect(selectorIndex, formSelector, selectedColor, unselectedColor);
                    }
                    else
                    {
                        setItem(int.Parse(newText), selectorIndex);
                    }
                }
                else if (selectorIndex < formSelector.Count - 1)
                {
                    newText = oldText + addedText;
                    formSelector[selectorIndex].Text = newText;
                    setItem(int.Parse(newText), selectorIndex);
                    selectorIndex++; toggleSelect(selectorIndex, formSelector, selectedColor, unselectedColor);
                }
            }

            Grid numPadGrid = GridManager.InitializeGrid(4, 3, GridLength.Star, GridLength.Star);

            numPadGrid.BackgroundColor = ContentManager.ThemeColor;
            ContentManager.AddOnBackgroundChangeListener(c => numPadGrid.BackgroundColor = c);
            numPadGrid.Margin        = new Thickness(form_label_horizontal_margin, 0);
            numPadGrid.RowSpacing    = numpad_spacing;
            numPadGrid.ColumnSpacing = numpad_spacing;

            List <View> numPadList = new List <View>();

            for (int i = 1; i < 10; i++)
            {
                var button = new Button()
                {
                    Text = i.ToString(), TextColor = Color.Black, Margin = new Thickness(0), BackgroundColor = numpadBackground, FontSize = numpad_font_size
                };
                int index = i;
                button.Clicked += (obj, arg) => changeText(index.ToString());
                numPadList.Add(button);
            }

            var numpadBottomLeftEmptyBuffer = new Button()
            {
                IsVisible = false
            };
            var zeroButton = new Button()
            {
                Text = "0", TextColor = Color.Black, FontSize = numpad_font_size, BackgroundColor = numpadBackground
            };

            zeroButton.Clicked += (obj, arg) => changeText("0");

            numPadList.Add(numpadBottomLeftEmptyBuffer);
            numPadList.Add(zeroButton);

            numPadGrid.OrganizeGrid(numPadList, GridOrganizer.OrganizeMode.HorizontalLeft);

            var scanButton = new Button()
            {
                Text = "Scan", TextColor = Color.Black, FontAttributes = FontAttributes.Bold, BackgroundColor = numpadBackground
            };
            var exitButton = new Button()
            {
                Text = "Exit", BackgroundColor = Color.WhiteSmoke, TextColor = Color.Black, Margin = new Thickness(5), BorderColor = Color.Black, BorderWidth = 1
            };
            var newFormButton = new Button()
            {
                BackgroundColor = Color.WhiteSmoke, Text = "Add", TextColor = Color.Black, Margin = new Thickness(5), BorderColor = Color.Black, BorderWidth = 1
            };

            manualEditLayout = new AbsoluteLayout()
            {
                HorizontalOptions = LayoutOptions.Center,
                HeightRequest     = ContentManager.screenHeight,
                WidthRequest      = ContentManager.screenWidth,
                BackgroundColor   = ContentManager.ThemeColor,
                Children          =
                {
                    form,
                    autoDetectLabel,
                    numPadGrid,
                    scanButton,
                    newFormButton,
                    exitButton
                }
            };

            scanButton.Clicked += (o, a) =>
            {
                ContentManager.pageController.ToScanPage(this);
            };
            async void OnNewItemAdded(object obj, EventArgs args)
            {
                var animatedImage = new Image()
                {
                    Source = item.Icon.Substring(6), Aspect = Aspect.Fill
                };

                manualEditLayout.Children.Add(animatedImage, new Rectangle(0.5, 0.9, 100, 100), AbsoluteLayoutFlags.PositionProportional);
                await animatedImage.QuadraticFlight(15, 75, -80, 30, (v) => { animatedImage.TranslationX = -v.X; animatedImage.TranslationY = -v.Y; }, 1500, easing : Easing.Linear);
            }

            newFormButton.Clicked += OnNewItemAdded;
            newFormButton.Clicked += (obj, args) =>
            {
                Item itemInstance = new Item().SetItem(item.expYear, item.expMonth, item.expDay, item.Amount, item.Name, item.Icon);
                Console.WriteLine("Addview 309 item icon " + itemInstance.Icon + " " + item.Icon);
                itemInstance.SetDaysUntilExpiration();
                newItem.Add(itemInstance);
                if (ContentManager.isLocal)
                {
                    localUnplacedEvent?.Invoke(itemInstance);
                }
                else
                {
                    Console.WriteLine("Addview 302 base unplace storage");
                    baseUnplacedEvent?.Invoke(itemInstance);
                }
                ResetForm();
            };

            AbsoluteLayout.SetLayoutBounds(autoDetectLabel, new Rectangle(0, 0, 1, .06));
            AbsoluteLayout.SetLayoutFlags(autoDetectLabel, AbsoluteLayoutFlags.All);
            AbsoluteLayout.SetLayoutBounds(form, new Rectangle(0, 0, 1, form_height_proportional));
            AbsoluteLayout.SetLayoutFlags(form, AbsoluteLayoutFlags.All);
            AbsoluteLayout.SetLayoutBounds(numPadGrid, new Rectangle(.5, .7, 1, numpad_height_proportional));
            AbsoluteLayout.SetLayoutFlags(numPadGrid, AbsoluteLayoutFlags.All);
            AbsoluteLayout.SetLayoutBounds(scanButton, new Rectangle(.5, .88, .5, .08));
            AbsoluteLayout.SetLayoutFlags(scanButton, AbsoluteLayoutFlags.All);
            AbsoluteLayout.SetLayoutBounds(newFormButton, new Rectangle(0, 1, 0.5, .1));
            AbsoluteLayout.SetLayoutFlags(newFormButton, AbsoluteLayoutFlags.All);
            AbsoluteLayout.SetLayoutBounds(exitButton, new Rectangle(1, 1, 0.5, .1));
            AbsoluteLayout.SetLayoutFlags(exitButton, AbsoluteLayoutFlags.All);

            exitButton.Clicked += (obj, args) =>
            {
                List <ItemLayout> newItemLayouts      = new List <ItemLayout>();
                List <ItemLayout> newItemLayoutsCopy  = new List <ItemLayout>();
                List <ItemLayout> newItemLayoutsCopy2 = new List <ItemLayout>();

                SaveInput(newItemLayouts, newItemLayoutsCopy, newItemLayoutsCopy2);

                if (partialUnplacedGrid != null)
                {
                    GridManager.AddGridItem(partialUnplacedGrid, newItemLayoutsCopy2, false);
                }

                newItemLayouts.Clear();
                newItemLayoutsCopy.Clear();
                newItemLayoutsCopy2.Clear();
                ResetForm();
                newItem.Clear();
                ContentManager.pageController.ReturnToPrevious();
            };

            Content = manualEditLayout;
        }