Esempio n. 1
0
        protected override Panel CreateSinglePanel(Entity entity, int width, int height)
        {
            Grid grid = new Grid
            {
                Width   = width,
                Height  = height,
                Padding = new Thickness(PanelMargins)
            };

            TextBlock textName = new TextBlock
            {
                Text                = entity.Attributes["friendly_name"] ?? string.Empty,
                FontSize            = base.FontSize,
                TextWrapping        = TextWrapping.Wrap,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment   = VerticalAlignment.Top,
                Foreground          = FontColorBrush,
                Padding             = new Thickness(12)
            };

            TextBlock textTemperature = new TextBlock
            {
                Foreground = FontColorBrush,
                FontWeight = FontWeights.Bold,
                FontSize   = FontSize ?? base.FontSize,
                Text       = entity.Attributes.ContainsKey("temperature") ? entity.Attributes["temperature"] != null?
                             Convert.ToString(entity.Attributes["temperature"]) : entity.State : entity.State,
                TextWrapping        = TextWrapping.Wrap,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment   = VerticalAlignment.Center
            };

            TextBlock textCurrentTemperature = new TextBlock
            {
                Foreground = FontColorBrush,
                FontWeight = FontWeights.Bold,
                FontSize   = 14,
                Text       = "Actual: " + (entity.Attributes.ContainsKey("current_temperature") ? entity.Attributes["current_temperature"] != null ?
                                           Convert.ToString(entity.Attributes["current_temperature"]) : string.Empty : string.Empty),
                TextWrapping        = TextWrapping.Wrap,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment   = VerticalAlignment.Bottom,
                Padding             = new Thickness(12)
            };

            if (entity.Attributes.ContainsKey("unit_of_measurement"))
            {
                if (entity.Attributes.ContainsKey("temperature"))
                {
                    textTemperature.Text += entity.Attributes["unit_of_measurement"];
                }

                if (entity.Attributes.ContainsKey("unit_of_measurement"))
                {
                    textCurrentTemperature.Text += entity.Attributes["unit_of_measurement"];
                }
            }

            grid.Background = ClimateControl.CreateLinearGradientBrush(entity);

            grid.Children.Add(textName);
            grid.Children.Add(textTemperature);
            grid.Children.Add(textCurrentTemperature);

            return(grid);
        }
Esempio n. 2
0
        /// <summary>
        /// Show the PopUp custom UI.
        /// </summary>
        private void ShowPopupControl(string popupControl, Entity entity, IEnumerable <Entity> childrenEntities = null)
        {
            UserControl popupContent;

            switch (popupControl)
            {
            case nameof(LightControl):
                popupContent            = new LightControl(entity, childrenEntities);
                NotifyPopupEntityUpdate = (popupContent as LightControl).EntityUpdated;
                break;

            case nameof(MediaControl):
                popupContent            = new MediaControl(entity);
                NotifyPopupEntityUpdate = (popupContent as MediaControl).EntityUpdated;
                break;

            case nameof(ClimateControl):
                popupContent            = new ClimateControl(entity);
                NotifyPopupEntityUpdate = (popupContent as ClimateControl).EntityUpdated;
                break;

            case nameof(SettingsControl):
                popupContent = new SettingsControl();
                break;

            case nameof(ThemeControl):
                popupContent = new ThemeControl(LoadFrame);
                break;

            case nameof(WebLinkControl):
                popupContent = new WebLinkControl(entity, ActualWidth, ActualHeight);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            if (popupContent != null)
            {
                // When a entity-updated callback is requested, keep track of the entity
                // which the pop-up control is monitoring.
                if (null != NotifyPopupEntityUpdate)
                {
                    PopupEntity = entity;
                }

                Popup popup = new Popup()
                {
                    IsLightDismissEnabled   = true,
                    LightDismissOverlayMode = LightDismissOverlayMode.On,
                    Child = popupContent,
                };

                popup.Closed += async(s, re) =>
                {
                    PopupEntity             = null;
                    NotifyPopupEntityUpdate = null;

                    if (popup.Child is SettingsControl)
                    {
                        // Save application settings when closing the Settings popup
                        if ((popup.Child as SettingsControl).SaveSettings())
                        {
                            // Settings value changed, so update frame, reconnect to MQTT and refresh al tiles
                            await LoadFrame();

                            await MqttSubscriber.Connect();

                            await UpdateEntitiesSinceLastUpdate(default(DateTime));
                        }
                    }
                    else if (popup.Child is ThemeControl)
                    {
                        this.RequestedTheme = ThemeControl.GetApplicationTheme();
                    }
                };

                popupContent.Loaded += (s, re) =>
                {
                    UserControl userControl = s as UserControl;

                    popup.HorizontalOffset = Window.Current.Bounds.Width / 2 - userControl.ActualWidth / 2;
                    popup.VerticalOffset   = Window.Current.Bounds.Height / 2 - userControl.ActualHeight / 2;
                };

                popup.IsOpen = true;
            }
        }