Ejemplo n.º 1
0
        /// <summary>
        /// Return a range of all saved posts if they exist
        /// </summary>
        /// <param name="category"></param>
        /// <param name="index"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static ModelWrapper <List <Post> > GetPostsOffline(WordPressManager.PostRequest postRequest)
        {
            var allPosts = GetAllPostsOffline(postRequest);

            // there is some posts
            if (allPosts.Result == TaskResult.Success)
            {
                List <Post> queriedPostList = allPosts.Data;

                // apply search
                if (postRequest.CurrentSearch != string.Empty)
                {
                    CompareInfo compareInfo = CultureInfo.CurrentCulture.CompareInfo;

                    queriedPostList = queriedPostList
                                      .Where(post =>
                                             compareInfo.IndexOf(post.Title.Rendered, postRequest.CurrentSearch, CompareOptions.IgnoreCase) >= 0 ||
                                             compareInfo.IndexOf(post.Content.Rendered, postRequest.CurrentSearch, CompareOptions.IgnoreCase) >= 0)
                                      .ToList();
                }

                // apply range
                int validCount = Math.Min(queriedPostList.Count - postRequest.CurrentOffset, postRequest.CurrentQuantity);

                if (validCount > 0)
                {
                    queriedPostList = queriedPostList.GetRange(postRequest.CurrentOffset, validCount);

                    return(new ModelWrapper <List <Post> >(queriedPostList, TaskResult.Success));
                }
            }

            // at this point either postsList is empty or file does not exist
            return(new ModelWrapper <List <Post> >(TaskResult.NoData));
        }
Ejemplo n.º 2
0
        public async Task FetchNews(CNHCategory category)
        {
            // clear posts in case category is changed
            if (category != Category)
            {
                ClearItems();
            }

            IsLoading = true;
            OnLoading();

            WordPressManager.PostRequest postRequest = new WordPressManager.PostRequest();
            postRequest
            .Embeded()
            .Category(category)
            .Quantity(10)
            .Search(SearchKeyword)
            .Save(!IsSearchOn)
            .Update(IsSearchOn && ConnectionInfo.InternetConnected());
            var postsData = await WordpressExtension.GetPosts(postRequest);

            switch (postsData.Result)
            {
            case TaskResult.Error:
                OnError();
                break;

            case TaskResult.NoInternet:
                OnConnectionError();
                break;

            case TaskResult.NoData:
                OnNoData();
                break;

            case TaskResult.Success:
                ReplaceItems(postsData.Data);
                break;

            default:
                break;
            }

            Category = category;

            IsLoading = false;
            OnLoaded();

            CanLoadMore = true;
        }
Ejemplo n.º 3
0
        public async Task LoadMore()
        {
            if (IsLoadingMore || !CanLoadMore)
            {
                return;
            }

            SetLoadingAnimation(true);

            WordPressManager.PostRequest postRequest = new WordPressManager.PostRequest();
            int indexSubstitution = IsLoadingMore ? 1 : 0;

            postRequest
            .Embeded()
            .Category(Category)
            .Quantity(10)
            .Offset(ItemCount - indexSubstitution)
            .Search(SearchKeyword)
            .Save(!IsSearchOn)
            .Update(IsSearchOn && ConnectionInfo.InternetConnected());

            var postsData = await WordpressExtension.GetPosts(postRequest);

            switch (postsData.Result)
            {
            case TaskResult.Error:
                OnError();
                break;

            case TaskResult.NoInternet:
                OnConnectionError();
                break;

            case TaskResult.NoData:
                CanLoadMore = false;
                break;

            case TaskResult.Success:
                AddItems(postsData.Data);
                break;

            default:
                break;
            }

            SetLoadingAnimation(false);
        }
Ejemplo n.º 4
0
        public async Task RefreshNews()
        {
            IsLoading = true;
            OnLoading();

            WordPressManager.PostRequest postRequest = new WordPressManager.PostRequest();
            postRequest
            .Embeded()
            .Category(Category)
            .Quantity(10)
            .Search(SearchKeyword)
            .Update(true)
            .Save(!IsSearchOn);

            var postsData = await WordpressExtension.GetPosts(postRequest);

            switch (postsData.Result)
            {
            case TaskResult.Error:
                OnError();
                break;

            case TaskResult.NoInternet:
                OnConnectionError();
                break;

            case TaskResult.NoData:
                OnNoData();
                break;

            case TaskResult.Success:
                ReplaceItems(postsData.Data);
                break;

            default:
                break;
            }

            IsLoading = false;
            OnLoaded();

            CanLoadMore = true;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Return all saved posts if they exist
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        public static ModelWrapper <List <Post> > GetAllPostsOffline(WordPressManager.PostRequest postRequest)
        {
            List <Post> postsList = new List <Post>();

            string filePath = GetPostFilePath(postRequest.CurrentCategory);

            if (File.Exists(filePath))
            {
                using (StreamReader streamReader = new StreamReader(filePath))
                {
                    postsList = JsonConvert.DeserializeObject <List <Post> >(streamReader.ReadToEnd());

                    if (postsList != null && postsList.Count > 0)
                    {
                        return(new ModelWrapper <List <Post> >(postsList, TaskResult.Success));
                    }
                }
            }

            // at this point either postsList is empty or file does not exist
            return(new ModelWrapper <List <Post> >(TaskResult.NoData));
        }