public async Task <GalacticStandings> GetData(CancellationTokenSource cancelToken, bool ignoreCache = false) { TimeSpan expiry = TimeSpan.FromMinutes(15); if ((galacticStandings == null) || (galacticStandings.Cycle != CycleService.CurrentCycle() && (lastUpdated + expiry < DateTime.Now))) { // download the standings string json; DownloadService downloadService = DownloadService.Instance(); (json, lastUpdated) = await downloadService.GetData(URL, dataKey, lastUpdatedKey, expiry, cancelToken, ignoreCache).ConfigureAwait(false); // parse the standings galacticStandings = JsonConvert.DeserializeObject <GalacticStandings>(json); // cache till next cycle if updated if (galacticStandings.Cycle == CycleService.CurrentCycle()) { expiry = CycleService.TimeTillTick(); Barrel.Current.Add(dataKey, json, expiry); Barrel.Current.Add(lastUpdatedKey, lastUpdated.ToString(), expiry); } } return(galacticStandings); }
private async void GetStandingsAsync(bool ignoreCache = false) { int cycleNo = 0; if (!string.IsNullOrWhiteSpace(Cycle)) { int p = Cycle.IndexOf(" ") + 1; Int32.TryParse(Cycle.Substring(p, Cycle.Length - p), out cycleNo); } if ((Standings?.Any() == false) || (cycleNo != CycleService.CurrentCycle() && (LastUpdated + TimeSpan.FromMinutes(10) < DateTime.Now))) { ShowMessage = false; CancellationTokenSource cancelToken = new CancellationTokenSource(); using (UserDialogs.Instance.Loading("Loading", () => cancelToken.Cancel(), null, true, MaskType.Clear)) { try { // get the standings StandingsService standingsService = StandingsService.Instance(); GalacticStandings standings = await standingsService.GetData(cancelToken, ignoreCache).ConfigureAwait(false); Cycle = $"Cycle {standings.Cycle}"; LastUpdated = standings.LastUpdated; if (standings.Standings.Count < 1) { SetMessages("Unable to display Powerplay Standings due to parsing error.", true); } else { // show the standings Device.BeginInvokeOnMainThread(() => { Standings.Clear(); foreach (PowerStanding item in standings.Standings) { Standings.Add(item); } }); } } catch (OperationCanceledException) { SetMessages("Powerplay Standings download was cancelled or timed out.", true); } catch (HttpRequestException ex) { string err = ex.Message; int start = err.IndexOf("OPENSSL_internal:", StringComparison.OrdinalIgnoreCase); if (start > 0) { start += 17; int end = err.IndexOf(" ", start, StringComparison.OrdinalIgnoreCase); err = $"SSL Error ({err.Substring(start, end - start).Trim()})"; } else if (err.IndexOf("Error:", StringComparison.OrdinalIgnoreCase) > 0) { err = err.Substring(err.IndexOf("Error:", StringComparison.OrdinalIgnoreCase) + 6).Trim(); } SetMessages($"Network Error: {err}", true); } catch (Exception ex) { if (ex.Message.Contains("unexpected end of stream")) { SetMessages("Powerplay Standings download was cancelled.", true); } else { SetMessages($"Error: {ex.Message}", true); } } } } }