ProfileModel
Extends:
Class for the Profile model to be used by the ProfilePresenter
Constructor Summary
Public Constructor | ||
public |
Creates an instance of ProfileModel. |
Method Summary
Public Methods | ||
public |
async checkAuthenticationState(onComplete: Function) Check the authentication state of the user. |
|
public |
Get method for presenters to get data. |
|
public |
async getProfileData(callback: Function) Gets the profile data from Asyncstorage. |
|
public |
getProfilePicture(callback: *) this method was deprecated. Use getProfileData instead
Gets the profile data from Asyncstorage. |
|
public |
setCallback(callback: Function) Set the model's callback to a new callback. This callback can be used anywhere and is usually passed in from a presenter. |
|
public |
Update method for presenters to update the model's data. Datetime and Owner are handled in database class. |
Private Methods | ||
private |
async _addProfileDataLocally(userID: string, data: Object) Writes the profile data to Asyncstorage so it can be used later. |
|
private |
_checkImages(index: Number, thumbnails: List): Boolean Checks if there are new images in the bike stored vs what was passed in. |
|
private |
_defaultCallback(message: *) Default callback |
|
private |
_editExistingInDatabase(newData: Object, callback: Function): * Overwrite existing data in database and call the function callback depending on if it was successful or not. |
|
private |
Get the user ID from the database, authstate or persistent storage, then read from the database. |
|
private |
_hasProperty(obj: Object, property: string): Boolean Checks if an object has a certain property. |
|
private |
_insertDataOnRead(currentUser: string, databaseData: Object) Insert data into the data object on a read from the database. |
|
private |
_insertDataOnUpdate(newData: Object): Boolean Insert data into the data object on an update trigger (from Presenter). |
|
private |
_profileDataExists(profileData: Object): Boolean, Number Checks if the profile exists based on the data of the profile. |
|
private |
_profileIDExists(id: string): Boolean, Number Checks if the profile exists based on the id. |
|
private |
_readDBUserOnce(userID: string) Read from the database once with the user id. |
|
private |
_removeIllustrationKey(thumbnails: List): List Removes the illustration key from the object and only adds the actual link. |
|
private |
_writeImageToDBStorage(id: Number, images: List, imagesFolder: string, onSuccess: Function, onError: Function) Write the image to the firebase storage and call the callbacks with the urls that were defined. |
|
private |
_writeNewInDatabase(newData: Object, callback: Function): * Write new data in database and call the function callback depending on if it was successful or not. |
Public Constructors
public constructor() source
Creates an instance of ProfileModel. Sets the default callback, creates an observerlist, and registers an on read from the database.
Public Methods
public async checkAuthenticationState(onComplete: Function) source
Check the authentication state of the user. TODO : Consider extracting out to AuthenticationModel
Params:
Name | Type | Attribute | Description |
onComplete | Function | A callback function to call when authentication has completed |
public async getProfileData(callback: Function) source
Gets the profile data from Asyncstorage.
Params:
Name | Type | Attribute | Description |
callback | Function | A callback to call with the retrieved data |
public getProfilePicture(callback: *) source
Gets the profile data from Asyncstorage.
Params:
Name | Type | Attribute | Description |
callback | * |
public setCallback(callback: Function) source
Set the model's callback to a new callback. This callback can be used anywhere and is usually passed in from a presenter.
Params:
Name | Type | Attribute | Description |
callback | Function | A callback to run when certain code is executed |
public update(newData: Object) source
Update method for presenters to update the model's data. Datetime and Owner are handled in database class.
Params:
Name | Type | Attribute | Description |
newData | Object | New data to add |
Private Methods
private async _addProfileDataLocally(userID: string, data: Object) source
Writes the profile data to Asyncstorage so it can be used later.
private _checkImages(index: Number, thumbnails: List): Boolean source
Checks if there are new images in the bike stored vs what was passed in.
Params:
Name | Type | Attribute | Description |
index | Number | The index of the bike in the local data |
|
thumbnails | List | A list of thumbnails |
Return:
Boolean | true: If the thumbnails are the same; false: If the thumbnails are different or if the bike doesn't exist |
private _defaultCallback(message: *) source
Default callback
Params:
Name | Type | Attribute | Description |
message | * |
private _editExistingInDatabase(newData: Object, callback: Function): * source
Overwrite existing data in database and call the function callback depending on if it was successful or not.
Return:
* |
private _getUserDataFromDB() source
Get the user ID from the database, authstate or persistent storage, then read from the database.
private _hasProperty(obj: Object, property: string): Boolean source
Checks if an object has a certain property.
private _insertDataOnRead(currentUser: string, databaseData: Object) source
Insert data into the data object on a read from the database.
private _insertDataOnUpdate(newData: Object): Boolean source
Insert data into the data object on an update trigger (from Presenter).
Params:
Name | Type | Attribute | Description |
newData | Object | New data passed in, of the form : {data: []} |
private _profileDataExists(profileData: Object): Boolean, Number source
Checks if the profile exists based on the data of the profile.
Params:
Name | Type | Attribute | Description |
profileData | Object | The data to check |
Return:
Boolean, Number | exists: true: If the profile exists; false: otherwise. index - The index of the profile if it exists, -1 if not |
private _profileIDExists(id: string): Boolean, Number source
Checks if the profile exists based on the id.
Params:
Name | Type | Attribute | Description |
id | string | The id of a profile |
Return:
Boolean, Number | exists: true: If the profile exists; false: otherwise. index - The index of the profile if it exists, -1 if not |
private _readDBUserOnce(userID: string) source
Read from the database once with the user id.
Params:
Name | Type | Attribute | Description |
userID | string | The user's ID |
private _removeIllustrationKey(thumbnails: List): List source
Removes the illustration key from the object and only adds the actual link.
Params:
Name | Type | Attribute | Description |
thumbnails | List | A list of thumbnail objects with the property 'illustration' |
Return:
List | A list of thumbnails |
private _writeImageToDBStorage(id: Number, images: List, imagesFolder: string, onSuccess: Function, onError: Function) source
Write the image to the firebase storage and call the callbacks with the urls that were defined.
Params:
Name | Type | Attribute | Description |
id | Number | The id of the profile corresponding to the image |
|
images | List | A list of objects with the property 'illustration' |
|
imagesFolder | string | The folder to upload images to |
|
onSuccess | Function | A callback to call when an image has been successfully uploaded |
|
onError | Function | A callback to call when an image has failed to upload |