private void _changer_VolumeChanged(object sender, EventsArgs <int> args)
        {
            //megaCrutchCode
            if (_model.AcceptedValues.Length > 1)
            {
                var direction = args.Value > 0 ? -1 : 1;

                var currentValues = listView.ItemsSource as string[];

                var currentIndex = currentValues.ToList().IndexOf(_currentVal);
                var nextIndex    = currentIndex + direction;
                if (nextIndex >= currentValues.Length)
                {
                    nextIndex = 0;
                }
                else if (nextIndex < 0)
                {
                    nextIndex = currentValues.Length - 1;
                }

                _currentVal = currentValues[nextIndex];

                if (_prevItem != null)
                {
                    _prevItem.StrokeVisible = false;
                }

                ScrollTo(_currentVal);
                if (_visibleItems.ContainsKey(_currentVal))
                {
                    var itemView = _visibleItems[_currentVal];
                    _prevItem = itemView;
                    itemView.StrokeVisible = true;
                }
            }
        }
        public StatusViewSwitch(SwitchScenarioModel scenarioModel) : this()
        {
            //megaCrutchCode

            BindingContext = _model = scenarioModel;

            _model.PropertyChanged += _model_PropertyChanged;

            _currentVal = _model.ScenarioValue;

            listView.ItemsSource = _model.AcceptedValues; // Crutch to allocate listView height

            if (IsNeedShowSearchButton())
            {
                tbSearch.Text = GetSearchCache(_model.Scenario.ScenarioId);

                tbSearch.Completed += (o, e) => HandleSearch();

                // HELL
                bool initiateSearchHandled = false;
                listView.SizeChanged += (o, e) =>
                {
                    if (!initiateSearchHandled)
                    {
                        initiateSearchHandled = true;
                        if (!string.IsNullOrEmpty(tbSearch.Text))
                        {
                            HandleSearch();
                        }
                    }
                };

                btClearSearch.Click += (o, e) =>
                {
                    tbSearch.Text = string.Empty;
                    HandleSearch();
                };

                iconSearch.IsVisible = true;
                tbSearch.IsVisible   = true;
            }
            else
            {
                iconTitle.IsVisible = true;
                lblTitle.IsVisible  = true;
            }

            // Невозможно замапить объекты, так как, почему-то, сбивается размер всего контрола.
            // Если мапить строки, то размер нормальный.

            listView.ItemTemplate = new DataTemplate(() =>
            {
                var itemView = new ItemViewFast();
                itemView.SetBinding(ItemViewFast.TextProperty, ".");
                itemView.Selectable            = true;
                itemView.StrokeVisibilityClick = true;
                itemView.HeightRequest         = 42;

                itemView.SelectionChanged += (o, e) =>
                {
                    if (itemView.Selected)
                    {
                        foreach (var item in _visibleItems.Values)
                        {
                            if (item != itemView && item.Selected)
                            {
                                item.Selected = false;
                            }
                        }
                    }
                };

                itemView.PropertyChanged += (o, e) =>
                {
                    if (e.PropertyName == nameof(itemView.Text))
                    {
                        if (itemView.Text == _model.ScenarioValue)
                        {
                            itemView.Selected = true;
                        }
                        else if (itemView.Selected)
                        {
                            itemView.Selected = false;
                        }
                    }
                };

                itemView.Click += (o, e) =>
                {
                    if (_model.ScenarioValue == itemView.Text)
                    {
                        var valueType = _model.Scenario.ValueType as StateValueType;
                        RaiseSelect(valueType.DefaultValue, ItemView.ClickSource.CloseAnyway);
                    }
                    else
                    {
                        RaiseSelect(itemView.Text, (ItemView.ClickSource)e.Value);
                    }
                };

                var viewCell = new ViewCell();

                viewCell.Appearing += (o, e) =>
                {
                    if (itemView.Text != null)
                    {
                        if (_visibleItems.ContainsKey(itemView.Text))
                        {
                            _visibleItems.Remove(itemView.Text);
                        }

                        _visibleItems.Add(itemView.Text, itemView);

                        itemView.Selected = itemView.Text == _model.ScenarioValue;
                    }
                };
                viewCell.Disappearing += (o, e) =>
                {
                    if (itemView.Text != null && _visibleItems.ContainsKey(itemView.Text))
                    {
                        _visibleItems.Remove(itemView.Text);
                    }

                    itemView.Selected = false;
                };
                viewCell.View = itemView;
                return(viewCell);
            });
        }
예제 #3
0
        public StatusViewSwitch(ScenarioModel scenarioModel) : this()
        {
            DataContext = scenarioModel;

            BeginInit();

#if !DEBUG
            spSearch.Visibility = scenarioModel.AcceptedValues.Length > 150 ? Visibility.Visible : Visibility.Collapsed;
#endif

            tbScenarioName.Text = scenarioModel.ScenarioName;
            ItemViewFast initialSelectedItem = null;
            foreach (var state in scenarioModel.AcceptedValues)
            {
                var itemView = new ItemViewFast();
                itemView.Text   = state;
                itemView.Margin = new Thickness(0, 1, 0, 1);
                if (scenarioModel.ScenarioValue?.Equals(state) ?? false)
                {
                    initialSelectedItem = itemView;
                }
                listItemsStates.Children.Add(itemView);
            }

            tbSearch.Text = GetSearchCache(scenarioModel.Scenario.Id);

            if (!string.IsNullOrEmpty(tbSearch.Text))
            {
                ShowSearchResult();
            }

            Loaded += (o, e) =>
            {
                if (initialSelectedItem != null)
                {
                    initialSelectedItem.Selected = true;
                    initialSelectedItem.Focus();
                }
            };

            listItemsStates.SelectionChanged += (o, e) =>
            {
                var selectedItem = listItemsStates.GetSelectedItems().FirstOrDefault() as ItemViewFast;
                if (selectedItem != null)
                {
                    if (selectedItem.Text != scenarioModel.ScenarioValue)
                    {
                        scenarioModel.ScenarioValue = selectedItem.Text;
                        StateChanged?.Invoke(this, new RoutedEventArgs());
                    }
                }
                else
                {
                    scenarioModel.ScenarioValue = (scenarioModel.Scenario.ValueType as StateValueType).DefaultValue;
                    StateChanged?.Invoke(this, new RoutedEventArgs());
                }
            };

            tbSearch.TextChanged += (o, e) => {
                _timer?.Dispose();
                SetSearchCache(scenarioModel.Scenario.Id, tbSearch.Text);
                _timer = new Timer((s) => {
                    _timer = null;
                    Dispatcher.BeginInvoke(new Action(ShowSearchResult));
                }, null, 600, Timeout.Infinite);
            };

            EndInit();
        }