Пример #1
0
        public PeopleTopicView()
        {
            InitializeComponent();
            var click = Observable
                        .FromEventPattern <ItemClickEventArgs>(PeopleTopicsList, nameof(PeopleTopicsList.ItemClick))
                        .Select(x => x.EventArgs.ClickedItem as TopicModel)
                        .ObserveOnCoreDispatcher()
                        .Subscribe(x => PageStack.Next("Right", "Right", typeof(RepliesAndTopicView), x.Id));

            NotifyData.LoadDataTask = async count =>
            {
                var html = await ApiClient.GetFavoriteTopics(NotifyData.CurrentPage);

                var dom = new HtmlParser().ParseDocument(html);
                return(new PagesBaseModel <TopicModel>
                {
                    Pages = DomParse.ParseMaxPage(dom),
                    Entity = DomParse.ParseTopics(dom) ?? new List <TopicModel>()
                });
            };

            _events = new List <IDisposable> {
                click
            };
        }
Пример #2
0
        public MemberView()
        {
            InitializeComponent();
            async Task <MemberModel> loadData()
            {
                var html = await ApiClient.GetMemberInformation(_username);

                var cell   = new HtmlParser().ParseDocument(html).GetElementById("Main").QuerySelector("div.cell");
                var inputs = cell.QuerySelectorAll("input");

                if (!inputs.Any())
                {
                    return new MemberModel
                           {
                               Image    = cell.QuerySelector("img").GetAttribute("src"),
                               Username = cell.QuerySelector("h1").TextContent
                           }
                }
                ;
                return(new MemberModel
                {
                    Image = cell.QuerySelector("img").GetAttribute("src"),
                    Notice = "https://www.v2ex.com" + inputs[0].GetAttribute("onclick").Split('\'')[3],
                    IsNotice = inputs[0].GetAttribute("value"),
                    Block = "https://www.v2ex.com" + inputs[1].GetAttribute("onclick").Split('\'')[3],
                    IsBlock = inputs[1].GetAttribute("value"),
                    Username = cell.QuerySelector("h1").TextContent
                });
            }

            var load = Observable.FromEventPattern <RoutedEventArgs>(MemberPage, nameof(MemberPage.Loaded))
                       .SelectMany(x => loadData())
                       .Retry(10)
                       .ObserveOnCoreDispatcher()
                       .Subscribe(x => Member = x, x => { });
            var notice = Observable.FromEventPattern <RoutedEventArgs>(Notice, nameof(Notice.Tapped))
                         .Select(async x => await ApiClient.OnlyGet(Member.Notice))
                         .ObserveOnCoreDispatcher()
                         .Subscribe(x => Member.IsNotice = "取消特别关注", (Exception ex) => { });
            var block = Observable.FromEventPattern <RoutedEventArgs>(Block, nameof(Block.Tapped))
                        .Select(async x => await ApiClient.OnlyGet(Member.Block))
                        .ObserveOnCoreDispatcher()
                        .Subscribe(x => Member.IsBlock = "取消Block", (Exception ex) => { });
            var info = Observable.FromEventPattern <ItemClickEventArgs>(MemberInfoList, nameof(MemberInfoList.ItemClick))
                       .ObserveOnCoreDispatcher()
                       .Subscribe(x =>
            {
                var item = x.EventArgs.ClickedItem as TopicModel;

                PageStack.Next("Right", "Right", typeof(RepliesAndTopicView), item.Id);
            });

            Topics.LoadDataTask = async count =>
            {
                var html = await ApiClient.GetTopicsByUsername(_username, Topics.CurrentPage);

                if (html.Contains("主题列表被隐藏"))
                {
                    ListHiddenPanel.Visibility = Visibility.Visible;
                    return(new PagesBaseModel <TopicModel>
                    {
                        Pages = 0,
                        Entity = new List <TopicModel>()
                    });
                }

                var dom    = new HtmlParser().ParseDocument(html);
                var pages  = DomParse.ParseMaxPage(dom);
                var topics = dom.GetElementById("Main").GetElementsByClassName("cell item").Select(node =>
                {
                    var hrefs = node.QuerySelectorAll("a");
                    var topic = new TopicModel
                    {
                        Title    = hrefs[0].TextContent,
                        NodeName = hrefs[1].TextContent,
                        Member   = new MemberModel
                        {
                            Username = hrefs[2].TextContent
                        },
                        Id = int.Parse(new Regex("/t/([0-9]*)").Match(hrefs[0].GetAttribute("href")).Groups[1].Value)
                    };
                    if (hrefs.Length == 5)
                    {
                        topic.LastUsername = $"最后回复者 :{hrefs[3].TextContent}";
                        topic.Replies      = int.Parse(hrefs[4].TextContent);
                        var last           = node.QuerySelector("span.topic_info").TextContent.Split('•')[2].Trim();
                        last            = last.Contains("最后回复") ? "" : last;
                        topic.LastReply = $"时间 : {last.Trim()}";
                    }

                    return(topic);
                });
                return(new PagesBaseModel <TopicModel>
                {
                    Pages = pages,
                    Entity = topics
                });
            };
            Notifications.LoadDataTask = async count =>
            {
                var html = await ApiClient.GetRepliesByUsername(_username, Notifications.CurrentPage);

                var dom           = new HtmlParser().ParseDocument(html);
                var main          = dom.GetElementById("Main");
                var nodes         = main.QuerySelectorAll("div.dock_area");
                var replies       = main.QuerySelectorAll("div.reply_content");
                var pages         = DomParse.ParseMaxPage(dom);
                var notifications = nodes.Select(
                    (node, i) =>
                {
                    var href = node.QuerySelector("a");
                    var span = node.QuerySelector("span");
                    return(new NotificationModel
                    {
                        Title = href.TextContent,
                        Content = replies[i].TextContent,
                        ReplyDate = span.TextContent
                    });
                });
                return(new PagesBaseModel <NotificationModel>
                {
                    Pages = pages,
                    Entity = notifications
                });
            };

            _events = new List <IDisposable> {
                load, notice, block, info
            };
        }