Ejemplo n.º 1
0
 public Task <int> SaveItemAsync(JokeItem item)
 {
     if (item.Id != 0)
     {
         return(Database.UpdateAsync(item));
     }
     else
     {
         return(Database.InsertAsync(item));
     }
 }
Ejemplo n.º 2
0
        public async void DeleteJoke(JokeItem joke)
        {
            Scroll = ScrollEnum.NoScroll;

            JokeList.Remove(joke);
            UpdateBindedCollection();

            if (Device.RuntimePlatform == Device.Android || Device.RuntimePlatform == Device.UWP)
            {
                await _databaseService.DeleteJoke(joke);
            }
        }
Ejemplo n.º 3
0
        public async void AddJokeAsync()
        {
            try
            {
                if (!string.IsNullOrEmpty(url))
                {
                    Scroll = ScrollEnum.Scroll;
                    var actualCategory = (Category == 0) ? (CategoryEnum)random.Next(1, 16) : Category;

                    var joke = await _jokeService.GetJokeAsync(url, actualCategory.ToString().ToLower()); //if category is "All", select random one

                    if (joke != null)
                    {
                        int id = 1;

                        if (JokeList.Count > 0)
                        {
                            id = JokeList.Last().Id + 1;
                        }

                        var jokeItem = new JokeItem
                        {
                            Id       = id,
                            Icon     = iconUrl,
                            Joke     = joke.value,
                            Url      = new Uri(joke.url),
                            RestId   = joke.icon_url,
                            Category = actualCategory
                        };

                        JokeList.Add(jokeItem);
                        UpdateBindedCollection();

                        if (Device.RuntimePlatform == Device.Android || Device.RuntimePlatform == Device.UWP)
                        {
                            await _databaseService.AddJoke(jokeItem);
                        }
                    }
                    else
                    {
                        _toastMessage.ShowToast(commonErrorMessage);
                    }
                }
                else
                {
                    _toastMessage.ShowToast(apiErrorMessage);
                }
            }
            catch { _toastMessage.ShowToast(commonErrorMessage); }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 获取笑话列表
        /// </summary>
        /// <param name="htmlContent"></param>
        public static List <JokeItem> GetJokeList(int pageIndex)
        {
            string          htmlContent = GetUrlContent(GetWBJokeUrl(pageIndex));
            List <JokeItem> jokeList    = new List <JokeItem>();
            HtmlDocument    htmlDoc     = new HtmlDocument();

            htmlDoc.LoadHtml(htmlContent);

            HtmlNode rootNode           = htmlDoc.DocumentNode;
            string   xpathOfJokeDiv     = "//div[@class='article block untagged mb15']";
            string   xpathOfJokeContent = "./a/div[@class='content']/span";
            string   xpathOfImg         = "./div[@class='author clearfix']/a/img";

            try
            {
                HtmlNodeCollection jokeCollection = rootNode.SelectNodes(xpathOfJokeDiv);
                int      jokeCount = jokeCollection.Count;
                JokeItem joke;
                foreach (HtmlNode jokeNode in jokeCollection)
                {
                    joke = new JokeItem();
                    HtmlNode contentNode = jokeNode.SelectSingleNode(xpathOfJokeContent);
                    if (contentNode != null)
                    {
                        joke.JokeContent = Regex.Replace(contentNode.InnerText, "(\r\n)+", "\r\n");
                    }
                    else
                    {
                        joke.JokeContent = "";
                    }
                    HtmlNode imgornameNode = jokeNode.SelectSingleNode(xpathOfImg);
                    if (imgornameNode != null)
                    {
                        joke.NickName  = imgornameNode.GetAttributeValue("alt", "");
                        joke.HeadImage = GetWebImage("http:" + imgornameNode.GetAttributeValue("src", ""));
                        joke.HeadImage = joke.HeadImage != null ? new Bitmap(joke.HeadImage, 50, 50) : null;
                    }
                    else
                    {
                        joke.NickName  = "匿名用户";
                        joke.HeadImage = null;
                    }
                    jokeList.Add(joke);
                }
            }
            catch { }
            return(jokeList);
        }
Ejemplo n.º 5
0
        public async Task <bool> DeleteJoke(JokeItem joke)
        {
            try
            {
                using (var db = new SQLiteConnection(GetDataPath(), SQLiteOpenFlags.ReadWrite |
                                                     SQLiteOpenFlags.FullMutex))
                {
                    await Task.Run(() => db.Delete(joke));

                    return(true);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                return(false);
            }
        }
Ejemplo n.º 6
0
        public async Task <bool> AddJoke(JokeItem joke)
        {
            if (FileExists())
            {
                try
                {
                    using (var db = new SQLiteConnection(GetDataPath(), SQLiteOpenFlags.ReadWrite |
                                                         SQLiteOpenFlags.FullMutex))
                    {
                        await Task.Run(() => db.Insert(joke));

                        return(true);
                    }
                }
                catch { return(false); }
            }
            else
            {
                if (DbCreate())
                {
                    try
                    {
                        using (var db = new SQLiteConnection(GetDataPath(), SQLiteOpenFlags.ReadWrite |
                                                             SQLiteOpenFlags.FullMutex))
                        {
                            await Task.Run(() => db.Insert(joke));

                            return(true);
                        }
                    }
                    catch { return(false); }
                }
                else
                {
                    return(false);
                }
            }
        }
Ejemplo n.º 7
0
 public Task <int> DeleteItemAsync(JokeItem item)
 {
     return(Database.DeleteAsync(item));
 }