Skip to content

Commit

Permalink
- Adds blockig mechanism for notes reloading
Browse files Browse the repository at this point in the history
- Fixes deprecated APIs
  • Loading branch information
glushchenko committed Jul 12, 2024
1 parent 78bdbc9 commit a673dd3
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 22 deletions.
4 changes: 2 additions & 2 deletions FSNotes.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -4557,7 +4557,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 630;
CURRENT_PROJECT_VERSION = 632;
DEVELOPMENT_TEAM = 866P6MTE92;
EMBED_ASSET_PACKS_IN_PRODUCT_BUNDLE = YES;
ENABLE_HARDENED_RUNTIME = YES;
Expand Down Expand Up @@ -4597,7 +4597,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 630;
CURRENT_PROJECT_VERSION = 632;
DEPLOYMENT_LOCATION = NO;
DEVELOPMENT_TEAM = 866P6MTE92;
EMBED_ASSET_PACKS_IN_PRODUCT_BUNDLE = YES;
Expand Down
27 changes: 15 additions & 12 deletions FSNotes/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,21 +132,22 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {

let noteWindows = self.noteWindows.sorted(by: { $0.window!.orderedIndex > $1.window!.orderedIndex })
for windowController in noteWindows {
if let frame = windowController.window?.frame {
let data = NSKeyedArchiver.archivedData(withRootObject: frame)
if let controller = windowController.contentViewController as? NoteViewController,
let note = controller.editor.note {
let key = windowController.window?.isKeyWindow == true
result.append(["frame": data, "preview": controller.editor.isPreviewEnabled(), "url": note.url, "main": false, "key": key])
}
if let frame = windowController.window?.frame,
let data = try? NSKeyedArchiver.archivedData(withRootObject: frame, requiringSecureCoding: true),
let controller = windowController.contentViewController as? NoteViewController,
let note = controller.editor.note {


let key = windowController.window?.isKeyWindow == true

result.append(["frame": data, "preview": controller.editor.isPreviewEnabled(), "url": note.url, "main": false, "key": key])
}
}

// Main frame
if let vc = ViewController.shared(), let note = vc.editor?.note, let mainFrame = vc.view.window?.frame {
let data = NSKeyedArchiver.archivedData(withRootObject: mainFrame)
if let vc = ViewController.shared(), let note = vc.editor?.note, let mainFrame = vc.view.window?.frame,
let data = try? NSKeyedArchiver.archivedData(withRootObject: mainFrame, requiringSecureCoding: true) {

let key = vc.view.window?.isKeyWindow == true

result.append(["frame": data, "preview": vc.editor.isPreviewEnabled(), "url": note.url, "main": true, "key": key])
Expand Down Expand Up @@ -288,7 +289,9 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
attachMenu()

DispatchQueue.main.async {
self.statusItem?.popUpMenu(self.statusItem!.menu!)
if let statusItem = self.statusItem, let button = statusItem.button {
statusItem.menu?.popUp(positioning: nil, at: NSPoint(x: button.frame.origin.x, y: button.frame.height + 10), in: button)
}
}
}

Expand Down
14 changes: 14 additions & 0 deletions FSNotes/Business/Note.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public class Note: NSObject {

public var tags = [String]()
public var originalExtension: String?

public var isBlocked: Bool = false
public var unBlockTimer: Timer?

/*
Filename with extension ie "example.textbundle"
Expand Down Expand Up @@ -881,6 +884,16 @@ public class Note: NSObject {
return fileName
}

public func scheduleUnBlock() {
unBlockTimer?.invalidate()
unBlockTimer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(unBlock), userInfo: nil, repeats: true)
}

@objc public func unBlock() {
isBlocked = false
unBlockTimer = nil
}

public func save(attributed: NSAttributedString) {
modifiedLocalAt = Date()

Expand All @@ -890,6 +903,7 @@ public class Note: NSObject {
let mutable = NSMutableAttributedString(attributedString: copy)
self.save(content: mutable)
usleep(100000)
self.isBlocked = false
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions FSNotes/EditorViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1378,10 +1378,13 @@ class EditorViewController: NSViewController, NSTextViewDelegate, WebFrameLoadDe
vc.prevCommit = nil

if editor.isEditable {
note.isBlocked = true

editor.removeHighlight()
editor.saveImages()

note.save(attributed: editor.attributedString())
note.scheduleUnBlock()

updateLastEditedStatus()
vc.reSort(note: note)
Expand Down
6 changes: 5 additions & 1 deletion FSNotes/Helpers/FileSystemEventManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,11 @@ class FileSystemEventManager {
}

private func reloadNote(note: Note) {
guard note.container != .encryptedTextPack else { return }
guard !note.isBlocked, note.container != .encryptedTextPack else {
print("skip")
print(note.isBlocked)
return
}

guard var fsContent = note.getContent() else { return }

Expand Down
14 changes: 7 additions & 7 deletions FSNotes/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1669,8 +1669,8 @@ class ViewController: EditorViewController,

private func loadBookmarks(data: Data?) {
if let accessData = data,
let bookmarks = NSKeyedUnarchiver.unarchiveObject(with: accessData) as? [URL: Data] {
let bookmarks = try? NSKeyedUnarchiver.unarchivedObject(ofClasses: [NSDictionary.self, NSURL.self, NSData.self], from: accessData) as? [URL: Data] {

for bookmark in bookmarks {
var isStale = false

Expand Down Expand Up @@ -2012,8 +2012,8 @@ class ViewController: EditorViewController,
let projectsDataUrl = documentDir.appendingPathComponent("editors.settings")

guard let data = try? Data(contentsOf: projectsDataUrl) else { return }
guard let unarchivedData = NSKeyedUnarchiver.unarchiveObject(with: data) as? [[String: Any]] else { return }
guard let unarchivedData = try? NSKeyedUnarchiver.unarchivedObject(ofClasses: [NSArray.self, NSDictionary.self, NSString.self, NSData.self, NSNumber.self, NSURL.self], from: data) as? [[String: Any]] else { return }

var mainKey = false
for item in unarchivedData.reversed() {
guard let url = item["url"] as? URL,
Expand Down Expand Up @@ -2041,8 +2041,8 @@ class ViewController: EditorViewController,
self.editor.window?.makeFirstResponder(self.editor)
}
} else {
guard let frame = NSKeyedUnarchiver.unarchiveObject(with: frameData) as? NSRect else { continue }
guard let frame = try? NSKeyedUnarchiver.unarchivedObject(ofClass: NSValue.self, from: frameData)?.rectValue else { continue }

self.openInNewWindow(note: note, frame: frame, preview: preview)
}
}
Expand Down Expand Up @@ -2103,7 +2103,7 @@ class ViewController: EditorViewController,
// Transfer private git key to default project
if UserDefaultsManagement.gitPrivateKeyData != nil {
if let accessData = UserDefaultsManagement.gitPrivateKeyData,
let bookmarks = NSKeyedUnarchiver.unarchiveObject(with: accessData) as? [URL: Data] {
let bookmarks = try? NSKeyedUnarchiver.unarchivedObject(ofClasses: [NSDictionary.self, NSURL.self, NSData.self], from: accessData) as? [URL: Data] {
for bookmark in bookmarks {
if let data = try? Data(contentsOf: bookmark.key) {

Expand Down

0 comments on commit a673dd3

Please sign in to comment.