diff --git a/Yep/Base.lproj/Main.storyboard b/Yep/Base.lproj/Main.storyboard index fe2c483b..f8f15324 100644 --- a/Yep/Base.lproj/Main.storyboard +++ b/Yep/Base.lproj/Main.storyboard @@ -734,8 +734,27 @@ + + + + + + + + + + + + + + + + + + + diff --git a/Yep/Services/YepService.swift b/Yep/Services/YepService.swift index a549bef6..1a1d6d58 100644 --- a/Yep/Services/YepService.swift +++ b/Yep/Services/YepService.swift @@ -451,7 +451,7 @@ func searchUsersByMobile(mobile: String, #failureHandler: ((Reason, String?) -> typealias UploadContact = [String: String] -func friendsInContacts(contacts: [UploadContact], #failureHandler: ((Reason, String?) -> Void)?, #completion: [JSONDictionary] -> Void) { +func friendsInContacts(contacts: [UploadContact], #failureHandler: ((Reason, String?) -> Void)?, #completion: [DiscoveredUser] -> Void) { if let contactsData = NSJSONSerialization.dataWithJSONObject(contacts, options: .PrettyPrinted, error: nil), @@ -461,9 +461,21 @@ func friendsInContacts(contacts: [UploadContact], #failureHandler: ((Reason, Str "contacts": contactsString ] - let parse: JSONDictionary -> [JSONDictionary]? = { data in - if let registeredContacts = data["registered_contacts"] as? [JSONDictionary] { - return registeredContacts + let parse: JSONDictionary -> [DiscoveredUser]? = { data in + if let registeredContacts = data["registered_users"] as? [JSONDictionary] { + + //println("registeredContacts: \(registeredContacts)") + + var discoveredUsers = [DiscoveredUser]() + + for registeredContact in registeredContacts { + if let discoverUser = parseDiscoveredUser(registeredContact) { + discoveredUsers.append(discoverUser) + } + } + + return discoveredUsers + } else { return nil } @@ -560,47 +572,56 @@ struct DiscoveredUser { let socialAccountProviders: [SocialAccountProvider] } +let parseDiscoveredUser: JSONDictionary -> DiscoveredUser? = { userInfo in + if let + id = userInfo["id"] as? String, + nickname = userInfo["nickname"] as? String, + avatarURLString = userInfo["avatar_url"] as? String, + createdAtString = userInfo["created_at"] as? String, + lastSignInAtString = userInfo["last_sign_in_at"] as? String, + longitude = userInfo["longitude"] as? Double, + latitude = userInfo["latitude"] as? Double, + distance = userInfo["distance"] as? Double, + masterSkillsData = userInfo["master_skills"] as? [JSONDictionary], + learningSkillsData = userInfo["learning_skills"] as? [JSONDictionary], + socialAccountProvidersInfo = userInfo["providers"] as? [String: Bool] { + + let createdAt = NSDate.dateWithISO08601String(createdAtString) + let lastSignInAt = NSDate.dateWithISO08601String(lastSignInAtString) + + let masterSkills = skillsFromSkillsData(masterSkillsData) + let learningSkills = skillsFromSkillsData(learningSkillsData) + + var socialAccountProviders = Array() + + for (name, enabled) in socialAccountProvidersInfo { + let provider = DiscoveredUser.SocialAccountProvider(name: name, enabled: enabled) + + socialAccountProviders.append(provider) + } + + let introduction = userInfo["introduction"] as? String + + let discoverUser = DiscoveredUser(id: id, nickname: nickname, introduction: introduction, avatarURLString: avatarURLString, createdAt: createdAt, lastSignInAt: lastSignInAt, longitude: longitude, latitude: latitude, distance: distance, masterSkills: masterSkills, learningSkills: learningSkills, socialAccountProviders: socialAccountProviders) + + return discoverUser + } + + return nil +} + let parseDiscoveredUsers: JSONDictionary -> [DiscoveredUser]? = { data in - println("discoverUsers: \(data)") + //println("discoverUsers: \(data)") if let usersData = data["users"] as? [JSONDictionary] { var discoveredUsers = [DiscoveredUser]() for userInfo in usersData { - if let - id = userInfo["id"] as? String, - nickname = userInfo["nickname"] as? String, - avatarURLString = userInfo["avatar_url"] as? String, - createdAtString = userInfo["created_at"] as? String, - lastSignInAtString = userInfo["last_sign_in_at"] as? String, - longitude = userInfo["longitude"] as? Double, - latitude = userInfo["latitude"] as? Double, - distance = userInfo["distance"] as? Double, - masterSkillsData = userInfo["master_skills"] as? [JSONDictionary], - learningSkillsData = userInfo["learning_skills"] as? [JSONDictionary], - socialAccountProvidersInfo = userInfo["providers"] as? [String: Bool] { - let createdAt = NSDate.dateWithISO08601String(createdAtString) - let lastSignInAt = NSDate.dateWithISO08601String(lastSignInAtString) - - let masterSkills = skillsFromSkillsData(masterSkillsData) - let learningSkills = skillsFromSkillsData(learningSkillsData) - - var socialAccountProviders = Array() - - for (name, enabled) in socialAccountProvidersInfo { - let provider = DiscoveredUser.SocialAccountProvider(name: name, enabled: enabled) - - socialAccountProviders.append(provider) - } - - let introduction = userInfo["introduction"] as? String - - let discoverUser = DiscoveredUser(id: id, nickname: nickname, introduction: introduction, avatarURLString: avatarURLString, createdAt: createdAt, lastSignInAt: lastSignInAt, longitude: longitude, latitude: latitude, distance: distance, masterSkills: masterSkills, learningSkills: learningSkills, socialAccountProviders: socialAccountProviders) - - discoveredUsers.append(discoverUser) + if let discoverUser = parseDiscoveredUser(userInfo) { + discoveredUsers.append(discoverUser) } } diff --git a/Yep/ViewControllers/FriendsInContacts/FriendsInContactsViewController.swift b/Yep/ViewControllers/FriendsInContacts/FriendsInContactsViewController.swift index d2b36cfd..7df63d7f 100644 --- a/Yep/ViewControllers/FriendsInContacts/FriendsInContactsViewController.swift +++ b/Yep/ViewControllers/FriendsInContacts/FriendsInContactsViewController.swift @@ -11,17 +11,29 @@ import APAddressBook class FriendsInContactsViewController: UIViewController { + @IBOutlet weak var friendsTableView: UITableView! + lazy var addressBook: APAddressBook = { let addressBook = APAddressBook() addressBook.fieldsMask = APContactField(rawValue: APContactField.CompositeName.rawValue | APContactField.Phones.rawValue) return addressBook }() + var discoveredUsers = [DiscoveredUser]() { + didSet { + updateDiscoverTableView() + } + } + + let cellIdentifier = "ContactsCell" + override func viewDidLoad() { super.viewDidLoad() title = NSLocalizedString("Available Friends", comment: "") + friendsTableView.registerNib(UINib(nibName: cellIdentifier, bundle: nil), forCellReuseIdentifier: cellIdentifier) + friendsTableView.rowHeight = 80 addressBook.loadContacts{ (contacts: [AnyObject]!, error: NSError!) in if let contacts = contacts as? [APContact] { @@ -29,7 +41,6 @@ class FriendsInContactsViewController: UIViewController { var uploadContacts = [UploadContact]() for contact in contacts { - println(contact.compositeName, contact.phones) let name = contact.compositeName @@ -41,10 +52,12 @@ class FriendsInContactsViewController: UIViewController { } } - println(uploadContacts) + //println(uploadContacts) - friendsInContacts(uploadContacts, failureHandler: nil, completion: { x in - println(x) + friendsInContacts(uploadContacts, failureHandler: nil, completion: { discoveredUsers in + dispatch_async(dispatch_get_main_queue()) { + self.discoveredUsers = discoveredUsers + } }) } else if (error != nil) { @@ -53,4 +66,50 @@ class FriendsInContactsViewController: UIViewController { } } + // MARK: Actions + + func updateDiscoverTableView() { + friendsTableView.reloadSections(NSIndexSet(index: 0), withRowAnimation: UITableViewRowAnimation.Automatic) + } + } + +// MARK: UITableViewDataSource, UITableViewDelegate + +extension FriendsInContactsViewController: UITableViewDataSource, UITableViewDelegate { + + func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + return discoveredUsers.count + } + + func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { + var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as! ContactsCell + + let discoveredUser = discoveredUsers[indexPath.row] + + let radius = min(CGRectGetWidth(cell.avatarImageView.bounds), CGRectGetHeight(cell.avatarImageView.bounds)) * 0.5 + + let avatarURLString = discoveredUser.avatarURLString + AvatarCache.sharedInstance.roundAvatarWithAvatarURLString(avatarURLString, withRadius: radius) { roundImage in + dispatch_async(dispatch_get_main_queue()) { + cell.avatarImageView.image = roundImage + } + } + + cell.joinedDateLabel.text = discoveredUser.introduction + + let distance = discoveredUser.distance.format(".1") + cell.lastTimeSeenLabel.text = "\(distance) km | \(discoveredUser.lastSignInAt.timeAgo)" + + cell.nameLabel.text = discoveredUser.nickname + + return cell + } + + func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { + tableView.deselectRowAtIndexPath(indexPath, animated: true) + + //performSegueWithIdentifier("showProfile", sender: indexPath) + } +} +