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); }
public override async Task OnNavigatedToAsync(INavigationParameters parameters) { IsNavigationFailed.Value = false; NavigationFailedReason.Value = null; try { string keyword = null; if (parameters.TryGetValue("keyword", out keyword)) { keyword = Uri.UnescapeDataString(keyword); } SearchTarget target = SearchTarget.Keyword; if (parameters.TryGetValue("service", out string modeString)) { Enum.TryParse <SearchTarget>(modeString, out target); } else if (parameters.TryGetValue("service", out target)) { } var pageName = target switch { SearchTarget.Keyword => nameof(SearchResultKeywordPage), SearchTarget.Tag => nameof(SearchResultTagPage), SearchTarget.Niconama => nameof(SearchResultLivePage), _ => null }; if (pageName != null && keyword != null) { var result = await NavigationService.NavigateAsync(pageName, ("keyword", keyword)); if (!result.IsSuccess) { throw result.Exception; } } SearchText.Value = keyword; SelectedTarget.Value = target; _LastSelectedTarget = target; _LastKeyword = keyword; DoSearchCommand.Throttle(TimeSpan.FromMilliseconds(50), _scheduler).Subscribe(_ => { //await Task.Delay(50); if (SearchText.Value?.Length == 0) { return; } if (_LastSelectedTarget == SelectedTarget.Value && _LastKeyword == SearchText.Value) { return; } // 検索結果を表示 PageManager.Search(SelectedTarget.Value, SearchText.Value); var searched = _searchHistoryRepository.Searched(SearchText.Value, SelectedTarget.Value); var oldSearchHistory = SearchHistoryItems.FirstOrDefault(x => x.Keyword == SearchText.Value); if (oldSearchHistory != null) { SearchHistoryItems.Remove(oldSearchHistory); } SearchHistoryItems.Insert(0, new SearchHistoryListItemViewModel(searched, this)); }) .AddTo(_navigationDisposables); } catch (Exception e) { IsNavigationFailed.Value = true; #if DEBUG NavigationFailedReason.Value = e.Message; #endif Debug.WriteLine(e.ToString()); } await base.OnNavigatedToAsync(parameters); }