//--------------------------------------------------------Constructor:----------------------------------------------------------------\\ #region --Constructors-- #endregion //--------------------------------------------------------Set-, Get- Methods:---------------------------------------------------------\\ #region --Set-, Get- Methods-- #endregion //--------------------------------------------------------Misc Methods:---------------------------------------------------------------\\ #region --Misc Methods (Public)-- public async Task DeleteTumOnlineTokenAndIdAsync(ConfirmDialogContext ctx) { if (ctx.MODEL.Confirmed) { // Delete token: Storage.Classes.Settings.SetSetting(SettingsConsts.TUM_ID, ""); Vault.DeleteAllVaults(); // Delete DB: using (TumOnlineDbContext dbCtx = new TumOnlineDbContext()) { await dbCtx.Database.EnsureDeletedAsync(); await dbCtx.Database.EnsureCreatedAsync(); } // Remove all the cached stuff: using (CacheDbContext dbCtx = new CacheDbContext()) { await dbCtx.Database.EnsureDeletedAsync(); await dbCtx.Database.EnsureCreatedAsync(); } } }
//--------------------------------------------------------Constructor:----------------------------------------------------------------\\ #region --Constructors-- #endregion //--------------------------------------------------------Set-, Get- Methods:---------------------------------------------------------\\ #region --Set-, Get- Methods-- #endregion //--------------------------------------------------------Misc Methods:---------------------------------------------------------------\\ #region --Misc Methods (Public)-- public async Task <User> UpdateUserAsync(TumOnlineCredentials credentials, string obfuscatedId, bool force) { // Wait for the old update to finish first: if (!(updateTask is null) && !updateTask.IsCompleted) { try { return(await updateTask.ConfAwaitFalse()); } catch (Exception e) { InvokeOnRequestError(new RequestErrorEventArgs(e)); Logger.Error("Awaiting for user task failed with:", e); return(null); } } updateTask = Task.Run(async() => { if (!force && CacheDbContext.IsCacheEntryValid(TumOnlineService.PERSON_DETAILS.NAME)) { Logger.Info("No need to fetch user. Cache is still valid."); using (TumOnlineDbContext ctx = new TumOnlineDbContext()) { return(ctx.Users.Include(ctx.GetIncludePaths(typeof(User))).FirstOrDefault()); } } User user = null; try { user = await DownloadUserAsync(credentials, obfuscatedId, force); } catch (Exception e) { InvokeOnRequestError(new RequestErrorEventArgs(e)); Logger.Error("Failed to request user with:", e); } if (!(user is null)) { using (TumOnlineDbContext ctx = new TumOnlineDbContext()) { foreach (User userOld in ctx.Users.Where(u => u.Id == user.Id).Include(ctx.GetIncludePaths(typeof(User)))) { if (userOld.Groups.Count() > 0) { ctx.RemoveRange(userOld.Groups); userOld.Groups.Clear(); } ctx.Remove(userOld); } ctx.Add(user); } CacheDbContext.UpdateCacheEntry(TumOnlineService.PERSON_DETAILS.NAME, DateTime.Now.Add(TumOnlineService.PERSON_DETAILS.VALIDITY)); }
//--------------------------------------------------------Constructor:----------------------------------------------------------------\\ #region --Constructors-- #endregion //--------------------------------------------------------Set-, Get- Methods:---------------------------------------------------------\\ #region --Set-, Get- Methods-- #endregion //--------------------------------------------------------Misc Methods:---------------------------------------------------------------\\ #region --Misc Methods (Public)-- public async Task <IEnumerable <CalendarEvent> > UpdateAsync(TumOnlineCredentials credentials, bool force) { // Wait for the old update to finish first: if (!(updateTask is null) && !updateTask.IsCompleted) { try { return(await updateTask.ConfAwaitFalse()); } catch (Exception e) { InvokeOnRequestError(new RequestErrorEventArgs(e)); Logger.Error("Awaiting for calendar task failed with:", e); return(new List <CalendarEvent>()); } } updateTask = Task.Run(async() => { if (!force && CacheDbContext.IsCacheEntryValid(TumOnlineService.CALENDAR.NAME)) { Logger.Info("No need to fetch calendar events. Cache is still valid."); return(LoadCalendarEventsFromDb()); } IEnumerable <CalendarEvent> events = null; try { events = await DownloadCalendarEventsAsync(credentials, force); } catch (Exception e) { InvokeOnRequestError(new RequestErrorEventArgs(e)); Logger.Error("Failed to request calendar events with:", e); } if (!(events is null)) { using (TumOnlineDbContext ctx = new TumOnlineDbContext()) { ctx.RemoveRange(ctx.CalendarEvents); ctx.AddRange(events); } CacheDbContext.UpdateCacheEntry(TumOnlineService.CALENDAR.NAME, DateTime.Now.Add(TumOnlineService.CALENDAR.VALIDITY)); // Update the Windows calendar: if (!Settings.GetSettingBoolean(SettingsConsts.DISABLE_WINDOWS_CALENDAR_INTEGRATION)) { await UpdateWindowsCalendarAsync(events); } else { Logger.Debug("Not updating the Windows calendar. Setting disabled."); } }
//--------------------------------------------------------Constructor:----------------------------------------------------------------\\ #region --Constructors-- #endregion //--------------------------------------------------------Set-, Get- Methods:---------------------------------------------------------\\ #region --Set-, Get- Methods-- #endregion //--------------------------------------------------------Misc Methods:---------------------------------------------------------------\\ #region --Misc Methods (Public)-- public async Task <IEnumerable <TuitionFee> > UpdateAsync(TumOnlineCredentials credentials, bool force) { // Wait for the old update to finish first: if (!(updateTask is null) && !updateTask.IsCompleted) { try { return(await updateTask.ConfAwaitFalse()); } catch (Exception e) { InvokeOnRequestError(new RequestErrorEventArgs(e)); Logger.Error("Awaiting for tuition fees task failed with:", e); return(new List <TuitionFee>()); } } updateTask = Task.Run(async() => { if (!force && CacheDbContext.IsCacheEntryValid(TumOnlineService.TUITION_FEE_STATUS.NAME)) { Logger.Info("No need to fetch tuition fees. Cache is still valid."); using (TumOnlineDbContext ctx = new TumOnlineDbContext()) { return(ctx.TuitionFees.Include(ctx.GetIncludePaths(typeof(TuitionFee))).ToList()); } } IEnumerable <TuitionFee> fees = null; try { fees = await DownloadFeesAsync(credentials, force); } catch (Exception e) { InvokeOnRequestError(new RequestErrorEventArgs(e)); Logger.Error("Failed to request tuition fees with:", e); } if (!(fees is null)) { using (TumOnlineDbContext ctx = new TumOnlineDbContext()) { ctx.RemoveRange(ctx.TuitionFees); ctx.AddRange(fees); } CacheDbContext.UpdateCacheEntry(TumOnlineService.GRADES.NAME, DateTime.Now.Add(TumOnlineService.GRADES.VALIDITY)); }
//--------------------------------------------------------Constructor:----------------------------------------------------------------\\ #region --Constructors-- #endregion //--------------------------------------------------------Set-, Get- Methods:---------------------------------------------------------\\ #region --Set-, Get- Methods-- #endregion //--------------------------------------------------------Misc Methods:---------------------------------------------------------------\\ #region --Misc Methods (Public)-- public async Task <Identity> UpdateAsync(TumOnlineCredentials credentials, bool force) { // Wait for the old update to finish first: if (!(updateTask is null) && !updateTask.IsCompleted) { try { return(await updateTask.ConfAwaitFalse()); } catch (Exception e) { InvokeOnRequestError(new RequestErrorEventArgs(e)); Logger.Error("Awaiting for identity task failed with:", e); return(null); } } updateTask = Task.Run(async() => { if (!force && CacheDbContext.IsCacheEntryValid(TumOnlineService.ID.NAME)) { Logger.Info("No need to fetch identity. Cache is still valid."); using (TumOnlineDbContext ctx = new TumOnlineDbContext()) { return(ctx.Identities.Include(ctx.GetIncludePaths(typeof(Identity))).FirstOrDefault()); } } Identity identity = null; try { identity = await DownloadIdentityAsync(credentials, force); } catch (Exception e) { InvokeOnRequestError(new RequestErrorEventArgs(e)); Logger.Error("Failed to request identity with:", e); } if (!(identity is null)) { using (TumOnlineDbContext ctx = new TumOnlineDbContext()) { ctx.RemoveRange(ctx.Identities); ctx.Add(identity); } CacheDbContext.UpdateCacheEntry(TumOnlineService.ID.NAME, DateTime.Now.Add(TumOnlineService.ID.VALIDITY)); }
//--------------------------------------------------------Misc Methods:---------------------------------------------------------------\\ #region --Misc Methods (Public)-- /// <summary> /// Gets called on App start and performs update task e.g. migrate the DB to a new format. /// </summary> #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously public static async Task OnAppStartAsync() #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously { PackageVersion versionLastStart = GetLastStartedVersion(); // Check if version != 0.0.0.0 => first ever start of the app: if (!(versionLastStart.Major == 0 && versionLastStart.Major == versionLastStart.Minor && versionLastStart.Minor == versionLastStart.Revision && versionLastStart.Revision == versionLastStart.Build) || Settings.GetSettingBoolean(SettingsConsts.INITIALLY_STARTED)) { if (!Compare(versionLastStart, GetPackageVersion())) { // Total refactoring in version 2.0.0.0: if (versionLastStart.Major < 2) { Logger.Info("Started updating to version 2.0.0.0."); Settings.SetSetting(SettingsConsts.INITIALLY_STARTED, false); Logger.Info("Finished updating to version 2.0.0.0."); } // DB layout for dishes changed in 2.1.0.0: if (versionLastStart.Major <= 2 && versionLastStart.Minor < 1) { Logger.Info("Started updating to version 2.1.0.0."); Logger.Info("Resetting canteens DB..."); try { StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("canteens.db"); if (!(file is null)) { await file.DeleteAsync(); } } catch (Exception e) { Logger.Error("Failed to remove old canteens DB with:", e); } Logger.Info("Updating canteens and dishes..."); await CanteenManager.INSTANCE.UpdateCanteensAsync(true); Settings.SetSetting(SettingsConsts.INITIALLY_STARTED, false); Logger.Info("Finished updating to version 2.1.0.0."); } // DB layout for dishes changed in 2.2.0.0: if (versionLastStart.Major <= 2 && versionLastStart.Minor < 2) { Logger.Info("Started updating to version 2.2.0.0."); Logger.Info("Resetting canteens DB..."); try { StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("canteens.db"); if (!(file is null)) { await file.DeleteAsync(); } } catch (Exception e) { Logger.Error("Failed to remove old canteens DB with:", e); } Logger.Info("Updating canteens and dishes..."); await CanteenManager.INSTANCE.UpdateCanteensAsync(true); Settings.SetSetting(SettingsConsts.INITIALLY_STARTED, false); Logger.Info("Finished updating to version 2.2.0.0."); } // DB layout for TUMonline changed in 2.3.0.0: if (versionLastStart.Major <= 2 && versionLastStart.Minor < 3) { Logger.Info("Started updating to version 2.3.0.0."); using (TumOnlineDbContext ctx = new TumOnlineDbContext()) { await ctx.RecreateDbAsync(); } CacheDbContext.ClearCache(); Logger.Info("Finished updating to version 2.2.0.0."); } // New tables for the canteen DB in 2.4.0.0: if (versionLastStart.Major <= 2 && versionLastStart.Minor < 4) { Logger.Info("Started updating to version 2.4.0.0."); using (CanteensDbContext ctx = new CanteensDbContext()) { await ctx.RecreateDbAsync(); } CacheDbContext.ClearCache(); Logger.Info("Finished updating to version 2.4.0.0."); } } } SetVersion(GetPackageVersion()); }