Esempio n. 1
0
        private void Initialize()
        {
            this.Databases        = SettingService.Current.Databases;
            this.SelectedDatabase = SettingService.Current.CurrentDatabase;

            this.AddDatabaseCommand = new ReactiveCommand();
            this.AddDatabaseCommand.Subscribe(AddDatabase);

            this.WhenAny(controller => controller.SelectedDatabase, (currentDb) =>
            {
                return(currentDb.Value);
            }).Subscribe(value =>
            {
                if (!string.IsNullOrWhiteSpace(value) && SettingService.Current.CurrentDatabase != value)
                {
                    SettingService.Current.CurrentDatabase = value;
                    SettingService.Save();

                    EventAggrigator.Publish("CurrentDatabaseChanged", value);
                }
            });

            this.WhenAny(controller => controller.Databases, (databases) =>
            {
                return(databases.Value);
            }).Subscribe(value =>
            {
                if (value != null)
                {
                    SettingService.Current.Databases = value;
                    SettingService.Save();
                }
            });
        }
Esempio n. 2
0
        private async void Initialize()
        {
            await Task.Run(() =>
            {
                this.IsInitializing = true;

                this.Context = new WordService(SettingService.Current.CurrentDatabase);

                this.TimePeriod = this.ChosenTimePeriod;

                this.WordTypes =
                    Enum.GetNames(typeof(WordType)).Select(type => (WordType)Enum.Parse(typeof(WordType), type));
                this.WordType = WordType.Noun;

                this.SaveCommand = new ReactiveCommand(this.WhenAny(
                                                           controller => controller.WordText,
                                                           controller => controller.WordType,
                                                           (text, type) => !string.IsNullOrWhiteSpace(text.Value) && type.Value.GetHashCode() != 0));

                this.RemoveCommand =
                    new ReactiveCommand(this.WhenAny(controller => controller.Word,
                                                     (word) => word.Value != null && word.Value.Registered.HasValue));

                this.CreateWordCommand = new ReactiveCommand();

                this.PronounceCommand = new ReactiveCommand();

                this.SwitchEditModeCommand = new ReactiveCommand();
                this.SwitchEditModeCommand.Subscribe(SwitchEditMode);


                this.SaveCommand.Subscribe(_ => this.Save());

                this.RemoveCommand.Subscribe(_ => this.Remove());

                this.CreateWordCommand.Subscribe(this.Create);

                this.PronounceCommand.Subscribe(Pronounce);

                this.WhenAny(controller => controller.Keyword, controller => controller.TimePeriod,
                             (keyword, period) =>
                {
                    this.ChosenTimePeriod = period.Value;
                    return(this.SearchWords(keyword.Value, this._timeRanges[period.Value]));
                })
                .Subscribe(words =>
                {
                    this.Words = words;
                    if (this.Words.Count > 0)
                    {
                        this.Word = this.Words[0];
                    }
                });


                this.WhenAny(controller => controller.Word, (word) => word).Subscribe(word =>
                {
                    if (!this.IsInitializing && word.Value != null)
                    {
                        InteractionService.OpenFlyout(Routes.Edit);

                        this.RaisePropertyChanged("WordText");
                        this.RaisePropertyChanged("WordDefinition");
                        this.RaisePropertyChanged("WordType");

                        this.ChosenWordRegisteredSetting = word.Value.Registered.HasValue
                            ? word.Value.Registered.Value
                            : DateTime.Today;
                    }
                });

                if (this.Words != null && this.Words.Any())
                {
                    this.Word = this.Words.SingleOrDefault(word => word.Registered == this.ChosenWordRegisteredSetting) ??
                                this.Words.FirstOrDefault();
                }

                this.SoundPlayer = new WaveOut();

                var pronounciationFilePath = Path.Combine(Environment.CurrentDirectory,
                                                          ConfigurationManager.AppSettings["PronounciationDatabaseName"]);

                this.PronounciationService = new PronounciationService(pronounciationFilePath);

                this.IsInitializing = false;
            });

            EventAggrigator.Subscribe("CurrentDatabaseChanged", parameter =>
            {
                if (parameter != null)
                {
                    var currentDatabase = parameter.ToString();
                    if (!string.IsNullOrWhiteSpace(currentDatabase))
                    {
                        this.Context = new WordService(currentDatabase);
                        this.Words   = this.SearchWords(this.Keyword, this._timeRanges[this.TimePeriod]);
                    }
                }
            });
        }