ModelController
class ModelController
Provides fuctions to safely add and fetch objects from the persistent relational database ‘Core Data’
-
Declaration
Swift
func formatDateToDay(date: Date) -> StringParameters
dateDate, 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) -> StringParameters
dateDate, 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() -> FavouritesReturn 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) -> DayParameters
dayDate 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 notificationFoodAdded
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
nameString, Name of meal to add.
timeString, Time of meal consumed.
dateDate, Date of meal to add.
carbsInt32, Total carbs (grams) of meal to add.
fatInt32, Total fat (grams) of meal to add.
proteinInt32, Total protein (grams) of meal to add.
-
Adds a new exercise log to core data
Note
Posts a notificationExerciseAdded
which is picked up by viewControllerGraph to update viewsDeclaration
Swift
func addExercise(name: String, time: String, date: Date, intensity: String, duration: String)Parameters
nameString, Name of exercise to add
timeString, Time of exercise done
dateDate, Date of exercise to add
intensityString, inensity of exercise to add
durationString, duration of exercise to add
-
Adds a new glucose log to core data
Declaration
Swift
func addGlucose(value: Double, time: String, date: Date)Parameters
valueDouble, Value of glucose in mM/L
timeString, Time of glucose log
dateDate, Date of glucose log to add
-
Adds a new insulin log to core data
Note
Posts a notificationInsulinAdded
which is picked up by viewControllerGraph to update viewsDeclaration
Swift
func addInslin(units: Double, time: String, date: Date)Parameters
unitsDouble, Insulin units injected
timeString, Time of insulin injection
dateDate, Date of insulin injected
-
Toggles whether a meal is favourited in core data
Declaration
Swift
func toggleFavouriteFood(item: Meals)Parameters
itemMeals, Meals object to toggle
-
Toggles whether an exercise is favourited in core data
Declaration
Swift
func toggleFavouriteExercise(item: Exercise)Parameters
itemExercise, Exercise object to toggle
-
Toggles whether a day is favourited in core data
Declaration
Swift
func toggleFavouriteDay(item: Day)Parameters
itemDay, Day object to toggle
-
Adds a stress log to core data
Declaration
Swift
func addStress(start: Date, end: Date)Parameters
startDate, Date containing start time of a stress log
endDate, Date containing end time of a stress log
-
Adds an illness log to core data
Declaration
Swift
func addIllness(start: Date, end: Date)Parameters
startDate, Date containing start time of an illness log
endDate, 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
dayDate, 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
dayDate, 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
dayDate, 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
dayDate, 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 astring
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
View on GitHub
ModelController Class Reference