Created
January 22, 2016 17:31
-
-
Save Bashta/c29d4f581e734e2969c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension DataManager { | |
//MARK: - Helpers | |
/** | |
Set the messages with given user as seen. | |
- Parameter userProfileId: The profile id of the user with whom the message have to be set as seen. | |
*/ | |
func setMessagesWithCurrentUserAsSeen(userProfileId: String) { | |
let params = NSMutableDictionary() | |
params.setObject(userProfileId, forKey: "senderId") | |
PFCloud.callFunctionInBackground("setMessagesSeen", withParameters: params as [NSObject : AnyObject]) | |
} | |
/** | |
Counts the number of unread messages for the current user. | |
- Parameter matches: Array of NSDictionary containg information for any current match that the user has. | |
*/ | |
func countNewMessages(matches: [NSDictionary]) -> String { | |
var numberOFNewMessages: Int = 0 | |
for match in matches { | |
if let newMessages = match.valueForKey("unread") as? Int { | |
numberOFNewMessages += newMessages | |
} | |
} | |
return numberOFNewMessages == 0 ? "" : String(numberOFNewMessages) | |
} | |
/** | |
Setting all singleton properties to nil | |
*/ | |
func clearSingletonData() { | |
//clear all data in order to prepare for a new user | |
currentUserID = nil | |
currentUserName = nil | |
currentUserProfileID = nil | |
userPictures = [] | |
matches = nil | |
} | |
/** | |
Handling users tap on match notification showing the match screen. | |
- Parameter data: NSDictionary containg the json data send from the backend. | |
*/ | |
//TODO: Move to private extension | |
func handleMatchPush(data: NSDictionary) { | |
guard let senderId = data.valueForKeyPath("push_container.senderId") as? String else { | |
return | |
} | |
fetchProfileDataForUserId(senderId) { | |
name, birthDate, nationality, occupation, height, about, location, avatars, lastActivity, profileId, gender in | |
guard let avatar = avatars?.first else { | |
return | |
} | |
self.fetchPictureFromPFFile(avatar, completion: { (image) -> Void in | |
let match = NSMutableDictionary() | |
match.setValue(name, forKey: "name") | |
match.setValue(profileId, forKey: "profileId") | |
match.setValue(image, forKey: "avatar") | |
guard let matchViewController = GlobalConstants.Controllers.navController?.storyboard?.instantiateViewControllerWithIdentifier(MatchViewController.storyboardId()) as? MatchViewController else { | |
return | |
} | |
matchViewController.matchPicture = image | |
matchViewController.matchName = name | |
matchViewController.match = match | |
if self.userPictures.isEmpty { | |
self.fetchCurrentUsersProfilePicture({ success, image in | |
guard let image = image else { | |
return | |
} | |
matchViewController.currentUserPicture = image | |
GlobalConstants.Controllers.navController?.presentViewController(matchViewController, animated: false, completion: nil) | |
}) | |
} else { | |
matchViewController.currentUserPicture = self.userPictures.first | |
GlobalConstants.Controllers.navController?.presentViewController(matchViewController, animated: false, completion: nil) | |
} | |
}) | |
} | |
} | |
/** | |
Handling users tap on message notification showing the chat screen if the user is not already on chat screen. | |
- Parameter data: NSDictionary containg the json data send from the backend. | |
*/ | |
func handleMessagePush(data: NSDictionary) { | |
guard let senderId = data.valueForKeyPath("push_container.senderId") as? String, | |
let chatViewController = GlobalConstants.Controllers.navController?.storyboard?.instantiateViewControllerWithIdentifier(ChatViewController.storyboardId()) as? ChatViewController else { | |
return | |
} | |
//prevent pushing the chat controller twice | |
if let _ = GlobalConstants.Controllers.navController?.viewControllers.last as? ChatViewController { | |
return | |
} | |
fetchProfileDataForUserId(senderId) { | |
name, birthDate, nationality, occupation, height, about, location, avatars, lastActivity, profileId, gender in | |
guard let avatar = avatars?.first else { | |
return | |
} | |
chatViewController.receiverId = senderId | |
chatViewController.receiverName = name | |
chatViewController.receiverPictureUrl = avatar.url | |
GlobalConstants.Controllers.navController?.pushViewController(chatViewController, animated: false) | |
} | |
} | |
//MARK: - User Lifecycle Helpers | |
/** | |
Creates and links a profile with the default values for the newly created user | |
- Parameter user: The current PFUser that the profile should be linked to. | |
- Parameter name: The name entered by the user in the signUp screen or FB name. | |
*/ | |
func createUserWithProfileAfterSuccessfullLogin(user: PFUser, name: String, completion: (completed: Bool) -> Void) { | |
let profile = PFObject(className: "Profile") | |
profile.setObject(user, forKey: "user") | |
//Set default search criteria values for new user | |
profile.setValue(50, forKey: "searchDistance") | |
profile.setValue(23, forKey: "searchAgeMin") | |
profile.setValue(35, forKey: "searchAgeMax") | |
profile.setValue("All Nationalities", forKey: "searchNationality") | |
profile.setValue(true , forKey: "isInterestedInWomen") | |
profile.setValue(true, forKey: "isInterestedInMen") | |
profile.setValue("Algeria", forKey: "nationality") | |
profile.setValue(name, forKey: "name") | |
profile.saveInBackgroundWithBlock({ success, error in | |
self.currentUserProfileID = profile.objectId | |
if success == true { | |
user.setObject(profile, forKey: "profile") | |
user.saveInBackgroundWithBlock({ success, error in | |
completion(completed: success) | |
}) | |
} | |
}) | |
} | |
/** | |
Handles app opening when user taps the notification, showing the match screen. | |
- Parameter pushData: NSDictionary containg the match data received from the push notification. | |
*/ | |
func handleAppllicationOpeningFromPushNotification(pushData: NSDictionary) { | |
guard let pushType = pushData.valueForKey("pushType") as? String else { | |
return | |
} | |
pushType == "matchPush" ? handleMatchPush(pushData) : handleMessagePush(pushData) | |
} | |
/** | |
Removes current user Object from the instalation preventing it form reciving notifications in the current device | |
*/ | |
func removeUserPointerFromCurrentInstalation() { | |
let params = NSDictionary(object: PFInstallation.currentInstallation().installationId, forKey: "installationId") as [NSObject : AnyObject] | |
PFCloud.callFunctionInBackground("deleteInstallations", withParameters: params) | |
} | |
/** | |
Adds current user to the instalation allowing it to receive notifications in the current device | |
*/ | |
func linkCurrentUserWithInstallation() { | |
guard let user = PFUser.currentUser() else { | |
return | |
} | |
PFInstallation.currentInstallation().setValue(user, forKey: "user") | |
PFInstallation.currentInstallation().saveInBackground() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment