diff --git a/Yep.xcodeproj/project.pbxproj b/Yep.xcodeproj/project.pbxproj index 21c2fe43..375dd383 100644 --- a/Yep.xcodeproj/project.pbxproj +++ b/Yep.xcodeproj/project.pbxproj @@ -8,6 +8,7 @@ /* Begin PBXBuildFile section */ 0A5D83941AC5D3C8000045BF /* YepTabBarController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A5D83931AC5D3C8000045BF /* YepTabBarController.swift */; }; + 0A5D83991AC5D91E000045BF /* ContactsSearchTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A5D83981AC5D91E000045BF /* ContactsSearchTableViewController.swift */; }; 0A8F54B11AB67D11004AD60E /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A8F54B01AB67D11004AD60E /* AppDelegate.swift */; }; 0A8F54B61AB67D11004AD60E /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 0A8F54B41AB67D11004AD60E /* Main.storyboard */; }; 0A8F54B81AB67D11004AD60E /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 0A8F54B71AB67D11004AD60E /* Images.xcassets */; }; @@ -88,6 +89,7 @@ /* Begin PBXFileReference section */ 0A5D83931AC5D3C8000045BF /* YepTabBarController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = YepTabBarController.swift; path = ViewControllers/TabBar/YepTabBarController.swift; sourceTree = ""; }; + 0A5D83981AC5D91E000045BF /* ContactsSearchTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ContactsSearchTableViewController.swift; path = ViewControllers/Contacts/Search/ContactsSearchTableViewController.swift; sourceTree = ""; }; 0A8F54AB1AB67D11004AD60E /* Yep.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Yep.app; sourceTree = BUILT_PRODUCTS_DIR; }; 0A8F54AF1AB67D11004AD60E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 0A8F54B01AB67D11004AD60E /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; @@ -204,6 +206,14 @@ name = TabBar; sourceTree = ""; }; + 0A5D839A1AC5D923000045BF /* Search */ = { + isa = PBXGroup; + children = ( + 0A5D83981AC5D91E000045BF /* ContactsSearchTableViewController.swift */, + ); + name = Search; + sourceTree = ""; + }; 0A8F54A21AB67D11004AD60E = { isa = PBXGroup; children = ( @@ -311,6 +321,7 @@ isa = PBXGroup; children = ( 5023DE291AB685BA00B3EE96 /* ContactsViewController.swift */, + 0A5D839A1AC5D923000045BF /* Search */, ); name = Contacts; sourceTree = ""; @@ -820,6 +831,7 @@ 502AE52B1AB819AF005BD199 /* YepConfig.swift in Sources */, 503315A01AC126100008A209 /* MessageToolbar.swift in Sources */, 502AE5531AB93215005BD199 /* ProfileFooterCell.swift in Sources */, + 0A5D83991AC5D91E000045BF /* ContactsSearchTableViewController.swift in Sources */, 5023DE521AB6C51300B3EE96 /* CGRect+Yep.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/Yep/Base.lproj/Intro.storyboard b/Yep/Base.lproj/Intro.storyboard index 5129b435..f62a5b94 100644 --- a/Yep/Base.lproj/Intro.storyboard +++ b/Yep/Base.lproj/Intro.storyboard @@ -1,7 +1,7 @@ - + - + diff --git a/Yep/Base.lproj/Main.storyboard b/Yep/Base.lproj/Main.storyboard index caf068eb..1e6c8dff 100644 --- a/Yep/Base.lproj/Main.storyboard +++ b/Yep/Base.lproj/Main.storyboard @@ -125,7 +125,13 @@ - + + + + + + + @@ -134,6 +140,64 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Yep/Services/YepService.swift b/Yep/Services/YepService.swift index c18aca76..a9cbcda4 100644 --- a/Yep/Services/YepService.swift +++ b/Yep/Services/YepService.swift @@ -207,6 +207,30 @@ func loginByMobile(mobile: String, withAreaCode areaCode: String, #verifyCode: S } } +// MARK: Contacts + +func searchUsersWithPhoneNumber(ofPhoneNumber phoneNumber: String, #failureHandler: ((Reason, String?) -> ())?, #completion: [AnyObject] -> Void) { + + let requestParameters = [ + "q": phoneNumber + ] + + let parse: JSONDictionary -> [AnyObject]? = { data in + if let users = data["users"] as? [AnyObject] { + return users + } + return [] + } + + let resource = authJsonResource(path: "/api/v1/users/search", method: .GET, requestParameters: requestParameters, parse: parse) + + if let failureHandler = failureHandler { + apiRequest({_ in}, baseURL, resource, failureHandler, completion) + } else { + apiRequest({_ in}, baseURL, resource, defaultFailureHandler, completion) + } +} + // MARK: Upload func publicUploadToken(#failureHandler: ((Reason, String?) -> ())?, #completion: QiniuProvider -> Void) { diff --git a/Yep/ViewControllers/Contacts/Search/ContactsSearchTableViewController.swift b/Yep/ViewControllers/Contacts/Search/ContactsSearchTableViewController.swift new file mode 100644 index 00000000..eb52465c --- /dev/null +++ b/Yep/ViewControllers/Contacts/Search/ContactsSearchTableViewController.swift @@ -0,0 +1,133 @@ +// +// ContactsSearchTableViewController.swift +// Yep +// +// Created by kevinzhow on 15/3/28. +// Copyright (c) 2015年 Catch Inc. All rights reserved. +// + +import UIKit + +class ContactsSearchTableViewController: UITableViewController,UISearchControllerDelegate,UISearchResultsUpdating { + + @IBOutlet weak var contactsSearchBar: UISearchBar! + + var searchController: UISearchController! + + var searchResult: NSArray = [] + + override func viewDidLoad() { + super.viewDidLoad() + + searchController = UISearchController(searchResultsController: nil) + searchController.delegate = self + searchController.searchBar.sizeToFit() + searchController.searchResultsUpdater = self + searchController.dimsBackgroundDuringPresentation = false + self.tableView.tableHeaderView = searchController.searchBar + // Uncomment the following line to preserve selection between presentations + // self.clearsSelectionOnViewWillAppear = false + + // Uncomment the following line to display an Edit button in the navigation bar for this view controller. + // self.navigationItem.rightBarButtonItem = self.editButtonItem() + } + + override func didReceiveMemoryWarning() { + super.didReceiveMemoryWarning() + // Dispose of any resources that can be recreated. + } + + // MARK: - Table view data source + + override func numberOfSectionsInTableView(tableView: UITableView) -> Int { + // #warning Potentially incomplete method implementation. + // Return the number of sections. + return 1 + } + + override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + // #warning Incomplete method implementation. + // Return the number of rows in the section. + return searchResult.count + } + + + override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { + let cell = tableView.dequeueReusableCellWithIdentifier("SearchUserCell", forIndexPath: indexPath) as! UITableViewCell + + let user = searchResult.objectAtIndex(indexPath.row) as! NSDictionary + cell.textLabel?.text = user.valueForKey("nickname") as? String + return cell + } + + func updateSearchResultsForSearchController(searchController: UISearchController) { + //do whatever with searchController here. + var searchText = searchController.searchBar.text + println(searchText) + + searchUsersWithPhoneNumber(ofPhoneNumber: searchText, failureHandler: { (reason, errorMessage) in + + }, completion: { users in + self.searchResult = users + if (self.searchResult.count > 0) { + dispatch_async(dispatch_get_main_queue()) { + self.tableView.reloadData() + } + } + } + ) + + } + + override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { + let user = searchResult.objectAtIndex(indexPath.row) as! NSDictionary + + } + + + /* + // Override to support conditional editing of the table view. + override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { + // Return NO if you do not want the specified item to be editable. + return true + } + */ + + /* + // Override to support editing the table view. + override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { + if editingStyle == .Delete { + // Delete the row from the data source + tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) + } else if editingStyle == .Insert { + // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view + } + } + */ + + /* + // Override to support rearranging the table view. + override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) { + + } + */ + + /* + // Override to support conditional rearranging of the table view. + override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool { + // Return NO if you do not want the item to be re-orderable. + return true + } + */ + + /* + // MARK: - Navigation + + // In a storyboard-based application, you will often want to do a little preparation before navigation + override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { + // Get the new view controller using [segue destinationViewController]. + // Pass the selected object to the new view controller. + } + */ + +}