Esempio n. 1
0
        private async Task <IDataModel> GetFromCacheOrApi(int item, string dataModelType)
        {
            IDataModel isCached = Cache.GetItem(item);
            IDataModel result;

            if (isCached != null)
            {
                result = isCached;
            }
            else
            {
                string url = BaseItemUrl + item.ToString();
                if (dataModelType == "story")
                {
                    result = await HackerNewsApi.ApiHandler <StoryModel>(url);
                }
                else
                {
                    result = await HackerNewsApi.ApiHandler <CommentModel>(url);
                }
                if (result != null)
                {
                    Cache.AddItem(result);
                }
            }

            return(result);
        }
Esempio n. 2
0
        public async Task <GetStoriesResponse> GetStories(string storyType, int count, int id = 0, string order = "asc")
        {
            if (CurrentStoryType != storyType)
            {
                CurrentStoryType = storyType;
                string url = BaseStoryUrl + CurrentStoryType;
                CurrentCollectionIds = await HackerNewsApi.ApiHandler <List <int> >(url);
            }

            int index = 0;

            if (id != 0)
            {
                //chequear si el id existe en la lista
                index = CurrentCollectionIds.FindIndex(a => a == id);

                if (order == "desc")
                {
                    index -= count;
                }
            }

            if (index + count > CurrentCollectionIds.Count)
            {
                count = CurrentCollectionIds.Count - index;
            }

            List <int> resultRange = CurrentCollectionIds.GetRange(index, count);

            List <StoryModel> storiesCollection = new List <StoryModel>();

            foreach (var item in resultRange)
            {
                StoryModel result = await GetFromCacheOrApi(item, "story") as StoryModel;

                storiesCollection.Add(result);
            }

            return(new GetStoriesResponse
            {
                StoriesCollection = storiesCollection,
                NextItem = CheckIfNextItem(resultRange)
            });
        }