public ReportsPageViewModel(IViewModelHost host) : base(host) { // setup... this.Items = new ObservableCollection<ReportViewItem>(); this.SelectedItems = new List<ReportViewItem>(); // commands... this.RefreshCommand = new DelegateCommand(async (e) => { this.Host.HideAppBar(); await this.DoRefresh(true); // toast... string message = "I found 1 report."; if (this.Items.Count != 1) message = string.Format("I found {0} reports.", this.Items.Count); var toast = new ToastNotificationBuilder(new string[] { "Reports refreshed.", message }); toast.ImageUri = "ms-appx:///Assets/Toast.jpg"; toast.Update(); }); // update any selection that we were given... this.SelectionCommand = new DelegateCommand((args) => { // update the selection... this.SelectedItems.Clear(); foreach (ReportViewItem item in (IEnumerable<object>)args) this.SelectedItems.Add(item); // raise... this.OnPropertyChanged("SelectedItems"); this.OnPropertyChanged("HasSelectedItems"); }); // dump the state... this.DumpSelectionCommand = new DelegateCommand(async (e) => { if (this.SelectedItems.Count > 0) { var builder = new StringBuilder(); foreach (var item in this.SelectedItems) { if (builder.Length > 0) builder.Append("\r\n"); builder.Append(item.Title); } // show... await this.Host.ShowAlertAsync(builder.ToString()); } else await this.Host.ShowAlertAsync("(No selection)"); }); }
private async Task SearchAsync(string queryText) { // flag... this.SearchDone = true; // set... this.QueryText = queryText; // set the narrative... if (string.IsNullOrEmpty(queryText)) this.QueryNarrative = string.Empty; else this.QueryNarrative = string.Format("Results for '{0}'", queryText); // load... var reports = await ReportItem.SearchCacheAsync(queryText); this.MasterItems.Clear(); foreach (var report in reports) this.MasterItems.Add(new ReportViewItem(report)); // what was our selected filter? var current = this.ActiveFilter; if (current != null) { var builder = new ToastNotificationBuilder(current.Description); builder.Update(); } // do we have anything? this.Filters.Clear(); if (this.MasterItems.Any()) { // all filter... var allFilter = new SearchFilter("all", this.MasterItems.Count, null, false); allFilter.SelectionCommand = new DelegateCommand(async (args) => await HandleFilterActivatedAsync(allFilter)); this.Filters.Add(allFilter); // build up a list of nouns... var nouns = new Dictionary<string, int>(); var regex = new Regex(@"\b\w+$", RegexOptions.Singleline | RegexOptions.IgnoreCase); foreach (var report in reports) { var match = regex.Match(report.Title); // word... string noun = match.Value.ToLower(); if (!(nouns.ContainsKey(noun))) nouns[noun] = 0; nouns[noun]++; } // add the filters... foreach (var noun in nouns.Keys) { var filter = new SearchFilter(noun, nouns[noun], noun); filter.SelectionCommand = new DelegateCommand(async (args) => await HandleFilterActivatedAsync(filter)); this.Filters.Add(filter); } // update... var manager = new ReportImageCacheManager(); foreach (var report in this.MasterItems) await report.InitializeAsync(manager); } // do we need to select the filter? var lastQuery = await SettingItem.GetValueAsync(LastQueryKey); if (lastQuery == queryText) { // select the filter... var lastFilterName = await SettingItem.GetValueAsync(LastFilterKey); if (!(string.IsNullOrEmpty(lastFilterName))) ActivateFilter(lastFilterName); } else { // update... await SettingItem.SetValueAsync(LastQueryKey, queryText); } // apply the filter... this.ApplyFilter(); }