private View GetTemplateDelegate(int i, AnimeReviewData animeReviewData, View convertView)
        {
            var view = convertView;

            if(!_reviewStates.ContainsKey(animeReviewData))
                _reviewStates.Add(animeReviewData, false);

            if (view == null)
            {
                view = MainActivity.CurrentContext.LayoutInflater.Inflate(Resource.Layout.AnimeReviewItemLayout,null);
                view.Click += OnReviewClick;
            }
            view.Tag = new ReviewWrapperClass() { Data = animeReviewData };

            view.FindViewById(Resource.Id.AnimeReviewItemLayoutReviewContent).Visibility =
                _reviewStates[animeReviewData] ? ViewStates.Visible : ViewStates.Gone;

            var img = view.FindViewById<ImageViewAsync>(Resource.Id.AnimeReviewItemLayoutAvatarImage);
            ImageService.Instance.LoadUrl(animeReviewData.AuthorAvatar, TimeSpan.FromDays(1))
                .FadeAnimation(true, true, 500)
                .Into(img);
            view.FindViewById<TextView>(Resource.Id.AnimeReviewItemLayoutAuthor).Text = animeReviewData.Author;
            view.FindViewById<TextView>(Resource.Id.AnimeReviewItemLayoutDate).Text = animeReviewData.Date;
            view.FindViewById<TextView>(Resource.Id.AnimeReviewItemLayoutOverallScore).Text = animeReviewData.OverallRating;
            view.FindViewById<TextView>(Resource.Id.AnimeReviewItemLayoutEpsSeen).Text = animeReviewData.EpisodesSeen;
            view.FindViewById<TextView>(Resource.Id.AnimeReviewItemLayoutHelpfulCount).Text = animeReviewData.HelpfulCount;
            view.FindViewById<TextView>(Resource.Id.AnimeReviewItemLayoutReviewContent).Text = animeReviewData.Review;

            var scores = view.FindViewById<LinearLayout>(Resource.Id.AnimeReviewItemLayoutMarksList);
            scores.RemoveAllViews();
            foreach (var score in animeReviewData.Score)
            {
                var txt = new TextView(view.Context);
                txt.Text = $"{score.Field} {score.Score}";
                txt.Typeface = Typeface.Create(ResourceExtension.FontSizeLight,TypefaceStyle.Normal);
                txt.SetTextColor(new Color(ResourceExtension.BrushText));
                scores.AddView(txt);
            }

            return view;
        }
Example #2
0
        public async Task<List<AnimeReviewData>> GetAnimeReviews(bool force = false)
        {
            var output = force
                ? new List<AnimeReviewData>()
                : await DataCache.RetrieveReviewsData(_targetId, _anime) ?? new List<AnimeReviewData>();
            if (output.Count != 0) return output;

            var raw = await GetRequestResponse(false);
            if (string.IsNullOrEmpty(raw))
                return null;

            try
            {
                var doc = new HtmlDocument();
                doc.LoadHtml(raw);
                var reviewNodes = doc.DocumentNode.Descendants("div")
                    .Where(
                        node =>
                            node.Attributes.Contains("class") &&
                            node.Attributes["class"].Value ==
                            HtmlClassMgr.ClassDefs["#Reviews:reviewNode:class"]).Take(Settings.ReviewsToPull);

                foreach (var reviewNode in reviewNodes)
                {
                    try
                    {
                        var current = new AnimeReviewData();
                        //Details
                        var detailsNode = reviewNode.Descendants("div").First();
                        var pictureNode = detailsNode.Descendants("div").Where(node =>
                            node.Attributes.Contains("class") &&
                            node.Attributes["class"].Value ==
                            HtmlClassMgr.ClassDefs["#Reviews:reviewNode:pictureNodeClass"])
                            .Skip(1)
                            .First() //2nd picSurround
                            .Descendants("a").First(); //2nd a tag
                        current.Author = pictureNode.Attributes["href"].Value.Split('/')[2];
                        current.AuthorAvatar = pictureNode.Descendants("img").First().Attributes["data-src"].Value;
                        //
                        current.HelpfulCount = detailsNode.Descendants("div")
                            .First(node =>
                                node.Attributes.Contains("class") &&
                                node.Attributes["class"].Value ==
                                HtmlClassMgr.ClassDefs["#Reviews:reviewNode:helpfulCountNode"]).InnerText;
                        //
                        var rightTableNodeDivs =
                            detailsNode.Descendants("td").Skip(2).First().Descendants("div").ToList();
                        current.Date = rightTableNodeDivs[0].InnerText;
                        current.EpisodesSeen = rightTableNodeDivs[1].InnerText;
                        current.OverallRating = rightTableNodeDivs[2].InnerText;
                        //Review Content
                        var reviewNodeContent = reviewNode.Descendants("div").First(node =>
                            node.Attributes.Contains("class") &&
                            node.Attributes["class"].Value == HtmlClassMgr.ClassDefs["#Reviews:reviewNode:contentNode"]);
                        foreach (var scoreRow in reviewNodeContent.ChildNodes[1].Descendants("tr").Skip(1))
                        {
                            var tds = scoreRow.Descendants("td").ToList();
                            current.Score.Add(new ReviewScore {Field = tds[0].InnerText,Score = tds[1].InnerText == "&nbsp;" ? "N/A" : tds[1].InnerText });
                        }
                        reviewNodeContent.ChildNodes.Remove(1);
                        current.Review =
                            WebUtility.HtmlDecode(reviewNodeContent.InnerText.Trim().Replace("read more", ""));

                        output.Add(current);
                    }
                    catch (Exception)
                    {
                        //something unexpected
                    }
                }
            }
            catch (Exception)
            {
                //no reviews
            }
            DataCache.SaveAnimeReviews(_targetId, output, _anime);

            return output;
        }