Example #1
0
        public static Context LoadCurrentContext()
        {
            ContextDatabaseAccess contextDB = new ContextDatabaseAccess();

            contextDB.InitializeSync();
            Context CurrentContext;

            try
            {
                CurrentContext = contextDB.GetContextSync();
            }
            // Can't find definitions for SQLiteNetExtensions exceptions, so catch generic Exception e and assume there is no context.
            catch (Exception e)
            {
                CurrentContext = null;
                //contextDB.CloseSyncConnection();
            }
            // If context doesn't exist, create it, save it, and populate vaccine/milestones databases.
            if (CurrentContext == null)
            {
                CurrentContext = new Context();
                // Exception probably broke the synchronous connection.
                //contextDB.InitializeSync();
                ContextDatabaseAccess newContextDB = new ContextDatabaseAccess();
                newContextDB.InitializeSync();
                newContextDB.SaveFirstContextSync(CurrentContext);
                newContextDB.CloseSyncConnection();
                Task tVaccine = Task.Run(async() =>
                {
                    await VaccineTableConstructor.ConstructVaccineTable();
                });
                tVaccine.Wait();
                Task tMilestone = Task.Run(async() =>
                {
                    MilestonesTableConstructor.ConstructMilestonesTable();
                });
                tMilestone.Wait();
                return(CurrentContext);
            }
            else
            {
                return(CurrentContext);
            }
        }
        // Load context and set value for current child if it exists.
        private async Task <Boolean> LoadContext()
        {
            ContextDatabaseAccess contextDB = new ContextDatabaseAccess();
            await contextDB.InitializeAsync();

            try
            {
                CurrentContext = contextDB.GetContextAsync().Result;
            }
            // Can't find definitions for SQLiteNetExtensions exceptions, so catch generic Exception e and assume there is no context.
            catch (Exception e)
            {
                CurrentContext = null;
                //contextDB.CloseSyncConnection();
            }
            // If context doesn't exist, create it, save it, and populate vaccine/milestones databases.
            if (CurrentContext == null)
            {
                CurrentContext = new Context();
                // Exception probably broke the synchronous connection.
                //contextDB.InitializeSync();
                ContextDatabaseAccess newContextDB = new ContextDatabaseAccess();
                await newContextDB.InitializeAsync();

                newContextDB.SaveFirstContextAsync(CurrentContext);
                //newContextDB.CloseSyncConnection();
                _currentChild        = null;
                CurrentChildBirthday = DateTime.Today;
                Task  tVaccine   = VaccineTableConstructor.ConstructVaccineTable();
                Task  tMilestone = MilestonesTableConstructor.ConstructMilestonesTable();
                await tVaccine;
                await tMilestone;
                return(true);
            }
            else
            {
                CurrentChild = CurrentContext.GetSelectedChild();
                return(true);
            }
        }