diff --git a/Notes/NotesPageViewController.swift b/Notes/NotesPageViewController.swift index 1228aaa..86ae010 100644 --- a/Notes/NotesPageViewController.swift +++ b/Notes/NotesPageViewController.swift @@ -12,6 +12,8 @@ class NotesPageViewController : UIPageViewController, UIPageViewControllerDelega fileprivate var _notes: [Note] = [] + fileprivate var _currentIndex: Int = 0 + override func viewDidLoad() { super.viewDidLoad() @@ -19,22 +21,29 @@ class NotesPageViewController : UIPageViewController, UIPageViewControllerDelega 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] + let deleteButton = UIBarButtonItem(barButtonSystemItem: .trash, target: self, action: #selector(deleteHandler)) + self.navigationItem.rightBarButtonItems = [addButton, shareButton, deleteButton] self.dataSource = self self.delegate = self + self._displayLastNote() + + } + + fileprivate func _displayLastNote(animated: Bool = false) { if let note = self._notes.last { - let vc = self.viewController(note: note, index: self._notes.count - 1) - self.setViewControllers([vc], direction: .forward, animated: false) + let index = self._notes.count - 1 + let vc = self.viewController(note: note, index: index) + self.setViewControllers([vc], direction: .forward, animated: animated) + self._currentIndex = index } - } - fileprivate func _displayNote(note: Note) { + fileprivate func _displayNote(note: Note, animated: Bool = false) { if let index = self._notes.firstIndex(of: note) { let vc = self.viewController(note: note, index: index) - self.setViewControllers([vc], direction: .forward, animated: false) + self.setViewControllers([vc], direction: .forward, animated: animated) } else { print("note not found") } @@ -96,11 +105,19 @@ class NotesPageViewController : UIPageViewController, UIPageViewControllerDelega return self.viewController(note: note, index: nextIndex) } + func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) { + + if completed, let vc = pageViewController.viewControllers?.first as? NoteViewController { + self._currentIndex = vc.index + } + + } + // MARK: - Business @objc func shareHandler() { - guard let vc = self.presentedViewController as? NoteViewController, let text = vc.note?.content else { + guard let text = self._notes[self._currentIndex].content else { return } @@ -112,6 +129,23 @@ class NotesPageViewController : UIPageViewController, UIPageViewControllerDelega self._createNote() } + @objc func deleteHandler() { + + let note = self._notes[self._currentIndex] + guard let content = note.content else { + self.showAlert(message: "Sorry!", title: "Note not found") + return + } + + areYouSure(message: "Do you want to delete this: \(content.prefix(20))...?") { + AppDelegate.viewContext.delete(note) + AppDelegate.shared.saveContext() + self._loadNotes() + self._displayLastNote(animated: true) + } + + } + fileprivate func _createNote() { let note = Note(context: AppDelegate.viewContext) self._loadNotes() diff --git a/Notes/UIViewController+Extensions.swift b/Notes/UIViewController+Extensions.swift index 653de91..550f51c 100644 --- a/Notes/UIViewController+Extensions.swift +++ b/Notes/UIViewController+Extensions.swift @@ -16,5 +16,20 @@ extension UIViewController { alertController.addAction(action) self.present(alertController, animated: true, completion: completion) } + + func areYouSure(title: String? = nil, + message: String? = NSLocalizedString("Please confirm the action", comment: ""), + executable: @escaping () -> Void) { + + let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) + alert.addAction(UIAlertAction(title: NSLocalizedString("No", comment:""), style: .cancel, handler: { _ in + // do nothing + })) + alert.addAction(UIAlertAction(title: NSLocalizedString("Yes", comment:""), style: .destructive, handler: { action in + executable() + })) + self.present(alert, animated: true, completion: nil) + + } }