application iOS de notes
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.
notes/Notes/NoteViewController.swift

171 lines
5.2 KiB

//
// NoteViewController.swift
// Notes
//
// Created by Laurent Morvillier on 04/09/2022.
//
import Foundation
import UIKit
import CoreData
class NoteViewController : UIViewController, UITextViewDelegate {
var note: Note? = nil
@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._loadLastNote()
self.textView.font = UIFont.systemFont(ofSize: 18.0, weight: .regular)
self.textView.delegate = self
self.textView.inputAccessoryView = self._inputAccessoryView()
self._updateLastEdit()
/// Store notifications
print("REGISTER")
NotificationCenter.default.addObserver(self, selector: #selector(self._storeRemoteChange(notification:)), name: NSPersistentCloudKitContainer.eventChangedNotification, object: nil)
/// Keyboard notifications
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
)
}
fileprivate func _loadLastNote() {
let request = Note.fetchRequest()
request.sortDescriptors = [NSSortDescriptor(key: "lastEditDate", ascending: false)]
do {
let notes = try AppDelegate.viewContext.fetch(request)
print("notes in store : \(notes.count)")
self.note = notes.first
self.textView.text = self.note?.content
} catch {
print("Fetch error = \(error)")
}
}
@objc fileprivate func _storeRemoteChange(notification: Notification) {
print("_storeRemoteChange...")
DispatchQueue.main.async {
self._loadLastNote()
}
}
func textViewDidChange(_ textView: UITextView) {
PreferencesStorage.main.requestStorage(filename: "main.txt", content: textView.text)
let note: Note
if let n = self.note {
note = n
} else {
note = Note(context: AppDelegate.viewContext)
self.note = note
}
CoreDataStorage.main.requestStorage(note: note, content: textView.text)
self._updateLastEdit()
}
fileprivate func _updateLastEdit() {
if let date = self.note?.lastEditDate {
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() {
}
deinit {
NotificationCenter.default.removeObserver(self)
}
}