public T Find(int id) { var cached = CachedItems.FirstOrDefault(_ => _.Id == id); if (cached == null) { var item = ItemsProvider.Find(id); if (item == null) { return(null); } var index = FetchIndexForItem(item); if (index == -1) { return(null); } int pageIndex = index / PageSize; int pageOffset = index % PageSize; RequestPage(pageIndex); return(pages[pageIndex][pageOffset]); } return(cached); }
async Task LoadMoreItemsAsync(bool refresh = false) { if (!HasMoreItems && !refresh) { IsLoading = false; return; } try { if (refresh) { PageCount = 1; Pagination = PaginationParameters.MaxPagesToLoad(1); } Views.Main.ActivitiesView.Current?.ShowTopLoadingFollowers(); var result = await InstaApi.UserProcessor.GetUserFollowingByIdAsync(CurrentUser.Pk, Pagination); PageCount++; FirstRun = false; Pagination.MaximumPagesToLoad = 2; if (!result.Succeeded) { IsLoading = false; if (result.Value == null || result.Value?.Count == 0) { Views.Main.ActivitiesView.Current?.HideTopLoadingFollowers(); return; } } if (string.IsNullOrEmpty(result.Value.NextMaxId)) { HasMoreItems = false; } Pagination.NextMaxId = result.Value.NextMaxId; if (refresh) { Items.Clear(); CachedItems.Clear(); } var userIds = new List <long>(); if (result.Value?.Count > 0) { result.Value.ForEach(x => { userIds.Add(x.Pk); CachedItems.Add(x.ToUserShortFriendship()); }); } try { if (userIds.Count > 0) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => { var friendshipStatuses = await InstaApi.UserProcessor.GetFriendshipStatusesAsync(userIds.ToArray()); if (friendshipStatuses.Succeeded) { var friends = friendshipStatuses.Value; friends.ForEach(x => { var t = CachedItems.FirstOrDefault(u => u.Pk == x.Pk); if (t != null) { t.FriendshipStatus = x; } }); } CachedItems.ForEach(x => { if (x.FriendshipStatus != null) { if (!x.FriendshipStatus.Following) { Items.Add(x); } } }); Views.Main.ActivitiesView.Current?.HideTopLoadingFollowers(); }); } } catch { Views.Main.ActivitiesView.Current?.HideTopLoadingFollowers(); } await Task.Delay(1000); IsLoading = false; } catch (Exception ex) { FirstRun = IsLoading = false; ex.PrintException("FollowingGenerator.LoadMoreItemsAsync"); Views.Main.ActivitiesView.Current?.HideTopLoadingFollowers(); } }