Advertisement
Guest User

EditViewController.swift

a guest
Jul 18th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 7.46 KB | None | 0 0
  1. //
  2. //  ViewController.swift
  3. //  Note
  4. //
  5. //  Created by Максим Бачурин on 20/06/2019.
  6. //  Copyright © 2019 Максим Бачурин. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import CocoaLumberjack
  11.  
  12. class EditViewController: UIViewController {
  13.    
  14.     @IBOutlet weak var miniColorPickerButton: UIButton!
  15.     @IBOutlet weak var mainView: UIView!
  16.     @IBOutlet var colorButtons: [UIButton]!
  17.     @IBOutlet weak var colorPickerView: ColorPickerView!
  18.     @IBOutlet weak var scrollView: UIScrollView!
  19.     @IBOutlet weak var stackColors: UIStackView!
  20.     @IBOutlet weak var datePickerHeightConstraint: NSLayoutConstraint!
  21.     @IBOutlet weak var destoyDatePicker: UIDatePicker!
  22.     private var checkMarkView: CheckMarkView?
  23.     var note: Note?
  24.     @IBOutlet weak var titleTextView: UITextField!
  25.     @IBOutlet weak var contentTextView: UITextView!
  26.    
  27.     override func viewDidLoad() {
  28.         super.viewDidLoad()
  29.         colorPickerView.delegate = self
  30.     }
  31.    
  32.     override func viewWillAppear(_ animated: Bool) {
  33.         super.viewWillAppear(animated)
  34.         initializeDatePicker()
  35.         initializeColorButtons()
  36.         initializeCheckMarkView()
  37.         addObservers()
  38.         initializeData()
  39.         let tapGesture = UITapGestureRecognizer(target: self, action: #selector(didTapView(_:)))
  40.         view.addGestureRecognizer(tapGesture)
  41.     }
  42.    
  43.     override func viewWillDisappear(_ animated: Bool) {
  44.         super.viewWillDisappear(animated)
  45.         removeObservers()
  46.         saveNote()
  47.     }
  48.    
  49.     func saveNote() {
  50.         for notesTableViewController in navigationController?.viewControllers ?? [] {
  51.             if let notesTableViewController = notesTableViewController as? NotesTableViewController {
  52.                 notesTableViewController.notebook.remove(with: note!.uid)
  53.                 notesTableViewController.notebook.add(Note(importance: note!.importance,
  54.                                                            uid: note!.uid,
  55.                                                            color: mainView.backgroundColor!,
  56.                                                            title: titleTextView.text ?? "",
  57.                                                            content: contentTextView.text ?? "",
  58.                                                            dateDestruction: note!.dateDestruction))
  59.                 notesTableViewController.tableView.reloadData()
  60.                 notesTableViewController.notebook.saveToFile()
  61.                 return
  62.             }
  63.         }
  64.     }
  65.    
  66.     func initializeDatePicker() {
  67.         datePickerHeightConstraint.constant = 0
  68.         destoyDatePicker.locale = Locale(identifier: "ru_RU")
  69.     }
  70.    
  71.     func initializeData() {
  72.         guard let note = note else {
  73.             print("Note is not found")
  74.             return
  75.         }
  76.         titleTextView.text = note.title
  77.         contentTextView.text = note.content
  78.     }
  79.    
  80.     func initializeCheckMarkView() {
  81.         if note == nil {
  82.             for button in colorButtons {
  83.                 if button.backgroundColor!.isEqual(UIColor.white) {
  84.                     installCheckMark(to: button)
  85.                     break
  86.                 }
  87.             }
  88.         } else {
  89.             for button in colorButtons {
  90.                 if button.backgroundColor == nil {
  91.                     mainView.backgroundColor = note!.color
  92.                     scrollView.backgroundColor = note!.color
  93.                     miniColorPickerButton.setImage(nil, for: .normal)
  94.                     installCheckMark(to: button)
  95.                 } else {
  96.                     if button.backgroundColor!.isEqual(note!.color) {
  97.                         mainView.backgroundColor = note!.color
  98.                         scrollView.backgroundColor = note!.color
  99.                         installCheckMark(to: button)
  100.                         return
  101.                     }
  102.                 }
  103.             }
  104.         }
  105.     }
  106.    
  107.     func initializeColorButtons() {
  108.         for button in colorButtons {
  109.             if button.backgroundColor == nil {
  110.                 let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self,
  111.                                                                               action: #selector(showColorPicker(_:)))
  112.                 button.addGestureRecognizer(longPressGestureRecognizer)
  113.             }
  114.             button.clipsToBounds = true
  115.             button.layer.cornerRadius = button.frame.size.width/8
  116.             button.layer.borderColor = UIColor.black.cgColor
  117.             button.layer.borderWidth = 1
  118.             button.addTarget(self, action: #selector(chooseColor(_:)), for: .touchUpInside)
  119.         }
  120.         colorButtons[0].backgroundColor = .white
  121.         colorButtons[1].backgroundColor = .green
  122.         colorButtons[2].backgroundColor = .red
  123.     }
  124.    
  125.     //MARK: - Actions
  126.     @IBAction func switchChanged(_ sender: UISwitch) {
  127.         datePickerHeightConstraint.constant = 216*(sender.isOn ? 1 : 0)
  128.         destoyDatePicker.minimumDate = Date()
  129.     }
  130.    
  131.     //MARK: - Selectors
  132.     @objc func chooseColor(_ sender: UIButton) {
  133.         if sender.backgroundColor == nil {
  134.             return
  135.         }
  136.         mainView.backgroundColor = sender.backgroundColor
  137.         scrollView.backgroundColor = sender.backgroundColor
  138.        
  139.         installCheckMark(to: sender)
  140.     }
  141.    
  142.     @objc func showColorPicker(_ sender: UILongPressGestureRecognizer) {
  143.         scrollView.isHidden = true
  144.     }
  145.    
  146.     @objc func didTapView(_ gesture: UITapGestureRecognizer) {
  147.         view.endEditing(true)
  148.     }
  149.    
  150.     func installCheckMark(to currentView: UIView) {
  151.         checkMarkView?.removeFromSuperview()
  152.         checkMarkView = CheckMarkView(frame: CGRect(x: 0,
  153.                                                     y: 0,
  154.                                                     width: currentView.bounds.width/3,
  155.                                                     height: currentView.bounds.height/3))
  156.         currentView.addSubview(checkMarkView!)
  157.     }
  158.    
  159.     //MARK: - Keyboard
  160.     @objc func keyboardWillShow(notification: Notification) {
  161.         guard let userInfo = notification.userInfo,
  162.             let frame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else {
  163.                 return
  164.         }
  165.         let contentInset = UIEdgeInsets(top: 0, left: 0, bottom: frame.height, right: 0)
  166.         scrollView.contentInset = contentInset
  167.     }
  168.    
  169.     @objc func keyboardWillHide(notification: Notification) {
  170.         scrollView.contentInset = UIEdgeInsets.zero
  171.        
  172.     }
  173.    
  174.     //MARK: - Observers
  175.     func addObservers() {
  176.         NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
  177.         NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
  178.     }
  179.    
  180.     func removeObservers() {
  181.         NotificationCenter.default.removeObserver(self)
  182.     }
  183.    
  184. }
  185.  
  186. extension EditViewController: ColorPickerDelegate {
  187.     func newColor(sender: ColorPickerView, color: UIColor) {
  188.         scrollView.isHidden = false;
  189.         mainView.backgroundColor = color
  190.         scrollView.backgroundColor = color
  191.         miniColorPickerButton.setImage(nil, for: .normal)
  192.         miniColorPickerButton.backgroundColor = color
  193.         installCheckMark(to: miniColorPickerButton)
  194.     }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement