diff --git a/Example/NotesExample-iOS/Base.lproj/Main.storyboard b/Example/NotesExample-iOS/Base.lproj/Main.storyboard index fa1a278..91e6987 100644 --- a/Example/NotesExample-iOS/Base.lproj/Main.storyboard +++ b/Example/NotesExample-iOS/Base.lproj/Main.storyboard @@ -1,11 +1,11 @@ - + - + @@ -465,15 +465,62 @@ - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + @@ -493,7 +540,7 @@ - + @@ -531,6 +578,8 @@ + + @@ -548,7 +597,7 @@ - + @@ -626,6 +675,6 @@ - + diff --git a/Example/NotesExample-iOS/NoteEditingViewController.swift b/Example/NotesExample-iOS/NoteEditingViewController.swift index b59a7b6..f1fc3e7 100644 --- a/Example/NotesExample-iOS/NoteEditingViewController.swift +++ b/Example/NotesExample-iOS/NoteEditingViewController.swift @@ -39,6 +39,10 @@ class NoteEditingViewController: UITableViewController { @IBOutlet weak var titleTextField: UITextField! @IBOutlet weak var authorPickerView: UIPickerView! @IBOutlet weak var contentTextView: UITextView! + @IBOutlet weak var creationDateLabel: UILabel! + @IBOutlet weak var modificationDateLabel: UILabel! + + private var noteModificationDateChangeSubscription: NotificationToken! lazy var authorModel: AuthorModel = AuthorModel(authorBox: Services.instance.authorBox) var noteBox: Box = Services.instance.noteBox @@ -65,6 +69,33 @@ class NoteEditingViewController: UITableViewController { if let contentTextView = contentTextView { contentTextView.text = note.text } + + self.refreshCreationDate() + self.refreshModificationDate() + + noteModificationDateChangeSubscription = NotificationCenter.default.observe(name: .noteModificationDateDidChange, object: nil) { _ in + self.refreshModificationDate() + } + } + + private func refreshCreationDate() { + guard let creationDateLabel = creationDateLabel, let note = note else { return } + + var dateString: String? + if let creationDate = note.creationDate { + dateString = DateFormatter.localizedString(from: creationDate, dateStyle: .short, timeStyle: .short) + } + creationDateLabel.text = dateString ?? "--" + } + + private func refreshModificationDate() { + guard let modificationDateLabel = modificationDateLabel, let note = note else { return } + + var dateString: String? + if let modificationDate = note.modificationDate { + dateString = DateFormatter.localizedString(from: modificationDate, dateStyle: .short, timeStyle: .short) + } + modificationDateLabel.text = dateString ?? "--" } private func refreshAuthors() { @@ -134,6 +165,10 @@ extension NoteEditingViewController: UITextFieldDelegate { name: .noteTitleDidChange, object: note, userInfo: [ "oldValue" : oldTitle, "newValue" : newTitle]) + NotificationCenter.default.post( + name: .noteModificationDateDidChange, + object: note, + userInfo: nil) } } @@ -181,6 +216,7 @@ extension NoteEditingViewController: UIPickerViewDelegate { let oldAuthorId = note.author.targetId note.author.target = newAuthor + note.modificationDate = Date() // Do not autosave drafts guard self.mode == .edit else { return } @@ -202,6 +238,10 @@ extension NoteEditingViewController: UIPickerViewDelegate { name: .noteAuthorDidChange, object: note, userInfo: changeUserInfo) + NotificationCenter.default.post( + name: .noteModificationDateDidChange, + object: note, + userInfo: nil) } } @@ -228,6 +268,10 @@ extension NoteEditingViewController: UITextViewDelegate { func textViewDidChange(_ textView: UITextView) { note?.text = textView.text + NotificationCenter.default.post( + name: .noteModificationDateDidChange, + object: note, + userInfo: nil) } func textViewDidEndEditing(_ textView: UITextView) { diff --git a/Example/NotesExample-iOS/NotesOverviewViewController.swift b/Example/NotesExample-iOS/NotesOverviewViewController.swift index a584e4a..2f4a6d4 100644 --- a/Example/NotesExample-iOS/NotesOverviewViewController.swift +++ b/Example/NotesExample-iOS/NotesOverviewViewController.swift @@ -114,6 +114,7 @@ extension NotesOverviewViewController { controller.note = { let draft = Note() draft.author.targetId = filter.authorId + draft.modificationDate = Date() return draft }() } diff --git a/Example/NotesExample-iOS/Notification+NoteChanges.swift b/Example/NotesExample-iOS/Notification+NoteChanges.swift index a45fefd..e7b6664 100644 --- a/Example/NotesExample-iOS/Notification+NoteChanges.swift +++ b/Example/NotesExample-iOS/Notification+NoteChanges.swift @@ -4,16 +4,18 @@ import Foundation extension Notification.Name { /// Its `userInfo` provides `oldValue` and `newValue` Strings. - static var noteTitleDidChange: Notification.Name { return .init("OB_NoteTitleDidChange") } + static var noteTitleDidChange: Notification.Name { return .init("OBXNoteTitleDidChange") } /// - If the note had an author before the change, `userInfo["oldValue"]` contains the old ID value. /// - If the note has an author after the change, `userInfo["newValue"]` contains the new ID value. - static var noteAuthorDidChange: Notification.Name { return .init("OB_NoteAuthorDidChange") } + static var noteAuthorDidChange: Notification.Name { return .init("OBXNoteAuthorDidChange") } + + static var noteModificationDateDidChange: Notification.Name { return .init("OBXNoteModificationDateDidChange") } /// Its `userInfo` contains `noteId`. - static var noteAdded: Notification.Name { return .init("OB_NoteAdded") } + static var noteAdded: Notification.Name { return .init("OBXNoteAdded") } /// Its `userInfo` contains `noteId`. - static var noteRemoved: Notification.Name { return .init("OB_NoteRemoved") } + static var noteRemoved: Notification.Name { return .init("OBXNoteRemoved") } } diff --git a/Example/Shared/Note.swift b/Example/Shared/Note.swift index 26a4d85..32828fb 100644 --- a/Example/Shared/Note.swift +++ b/Example/Shared/Note.swift @@ -4,8 +4,18 @@ import ObjectBox class Note: Entity { var id: Id = 0 // An `Id` is required by ObjectBox - var title: String = "" - var text: String = "" + var title: String = "" { + didSet { + modificationDate = Date() + } + } + var text: String = "" { + didSet { + modificationDate = Date() + } + } + var creationDate: Date? = Date() + var modificationDate: Date? var author: ToOne = nil // An initializer with no parameters is required by ObjectBox