Guest User

Untitled

a guest
Aug 22nd, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.23 KB | None | 0 0
  1. import UIKit
  2. import Parse
  3.  
  4. class UserProfile: UIViewController,UITableViewDelegate,UITableViewDataSource, UIImagePickerControllerDelegate,UINavigationControllerDelegate {
  5.  
  6.  
  7. var myUser = [PFUser.current()]
  8.  
  9. @objc func updatePhoto() {
  10. let myPickerImageController = UIImagePickerController()
  11. myPickerImageController.delegate = self
  12. myPickerImageController.sourceType = UIImagePickerControllerSourceType.photoLibrary
  13.  
  14. self.present(myPickerImageController, animated: true, completion: nil)
  15. }
  16.  
  17. func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
  18.  
  19. let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "ProfileUserHeaderView") as! ProfileUserHeaderView
  20. header.userFot.image = info [UIImagePickerControllerOriginalImage] as? UIImage
  21.  
  22. self.dismiss(animated: true, completion: nil)
  23.  
  24. }
  25.  
  26.  
  27. var activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.white)
  28. @IBOutlet weak var tableView: UITableView!
  29.  
  30.  
  31. lazy var refreshControl: UIRefreshControl = {
  32. let refreshControl = UIRefreshControl()
  33. refreshControl.attributedTitle = NSAttributedString(string: "Обновление")
  34.  
  35. refreshControl.addTarget(self, action:
  36. #selector(UserProfile.handleRefresh(_:)),
  37. for: UIControlEvents.valueChanged)
  38. refreshControl.tintColor = UIColor.universalColorYellow
  39.  
  40. return refreshControl
  41. }()
  42.  
  43.  
  44. override func viewDidLoad() {
  45. super.viewDidLoad()
  46.  
  47. activityIndicator.hidesWhenStopped = true;
  48.  
  49. activityIndicator.center = view.center;
  50. super.viewDidLoad()
  51.  
  52.  
  53. let nib: UINib = UINib(nibName: "ProfileUserHeaderView", bundle: nil)
  54. tableView.register(nib, forHeaderFooterViewReuseIdentifier: "ProfileUserHeaderView")
  55. self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Сохранить", style: .plain, target: self, action: #selector(saveBarItem))
  56. self.navigationController?.navigationBar.tintColor = UIColor.universalColorYellow
  57. self.navigationController?.title = "Назад"
  58.  
  59. self.navigationController!.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
  60. self.navigationController?.navigationBar.shadowImage = UIImage()
  61. self.navigationController?.navigationBar.isTranslucent = true
  62. self.navigationController!.view.backgroundColor = UIColor.clear
  63. self.navigationController?.navigationBar.backgroundColor = UIColor.clear
  64.  
  65. tableView.addSubview(self.refreshControl)
  66. tableView.tableFooterView = UIView(frame: .zero)
  67. tableView.estimatedRowHeight = 100
  68. tableView.rowHeight = UITableViewAutomaticDimension
  69. tableView.separatorStyle = .none
  70. tableView.delegate = self
  71. tableView.dataSource = self
  72.  
  73. tableView.reloadData()
  74. loadEditUserInfo()
  75.  
  76. // loadObjectUser()
  77. // Do any additional setup after loading the view.
  78.  
  79. }
  80.  
  81. /*
  82. func loadObjectUser() {
  83.  
  84. PFUser.current()!.fetchInBackground(block: { (currentUser, error) -> Void in
  85. if let user = currentUser as? PFUser {
  86. user.fetchFromLocalDatastoreInBackground()
  87.  
  88. }
  89. })
  90. } */
  91.  
  92.  
  93.  
  94. func segueUser() {
  95. self.performSegue(withIdentifier: "userprofile", sender: self)
  96.  
  97. }
  98.  
  99.  
  100.  
  101.  
  102. @objc func saveBarItem() {
  103. activityIndicator.isHidden = false
  104. activityIndicator.startAnimating()
  105. let user = PFUser.current()!
  106. let indexOne = IndexPath(row: 0, section: 0)
  107. let indexTwo = IndexPath(row: 1, section: 0)
  108. let indexThree = IndexPath(row: 2, section: 0)
  109. let indexFour = IndexPath(row: 3, section: 0)
  110. let cellOne : UserEditTableViewCell = tableView.cellForRow(at: indexOne) as! UserEditTableViewCell
  111. let cellTwo : UserEditTableViewCellTwo = tableView.cellForRow(at: indexTwo) as! UserEditTableViewCellTwo
  112. let cellThree : UserEditTableViewCellThree = tableView.cellForRow(at: indexThree) as! UserEditTableViewCellThree
  113. let cellFour : UserEditTableViewCellFour = tableView.cellForRow(at: indexFour) as! UserEditTableViewCellFour
  114. let username = cellOne.userTextField.text!
  115. let country = cellTwo.countryTextfield.text!
  116. let email = cellThree.emailTextField.text!
  117. let password = cellFour.passwordTextField.text!
  118. user["username"] = username
  119. user["country"] = country
  120. user["email"] = email
  121. user["password"] = password
  122.  
  123. if PFUser.current()?.password == "" {
  124. self.alert(message: "Введите пароль", title: "BRAVOBET")
  125.  
  126. } else {
  127.  
  128. user.saveInBackground(block: { (success, error) -> Void in
  129. if success {
  130. Alert.show(textTitle: "BRAVOBET", text: "Вы успешно изменили данные", view: self)
  131. self.activityIndicator.isHidden = true
  132. self.activityIndicator.stopAnimating()
  133. self.tableView.reloadData()
  134. self.dismiss(animated: true , completion: nil)
  135. } else {
  136. print("Error")
  137.  
  138. }
  139. })
  140. }
  141. }
  142.  
  143. func savePhoto() {
  144. let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "ProfileUserHeaderView") as! ProfileUserHeaderView
  145.  
  146. let userToUpdate = PFUser.current()!
  147. let imageData = UIImagePNGRepresentation(header.userFot.image!)
  148. let imageFile = PFFile(name:"noFoto", data:imageData!)
  149. userToUpdate["profile_picture"] = imageFile
  150. userToUpdate.saveInBackground()
  151.  
  152.  
  153. }
  154.  
  155.  
  156.  
  157. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  158.  
  159. let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "ProfileUserHeaderView") as! ProfileUserHeaderView
  160.  
  161. header.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 250)
  162. header.username.text = PFUser.current()!.username
  163.  
  164. header.buttonSave.addTarget(self, action: #selector(updatePhoto), for: .touchUpInside)
  165. // header.bravoBtn.addTarget(self, action: #selector(ProfileUserHeaderView.likeBtn(_:)), for: UIControlEvents.touchUpInside)
  166. if header.userFot.image != nil {
  167. header.userFot.image = UIImage(named: "noFoto")
  168. } else {
  169.  
  170. let imagePro: PFFile = PFUser.current()?.object(forKey: "profile_picture") as! PFFile;
  171. imagePro.getDataInBackground { (data, error) in
  172. header.userFot.image = error == nil ? UIImage(data: data!) : nil
  173. }
  174. }
  175.  
  176. return header
  177. }
  178.  
  179.  
  180.  
  181.  
  182. @IBAction func userSegue(_ sender: UIBarButtonItem) {
  183.  
  184. self.performSegue(withIdentifier: "pushToUser", sender: self)
  185.  
  186. }
  187.  
  188.  
  189. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  190.  
  191. return 250
  192. }
  193.  
  194. func numberOfSections(in tableView: UITableView) -> Int {
  195. return 1
  196. }
  197.  
  198. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  199. return 4
  200. }
  201.  
  202.  
  203. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  204. if indexPath.row == 0 {
  205. let cell = tableView.dequeueReusableCell(withIdentifier: "cellOne") as! UserEditTableViewCell
  206. cell.userTextField.text = PFUser.current()!.username
  207.  
  208. return cell
  209. } else if indexPath.row == 1 {
  210.  
  211. let cell = tableView.dequeueReusableCell(withIdentifier: "cellTwo") as! UserEditTableViewCellTwo
  212. cell.countryTextfield.text = PFUser.current()!["country"] as? String
  213.  
  214. return cell
  215. } else if indexPath.row == 2 {
  216.  
  217. let cell = tableView.dequeueReusableCell(withIdentifier: "cellThree") as! UserEditTableViewCellThree
  218. cell.emailTextField.text = PFUser.current()!.email
  219.  
  220. return cell
  221. } else {
  222. let cell = tableView.dequeueReusableCell(withIdentifier: "cellFour") as! UserEditTableViewCellFour
  223. cell.passwordTextField.text = PFUser.current()!.password
  224.  
  225. return cell
  226. }
  227.  
  228.  
  229. }
  230.  
  231. @objc func handleRefresh(_ refreshControl: UIRefreshControl) {
  232.  
  233. self.tableView.reloadData()
  234. refreshControl.endRefreshing()
  235. }
  236.  
  237.  
  238. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  239. return UITableViewAutomaticDimension
  240. }
  241.  
  242. func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
  243. return UITableViewAutomaticDimension
  244. }
  245.  
  246.  
  247. func alert(message: NSString, title: NSString) {
  248. let alert = UIAlertController(title: title as String, message: message as String, preferredStyle: UIAlertControllerStyle.alert)
  249. alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
  250. self.present(alert, animated: true, completion: nil)
  251.  
  252. }
  253.  
  254. func loadEditUserInfo() {
  255.  
  256. let userInformation = PFQuery(className: "User")
  257. userInformation.findObjectsInBackground(block: { (objectUser, error) in
  258.  
  259. self.myUser = objectUser! as! [PFUser]
  260. DispatchQueue.main.async(execute: {
  261. self.tableView.reloadData()
  262.  
  263.  
  264. })
  265.  
  266.  
  267. })
  268.  
  269. }
  270.  
  271.  
  272. }
Add Comment
Please, Sign In to add comment