Ejemplo n.º 1
0
        public async Task <GoogleBooks> GetGoogleBookList(string queryText)
        {
            try
            {
                Random              random     = new Random();
                int                 startIndex = random.Next(1, 10);
                HttpClient          client     = new HttpClient();
                HttpResponseMessage response   = await client.GetAsync(new Uri(googleBaseApi + "volumes?q=" + queryText + "&maxResults=40&startIndex=" + startIndex));

                var result = await response.Content.ReadAsStringAsync();

                GoogleBooks googleBooks = JsonConvert.DeserializeObject <GoogleBooks>(result);

                return(googleBooks);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
        public async Task <GoogleBooks> GetGoogleBookLists(string searchTerm, int startIndex)
        {
            ObservableCollection <GoogleBooks> BooksList = new ObservableCollection <GoogleBooks>();

            try
            {
                HttpClient          client   = new HttpClient();
                HttpResponseMessage response = await client.GetAsync(new Uri(googleBaseApi + "volumes?q=" + searchTerm + "&maxResults=40&startIndex=" + startIndex));

                var result = await response.Content.ReadAsStringAsync();

                GoogleBooks googleBooks = JsonConvert.DeserializeObject <GoogleBooks>(result);

                return(googleBooks);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
        private async Task <GoogleBooks> LoadGoogleBooks()
        {
            HttpWebRequest postRequest = (HttpWebRequest)WebRequest.Create(googleBaseApi + "volumes?q=a&maxResults=40&&startIndex=100");

            postRequest.Method          = "GET";
            postRequest.CookieContainer = new CookieContainer();

            HttpWebResponse postResponse = (HttpWebResponse)await postRequest.GetResponseAsync();

            string response = String.Empty;

            if (postResponse != null)
            {
                Stream       postResponseStream = postResponse.GetResponseStream();
                StreamReader postStreamReader   = new StreamReader(postResponseStream);
                response = await postStreamReader.ReadToEndAsync();
            }

            GoogleBooks booksList = JsonConvert.DeserializeObject <GoogleBooks>(response);

            return(booksList);
        }
Ejemplo n.º 4
0
        protected async override void OnNavigatedTo(NavigationEventArgs args)
        {
            var queryText = args.Parameter.ToString().ToLower() as String;

            GoogleBooks booksList = await GetGoogleBookList(queryText);

            if (booksList != null)
            {
                List <GroupBookList <object> > BookGroups = new List <GroupBookList <object> >();
                ObservableCollection <Items>   booksItems = booksList.items;

                var query = from book in booksItems
                            orderby((Items)book).volumeInfo.title
                            group book by((Items) book).volumeInfo.categories[0] into books
                            select new { GroupName = books.Key, Items = books };

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

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

                    BookGroups.Add(info);
                }

                int countBooks    = BookGroups.Count;
                var totalItems    = 0;
                var searchFilters = new List <Filter>();
                SearchResult = new Dictionary <string, IEnumerable <Items> >();

                if (countBooks > 0)
                {
                    foreach (var groups in BookGroups)
                    {
                        IEnumerable <Items> matchItems = booksItems.Where(book => book.volumeInfo.title.ToLower().Contains(queryText) && book.volumeInfo.categories[0] == (string)groups.Group);
                        int itemsNumber = matchItems.Count <Items>();

                        totalItems = totalItems + itemsNumber;
                    }

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


                    foreach (var groups in BookGroups)
                    {
                        IEnumerable <Items> matchItems = booksItems.Where(book => book.volumeInfo.title.ToLower().Contains(queryText) && book.volumeInfo.categories[0] == (string)groups.Group);
                        int itemsNumber = matchItems.Count <Items>();

                        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();
                }
            }
        }
        protected async override void OnNavigatedTo(NavigationEventArgs args)
        {
            try
            {
                Dictionary <string, string> parameter = new Dictionary <string, string>();
                parameter = args.Parameter as Dictionary <string, string>;
                int    index     = Convert.ToInt32(parameter["index"]);
                string queryText = parameter["query"];

                GoogleBooks booksList = await GetGoogleBookLists(queryText, index);

                List <GroupBookList <object> > BookGroups = new List <GroupBookList <object> >();
                ObservableCollection <Items>   booksItems = booksList.items;

                var query = from book in booksItems
                            orderby((Items)book).volumeInfo.title
                            group book by((Items) book).volumeInfo.categories[0] into books
                            select new { GroupName = books.Key, Items = books };

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

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

                    BookGroups.Add(info);
                }
                if (BookGroups != null)
                {
                    VisualStateManager.GoToState(this, "ResultsFound", true);
                    groupedGoogleBooks.Source = BookGroups;
                }
                else
                {
                    ShowProgressRing();
                }
            }
            catch (Exception ex)
            {
                var popup = new Windows.UI.Popups.MessageDialog("The app couldn't load books. Please check your connection and try again");
                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")
                {
                    ShowProgressRing();
                    OnNavigatedTo(args);
                }
                else
                {
                    this.Frame.Navigate(typeof(HomePage));
                }
            }
        }