Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.swift
- // Note
- //
- // Created by Максим Бачурин on 20/06/2019.
- // Copyright © 2019 Максим Бачурин. All rights reserved.
- //
- import UIKit
- import CocoaLumberjack
- class EditViewController: UIViewController {
- @IBOutlet weak var miniColorPickerButton: UIButton!
- @IBOutlet weak var mainView: UIView!
- @IBOutlet var colorButtons: [UIButton]!
- @IBOutlet weak var colorPickerView: ColorPickerView!
- @IBOutlet weak var scrollView: UIScrollView!
- @IBOutlet weak var stackColors: UIStackView!
- @IBOutlet weak var datePickerHeightConstraint: NSLayoutConstraint!
- @IBOutlet weak var destoyDatePicker: UIDatePicker!
- private var checkMarkView: CheckMarkView?
- var note: Note?
- @IBOutlet weak var titleTextView: UITextField!
- @IBOutlet weak var contentTextView: UITextView!
- override func viewDidLoad() {
- super.viewDidLoad()
- colorPickerView.delegate = self
- }
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
- initializeDatePicker()
- initializeColorButtons()
- initializeCheckMarkView()
- addObservers()
- initializeData()
- let tapGesture = UITapGestureRecognizer(target: self, action: #selector(didTapView(_:)))
- view.addGestureRecognizer(tapGesture)
- }
- override func viewWillDisappear(_ animated: Bool) {
- super.viewWillDisappear(animated)
- removeObservers()
- saveNote()
- }
- func saveNote() {
- for notesTableViewController in navigationController?.viewControllers ?? [] {
- if let notesTableViewController = notesTableViewController as? NotesTableViewController {
- notesTableViewController.notebook.remove(with: note!.uid)
- notesTableViewController.notebook.add(Note(importance: note!.importance,
- uid: note!.uid,
- color: mainView.backgroundColor!,
- title: titleTextView.text ?? "",
- content: contentTextView.text ?? "",
- dateDestruction: note!.dateDestruction))
- notesTableViewController.tableView.reloadData()
- notesTableViewController.notebook.saveToFile()
- return
- }
- }
- }
- func initializeDatePicker() {
- datePickerHeightConstraint.constant = 0
- destoyDatePicker.locale = Locale(identifier: "ru_RU")
- }
- func initializeData() {
- guard let note = note else {
- print("Note is not found")
- return
- }
- titleTextView.text = note.title
- contentTextView.text = note.content
- }
- func initializeCheckMarkView() {
- if note == nil {
- for button in colorButtons {
- if button.backgroundColor!.isEqual(UIColor.white) {
- installCheckMark(to: button)
- break
- }
- }
- } else {
- for button in colorButtons {
- if button.backgroundColor == nil {
- mainView.backgroundColor = note!.color
- scrollView.backgroundColor = note!.color
- miniColorPickerButton.setImage(nil, for: .normal)
- installCheckMark(to: button)
- } else {
- if button.backgroundColor!.isEqual(note!.color) {
- mainView.backgroundColor = note!.color
- scrollView.backgroundColor = note!.color
- installCheckMark(to: button)
- return
- }
- }
- }
- }
- }
- func initializeColorButtons() {
- for button in colorButtons {
- if button.backgroundColor == nil {
- let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self,
- action: #selector(showColorPicker(_:)))
- button.addGestureRecognizer(longPressGestureRecognizer)
- }
- button.clipsToBounds = true
- button.layer.cornerRadius = button.frame.size.width/8
- button.layer.borderColor = UIColor.black.cgColor
- button.layer.borderWidth = 1
- button.addTarget(self, action: #selector(chooseColor(_:)), for: .touchUpInside)
- }
- colorButtons[0].backgroundColor = .white
- colorButtons[1].backgroundColor = .green
- colorButtons[2].backgroundColor = .red
- }
- //MARK: - Actions
- @IBAction func switchChanged(_ sender: UISwitch) {
- datePickerHeightConstraint.constant = 216*(sender.isOn ? 1 : 0)
- destoyDatePicker.minimumDate = Date()
- }
- //MARK: - Selectors
- @objc func chooseColor(_ sender: UIButton) {
- if sender.backgroundColor == nil {
- return
- }
- mainView.backgroundColor = sender.backgroundColor
- scrollView.backgroundColor = sender.backgroundColor
- installCheckMark(to: sender)
- }
- @objc func showColorPicker(_ sender: UILongPressGestureRecognizer) {
- scrollView.isHidden = true
- }
- @objc func didTapView(_ gesture: UITapGestureRecognizer) {
- view.endEditing(true)
- }
- func installCheckMark(to currentView: UIView) {
- checkMarkView?.removeFromSuperview()
- checkMarkView = CheckMarkView(frame: CGRect(x: 0,
- y: 0,
- width: currentView.bounds.width/3,
- height: currentView.bounds.height/3))
- currentView.addSubview(checkMarkView!)
- }
- //MARK: - Keyboard
- @objc func keyboardWillShow(notification: Notification) {
- guard let userInfo = notification.userInfo,
- let frame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else {
- return
- }
- let contentInset = UIEdgeInsets(top: 0, left: 0, bottom: frame.height, right: 0)
- scrollView.contentInset = contentInset
- }
- @objc func keyboardWillHide(notification: Notification) {
- scrollView.contentInset = UIEdgeInsets.zero
- }
- //MARK: - Observers
- func addObservers() {
- NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
- }
- func removeObservers() {
- NotificationCenter.default.removeObserver(self)
- }
- }
- extension EditViewController: ColorPickerDelegate {
- func newColor(sender: ColorPickerView, color: UIColor) {
- scrollView.isHidden = false;
- mainView.backgroundColor = color
- scrollView.backgroundColor = color
- miniColorPickerButton.setImage(nil, for: .normal)
- miniColorPickerButton.backgroundColor = color
- installCheckMark(to: miniColorPickerButton)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement