Reference Source
public class | source

ProfileModel

Extends:

* → ProfileModel

Class for the Profile model to be used by the ProfilePresenter

Constructor Summary

Public Constructor
public

Creates an instance of ProfileModel.

Member Summary

Private Members
private
private

_data: {"data": *}

Method Summary

Public Methods
public

async checkAuthenticationState(onComplete: Function)

Check the authentication state of the user.

public

get(): Object

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(newData: Object)

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

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

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.

Private Members

private _callback: * source

private _data: {"data": *} source

Public Methods

public async checkAuthenticationState(onComplete: Function) source

Check the authentication state of the user. TODO : Consider extracting out to AuthenticationModel

Params:

NameTypeAttributeDescription
onComplete Function

A callback function to call when authentication has completed

public get(): Object source

Get method for presenters to get data.

Return:

Object

data stored in the model

public async getProfileData(callback: Function) source

Gets the profile data from Asyncstorage.

Params:

NameTypeAttributeDescription
callback Function

A callback to call with the retrieved data

public getProfilePicture(callback: *) source

this method was deprecated. Use getProfileData instead

Gets the profile data from Asyncstorage.

Params:

NameTypeAttributeDescription
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:

NameTypeAttributeDescription
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:

NameTypeAttributeDescription
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.

Params:

NameTypeAttributeDescription
userID string

The current user's id

data Object

The data to be stored

private _checkImages(index: Number, thumbnails: List): Boolean source

Checks if there are new images in the bike stored vs what was passed in.

Params:

NameTypeAttributeDescription
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:

NameTypeAttributeDescription
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.

Params:

NameTypeAttributeDescription
newData Object

Data to be written to the database

callback Function

A function to call on the success or failure of the call

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.

Params:

NameTypeAttributeDescription
obj Object

An object to check

property string

The name of a property

Return:

Boolean

true: if the object has the property; false: otherwise

private _insertDataOnRead(currentUser: string, databaseData: Object) source

Insert data into the data object on a read from the database.

Params:

NameTypeAttributeDescription
currentUser string

The user id of the current user

databaseData Object

An objects of objects containing data from the database.

private _insertDataOnUpdate(newData: Object): Boolean source

Insert data into the data object on an update trigger (from Presenter).

Params:

NameTypeAttributeDescription
newData Object

New data passed in, of the form : {data: []}

Return:

Boolean

true: Data was an edited value; false: Data was a new value

private _profileDataExists(profileData: Object): Boolean, Number source

Checks if the profile exists based on the data of the profile.

Params:

NameTypeAttributeDescription
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:

NameTypeAttributeDescription
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:

NameTypeAttributeDescription
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:

NameTypeAttributeDescription
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:

NameTypeAttributeDescription
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

private _writeNewInDatabase(newData: Object, callback: Function): * source

Write new data in database and call the function callback depending on if it was successful or not.

Params:

NameTypeAttributeDescription
newData Object

Data to be written to the database

callback Function

A function to call on the success or failure of the call

Return:

*