diff --git a/adam.xcodeproj/project.pbxproj b/adam.xcodeproj/project.pbxproj index f291d38..bbedaf1 100644 --- a/adam.xcodeproj/project.pbxproj +++ b/adam.xcodeproj/project.pbxproj @@ -29,6 +29,7 @@ 9656ADAB1AECC62800933A8E /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 9656ADA91AECC62800933A8E /* LaunchScreen.xib */; }; 9656ADB71AECC62900933A8E /* benriTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9656ADB61AECC62900933A8E /* benriTests.swift */; }; 967867701B2C9A7F0047C7BB /* SearchCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9678676F1B2C9A7F0047C7BB /* SearchCell.swift */; }; + 969AB5B21B36FC9100E92A9F /* User.swift in Sources */ = {isa = PBXBuildFile; fileRef = 969AB5B11B36FC9100E92A9F /* User.swift */; }; 969FB14E1B1AB32300439BDC /* FBSDKLoginKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 969FB14D1B1AB32300439BDC /* FBSDKLoginKit.framework */; }; 969FB1501B1AB35700439BDC /* FBSDKCoreKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 969FB14F1B1AB35700439BDC /* FBSDKCoreKit.framework */; }; 96C24E901B244527006C5806 /* SearchViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96C24E8F1B244527006C5806 /* SearchViewController.swift */; }; @@ -85,6 +86,7 @@ 9656ADB51AECC62900933A8E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 9656ADB61AECC62900933A8E /* benriTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = benriTests.swift; sourceTree = ""; }; 9678676F1B2C9A7F0047C7BB /* SearchCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SearchCell.swift; sourceTree = ""; }; + 969AB5B11B36FC9100E92A9F /* User.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = User.swift; sourceTree = ""; }; 969FB14D1B1AB32300439BDC /* FBSDKLoginKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = FBSDKLoginKit.framework; sourceTree = ""; }; 969FB14F1B1AB35700439BDC /* FBSDKCoreKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = FBSDKCoreKit.framework; sourceTree = ""; }; 96C24E8F1B244527006C5806 /* SearchViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SearchViewController.swift; sourceTree = ""; }; @@ -188,13 +190,12 @@ 96E3771B1B359C1F00229684 /* Tutorial */, 96DA5A131AF284A80003E506 /* Menu */, 96DA5A0A1AF2744F0003E506 /* Lib */, + 969AB5B01B36F52700E92A9F /* Profile */, 9656AD9E1AECC62800933A8E /* AppDelegate.swift */, - 964690851B3308D800DB187D /* TutorialViewController.swift */, 9656ADA01AECC62800933A8E /* FirstViewController.swift */, 96C24E8F1B244527006C5806 /* SearchViewController.swift */, 9616799A1B2C72BA00A43AD3 /* LocationSearchViewController.swift */, 9656ADA21AECC62800933A8E /* SecondViewController.swift */, - 2150F5001B0EFEE200AFDF5E /* ProfileViewController.swift */, 21D7412B1B21AD36008A8EB5 /* DiscoverViewController.swift */, 21D741291B21AA4B008A8EB5 /* DiscoverView.swift */, 9678676F1B2C9A7F0047C7BB /* SearchCell.swift */, @@ -233,6 +234,15 @@ name = "Supporting Files"; sourceTree = ""; }; + 969AB5B01B36F52700E92A9F /* Profile */ = { + isa = PBXGroup; + children = ( + 2150F5001B0EFEE200AFDF5E /* ProfileViewController.swift */, + 969AB5B11B36FC9100E92A9F /* User.swift */, + ); + name = Profile; + sourceTree = ""; + }; 96DA5A0A1AF2744F0003E506 /* Lib */ = { isa = PBXGroup; children = ( @@ -272,6 +282,7 @@ isa = PBXGroup; children = ( 964690891B3392F600DB187D /* PageContentViewController.swift */, + 964690851B3308D800DB187D /* TutorialViewController.swift */, ); name = Tutorial; sourceTree = ""; @@ -454,6 +465,7 @@ 21577E061B2E875000D4218F /* CurrencyConverter.swift in Sources */, 96DA5A121AF274670003E506 /* ImageCache.swift in Sources */, 96C24E901B244527006C5806 /* SearchViewController.swift in Sources */, + 969AB5B21B36FC9100E92A9F /* User.swift in Sources */, 2150F5011B0EFEE200AFDF5E /* ProfileViewController.swift in Sources */, 21AF14871B05FA0B00108190 /* RestaurantServerAPI.swift in Sources */, 9656ADA11AECC62800933A8E /* FirstViewController.swift in Sources */, diff --git a/benri/Base.lproj/Main.storyboard b/benri/Base.lproj/Main.storyboard index d6591b7..72424f6 100644 --- a/benri/Base.lproj/Main.storyboard +++ b/benri/Base.lproj/Main.storyboard @@ -79,9 +79,6 @@ - - - @@ -93,8 +90,7 @@ - - + @@ -116,8 +112,7 @@ - - + @@ -128,18 +123,12 @@ - - - @@ -1674,20 +1663,219 @@ - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/benri/PageContentViewController.swift b/benri/PageContentViewController.swift index 01211c3..3177e1e 100644 --- a/benri/PageContentViewController.swift +++ b/benri/PageContentViewController.swift @@ -41,8 +41,9 @@ class PageContentViewController: UIViewController, FBSDKLoginButtonDelegate { if self.pageIndex == 5 { self.textView.hidden = true self.loginTextView.text = NSLocalizedString(detailText, comment: "Tutorial text") + let facebookLoginButton: FBSDKLoginButton = FBSDKLoginButton() - self.loginButton = FBSDKLoginButton() + self.loginButton = facebookLoginButton self.loginButton.readPermissions = ["public_profile", "email", "user_friends"] self.loginButton.delegate = self @@ -111,6 +112,7 @@ class PageContentViewController: UIViewController, FBSDKLoginButtonDelegate { var email:String! var fbID:String! var userName:String! + var profileImgURL:NSURL! if ((error) != nil) { // Process error @@ -121,8 +123,10 @@ class PageContentViewController: UIViewController, FBSDKLoginButtonDelegate { email = result.valueForKey("email") as! String token = FBSDKAccessToken.currentAccessToken().tokenString userName = result.valueForKey("name") as! String + profileImgURL = NSURL(fileURLWithPath: String(format: "https://graph.facebook.com/%@/picture?type=large", fbID)) } - self.delegate.didLoginFacebook(email, token:token, id: fbID, userName: userName) + println(profileImgURL) + self.delegate.didLoginFacebook(email, token:token, profileImgURL: profileImgURL, userName: userName) }) } } diff --git a/benri/ProfileViewController.swift b/benri/ProfileViewController.swift index 0581d78..db5755d 100644 --- a/benri/ProfileViewController.swift +++ b/benri/ProfileViewController.swift @@ -8,14 +8,69 @@ import Foundation import UIKit +import Alamofire -class ProfileViewController: UIViewController { +extension UIImageView { + func setRoundImage() { + self.layer.cornerRadius = self.frame.size.width / 2 + self.clipsToBounds = true + } +} + +class ProfileViewController: UIViewController, FBSDKLoginButtonDelegate, UIAlertViewDelegate { + + @IBOutlet weak var imageView: UIImageView! + @IBOutlet weak var nameLabel: UILabel! + @IBOutlet weak var emailLabel: UILabel! + + @IBOutlet weak var logOutButton: UIButton! + @IBOutlet weak var upperView: UIView! + @IBOutlet weak var detailTable: UITableView! + + var loginView:FBSDKLoginButton! + + var userDefault:NSUserDefaults! + var imageCache:ImageCache! + var request: Alamofire.Request? + + var fullName:String! + var email:String! + var profileImageURLString:String! + + + @IBAction func logOut(sender: AnyObject) { + var alert:UIAlertView = UIAlertView(title: "Confirm log out", message: "Are you sure you want to log out?", delegate: self, cancelButtonTitle: "cancel", otherButtonTitles: "yes") + alert.show() + } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. - //var facebookButton:FBSDKLoginButton = FBSDKLoginButton() - //self.view.addSubview(facebookButton) + + self.userDefault = NSUserDefaults.standardUserDefaults() + self.imageCache = ImageCache.sharedInstance + + if (FBSDKAccessToken.currentAccessToken() != nil) { + // User is already logged in, do work such as go to next view controller. + + // Or Show Logout Button + self.loginView = FBSDKLoginButton() + self.view.addSubview(loginView) + self.loginView.center = self.view.center + self.loginView.readPermissions = ["public_profile", "email", "user_friends"] + self.loginView.delegate = self + self.loginView.hidden = true + self.returnUserData() + + } + else { + self.loginView = FBSDKLoginButton() + self.view.addSubview(loginView) + self.loginView.center = self.view.center + self.loginView.readPermissions = ["public_profile", "email", "user_friends"] + self.loginView.delegate = self + self.hideProfilePage() + } } override func didReceiveMemoryWarning() { @@ -23,5 +78,146 @@ class ProfileViewController: UIViewController { // Dispose of any resources that can be recreated. } + // Facebook Delegate Methods + + func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) { + if ((error) != nil){ + + } + else if result.isCancelled { + + } + else { + // If you ask for multiple permissions at once, you + // should check if specific permissions missing + if result.grantedPermissions.contains("email") + { + // Do work + } + self.returnUserData() + self.loginView.hidden = true + } + } + + func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) { + println("User Logged Out") + } + + func returnUserData() { + let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil) + graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in + + if ((error) != nil) + { + // Process error + println("Error: \(error)") + } + else + { + self.userDefault = NSUserDefaults.standardUserDefaults() + + // Email + let myEmail = result.valueForKey("email") as! String + self.userDefault.setObject(myEmail, forKey: "email") + + + // Name + let myName = result.valueForKey("name") as! String + self.userDefault.setObject(myName, forKey: "userName") + + // Profile Image + let fbID = result.valueForKey("id") as! String + let profileImgURL = String(format: "https://graph.facebook.com/%@/picture?type=large", fbID) + self.userDefault.setObject(profileImgURL, forKey: "profileImgURL") + self.userDefault.synchronize() + + self.setupProfileView() + self.view.setNeedsDisplay() + + println("fetched user: \(result)") + + if FBSDKAccessToken.currentAccessToken() != nil { + println(FBSDKAccessToken.currentAccessToken().tokenString) + } + } + }) + } + + private func showLoginButton() { + + } + + private func setupProfileView() { + // Hide detail for now + self.detailTable.hidden = true + + self.userDefault = NSUserDefaults.standardUserDefaults() + + // Email + if let myEmail = self.userDefault.objectForKey("email") as? String { + self.emailLabel.text = myEmail + } + + // Name + if let myName = self.userDefault.objectForKey("userName") as? String { + self.nameLabel.text = myName + } + + self.request?.cancel() + // Profile Image + if let imageURL = self.userDefault.objectForKey("profileImgURL") as? String { + if let profileImage = self.imageCache.loadImage(NSURL(string: imageURL)!) { + self.imageView.image = profileImage + self.imageView.setRoundImage() + } else { + + self.request = Alamofire.request(.GET, imageURL).validate(contentType: ["image/*"]).responseImage() { + (request, _, image, error) in + if error == nil && image != nil { + self.imageCache.cacheImage(request.URL, image: image!) + self.imageView.image = image + self.imageView.setRoundImage() + + } + } + } + } + + self.showProfilePage() + } + + private func showProfilePage() { + self.upperView.hidden = false + //self.detailTable.hidden = false + self.logOutButton.hidden = false + } + private func hideProfilePage() { + self.upperView.hidden = true + self.detailTable.hidden = true + self.logOutButton.hidden = true + } + // MARK: Alertview delegate + + func alertView(alertView: UIAlertView, didDismissWithButtonIndex buttonIndex: Int) { + if (buttonIndex == 0) { + + } + else if (buttonIndex == 1) { + let loginManager = FBSDKLoginManager() + loginManager.logOut() + + self.userDefault = NSUserDefaults.standardUserDefaults() + + self.userDefault.removeObjectForKey("email") + self.userDefault.removeObjectForKey("userName") + self.userDefault.removeObjectForKey("profileImgURL") + self.userDefault.synchronize() + + self.loginView.hidden = false + self.hideProfilePage() + self.view.setNeedsDisplay() + } + + } } \ No newline at end of file diff --git a/benri/TutorialViewController.swift b/benri/TutorialViewController.swift index 28bc412..852b8b2 100644 --- a/benri/TutorialViewController.swift +++ b/benri/TutorialViewController.swift @@ -9,7 +9,7 @@ import UIKit protocol TutorialDelegate { - func didLoginFacebook(email:String?, token:String?, id:String?, userName:String?) + func didLoginFacebook(email:String?, token:String?, profileImgURL:NSURL?, userName:String?) func didSkipSignIn() } @@ -132,10 +132,11 @@ class TutorialViewController:UIViewController, UIPageViewControllerDataSource, U } // MARK: - Tutorial Delegate - func didLoginFacebook(email:String?,token: String?, id: String?, userName:String?) { + func didLoginFacebook(email:String?, token: String?, profileImgURL:NSURL?, userName:String?) { userDefault.setBool(true, forKey: "didFinishedTutorial") userDefault.setObject(email, forKey: "email") userDefault.setObject(userName, forKey: "userName") + userDefault.setObject(profileImgURL, forKey: "profileImgURL") userDefault.synchronize() self.performSegueWithIdentifier("tutorialSegueUnwind", sender: self) } diff --git a/benri/User.swift b/benri/User.swift new file mode 100644 index 0000000..0c3e582 --- /dev/null +++ b/benri/User.swift @@ -0,0 +1,50 @@ +// +// User.swift +// adam +// +// Created by Kittikorn Ariyasuk on 6/21/15. +// Copyright (c) 2015 gobbl. All rights reserved. +// + +import Foundation + +class User:NSObject { + + private var email: String! + private var facebookID: String! + private var fullName: String! + private var profileImageURL: String! + private var isUserSet:Bool! + + override init() { + self.isUserSet = false + } + + func setUpUserData(email:String, facebookID:String, fullName:String, profileImageURL:String) { + self.email = email + self.facebookID = facebookID + self.fullName = fullName + self.profileImageURL = profileImageURL + self.isUserSet = true + } + + func clearData() { + self.email = nil + self.facebookID = nil + self.fullName = nil + self.profileImageURL = nil + self.isUserSet = false + } + + class var sharedInstance : User { + struct Static { + static var onceToken : dispatch_once_t = 0 + static var instance : User? = nil + } + dispatch_once(&Static.onceToken) { + Static.instance = User() + } + return Static.instance! + } + +} \ No newline at end of file