daily pastebin goal
4%
SHARE
TWEET

Untitled

a guest Oct 21st, 2018 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  ViewController.swift
  3. //  TinkoffChat
  4. //
  5. //  Created by Никита Бабенко on 23.09.2018.
  6. //  Copyright © 2018 Tinkoff Fintech. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class GCDDataManager{
  12.     var data: String
  13.     var queue: DispatchQueue
  14.     init(_ queueForFiles: DispatchQueue, _ dataForFile: String) {
  15.         queue = queueForFiles
  16.         data = dataForFile
  17.     }
  18.    
  19.     let file = "data.json"
  20.    
  21.     func writeData() -> Bool{
  22.         var success: Bool
  23.         success = false
  24.         queue.sync(flags: .barrier) {
  25.             if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
  26.                 let fileUrl = dir.appendingPathComponent(file)
  27.                 do {
  28.                     try self.data.write(to: fileUrl, atomically: false, encoding: .utf8)
  29.                     success = true
  30.                    
  31.                 } catch {
  32.                     print("Couldn't read the file")
  33.                 }
  34.             }
  35.         }
  36.         return success
  37.     }
  38.    
  39.     func readData() -> String? {
  40.         var success: Bool = false
  41.         var data: String = ""
  42.         queue.sync(flags: .barrier) {
  43.             if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
  44.                 let fileUrl = dir.appendingPathComponent(file)
  45.                     do {
  46.                         data = try String(contentsOf: fileUrl, encoding: .utf8)
  47.                         success = true
  48.                    
  49.                     } catch {
  50.                         print("Couldn't read the file")
  51.                     }
  52.             }
  53.         }
  54.         if success {
  55.             return data
  56.         } else { return nil }
  57.     }
  58. }
  59.  
  60. class OperationDataManager {
  61.     var data: String
  62.     var queue: OperationQueue
  63.     weak var operationSave: SaveOperation?
  64.    
  65.     init(_ dataForFile: String, _ queueForFiles: OperationQueue) {
  66.         data = dataForFile
  67.         queue = queueForFiles
  68.     }
  69.    
  70.     func writeData() -> Bool {
  71.         var success: Bool = true
  72.         let operation = SaveOperation(data)
  73.         operation.completionBlock = {
  74.             success = operation.success
  75.         }
  76.         queue.addOperation(operation)
  77.         queue.waitUntilAllOperationsAreFinished()
  78.         return success
  79.     }
  80.    
  81. }
  82.  
  83. class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
  84.     @IBOutlet weak var aboutUserLabel: UILabel!
  85.     @IBOutlet weak var avatarImageView: UIImageView!
  86.     @IBOutlet weak var changePhotoButton: UIButton!
  87.     @IBOutlet weak var editButton: UIButton!
  88.     @IBAction func editButton(_ sender: UIButton) {
  89.         self.nameLabel.isHidden = true
  90.         self.aboutUserLabel.isHidden = true
  91.         self.editButton.isHidden = true
  92.         self.saveAsGCDButton.isHidden = false
  93.         self.saveAsOperationButton.isHidden = false
  94.         self.saveAsGCDButton.isEnabled = false
  95.         self.saveAsOperationButton.isEnabled = false
  96.         self.aboutField.isHidden = false
  97.         self.aboutLabel.isHidden = false
  98.         self.usernameField.isHidden = false
  99.         self.usernameLabel.isHidden = false
  100.         self.changePhotoButton.isHidden = false
  101.     }
  102.    
  103.     @IBOutlet weak var nameLabel: UILabel!
  104.     @IBOutlet weak var dismissButton: UIButton!
  105.     @IBOutlet weak var saveAsOperationButton: UIButton!
  106.    
  107.     @IBAction func saveAsOperationButton(_ sender: UIButton?) {
  108.         self.activityIndicator.startAnimating()
  109.         let string = """
  110.         {\"username\": \("\"" + self.usernameField.text!)\", \"about\": \("\"" + self.aboutField.text!)\"}
  111.         """
  112.         let queue = OperationQueue()
  113.         let dataManager = OperationDataManager(string, queue)
  114.         let success = dataManager.writeData()
  115.         if success {
  116.             self.successAlert()
  117.         }
  118.         else {
  119.             self.errorAlert()
  120.         }
  121.         self.saveAsOperationButton.isEnabled = false
  122.         self.saveAsGCDButton.isEnabled = false
  123.         self.activityIndicator.stopAnimating()
  124.         //returnToProfile()
  125.     }
  126.    
  127.     @IBAction func saveAsGCDButton(_ sender: UIButton) {
  128.         self.activityIndicator.startAnimating()
  129.         let queue = DispatchQueue(label: "com.app.queue", qos: .userInitiated, attributes: .concurrent)
  130.         let string = """
  131.         {\"username\": \("\"" + self.usernameField.text!)\", \"about\": \("\"" + self.aboutField.text!)\"}
  132.         """
  133.         //let data = [["username": self.usernameField.text!], ["about": self.aboutField.text!]]
  134.         let dataManager = GCDDataManager(queue, string)
  135.         let success = dataManager.writeData()
  136.         if success {
  137.             self.successAlert()
  138.         }
  139.         else {
  140.             self.errorAlert()
  141.         }
  142.         self.saveAsOperationButton.isEnabled = false
  143.         self.saveAsGCDButton.isEnabled = false
  144.         self.activityIndicator.stopAnimating()
  145.         //returnToProfile()
  146.     }
  147.    
  148.     func errorAlert() {
  149.         let alert = UIAlertController(title: "Error", message: "Didn't save the data", preferredStyle: UIAlertControllerStyle.alert)
  150.        
  151.         alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
  152.         alert.addAction(UIAlertAction(title: "Try Again", style: .default, handler: {action in
  153.             self.saveAsOperationButton(nil)
  154.         }))
  155.         self.present(alert, animated: true, completion: nil)
  156.     }
  157.    
  158.     func successAlert() {
  159.         let alert = UIAlertController(title: "Data is saved", message: "", preferredStyle: UIAlertControllerStyle.alert)
  160.        
  161.         alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
  162.         self.present(alert, animated: true, completion: nil)
  163.     }
  164.    
  165.     func convertToDictionary(text: String) -> [String: Any]? {
  166.         if let data = text.data(using: .utf8) {
  167.             do {
  168.                 return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
  169.             } catch {
  170.                 print(error.localizedDescription)
  171.             }
  172.         }
  173.         return nil
  174.     }
  175.  
  176.    
  177.     @IBOutlet weak var usernameField: UITextField!
  178.     @IBAction func usernameFieldChanged(_ sender: UITextField) {
  179.         self.saveAsGCDButton.isEnabled = true
  180.         self.saveAsOperationButton.isEnabled = true
  181.     }
  182.     @IBOutlet weak var saveAsGCDButton: UIButton!
  183.     @IBOutlet weak var activityIndicator: UIActivityIndicatorView!
  184.     @IBOutlet weak var aboutField: UITextView!
  185.     @IBOutlet weak var aboutLabel: UILabel!
  186.     @IBOutlet weak var usernameLabel: UILabel!
  187.    
  188.     var imagePicker = UIImagePickerController()
  189.    
  190.     @IBAction func changePhotoButton(_ sender: Any) {
  191.         self.showActionSheet()
  192.     }
  193.    
  194.     @IBAction func dismissButton(_ sender: UIButton) {
  195.         dismiss(animated: true, completion: nil)
  196.     }
  197.    
  198.     func returnToProfile() {
  199.         self.activityIndicator.stopAnimating()
  200.         self.nameLabel.isHidden = false
  201.         self.aboutUserLabel.isHidden = false
  202.         self.editButton.isHidden = false
  203.         self.saveAsGCDButton.isHidden = true
  204.         self.saveAsOperationButton.isHidden = true
  205.         self.aboutField.isHidden = true
  206.         self.aboutLabel.isHidden = true
  207.         self.usernameField.isHidden = true
  208.         self.usernameLabel.isHidden = true
  209.         self.changePhotoButton.isHidden = true
  210.         self.saveAsGCDButton.isEnabled = true
  211.         self.saveAsOperationButton.isEnabled = true
  212.     }
  213.    
  214.     override func viewDidLoad() {
  215.         super.viewDidLoad()
  216.         aboutField.delegate = self
  217.        
  218.         let crossImage: UIImage = UIImage(named: "cross")!
  219.         self.dismissButton.setImage(crossImage , for: .normal)
  220.        
  221.         let changePhotoImage: UIImage = UIImage(named: "slr-camera-2-xxl")!
  222.         self.changePhotoButton.contentMode = .scaleAspectFit
  223.         self.changePhotoButton.imageEdgeInsets = UIEdgeInsetsMake(15, 15, 15, 15)
  224.         self.changePhotoButton.setImage(changePhotoImage , for: .normal)
  225.         self.changePhotoButton.clipsToBounds = true
  226.         self.changePhotoButton.isHidden = true
  227.        
  228.         let defaultAvatarImage: UIImage = UIImage(named: "placeholder-user")!
  229.         self.avatarImageView.image = defaultAvatarImage
  230.         self.avatarImageView.layer.masksToBounds = true
  231.         self.avatarImageView.clipsToBounds = true
  232.         self.avatarImageView.layer.cornerRadius = changePhotoButton.bounds.size.width / 2.0
  233.        
  234.         self.changePhotoButton.layer.masksToBounds = true
  235.         self.changePhotoButton.layer.cornerRadius = changePhotoButton.bounds.size.width / 2.0
  236.        
  237.         self.editButton.layer.cornerRadius = 10
  238.         self.editButton.layer.borderWidth = 1
  239.        
  240.         self.nameLabel.font = UIFont.boldSystemFont(ofSize: 30.0)
  241.        
  242.         self.aboutLabel.isHidden = true
  243.        
  244.         self.usernameField.isHidden = true
  245.        
  246.         self.usernameLabel.isHidden = true
  247.        
  248.         self.activityIndicator.hidesWhenStopped = true
  249.        
  250.         self.saveAsOperationButton.isHidden = true
  251.         self.saveAsOperationButton.layer.cornerRadius = 10
  252.         self.saveAsOperationButton.layer.borderWidth = 1
  253.        
  254.         self.saveAsGCDButton.isHidden = true
  255.         self.saveAsGCDButton.layer.cornerRadius = 10
  256.         self.saveAsGCDButton.layer.borderWidth = 1
  257.        
  258.         self.aboutField.isHidden = true
  259.         self.aboutField.layer.borderWidth = 1
  260.        
  261.         let queue = DispatchQueue(label: "com.app.queue", attributes: .concurrent)
  262.         let dataManager = GCDDataManager(queue, "")
  263.         let string: String? = dataManager.readData()
  264.         let data = string?.data(using: .utf8)!
  265.         do {
  266.             let dictionary = try JSONSerialization.jsonObject(with: data!, options : .allowFragments) as? [String : String]
  267.             self.nameLabel.text = dictionary?["username"]
  268.             self.aboutUserLabel.text = dictionary?["about"]
  269.         } catch let error as NSError {
  270.             self.nameLabel.text = "No name yet"
  271.             self.aboutUserLabel.text = "No info yet"
  272.             print(error)
  273.         }
  274.         self.aboutField.text = self.aboutUserLabel.text
  275.         self.usernameField.text = self.nameLabel.text
  276.     }
  277.    
  278.     func chooseFromGallery()
  279.     {
  280.         if UIImagePickerController.isSourceTypeAvailable(.savedPhotosAlbum){
  281.             //print("Button capture")
  282.             imagePicker.sourceType = .photoLibrary
  283.             self.present(imagePicker, animated: true, completion: nil)
  284.         }
  285.     }
  286.  
  287.     func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
  288.         let image = info[UIImagePickerControllerOriginalImage] as! UIImage
  289.         self.avatarImageView.image = image
  290.         self.dismiss(animated: true, completion: nil)
  291.     }
  292.    
  293.     func showActionSheet() {
  294.         imagePicker.delegate = self
  295.         imagePicker.allowsEditing = false
  296.         print("Choose your profile pic")
  297.        
  298.         let actionSheet = UIAlertController(title: nil, message: "Choose your profile picture", preferredStyle: .actionSheet)
  299.        
  300.         let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
  301.        
  302.         let choosePhoto = UIAlertAction(title: "Choose from galerry", style: .default) { action in
  303.             self.imagePicker.sourceType = .photoLibrary
  304.             self.present(self.imagePicker, animated: true, completion: nil)
  305.             //self.chooseFromGallery()
  306.         }
  307.        
  308.         let makePhoto = UIAlertAction(title: "Make a photo", style: .default) { action in
  309.             self.imagePicker.sourceType = .camera
  310.             self.present(self.imagePicker, animated: true, completion: nil)
  311.         }
  312.        
  313.         actionSheet.addAction(choosePhoto)
  314.        
  315.         if UIImagePickerController.isSourceTypeAvailable(.camera) {
  316.             actionSheet.addAction(makePhoto)
  317.         }
  318.         else {
  319.             print("Camera is not available")
  320.         }
  321.        
  322.         actionSheet.addAction(cancel)
  323.        
  324.         present(actionSheet, animated: true, completion: nil)
  325.     }
  326.     /*
  327.     required init?(coder aDecoder: NSCoder) {
  328.         super.init(coder: aDecoder)
  329.         //во время вызова init() все объекты на view находятся в состоянии nil, именно поэтому ошибка
  330.         //при попытке получения frame
  331.     }*/
  332.    
  333.     override func didReceiveMemoryWarning() {
  334.         super.didReceiveMemoryWarning()
  335.         // Dispose of any resources that can be recreated.
  336.     }
  337.  
  338.     override func viewWillAppear(_ animated: Bool) {
  339.         //print("Method: \(#function)")
  340.     }
  341.    
  342.     override func viewWillDisappear(_ animated: Bool) {
  343.         //print("Method: \(#function)")
  344.     }
  345.    
  346.     override func viewDidAppear(_ animated: Bool) {
  347.         print("viewDidAppear()")
  348.         //Frame во время вызова метода viewDidAppear() отличается от frame'а во время вызова viewDidLoad() потому, что в качестве основного девайса выбран IPhone SE,
  349.         //а приложение запущено в симуляторе IPhone 8 Plus или IPhone X, у которых отличное от SE разрешение. Именно поэтому при viewDidLoad() фрейм заранее
  350.         //определен под основное устройство, а уже при вызове viewDidAppear() фрейм изменён под девайс, запущенный в симуляторе.
  351.        
  352.         //print("Method: \(#function)")
  353.     }
  354.    
  355.     override func viewDidDisappear(_ animated: Bool) {
  356.         //print("Method: \(#function)")
  357.     }
  358.    
  359.     override func viewWillLayoutSubviews() {
  360.         //print("Method: \(#function)")
  361.     }
  362.    
  363.     override func viewDidLayoutSubviews() {
  364.         //print("Method: \(#function)")
  365.     }
  366.    
  367.    
  368. }
  369.  
  370. extension ViewController: UITextViewDelegate {
  371.     func textViewDidChange(_ textView: UITextView) {
  372.         self.saveAsGCDButton.isEnabled = true
  373.         self.saveAsOperationButton.isEnabled = true
  374.     }
  375. }
  376.  
  377. extension String {
  378.     func toJSON() -> Any? {
  379.         guard let data = self.data(using: .utf8, allowLossyConversion: false) else { return nil }
  380.         return try? JSONSerialization.jsonObject(with: data, options: .mutableContainers)
  381.     }
  382. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top