/// <summary> /// Requests a collection of rants sorted and selected by the arguments from the rest-api. /// </summary> /// <param name="sort">Sorting of the rant collection.</param> /// <param name="limit">Maximal rants to return.</param> /// <param name="skip">Number of rants to skip.</param> /// <inheritdoc /> public async Task <IReadOnlyCollection <RantInfo> > GetRantsAsync(RantSort sort = RantSort.Algo, int limit = 50, int skip = 0) { var sortText = sort .ToString() .ToLower(); var response = await client.GetAsync($"/api/devrant/rants?app={appVersion}&sort={sortText}&limit={limit}&skip={skip}"); var responseText = await response.Content.ReadAsStringAsync(); return(ParseProperty <List <RantInfo> >(responseText, "rants")); }
/// <summary> /// Requests a collection of rants sorted and selected by the arguments from the rest-api. /// </summary> /// <param name="sort">Sorting of the rant collection.</param> /// <param name="limit">Maximal rants to return.</param> /// <param name="skip">Number of rants to skip.</param> /// <inheritdoc /> public async Task <IReadOnlyCollection <Rant> > GetRantsAsync(RantSort sort = RantSort.Algo, RantRange range = RantRange.Day, int limit = 50, int skip = 0, ValuesCollection settings = null) { var sortText = sort.ToString().ToLower(); var paramz = new Parameters() { { "sort", sortText }, { "limit", limit.ToString() }, { "skip", skip.ToString() }, }; if (sort == RantSort.Top) { paramz.Add("range", range.ToString().ToLower()); } string url = owner.MakeUrl("/api/devrant/rants", paramz); var response = await client.GetAsync(url); var responseText = await response.Content.ReadAsStringAsync(); List <Rant> rants = new List <Rant>(); JObject tmp = JObject.Parse(responseText); foreach (JObject obj in tmp["rants"].Children()) { var r = ContentObject.Parse <Rant>(obj); rants.Add(r); } if (settings != null) { var num = tmp[ValuesCollection.NumNotifs]; if (tmp[ValuesCollection.NumNotifs] != null) { var notifs = Convert.ToInt32(num.ToString()); settings[ValuesCollection.NumNotifs] = notifs; } if (tmp[ValuesCollection.Set] != null) { settings[ValuesCollection.Set] = tmp[ValuesCollection.Set]; } } return(rants); }
/// <summary> /// Requests a collection of stories sorted and selected by the arguments from the rest-api. /// </summary> /// <inheritdoc /> public async Task <IReadOnlyCollection <Rant> > GetStoriesAsync(RantSort sort = RantSort.Top, RantRange range = RantRange.Day, int limit = 50, int skip = 0) { var sortText = sort.ToString().ToLower(); var rangeText = range.ToString().ToLower(); string url = owner.MakeUrl("/api/devrant/story-rants", new Parameters() { { "range", rangeText }, { "sort", sortText }, { "limit", limit.ToString() }, { "skip", skip.ToString() }, }); var response = await client.GetAsync(url); var responseText = await response.Content.ReadAsStringAsync(); JObject tmp = JObject.Parse(responseText); if (owner.CheckSuccess(tmp)) { List <Rant> rants = new List <Rant>(); foreach (JObject obj in tmp["rants"].Children()) { var r = ContentObject.Parse <Rant>(obj); rants.Add(r); } return(rants); } else { return(null); } }
public void SetDefaultFeed(RantSort defaultFeed) { Settings.StorySort = defaultFeed; Settings.Save(); }
private async Task LoadFeed(FeedType type, RantSort sort = RantSort.Algo, RantRange range = RantRange.Day, bool filter = false) { currentFeedType = type; Func <int, int, Task <IReadOnlyCollection <Dtos.Rant> > > getter; ValuesCollection otherVals = new ValuesCollection(); switch (type) { case FeedType.General: getter = async(skip, limit) => await api.Feeds.GetRantsAsync(sort : sort, range : range, skip : skip, limit : limit, settings : otherVals); break; case FeedType.Stories: getter = async(skip, limit) => await api.Feeds.GetStoriesAsync(range : range, sort : sort, skip : skip, limit : limit); break; default: return; } List <Dtos.Rant> rants = new List <Dtos.Rant>(); //Used to remove duplicates List <long> ids = new List <long>(); int maxPages = ds.MaxPages; bool useOffset = true; //ds.UseOffset var lim = ds.ResultsLimit; int minScore = ds.MinScore; int page = 0; if (useOffset) { page = maxPages * offset; maxPages = (offset + 1) * maxPages; } bool shouldIncrement = true; while (rants.Count < lim && page < maxPages) { int skip = page * lim; try { var tmp = await getter.Invoke(skip, lim); if (tmp == null || tmp.Count == 0) { ResetOffset(); shouldIncrement = false; break; } foreach (var r in tmp) { if (ids.Contains(r.Id) || r.Username == LoggedInUser || VoteState.None != r.Voted || r.Score < minScore || (ds.FilterOutRead && db.IsRead(r.Id))) { continue; } rants.Add(r); ids.Add(r.Id); } } catch (Exception e) { } page++; } if (useOffset && shouldIncrement) { IncrementOffset(); } lock (feed_lock) { feeds.Clear(); foreach (var rant in rants) { ViewModels.Rant r = new ViewModels.Rant(rant); feeds.Add(r); } } if (otherVals.Count > 0 && otherVals.ContainsKey(ValuesCollection.NumNotifs)) { int count = Convert.ToInt32(otherVals[ValuesCollection.NumNotifs]); if (count > 0) { //Just refresh nchecker.Check(); } UpdateNotifications(new NotificationsChecker.UpdateArgs(count, count)); } UpdateFollowedInRants(); feedView.SortDescriptions.Clear(); string message = "Loaded {0} rants from {1} pages"; UpdateStatus(string.Format(message, rants.Count, page)); }