public static async Task <List <Category> > DownloadDB(Action retryMethod, Snackbar snack) { try { var response = await client.GetAsync(AppResources.DbLink); if (response.IsSuccessStatusCode) { var payload = await response.Content.ReadAsStringAsync(); newDB = JsonConvert.DeserializeObject <List <Category> >(payload); var newideasdbResponse = await client.GetAsync(AppResources.NewIdeasDbLink); if (newideasdbResponse.IsSuccessStatusCode) { newideastxt = await newideasdbResponse.Content.ReadAsStringAsync(); DBAssist.SerializeDBAsync(oldDBPath, Global.Categories); DBAssist.SerializeDBAsync(newideastxtPath, newideastxt); return(newDB); } } // To prevent too many requests to server, only invalidate cache if app is opening fresh from launcher Global.LockRequests = true; } catch (TaskCanceledException) { snack.SetText("Can't load ideas. Your connection might be too slow.").SetAction("Retry", (v) => retryMethod?.Invoke()).Show(); } catch (HttpRequestException) { snack.SetText("Can't load ideas. Check your connection.").SetAction("Retry", (v) => retryMethod?.Invoke()).Show(); } catch (Exception e) { snack.SetText($"Oops! {e.Message}.").SetAction("Retry", (v) => retryMethod?.Invoke()).Show(); } return(null); }
private static async void InvalidateOldData() { Log.Debug(TAG, "Starting full invalidation"); var notesPath = Path.Combine(Global.APP_PATH, "notesdb"); if (!File.Exists(notesPath)) { File.Create(notesPath); } var notes = await DBAssist.DeserializeDBAsync <List <Note> >(notesPath); notes = notes ?? new List <Note>(); for (int i = 0; i < newDB.Count; i++) { for (int j = 0; j < newDB[i].Items.Count; j++) { var newItem = newDB[i].Items[j]; var oldItem = Global.Categories[i].Items.FirstOrDefault(x => x.Id == newItem.Id); Note note = null; if (oldItem != null) { note = notes.FirstOrDefault(x => x.Title == oldItem.Title); if (note != null) { Log.Debug(TAG, $"Note *{note.Title}*, found for old idea *{oldItem.Title}* placed at new idea *{newItem.Title}*"); } } newItem.Note = note; newItem.State = oldItem?.State; } } DBAssist.SerializeDBAsync(oldDBPath, newDB); DBAssist.SerializeDBAsync(newideastxtPath, newideastxt); Global.Categories = newDB; Global.LockRequests = true; Log.Debug(TAG, "Invalidation completed."); }