/// <summary> /// Invoked when the application is launched normally by the end user. Other entry points /// will be used when the application is launched to open a specific file, to display /// search results, and so forth. /// </summary> /// <param name="args">Details about the launch request and process.</param> protected override void OnLaunched(LaunchActivatedEventArgs args) { // Initialize the database if necessary using (var db = new SQLite.SQLiteConnection(DBPath)) { // Create the tables if they don't exist db.CreateTable<Day>(); db.CreateTable<Intake>(); Day day = new Day(); day.Date = DateTime.Today; db.Insert(day); } CurrentDay = DayViewModel.GetDayByDate(DateTime.Today); Frame rootFrame = Window.Current.Content as Frame; // Do not repeat app initialization when the Window already has content, // just ensure that the window is active if (rootFrame == null) { // Create a Frame to act as the navigation context and navigate to the first page rootFrame = new Frame(); if (args.PreviousExecutionState == ApplicationExecutionState.Terminated) { //TODO: Load state from previously suspended application } // Place the frame in the current Window Window.Current.Content = rootFrame; } if (rootFrame.Content == null) { // When the navigation stack isn't restored navigate to the first page, // configuring the new page by passing required information as a navigation // parameter if (!rootFrame.Navigate(typeof(MainPage), args.Arguments)) { throw new Exception("Failed to create initial page"); } } // Ensure the current window is active Window.Current.Activate(); }
/// <summary> /// Gets day by date, if date doesn't exist, /// a new day by given date will be created to db /// </summary> /// <param name="date"></param> /// <returns></returns> public static DayViewModel GetDayByDate(DateTime date) { var day = new DayViewModel(); using (var db = new SQLite.SQLiteConnection(App.DBPath)) { try { var _day = (db.Table<Day>().Where(d => d.Date == date)).Single(); day.Id = _day.Id; day.Total = _day.Total; day.Date = _day.Date; day.Carbohydrates = _day.Carbohydrates; day.Protein = _day.Protein; day.Fat = _day.Fat; day.Fibre = _day.Fibre; } catch (Exception) { Day dayrow = new Day(); dayrow.Date = date; day.Date = date; db.Insert(dayrow); } return day; } }
/// <summary> /// Refreshes days total values, /// </summary> /// <param name="day">Day that has been queryed from db before</param> public void reduceDayTotals(Day day, IntakeViewModel intake) { using (var db = new SQLite.SQLiteConnection(App.DBPath)) { day.Total -= intake.Calories; day.Carbohydrates -= intake.Carbohydrates; day.Protein -= intake.Protein; day.Fat -= intake.Fat; day.Fibre -= intake.Fibre; db.Update(day); } }