Example #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ConfigSettingDropdownViewModel"/> class.
        /// </summary>
        /// <param name="item">Source item.</param>
        public ConfigSettingDropdownViewModel(Setting item)
            : base(item)
        {
            try
            {
                _dataProvider = TypeResolver.CreateConfigDataProviderInstance(item.Datasource, item.DatasourceAssembly);
            }
            catch (Exception ex)
            {
                var message = $"Could not resolve datasource. Datasource='{item.Datasource}', DatasourceAssembly='{item.DatasourceAssembly}'.";
                throw new HotConfig.Error.InvalidConfiguration(message, ex);
            }

            var dataProviderType = _dataProvider.GetType();

            if (typeof(IConfigDataProviderOnce).IsAssignableFrom(dataProviderType))
            {
                var onceProvider = (IConfigDataProviderOnce)_dataProvider;
                Items = onceProvider.FetchData().Select(x => new DropdownItem()
                {
                    Id = x.Key, Text = x.Value
                }).ToList();
            }
            else if (typeof(IConfigDataProviderPoll).IsAssignableFrom(dataProviderType))
            {
                Items = new List <DropdownItem>();

                _isPoll = true;
                WaitingForFirstPollResult = true;

                var pollProvider = (IConfigDataProviderPoll)_dataProvider;
                pollProvider.DataItems.CollectionChanged += DataProviderPollCollectionChanged;

                pollProvider.Start();
            }
            else
            {
                throw new HotConfig.Error.InvalidConfiguration("Settings dropdown doesn't implement a known interface.");
            }

            SelectedItem = Items.Where(x => x.Id == CurrentValue).FirstOrDefault();
        }