ModelController

class ModelController

Provides fuctions to safely add and fetch objects from the persistent relational database ‘Core Data’

  • Declaration

    Swift

    func formatDateToDay(date: Date) -> String

    Parameters

    date

    Date, date to be formatted and returned as a string

    Return Value

    String of input date in user’s locale short date format, with time component removed e.g. 20/06/2018

  • Declaration

    Swift

    func formatDateToHHmm(date: Date) -> String

    Parameters

    date

    Date, date to be formatted to HHmm and returned as a string

    Return Value

    String of input date’s time component in HH:mm format e.g. 11:35

  • Checks for an existing favourites object

    Declaration

    Swift

    private func checkForExistingFavourites() -> Favourites

    Return Value

    Existing favourites object or a newly created favourites object if none existed

  • Checks for an existing Day log or creates one

    Declaration

    Swift

    func findOrMakeDay(day: Date) -> Day

    Parameters

    day

    Date of day log to be found (type Date)

    Return Value

    Day object with date (not time) corresponding to input date, or a newly created Day object for that date

  • Adds a new meal log to core data.

    Note

    Posts a notification FoodAdded which is picked up by viewControllerGraph and IndicatorControllerFood to update views.

    Declaration

    Swift

    func addMeal(name: String, time: String, date: Date, carbs: Int32, fat: Int32, protein: Int32)

    Parameters

    name

    String, Name of meal to add.

    time

    String, Time of meal consumed.

    date

    Date, Date of meal to add.

    carbs

    Int32, Total carbs (grams) of meal to add.

    fat

    Int32, Total fat (grams) of meal to add.

    protein

    Int32, Total protein (grams) of meal to add.

  • Adds a new exercise log to core data

    Note

    Posts a notification ExerciseAdded which is picked up by viewControllerGraph to update views

    Declaration

    Swift

    func addExercise(name: String, time: String, date: Date, intensity: String, duration: String)

    Parameters

    name

    String, Name of exercise to add

    time

    String, Time of exercise done

    date

    Date, Date of exercise to add

    intensity

    String, inensity of exercise to add

    duration

    String, duration of exercise to add

  • Adds a new glucose log to core data

    Declaration

    Swift

    func addGlucose(value: Double, time: String, date: Date)

    Parameters

    value

    Double, Value of glucose in mM/L

    time

    String, Time of glucose log

    date

    Date, Date of glucose log to add

  • Adds a new insulin log to core data

    Note

    Posts a notification InsulinAdded which is picked up by viewControllerGraph to update views

    Declaration

    Swift

    func addInslin(units: Double, time: String, date: Date)

    Parameters

    units

    Double, Insulin units injected

    time

    String, Time of insulin injection

    date

    Date, Date of insulin injected

  • Toggles whether a meal is favourited in core data

    Declaration

    Swift

    func toggleFavouriteFood(item: Meals)

    Parameters

    item

    Meals, Meals object to toggle

  • Toggles whether an exercise is favourited in core data

    Declaration

    Swift

    func toggleFavouriteExercise(item: Exercise)

    Parameters

    item

    Exercise, Exercise object to toggle

  • Toggles whether a day is favourited in core data

    Declaration

    Swift

    func toggleFavouriteDay(item: Day)

    Parameters

    item

    Day, Day object to toggle

  • Adds a stress log to core data

    Declaration

    Swift

    func addStress(start: Date, end: Date)

    Parameters

    start

    Date, Date containing start time of a stress log

    end

    Date, Date containing end time of a stress log

  • Adds an illness log to core data

    Declaration

    Swift

    func addIllness(start: Date, end: Date)

    Parameters

    start

    Date, Date containing start time of an illness log

    end

    Date, Date containing end time of an illness log

  • Returns true if item (Meals object) is in favourites

    Declaration

    Swift

    func itemInFavouritesFood(item: Meals) -> Bool
  • Returns true if item (Exercise object) is in favourites

    Declaration

    Swift

    func itemInFavouritesExercise(item: Exercise) -> Bool
  • Returns true if item (Day object) is in favourites

    Declaration

    Swift

    func itemInFavouritesDay(item: Day) -> Bool
  • Fetches an array of Meals objects that are favourited, sorted by name

    Declaration

    Swift

    func fetchFavouritesFood() -> [Meals]

    Return Value

    Array of favourited Meals objects sorted by name or an empty array if no favourite Meals exist

  • Fetches an array of Exercise objects that are favourited, sorted by name

    Declaration

    Swift

    func fetchFavouritesExercise() -> [Exercise]

    Return Value

    Array of favourited Exercise objects sorted by name or an empty array if no favourite Exercise exist

  • Fetches an array of Day objects that are favourited, sorted by date

    Declaration

    Swift

    func fetchFavouritesDays() -> [Day]

    Return Value

    Array of favourited Day objects sorted by date or an empty array if no favourite Day objects exist

  • Fetches an array of Meals objects, sorted by time

    Declaration

    Swift

    func fetchMeals(day: Date) -> [Meals]

    Parameters

    day

    Date, date of day object whose meals are to be fetched

    Return Value

    Array of Meals objects sorted by time or an empty array if no Meals objects exist

  • Fetches an array of Glucose objects, sorted by time

    Declaration

    Swift

    func fetchGlucose(day: Date) -> [Glucose]

    Parameters

    day

    Date, date of day object whose glucose logs are to be fetched

    Return Value

    Array of Glucose objects sorted by time or an empty array if no Glucose objects exist

  • Fetches an array of Insulin objects, sorted by time

    Declaration

    Swift

    func fetchInsulin(day: Date) -> [Insulin]

    Parameters

    day

    Date, date of day object whose insulin logs are to be fetched

    Return Value

    Array of Insulin objects sorted by time or an empty array if no Insulin objects exist

  • Fetches an array of Exercise objects, sorted by time

    Declaration

    Swift

    func fetchExercise(day: Date) -> [Exercise]

    Parameters

    day

    Date, date of day object whose exercise logs are to be fetched

    Return Value

    Array of Exercise objects sorted by time or an empty array if no Exercise objects exist

  • Fetches an array of Day objects, sorted by date

    Note

    Currently fetches ALL days, then filters in ViewControllerHealth to last 7/30/60. This is due to Day.date being a string not a Date, and therefore not being filterable using a fetch predicate. Ideally this would be refactored for efficiency when many Days are stored, but even within several years, it is unlikely to have much effect on performance.

    Declaration

    Swift

    func fetchDay() -> [Day]

    Return Value

    Array of Day objects sorted by date or an empty array if no Day objects exist