public static async Task <Tuple <List <Category>, Exception> > DownloadIdeasFromGoogleDrive() { try { var response = await client.GetAsync(AppResources.DbLink); if (response.IsSuccessStatusCode) { var payload = await response.Content.ReadAsStringAsync(); onlineIdeas = JsonConvert.DeserializeObject <List <Category> >(payload); var newideasdbResponse = await client.GetAsync(AppResources.NewIdeasDbLink); if (newideasdbResponse.IsSuccessStatusCode) { newideastxt = await newideasdbResponse.Content.ReadAsStringAsync(); DBSerializer.SerializeDBAsync(Global.IDEAS_PATH, onlineIdeas); DBSerializer.SerializeDBAsync(Global.NEWIDEASTXT_PATH, newideastxt); return(Tuple.Create <List <Category>, Exception>(onlineIdeas, null)); } } // To prevent too many requests to server, only invalidate cache once when app is opening fresh from launcher Global.LockRequests = true; } catch (Exception e) { return(Tuple.Create <List <Category>, Exception>(null, e)); } return(null); }
// During app launch, we check the online data with the cached data. If anything new has been added online we invalidate the cache. private static async Task InvalidateOldData() { Log.Debug(TAG, "Starting full invalidation"); var notesPath = Global.NOTES_PATH; if (!File.Exists(notesPath)) { File.Create(notesPath); } var notes = await DBSerializer.DeserializeDBAsync <List <Note> >(notesPath); notes = notes ?? new List <Note>(); // This is the code that does the actual invalidation for (int i = 0; i < onlineIdeas.Count; i++) // Looping through categories { for (int j = 0; j < onlineIdeas[i].Items.Count; j++) // Looping through ideas in each category { var newItem = onlineIdeas[i].Items[j]; var oldItem = Global.Categories[i].Items.FirstOrDefault(x => x.Id == newItem.Id); // We don't want to clear the user's notes for a particular idea during invalidation 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; } } DBSerializer.SerializeDBAsync(Global.IDEAS_PATH, onlineIdeas); DBSerializer.SerializeDBAsync(Global.NEWIDEASTXT_PATH, newideastxt); Global.Categories = onlineIdeas; Global.LockRequests = true; Log.Debug(TAG, "Invalidation completed."); }