public SearchPageViewModel(
            Models.NiconicoSession niconicoSession,
            SearchProvider searchProvider,
            Services.PageManager pageManager
            )
            : base(pageManager)
        {
            NiconicoSession = niconicoSession;
            SearchProvider = searchProvider;

            HashSet<string> HistoryKeyword = new HashSet<string>();
            foreach (var item in Database.SearchHistoryDb.GetAll().OrderByDescending(x => x.LastUpdated))
            {
                if (HistoryKeyword.Contains(item.Keyword))
                {
                    continue;
                }

                SearchHistoryItems.Add(new SearchHistoryListItem(item, this));
                HistoryKeyword.Add(item.Keyword);
            }

            SearchText = new ReactiveProperty<string>(_LastKeyword)
                .AddTo(_CompositeDisposable);

            TargetListItems = new List<SearchTarget>()
            {
                SearchTarget.Keyword,
                SearchTarget.Tag,
                SearchTarget.Niconama,
                SearchTarget.Mylist,
                SearchTarget.Community,
            };

            SelectedTarget = new ReactiveProperty<SearchTarget>(_LastSelectedTarget)
                .AddTo(_CompositeDisposable);

            SearchOptionVM = new ReactiveProperty<SearchOptionViewModelBase>();
            SearchOptionVMDict = new Dictionary<SearchTarget, SearchOptionViewModelBase>();

            SelectedTarget.Subscribe(x =>
            {
                RaiseSearchTargetFlags();

                var keyword = SearchOptionVM.Value?.Keyword ?? "";
                SearchOptionViewModelBase searchOptionVM = null;
                if (SearchOptionVMDict.ContainsKey(x))
                {
                    searchOptionVM = SearchOptionVMDict[x];
                }
                else
                {
                    searchOptionVM = SearchOptioniViewModelHelper.CreateFromTarget(x);
                    SearchOptionVMDict.Add(x, searchOptionVM);
                }

                searchOptionVM.Keyword = keyword;

                SearchOptionVM.Value = searchOptionVM;

            });



            DoSearchCommand =
                SearchText.Select(x => !String.IsNullOrEmpty(x))
                .ToReactiveCommand()
                .AddTo(_CompositeDisposable);

            SearchText.Subscribe(x =>
            {
                Debug.WriteLine($"検索:{x}");
            });


            DoSearchCommand.CanExecuteChangedAsObservable()
                .Subscribe(x =>
                {
                    Debug.WriteLine(DoSearchCommand.CanExecute());
                });
            DoSearchCommand.Subscribe(_ =>
            {
                if (SearchText.Value.Length == 0) { return; }

                // Note: Keywordの管理はSearchPage側で行うべき?
                SearchOptionVM.Value.Keyword = SearchText.Value;

                var searchOption = SearchOptionVM.Value.MakePayload();

                // 検索結果を表示
                PageManager.Search(searchOption);

                var searched = Database.SearchHistoryDb.Searched(SearchText.Value, SelectedTarget.Value);

                var oldSearchHistory = SearchHistoryItems.FirstOrDefault(x => x.Keyword == SearchText.Value);
                if (oldSearchHistory != null)
                {
                    SearchHistoryItems.Remove(oldSearchHistory);
                }
                SearchHistoryItems.Insert(0, new SearchHistoryListItem(searched, this));

            })
            .AddTo(_CompositeDisposable);
        }
示例#2
0
        public SearchPageViewModel(HohoemaApp hohoemaApp, PageManager pageManager, Views.Service.MylistRegistrationDialogService mylistDialogService)
            : base(hohoemaApp, pageManager)
        {
            _MylistDialogService = mylistDialogService;

            SearchText = new ReactiveProperty <string>("")
                         .AddTo(_CompositeDisposable);

            TargetListItems = new List <SearchTarget>()
            {
                SearchTarget.Keyword,
                SearchTarget.Tag,
                SearchTarget.Mylist,
                SearchTarget.Community,
                SearchTarget.Niconama,
            };

            SelectedTarget = new ReactiveProperty <SearchTarget>(TargetListItems[0])
                             .AddTo(_CompositeDisposable);

            SearchOptionVM     = new ReactiveProperty <SearchOptionViewModelBase>();
            SearchOptionVMDict = new Dictionary <SearchTarget, SearchOptionViewModelBase>();

            SelectedTarget.Subscribe(x =>
            {
                RaiseSearchTargetFlags();

                var keyword = SearchOptionVM.Value?.Keyword ?? "";
                SearchOptionViewModelBase searchOptionVM = null;
                if (SearchOptionVMDict.ContainsKey(x))
                {
                    searchOptionVM = SearchOptionVMDict[x];
                }
                else
                {
                    searchOptionVM = SearchOptioniViewModelHelper.CreateFromTarget(x);
                    SearchOptionVMDict.Add(x, searchOptionVM);
                }

                searchOptionVM.Keyword = keyword;

                SearchOptionVM.Value = searchOptionVM;
            });



            DoSearchCommand =
                SearchText.Select(x => !String.IsNullOrEmpty(x))
                .ToReactiveCommand()
                .AddTo(_CompositeDisposable);

            SearchText.Subscribe(x =>
            {
                Debug.WriteLine($"検索:{x}");
            });


            DoSearchCommand.CanExecuteChangedAsObservable()
            .Subscribe(x =>
            {
                Debug.WriteLine(DoSearchCommand.CanExecute());
            });
            DoSearchCommand.Subscribe(_ =>
            {
                if (SearchText.Value.Length == 0)
                {
                    return;
                }

                // Note: Keywordの管理はSearchPage側で行うべき?
                SearchOptionVM.Value.Keyword = SearchText.Value;

                var searchOption = SearchOptionVM.Value.MakePayload();

                // 検索結果を表示
                PageManager.Search(searchOption);
            })
            .AddTo(_CompositeDisposable);
        }