示例#1
0
        //--------------------------------------------------------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());
        }