You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
120 lines
3.9 KiB
120 lines
3.9 KiB
//
|
|
// NoteViewController.swift
|
|
// Notes
|
|
//
|
|
// Created by Laurent Morvillier on 04/09/2022.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
|
|
class NoteViewController : UIViewController, UITextViewDelegate {
|
|
|
|
var filename: String = "main"
|
|
|
|
@IBOutlet weak var textView: UITextView!
|
|
|
|
@IBOutlet weak var textViewBottomConstraint: NSLayoutConstraint!
|
|
|
|
fileprivate weak var _lastEditLabel: UILabel?
|
|
|
|
// MARK: -
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
let shareButton = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(shareHandler))
|
|
let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addHandler))
|
|
self.navigationItem.rightBarButtonItems = [addButton, shareButton]
|
|
|
|
self.textView.text = Preferences.getContent(filename: self.filename)
|
|
self.textView.font = UIFont.systemFont(ofSize: 18.0, weight: .medium)
|
|
self.textView.delegate = self
|
|
self.textView.inputAccessoryView = self._inputAccessoryView()
|
|
|
|
self._updateLastEdit()
|
|
|
|
NotificationCenter.default.addObserver(
|
|
self,
|
|
selector: #selector(keyboardDidShow(notification:)),
|
|
name: UIResponder.keyboardDidShowNotification,
|
|
object: nil
|
|
)
|
|
|
|
NotificationCenter.default.addObserver(
|
|
self,
|
|
selector: #selector(keyboardWillHide),
|
|
name: UIResponder.keyboardWillHideNotification,
|
|
object: nil
|
|
)
|
|
|
|
}
|
|
|
|
func textViewDidChange(_ textView: UITextView) {
|
|
Preferences.store(filename: self.filename, content: textView.text)
|
|
self._updateLastEdit()
|
|
}
|
|
|
|
fileprivate func _updateLastEdit() {
|
|
if let date = Preferences.lastEditDate(filename: self.filename) {
|
|
let formattedDate: String = date.formatted()
|
|
self._lastEditLabel?.text = "last edit: \(formattedDate)"
|
|
}
|
|
}
|
|
|
|
fileprivate func _inputAccessoryView() -> UIView {
|
|
|
|
let toolbar: UIToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 333, height: 44.0))
|
|
|
|
let lastEditLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 280, height: 44))
|
|
lastEditLabel.textColor = UIColor.gray
|
|
let lastEditButtonItem = UIBarButtonItem(customView: lastEditLabel)
|
|
self._lastEditLabel = lastEditLabel
|
|
|
|
let flexButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
|
|
let dismissButton = UIBarButtonItem(image: UIImage(systemName: "checkmark"), style: .done, target: self, action: #selector(dismissKeyboard))
|
|
toolbar.items = [lastEditButtonItem, flexButton, dismissButton]
|
|
|
|
let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(dismissKeyboard))
|
|
swipeDown.direction = .down
|
|
toolbar.addGestureRecognizer(swipeDown)
|
|
|
|
return toolbar
|
|
}
|
|
|
|
// MARK: - Keyboard
|
|
|
|
@objc func dismissKeyboard() {
|
|
self.textView.resignFirstResponder()
|
|
}
|
|
|
|
@objc func keyboardDidShow(notification: NSNotification) {
|
|
guard let keyboardRect = notification
|
|
.userInfo?[UIResponder.keyboardFrameEndUserInfoKey]
|
|
as? NSValue else { return }
|
|
let frameKeyboard = keyboardRect.cgRectValue
|
|
|
|
self.textViewBottomConstraint.constant = frameKeyboard.size.height
|
|
}
|
|
|
|
@objc func keyboardWillHide() {
|
|
self.textViewBottomConstraint.constant = 0.0
|
|
}
|
|
|
|
// MARK: - Business
|
|
|
|
@objc func shareHandler() {
|
|
|
|
guard let text = self.textView.text else {
|
|
return
|
|
}
|
|
|
|
let activityViewController = UIActivityViewController(activityItems: [text], applicationActivities: nil)
|
|
self.present(activityViewController, animated: true)
|
|
}
|
|
|
|
@objc func addHandler() {
|
|
|
|
}
|
|
|
|
}
|
|
|