Exemplo n.º 1
0
        /// <summary>
        /// Constructor for the Application object.
        /// </summary>
        public App()
        {
            // Global handler for uncaught exceptions.
            UnhandledException += Application_UnhandledException;

            // Standard Silverlight initialization
            InitializeComponent();

            // Phone-specific initialization
            InitializePhoneApplication();

            // Show graphics profiling information while debugging.
            if (System.Diagnostics.Debugger.IsAttached)
            {

                // Display the current frame rate counters.
                Application.Current.Host.Settings.EnableFrameRateCounter = true;

                // Show the areas of the app that are being redrawn in each frame.
                //Application.Current.Host.Settings.EnableRedrawRegions = true;

                // Enable non-production analysis visualization mode,
                // which shows areas of a page that are handed off to GPU with a colored overlay.
                //Application.Current.Host.Settings.EnableCacheVisualization = true;

                // Disable the application idle detection by setting the UserIdleDetectionMode property of the
                // application's PhoneApplicationService object to Disabled.
                // Caution:- Use this under debug mode only. Application that disables user idle detection will continue to run
                // and consume battery power when the user is not using the phone.
                PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;
            }

            string DBConnectionString = "Data Source=isostore:/Tasks_v3.sdf";
            bool createDB = false;

            using (TasksDataContext db = new TasksDataContext(DBConnectionString))
            {

                if (db.DatabaseExists() == false)
                {
                    db.CreateDatabase();

                    createDB = true;

                    db.SubmitChanges();
                }
                else
                {
                    //Create the database schema updater
                    DatabaseSchemaUpdater dbUpdate = db.CreateDatabaseSchemaUpdater();

                    //Get database version
                    int dbVersion = dbUpdate.DatabaseSchemaVersion;

                    Debug.WriteLine("Database version " + dbVersion);

                    if (dbVersion == 2)
                    {
                        dbUpdate.DatabaseSchemaVersion = 3;
                        dbUpdate.Execute();
                    }
                }
            }

            App.viewModel = new TaskViewModel(DBConnectionString);

            if (createDB)
            {
                //TODO: Update this with the new database
                //Add default Categorys
                viewModel.All = viewModel.AddCategory(new Category { Name = "All", Parent=null });
                Category health = viewModel.AddCategory(new Category { Name = "Health", Parent = ViewModel.All});
                Category fitness = viewModel.AddCategory(new Category { Name = "Fitness", Parent = health});
                Category organization = viewModel.AddCategory(new Category { Name = "Organization", Parent = ViewModel.All});

                //Add default tasks
                viewModel.AddTask(new Task
                {
                    Name = "Eat 2 servings of fruit",
                    Notes = "",
                    Reward = 5,
                    Category = health,
                    RecurringOption = Task.RecurringOptions.Days,
                    RecurringAmount = 1,
                    When = DateTime.Now.Date.AddSeconds(5),
                    IsComplete = false,
                    HasReminders = false,
                });

                viewModel.AddTask(new Task {
                    Name = "Take vitamins",
                    Notes = "",
                    Reward = 5,
                    Category = health,
                    RecurringOption = Task.RecurringOptions.Days,
                    RecurringAmount = 1,
                    When = DateTime.Now.Date.AddSeconds(5),
                    IsComplete = false,
                    HasReminders = false, });

                viewModel.AddTask(new Task
                {
                    Name = "Cardio",
                    Notes = "",
                    Reward = 5,
                    Category = fitness,
                    RecurringOption = Task.RecurringOptions.Days,
                    RecurringAmount = 1,
                    When = DateTime.Now.Date.AddSeconds(5),
                    IsComplete = false,
                    HasReminders = false,
                });

                viewModel.AddTask(new Task
                {
                    Name = "Weights",
                    Notes = "",
                    Reward = 5,
                    Category = fitness,
                    RecurringOption = Task.RecurringOptions.DaysOfWeek,
                    RecurringAmount = 17,
                    When = DateTime.Now.Date.AddSeconds(5),
                    IsComplete = false,
                    HasReminders = false,
                });

                viewModel.AddTask(new Task
                {
                    Name = "Sport",
                    Notes = "",
                    Reward = 5,
                    Category = fitness,
                    RecurringOption = Task.RecurringOptions.DaysOfWeek,
                    RecurringAmount = 4,
                    When = DateTime.Now.Date.AddSeconds(5),
                    IsComplete = false,
                    HasReminders = false,
                });

                viewModel.AddTask(new Task
                {
                    Name = "Add something to Reward Your Tasks",
                    Notes = "",
                    Reward = 5,
                    Category = organization,
                    RecurringOption = Task.RecurringOptions.Days,
                    RecurringAmount = 1,
                    When = DateTime.Now.Date.AddHours(18),
                    IsComplete = false,
                    HasReminders = true,
                });

                viewModel.AddTask(new Task
                {
                    Name = "Explore Reward Your Tasks",
                    Notes = "",
                    Reward = 15,
                    Category = organization,
                    RecurringOption = Task.RecurringOptions.Never,
                    RecurringAmount = 0,
                    When = DateTime.Now.Date.AddSeconds(5),
                    IsComplete = false,
                    HasReminders = false,
                });

                //Add default rewards
                viewModel.AddReward(new Reward { Name = "Satisfy that sweet tooth", Category = health, PointsPerReward = 40 });
            }

            viewModel.LoadCollectionsWorker.RunWorkerAsync();

            /*
            foreach (Task t in viewModel.AllTasks)
            {
                t.UpdateTask();
            }

            viewModel.AddFromChangedTasks();
            */
        }