PageRenderTime 26ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/Project/C00Peeps/ViewControllers/SigninVc/SigninVc.swift

https://bitbucket.org/alexmad327/c00peeps
Swift | 335 lines | 162 code | 44 blank | 129 comment | 22 complexity | 987e82aecf5527e120838dc598ab6466 MD5 | raw file
Possible License(s): JSON, BSD-3-Clause, MIT
  1. //
  2. // SigninVc.swift
  3. // C00Peeps
  4. //
  5. // Created by SOTSYS011 on 3/9/16.
  6. // Copyright © 2016 SOTSYS011. All rights reserved.
  7. //
  8. import UIKit
  9. import SwiftyJSON
  10. import FBSDKLoginKit
  11. import TwitterKit
  12. class SigninVc: BaseVC
  13. {
  14. @IBOutlet var txtUserName: UITextField!
  15. @IBOutlet var txtPassword: UITextField!
  16. @IBOutlet weak var imgVwBg: UIImageView!
  17. @IBOutlet weak var imgVwLogo: UIImageView!
  18. @IBOutlet weak var btnSignIn: UIButton!
  19. @IBOutlet weak var btnFacebook: UIButton!
  20. @IBOutlet weak var btnTwitter: UIButton!
  21. @IBOutlet weak var btnInstagram: UIButton!
  22. var socialDetailsFromLogin = (accountType: AccountType.normal.rawValue, id:"", name: "", email: "")
  23. override func viewWillAppear(_ animated: Bool) {
  24. super.viewDidAppear(animated)
  25. self.navigationController?.isNavigationBarHidden = false
  26. }
  27. override func viewDidLoad()
  28. {
  29. super.viewDidLoad()
  30. self.title = "Sign In"
  31. SetPaddingView(txtUserName)
  32. SetPaddingView(txtPassword)
  33. txtUserName.text = "ashishkhurana"
  34. txtPassword.text = "Trantor123"
  35. // txtUserName.text = "aditya"
  36. // txtPassword.text = "admin"
  37. self.updateViewAccordingToTheme()
  38. self.perform(#selector (SigninVc.setTextFieldLines), with: nil, afterDelay: 0.1)
  39. NotificationCenter.default.addObserver(self, selector: #selector(navigateToSignUpScreen), name: NSNotification.Name(rawValue: "NavigateToSignup"), object: nil)
  40. }
  41. // MARK: - Update View According To Selected Theme
  42. func updateViewAccordingToTheme() {
  43. imgVwBg.image = Utilities().themedImage(img_bg)
  44. imgVwLogo.image = Utilities().themedImage(img_signInLogo)
  45. // Submit
  46. btnSignIn.setBackgroundImage(Utilities().themedImage(img_btn), for: .normal)
  47. btnSignIn.setBackgroundImage(Utilities().themedImage(img_btnSelected), for: .highlighted)
  48. // Facebook
  49. btnFacebook.setImage(Utilities().themedImage(img_facebook), for: .normal)
  50. btnFacebook.setImage(Utilities().themedImage(img_facebookSelected), for: .highlighted)
  51. // Twitter
  52. btnTwitter.setImage(Utilities().themedImage(img_twitter), for: .normal)
  53. btnTwitter.setImage(Utilities().themedImage(img_twitterSelected), for: .highlighted)
  54. // Instagram
  55. btnInstagram.setImage(Utilities().themedImage(img_instagram), for: .normal)
  56. btnInstagram.setImage(Utilities().themedImage(img_instagramSelected), for: .highlighted)
  57. }
  58. func setTextFieldLines(){
  59. setSubViewBorder(txtUserName, color: UIColor.white)
  60. setSubViewBorder(txtPassword, color: UIColor.white)
  61. }
  62. override func viewDidLayoutSubviews() {
  63. // setSubViewBorder(txtUserName, color: UIColor.whiteColor())
  64. // setSubViewBorder(txtPassword, color: UIColor.whiteColor())
  65. }
  66. // MARK: - Fetch Facebook Profile
  67. func fetchFacebookProfile() {
  68. ApplicationDelegate.showLoader(loaderTitle_FetchingProfile)
  69. FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, email"]).start(completionHandler: { (connection, result, error) -> Void in
  70. ApplicationDelegate.hideLoader()
  71. if (error == nil){
  72. let resultDict = result as! NSDictionary
  73. printCustom("profile:\(result)")
  74. self.socialDetailsFromLogin.accountType = AccountType.facebook.rawValue
  75. self.socialDetailsFromLogin.id = resultDict["id"] as! String
  76. self.socialDetailsFromLogin.name = resultDict["name"] as! String
  77. self.socialDetailsFromLogin.email = resultDict["email"] as! String
  78. self.callAPI_CheckSocialUserExists(AccountType.facebook, socialMediaID: resultDict["id"] as! String)
  79. }
  80. })
  81. }
  82. // MARK: - Instagram Login Callback
  83. func instagramProfile(id:String, name:String) {
  84. self.socialDetailsFromLogin.accountType = AccountType.instagram.rawValue
  85. self.socialDetailsFromLogin.id = id
  86. self.socialDetailsFromLogin.name = name
  87. self.socialDetailsFromLogin.email = ""
  88. self.callAPI_CheckSocialUserExists(AccountType.instagram, socialMediaID: id)
  89. }
  90. //MARK: - IBAction
  91. @IBAction func btnLogin_Clicked(sender: AnyObject)
  92. {
  93. // self.navigationController?.navigationBarHidden = true
  94. // let homeScreen = self.storyboard?.instantiateViewControllerWithIdentifier("tabBarcontroller") as? UITabBarController
  95. // self.navigationController?.pushViewController(homeScreen!, animated: true)
  96. // let ContactListObject = self.storyboard?.instantiateViewControllerWithIdentifier("ContactSelectionVc") as? ContactSelectionVc
  97. // self.navigationController?.pushViewController(ContactListObject!, animated: true)
  98. //
  99. //
  100. // return
  101. if txtUserName.text!.isEmpty {
  102. self.showCommonAlert(alertMsg_EmptyUsername)
  103. return
  104. }
  105. else if txtPassword.text!.isEmpty {
  106. self.showCommonAlert(alertMsg_EmptyPasscode)
  107. return
  108. }
  109. else
  110. {
  111. checkAccountActivatedFromAddContacts = false
  112. self.callAPI_SignIn(self.txtUserName.text!, password:self.txtPassword.text!)
  113. }
  114. }
  115. @IBAction func btnLoginWithFBClicked(sender: AnyObject) {
  116. if FBSDKAccessToken.current() != nil {
  117. self.fetchFacebookProfile()
  118. }
  119. else {
  120. let facebookLogin = FBSDKLoginManager()
  121. facebookLogin.logIn(withReadPermissions: ["email"], from: self, handler: { (facebookResult: FBSDKLoginManagerLoginResult!, facebookError: Error!) in
  122. if facebookResult != nil && !facebookResult.isCancelled {
  123. self.fetchFacebookProfile()
  124. }
  125. })
  126. //
  127. // facebookLogin.logInWithReadPermissions(["email"], fromViewController: self) { (facebookResult: FBSDKLoginManagerLoginResult!, facebookError: NSError!) -> Void in
  128. // if facebookResult != nil && !facebookResult.isCancelled {
  129. // self.fetchFacebookProfile()
  130. // }
  131. // }
  132. }
  133. }
  134. @IBAction func btnLoginWithTwitterClicked(sender: AnyObject) {
  135. ApplicationDelegate.showLoader(loaderTitle_PleaseWait)
  136. /*let store = Twitter.sharedInstance().sessionStore
  137. if store.session() != nil {
  138. if let userID:String = store.session()!.userID {
  139. store.logOutUserID(userID)
  140. ApplicationDelegate.hideLoader()
  141. if ACAccountStore().accountTypeWithAccountTypeIdentifier(ACAccountTypeIdentifierTwitter).accessGranted {
  142. ApplicationDelegate.showLoader(loaderTitle_ConnectingTwitter)
  143. }
  144. }
  145. }
  146. else {
  147. self.performSelector(#selector(hideLoaderConnectingTwitter), withObject: nil, afterDelay: 2)
  148. }
  149. Twitter.sharedInstance().logInWithCompletion { session, error in
  150. ApplicationDelegate.hideLoader()
  151. if error != nil {
  152. printCustom("error:\(error?.code)")
  153. printCustom("description:\(error?.description)")
  154. if error?.code != 1 {
  155. self.showCommonAlert(alertMsg_ErrorConnectingTwitter)
  156. }
  157. return
  158. }
  159. if (session != nil) {
  160. printCustom("signed in as \(session!.userName)");
  161. let client = TWTRAPIClient(userID: session?.userID)
  162. client.loadUserWithID(session!.userID) { (user, error) -> Void in
  163. printCustom("twitter user profile: \(user)");
  164. self.socialDetailsFromLogin.accountType = AccountType.Twitter.rawValue
  165. self.socialDetailsFromLogin.id = session!.userID
  166. self.socialDetailsFromLogin.name = user!.name
  167. self.socialDetailsFromLogin.email = ""
  168. self.callAPI_CheckSocialUserExists(AccountType.Twitter, socialMediaID: session!.userID)
  169. }
  170. }
  171. }*/
  172. let store = TWTRTwitter.sharedInstance().sessionStore
  173. if store.session() == nil {
  174. ApplicationDelegate.showLoader(loaderTitle_ConnectingTwitter)
  175. self.perform(#selector(hideLoaderConnectingTwitter), with: nil, afterDelay: 2)
  176. TWTRTwitter.sharedInstance().logIn { session, error in
  177. ApplicationDelegate.hideLoader()
  178. if error != nil {
  179. printCustom("error:\(error?.code)")
  180. //printCustom("description:\(error?.description)")
  181. if error?.code != 1 {
  182. self.showCommonAlert(alertMsg_ErrorConnectingTwitter)
  183. }
  184. return
  185. }
  186. else {
  187. ApplicationDelegate.showLoader(loaderTitle_FetchingProfile)
  188. }
  189. if (session != nil) {
  190. printCustom("signed in as \(session!.userName)");
  191. self.signedInWithTwitter(userID: session!.userID)
  192. }
  193. else {
  194. ApplicationDelegate.hideLoader()
  195. }
  196. }
  197. }
  198. else {
  199. ApplicationDelegate.showLoader(loaderTitle_FetchingProfile)
  200. self.signedInWithTwitter(userID: store.session()!.userID)
  201. }
  202. }
  203. func signedInWithTwitter(userID:String) {
  204. let client = TWTRAPIClient(userID: userID)
  205. client.loadUser(withID: userID) { (user, error) -> Void in
  206. printCustom("twitter user profile: \(user)");
  207. self.socialDetailsFromLogin.accountType = AccountType.twitter.rawValue
  208. self.socialDetailsFromLogin.id = userID
  209. self.socialDetailsFromLogin.name = user!.name
  210. self.socialDetailsFromLogin.email = ""
  211. self.callAPI_CheckSocialUserExists(AccountType.twitter, socialMediaID: userID)
  212. }
  213. }
  214. @IBAction func btnLoginWithInstagramClicked(sender: AnyObject) {
  215. let navController:UINavigationController = self.storyboard?.instantiateViewController(withIdentifier: "InstagramLoginID") as! UINavigationController
  216. let instagramLoginVC:InstagramLoginVC = navController.topViewController as! InstagramLoginVC
  217. instagramLoginVC.signInDelegate = self
  218. self.present(navController, animated: true, completion: nil)
  219. }
  220. //MARK: - Hide Loader
  221. func hideLoaderConnectingTwitter() {
  222. ApplicationDelegate.hideLoader()
  223. }
  224. override func navigateToSignUpScreen() {
  225. printCustom("signin vc navigateToSignUpScreen")
  226. let signUpScreen = self.storyboard?.instantiateViewController(withIdentifier: "SignUpVc_Main") as? SignUpVc_Main
  227. signUpScreen?.socialDetailsFromLogin.accountType = self.socialDetailsFromLogin.accountType
  228. signUpScreen?.socialDetailsFromLogin.id = self.socialDetailsFromLogin.id
  229. signUpScreen?.socialDetailsFromLogin.name = self.socialDetailsFromLogin.name
  230. signUpScreen?.socialDetailsFromLogin.email = self.socialDetailsFromLogin.email
  231. self.navigationController?.pushViewController(signUpScreen!, animated: true)
  232. }
  233. // //MARK: - Call API
  234. //
  235. // func callAPI_CheckSocialUserExists(accountType: AccountType, socialMediaID: String) {
  236. // var parameters = [String: AnyObject]()
  237. //
  238. // parameters["social_account_type"] = accountType.rawValue
  239. // if accountType.rawValue != 0 {
  240. // parameters["social_unique_id"] = socialMediaID
  241. // }
  242. //
  243. // printCustom("check social user exists parameters:\(parameters)")
  244. //
  245. // ApplicationDelegate.showLoader(loaderTitle_SigningIn)
  246. // APIManager.sharedInstance.requestCheckSocialUserExists(parameters, Target: self)
  247. // }
  248. //
  249. // //MARK: - API Response
  250. //
  251. // func responseCheckSocialUserExists (notify: NSNotification) {
  252. // NSNotificationCenter.defaultCenter().removeObserver(self, name: NOTIFICATION_CHECKSOCIALUSEREXISTS, object: nil)
  253. //
  254. // ApplicationDelegate.hideLoader ()
  255. //
  256. // let swiftyJsonVar = JSON(notify.object!)
  257. // let message = swiftyJsonVar["message"].stringValue
  258. // let status = swiftyJsonVar["status"].intValue
  259. //
  260. // if swiftyJsonVar [ERROR_KEY].exists() {
  261. // self.showCommonAlert(swiftyJsonVar[ERROR_KEY].stringValue)
  262. // }
  263. // else if (status == 1) {
  264. // let response = swiftyJsonVar["response"]
  265. // let responseUserDetail:[String:AnyObject] = response.dictionaryObject!
  266. // let userDetail:UserDetail = UserDetail()
  267. // Parser.parseUserDetails(responseUserDetail, userDetail: userDetail)
  268. //
  269. // self.navigationController?.navigationBarHidden = true
  270. // let homeScreen = self.storyboard?.instantiateViewControllerWithIdentifier("tabBarcontroller") as? UITabBarController
  271. // self.navigationController?.pushViewController(homeScreen!, animated: true)
  272. // }
  273. // else if (status == 0) {//If user not registered as social user, then redirect sign up flow with social info prefilled.
  274. //
  275. // let signUpScreen = self.storyboard?.instantiateViewControllerWithIdentifier("SignUpVc_Main") as? SignUpVc_Main
  276. // signUpScreen?.socialDetailsFromLogin.accountType = self.socialDetailsFromLogin.accountType
  277. // signUpScreen?.socialDetailsFromLogin.id = self.socialDetailsFromLogin.id
  278. // signUpScreen?.socialDetailsFromLogin.name = self.socialDetailsFromLogin.name
  279. // signUpScreen?.socialDetailsFromLogin.email = self.socialDetailsFromLogin.email
  280. // self.navigationController?.pushViewController(signUpScreen!, animated: true)
  281. // }
  282. // else {
  283. // self.showCommonAlert(message)
  284. // }
  285. // }
  286. }