public MainWindowVm(string directory,
                            VirtualCollectionSource <FileModel, FileViewModel> virtualSource)
        {
            Directory        = directory;
            VirtualSource    = virtualSource;
            FilterName       = new ReactivePropertySlim <string>().AddTo(Disposables);
            FilterExtension  = new ReactivePropertySlim <string>().AddTo(Disposables);
            CollectedSize    = new ReactivePropertySlim <int>().AddTo(Disposables);
            StagedSize       = new ReactivePropertySlim <int>().AddTo(Disposables);
            VirtualProxySize = new ReactivePropertySlim <int>().AddTo(Disposables);

            LowerFilterName      = FilterName.Select(x => x?.ToLower()).ToReadOnlyReactivePropertySlim().AddTo(Disposables);
            LowerFilterExtension = FilterExtension.Select(x => x?.ToLower()).ToReadOnlyReactivePropertySlim().AddTo(Disposables);

            // 仮想Cコンテナにフィルター関数を設定します。
            virtualSource.SetFilter( x =>  x.Filter(LowerFilterName.Value, LowerFilterExtension.Value)  );

            // 検索ボックスの入力からトリガしてフィルターを実行します。
            FilterName
            .Merge(FilterExtension)
            .Throttle(TimeSpan.FromMilliseconds(200), UIDispatcherScheduler.Default)
            .Subscribe(async _ => await VirtualSource.ResetCollection())
            .AddTo(Disposables);

            // 画面表示用に定期更新します
            Observable.Interval(TimeSpan.FromMilliseconds(100), UIDispatcherScheduler.Default)
            .Subscribe(_ =>
            {
                CollectedSize.Value    = VirtualSource.SourceSize;
                StagedSize.Value       = VirtualSource.Items.Count;
                VirtualProxySize.Value = VirtualSource.ProxySize;
            }).AddTo(Disposables);
        }
Пример #2
0
        public MainWindowVm()
        {
            FilterText        = new ReactivePropertySlim <string>(string.Empty).AddTo(CompositeDisposable);
            ShowMegaEvolution = new ReactivePropertySlim <bool>().AddTo(CompositeDisposable);
            ShowBanLegend     = new ReactivePropertySlim <bool>().AddTo(CompositeDisposable);

            ShowDetailCommand = new DelegateCommand <PokemonListItemVm>(x => Messenger.Raise(new TransitionMessage(new PokemonDetailVm(x.Model), "ShowDetail")));

            var abilities = DataLoader.LoadAbilities();
            var types     = DataLoader.LoadTypes();
            var persons   = DataLoader.LoadPersonarities();
            var pokemons  = DataLoader.LoadPokemons();
            var moves     = DataLoader.LoadMoves();
            var forms     = DataLoader.LoadForms();

            var db = DataBase.Build(abilities, types, persons, pokemons, moves, forms);

            DataBaseService.Initialize(db);

            ItemsSource = db.Pokemons
                          .Select(x => new PokemonListItemVm(x))
                          .Concat(db.Forms.Select(x => new PokemonListItemVm(x)))
                          .OrderBy(x => x.ID)
                          .ToList();

            IdFilter = new List <FilterVm>()
            {
                new FilterVm("全国", (x) => true),
                new FilterVm("ガラル", (x) => x.GalarID != null),
            };

            IdFilter
            .Select(x => x.ObserveProperty(filterVm => filterVm.IsEnabled))
            .Merge()
            .StartWith()
            .Where(x => IdFilter.Any(filterVm => filterVm.IsEnabled))
            .Select(_ => IdFilter.FirstOrDefault(x => x.IsEnabled))
            .Throttle(TimeSpan.FromMilliseconds(50))
            .ObserveOnUIDispatcher()
            .Do(x =>
            {
                switch (x.Label)
                {
                case "全国":
                    foreach (var item in ItemsSource)
                    {
                        item.ID = item.Model.Id;
                    }
                    break;

                case "ガラル":
                    foreach (var item in ItemsSource)
                    {
                        item.ID = item.Model.GalarID ?? -1;
                    }
                    break;
                }
            })
            .Do(x => ApplyFilters())
            .Subscribe()
            .AddTo(CompositeDisposable);

            FilterText
            .Throttle(TimeSpan.FromMilliseconds(50))
            .ObserveOnUIDispatcher()
            .Subscribe(_ => Messenger.Raise(new InteractionMessage("RaiseFilter")))
            .AddTo(CompositeDisposable);

            ShowMegaEvolution
            .Merge(ShowBanLegend)
            .Throttle(TimeSpan.FromMilliseconds(5))
            .ObserveOnDispatcher()
            .Do(_ => ApplyFilters())
            .Subscribe();
        }