mirror of
https://github.com/wahyd4/Yep.git
synced 2026-08-15 07:45:55 +10:00
friendsInContacts to discoveredUsers
This commit is contained in:
@@ -734,8 +734,27 @@
|
||||
<view key="view" contentMode="scaleToFill" id="rqb-L0-RRO">
|
||||
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
|
||||
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
|
||||
<subviews>
|
||||
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="22" sectionFooterHeight="22" translatesAutoresizingMaskIntoConstraints="NO" id="vdN-uK-rFe">
|
||||
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
|
||||
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
|
||||
<connections>
|
||||
<outlet property="dataSource" destination="KtV-SQ-G84" id="OVq-w5-ONj"/>
|
||||
<outlet property="delegate" destination="KtV-SQ-G84" id="VCQ-Db-f3r"/>
|
||||
</connections>
|
||||
</tableView>
|
||||
</subviews>
|
||||
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
|
||||
<constraints>
|
||||
<constraint firstAttribute="bottom" secondItem="vdN-uK-rFe" secondAttribute="bottom" id="K2v-hJ-HU9"/>
|
||||
<constraint firstItem="vdN-uK-rFe" firstAttribute="top" secondItem="rqb-L0-RRO" secondAttribute="top" id="dOq-t0-giI"/>
|
||||
<constraint firstAttribute="trailing" secondItem="vdN-uK-rFe" secondAttribute="trailing" id="nZB-Gj-f5W"/>
|
||||
<constraint firstItem="vdN-uK-rFe" firstAttribute="leading" secondItem="rqb-L0-RRO" secondAttribute="leading" id="vMg-c4-RUe"/>
|
||||
</constraints>
|
||||
</view>
|
||||
<connections>
|
||||
<outlet property="friendsTableView" destination="vdN-uK-rFe" id="aVj-dZ-a7Q"/>
|
||||
</connections>
|
||||
</viewController>
|
||||
<placeholder placeholderIdentifier="IBFirstResponder" id="4Rg-GM-ojT" userLabel="First Responder" sceneMemberID="firstResponder"/>
|
||||
</objects>
|
||||
|
||||
@@ -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<DiscoveredUser.SocialAccountProvider>()
|
||||
|
||||
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<DiscoveredUser.SocialAccountProvider>()
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user