Пример #1
0
        private async Task <List <GroupBookList <object> > > GetBookSearchResult(string searchTerm)
        {
            try
            {
                BookList instanceBooks = new BookList();
                List <GroupBookList <object> > BookGroups = new List <GroupBookList <object> >();
                ObservableCollection <Books>   bookList   = await instanceBooks.GetBookLists();

                var query = from book in bookList
                            where ((Books)book).title.ToLower().Contains(searchTerm.ToLower())
                            orderby((Books)book).title
                            group book by((Books)book).category__name into books
                            select new { GroupName = books.Key, Items = books, Number = books.Count() };

                foreach (var books in query)
                {
                    GroupBookList <object> info = new GroupBookList <object>();
                    info.Group       = books.GroupName;
                    info.ItemsNumber = books.Number;

                    foreach (var book in books.Items)
                    {
                        info.Add(book);
                    }

                    BookGroups.Add(info);
                }

                return(BookGroups);
            }
            catch (TaskCanceledException ex)
            {
                return(null);
            }
        }
Пример #2
0
        protected async override void OnNavigatedTo(NavigationEventArgs args)
        {
            BookList instanceBooks = new BookList();
            ObservableCollection <Books> bookList = await instanceBooks.GetBookLists();

            var queryText = args.Parameter.ToString().ToLower() as String;

            var requestedBooks = await GetBookSearchResult(queryText);

            if (requestedBooks != null)
            {
                int countBooks = requestedBooks.Count;

                var totalItems    = 0;
                var searchFilters = new List <Filter>();
                SearchResult = new Dictionary <string, IEnumerable <Books> >();

                if (countBooks > 0)
                {
                    foreach (var groups in requestedBooks)
                    {
                        IEnumerable <Books> matchItems = bookList.Where(book => book.title.ToLower().Contains(queryText) && book.category__name == (string)groups.Group);
                        int itemsNumber = matchItems.Count <Books>();

                        totalItems = totalItems + itemsNumber;
                    }

                    searchFilters.Add(new Filter("All", totalItems, true));

                    foreach (var groups in requestedBooks)
                    {
                        IEnumerable <Books> matchItems = bookList.Where(book => book.title.ToLower().Contains(queryText) && book.category__name == (string)groups.Group);
                        int itemsNumber = matchItems.Count <Books>();

                        if (itemsNumber > 0)
                        {
                            SearchResult.Add(groups.Group.ToString(), matchItems);
                            searchFilters.Add(new Filter(groups.Group.ToString(), itemsNumber, false));
                            totalItems = totalItems + itemsNumber;
                        }
                    }
                }
                else
                {
                    VisualStateManager.GoToState(this, "NoResultsFound", true);
                }

                this.DefaultViewModel["QueryText"]   = '\u201c' + queryText + '\u201d';
                this.DefaultViewModel["Filters"]     = searchFilters;
                this.DefaultViewModel["ShowFilters"] = searchFilters.Count > 1;
            }
            else
            {
                var popup = new Windows.UI.Popups.MessageDialog("Couldnt fetch books. Plaease check your internet connection.");
                popup.Commands.Add(new Windows.UI.Popups.UICommand("Try Again"));
                popup.Commands.Add(new Windows.UI.Popups.UICommand("Cancel"));

                popup.DefaultCommandIndex = 0;
                popup.CancelCommandIndex  = 1;

                var results = await popup.ShowAsync();

                if (results.Label == "Try Again")
                {
                    OnNavigatedTo(args);
                }
                else
                {
                    this.Frame.GoBack();
                }
            }
        }