Skip to content

Commit

Permalink
- Fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
glushchenko committed Feb 5, 2024
1 parent c96e046 commit 3c6bc78
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 120 deletions.
4 changes: 2 additions & 2 deletions FSNotes.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -4559,7 +4559,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 611;
CURRENT_PROJECT_VERSION = 614;
DEVELOPMENT_TEAM = 866P6MTE92;
EMBED_ASSET_PACKS_IN_PRODUCT_BUNDLE = YES;
ENABLE_HARDENED_RUNTIME = YES;
Expand Down Expand Up @@ -4599,7 +4599,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 611;
CURRENT_PROJECT_VERSION = 614;
DEPLOYMENT_LOCATION = NO;
DEVELOPMENT_TEAM = 866P6MTE92;
EMBED_ASSET_PACKS_IN_PRODUCT_BUNDLE = YES;
Expand Down
14 changes: 4 additions & 10 deletions FSNotes/Business/Note.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,6 @@ public class Note: NSObject {
lastSelectedRange = value
}

public func getLastSelectedRange() -> NSRange? {
return lastSelectedRange
}

public func hasTitle() -> Bool {
return !firstLineAsTitle
}
Expand Down Expand Up @@ -290,7 +286,7 @@ public class Note: NSObject {
}

public func getFileModifiedDate() -> Date? {
let url = getURL()
let url = getContentFileURL() ?? url

do {
let attr = try FileManager.default.attributesOfItem(atPath: url.path)
Expand Down Expand Up @@ -885,6 +881,8 @@ public class Note: NSObject {
}

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

Storage.shared().plainWriter.cancelAllOperations()
Storage.shared().plainWriter.addOperation {
if let copy = attributed.copy() as? NSAttributedString {
Expand Down Expand Up @@ -981,10 +979,7 @@ public class Note: NSObject {
guard Storage.shared().ciphertextWriter.operationCount == 1 else { return }
self.writeEncrypted()
}
} else {
modifiedLocalAt = Date()
}

} catch {
NSLog("Write error \(error)")
return
Expand Down Expand Up @@ -1119,10 +1114,9 @@ public class Note: NSObject {
}

func getFileAttributes() -> [FileAttributeKey: Any] {
let url = getContentFileURL() ?? url
var attributes: [FileAttributeKey: Any] = [:]

modifiedLocalAt = Date()

do {
attributes = try FileManager.default.attributesOfItem(atPath: url.path)
} catch {}
Expand Down
9 changes: 3 additions & 6 deletions FSNotes/EditorViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -650,12 +650,12 @@ class EditorViewController: NSViewController, NSTextViewDelegate, WebFrameLoadDe
}

@IBAction func removeNoteEncryption(_ sender: Any) {
guard var notes = getSelectedNotes() else { return }
guard var notes = getSelectedNotes(),
let vc = ViewController.shared() else { return }

notes = decryptUnlocked(notes: notes)
guard notes.count > 0 else { return }

UserDataService.instance.fsUpdatesDisabled = true
getMasterPassword() { password in
for note in notes {
if note.container == .encryptedTextPack {
Expand All @@ -668,9 +668,8 @@ class EditorViewController: NSViewController, NSTextViewDelegate, WebFrameLoadDe
}
}

ViewController.shared()?.notesTableView.reloadRow(note: note)
vc.notesTableView.reloadRow(note: note)
}
UserDataService.instance.fsUpdatesDisabled = false
}
}

Expand Down Expand Up @@ -1378,8 +1377,6 @@ class EditorViewController: NSViewController, NSTextViewDelegate, WebFrameLoadDe

vc.prevCommit = nil

vc.blockFSUpdates()

if editor.isEditable {
editor.removeHighlight()
editor.saveImages()
Expand Down
34 changes: 14 additions & 20 deletions FSNotes/Helpers/FileSystemEventManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ class FileSystemEventManager {
public func start() {
watcher = FileWatcher(self.observedFolders)
watcher?.callback = { event in
if UserDataService.instance.fsUpdatesDisabled {
return
}

guard let path = event.path.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) else {
return
}
Expand Down Expand Up @@ -242,20 +238,19 @@ class FileSystemEventManager {
private func reloadNote(note: Note) {
guard note.container != .encryptedTextPack else { return }

let memoryContent = note.content.attributedSubstring(from: NSRange(0..<note.content.length))
guard var fsContent = note.getContent() else { return }

// Trying load content from encrypted note with current password
if note.url.pathExtension == "etp", let password = note.password, note.unLock(password: password) {
fsContent = note.content
}

if (
note.isRTF() && fsContent != memoryContent)
|| (
!note.isRTF() && fsContent.string != memoryContent.string
)
{
guard let modificationDate = note.getFileModifiedDate(),
let creationDate = note.getFileCreationDate() else { return }

if modificationDate > note.modifiedLocalAt {

note.modifiedLocalAt = modificationDate
note.cacheHash = nil
note.content = NSMutableAttributedString(attributedString: fsContent)

Expand Down Expand Up @@ -287,17 +282,16 @@ class FileSystemEventManager {
}
}
}
} else if let modificationDate = note.getFileModifiedDate(), let creationDate = note.getFileCreationDate() {
if modificationDate != note.modifiedLocalAt || creationDate != note.creationDate {
note.modifiedLocalAt = modificationDate
note.creationDate = creationDate
}

if creationDate != note.creationDate {
note.creationDate = creationDate

delegate.notesTableView.reloadDate(note: note)
delegate.reSort(note: note)
delegate.notesTableView.reloadDate(note: note)
delegate.reSort(note: note)

// Reload images if note moved (cache invalidated)
note.loadPreviewInfo()
}
// Reload images if note moved (cache invalidated)
note.loadPreviewInfo()
}
}

Expand Down
3 changes: 2 additions & 1 deletion FSNotes/Helpers/NotesTextProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,8 @@ public class NotesTextProcessor {

FSParser.yamlBlockRegex.matches(string, range: NSRange(location: 0, length: attributedString.length)) { (result) -> Void in
guard let range = result?.range(at: 1) else { return }

attributedString.addAttribute(.foregroundColor, value: NotesTextProcessor.fontColor, range: range)

if range.location == 0 {
let listOpeningRegex = MarklightRegex(pattern: "([a-zA-Z_]+):", options: [.allowCommentsAndWhitespace])
listOpeningRegex.matches(string, range: range) { (result) -> Void in
Expand Down
8 changes: 1 addition & 7 deletions FSNotes/View/EditTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class EditTextView: NSTextView, NSTextFinderClient, NSSharingServicePickerDelega
public var timer: Timer?
public var tagsTimer: Timer?
public var markdownView: MPreviewView?
public var restoreRange: NSRange? = nil
public var isLastEdited: Bool = false

@IBOutlet weak var previewMathJax: NSMenuItem!
Expand Down Expand Up @@ -1056,12 +1055,6 @@ class EditTextView: NSTextView, NSTextFinderClient, NSSharingServicePickerDelega
if UserDefaultsManagement.appearanceType == AppearanceType.Custom {
backgroundColor = UserDefaultsManagement.bgColor
}

if let restoreRange = self.restoreRange {
NSApp.mainWindow?.makeFirstResponder(self)
setSelectedRange(restoreRange)
self.restoreRange = nil
}
}

private func loadMarkdownWebView(note: Note, force: Bool) {
Expand Down Expand Up @@ -1583,6 +1576,7 @@ class EditTextView: NSTextView, NSTextFinderClient, NSSharingServicePickerDelega

let string = storage.attributedSubstring(from: NSRange(0..<storage.length))

note.modifiedLocalAt = Date()
note.content =
NSMutableAttributedString(attributedString: string)
.unLoadImages()
Expand Down
4 changes: 0 additions & 4 deletions FSNotes/ViewController+Git.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ extension EditorViewController {
guard let commit = sender.representedObject as? Commit else { return }
guard let note = vcEditor?.note else { return }

UserDataService.instance.fsUpdatesDisabled = true

if vc.prevCommit == nil {
saveRevision(commitMessage: "Auto save on history checkout")
}
Expand All @@ -110,8 +108,6 @@ extension EditorViewController {
ViewController.shared()?.notesTableView.reloadRow(note: note)

vcEditor?.scanTagsAndAutoRename()

UserDataService.instance.fsUpdatesDisabled = false
}

@IBAction private func makeFullSnapshot(_ sender: Any) {
Expand Down
37 changes: 8 additions & 29 deletions FSNotes/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@ class ViewController: EditorViewController,
private var isPreLoaded = false

let storage = Storage.shared()
var timer = Timer()
var sidebarTimer = Timer()

let searchQueue = OperationQueue()

var tagsScannerQueue = [Note]()

private var sidebarTimer = Timer()
private var selectRowTimer = Timer()

private let searchQueue = OperationQueue()

public static var gitQueue = OperationQueue()
public static var gitQueueBusy: Bool = false
public static var gitQueueOperationDate: Date?
Expand All @@ -42,7 +41,9 @@ class ViewController: EditorViewController,

/* Git */
private var updateViews = [Note]()


var tagsScannerQueue = [Note]()

override var representedObject: Any? {
didSet { } // Update the view, if already loaded.
}
Expand Down Expand Up @@ -1186,14 +1187,6 @@ class ViewController: EditorViewController,
}
}


public func blockFSUpdates() {
timer.invalidate()
timer = Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(enableFSUpdates), userInfo: nil, repeats: false)

UserDataService.instance.fsUpdatesDisabled = true
}

public func reSort(note: Note) {
if !updateViews.contains(note) {
updateViews.append(note)
Expand Down Expand Up @@ -1235,10 +1228,6 @@ class ViewController: EditorViewController,
}
}

@objc func enableFSUpdates() {
UserDataService.instance.fsUpdatesDisabled = false
}

@objc private func updateTableViews() {
let editors = AppDelegate.getEditTextViews()

Expand Down Expand Up @@ -1291,8 +1280,6 @@ class ViewController: EditorViewController,
return nil
}

private var selectRowTimer = Timer()

func updateTable(search: Bool = false, searchText: String? = nil, sidebarItem: SidebarItem? = nil, projects: [Project]? = nil, tags: [String]? = nil, completion: @escaping () -> Void = {}) {

var sidebarItem: SidebarItem? = sidebarItem
Expand Down Expand Up @@ -1992,14 +1979,6 @@ class ViewController: EditorViewController,

note.previewState = state == "preview"

if let position = Int(position),
position > -1,
let textStorage = editor.textStorage,
textStorage.length >= position {

editor.restoreRange = NSRange(location: position, length: 0)
}

notesTableView.selectRowAndSidebarItem(note: note)
}

Expand Down
10 changes: 0 additions & 10 deletions FSNotesCore/Core macOS/Helpers/UserDataService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class UserDataService {

fileprivate var _searchTrigger = false
fileprivate var _lastRenamed: URL?
fileprivate var _fsUpdates = false
fileprivate var _isNotesTableEscape = false
fileprivate var _isDark = false

Expand Down Expand Up @@ -41,15 +40,6 @@ public class UserDataService {
}
}

public var fsUpdatesDisabled: Bool {
get {
return _fsUpdates
}
set {
_fsUpdates = newValue
}
}

public var isNotesTableEscape: Bool {
get {
return _isNotesTableEscape
Expand Down
6 changes: 5 additions & 1 deletion FSNotesCore/Shared/Extensions/String+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ public extension String {
}

func trunc(length: Int) -> String {
return (self.count > length) ? String(self.prefix(length)) : self
let result = self
.replacingOccurrences(of: ":", with: "-")
.replacingOccurrences(of: "/", with: ":")

return (result.count > length) ? String(result.prefix(length)) : result
}

func startsWith(string: String) -> Bool {
Expand Down
30 changes: 0 additions & 30 deletions FSNotesCoreTests/macOSTests/Helpers/UserDataServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,4 @@ class UserDataServiceTests: XCTestCase {
service.searchTrigger = false
XCTAssertFalse(service.searchTrigger)
}

func testLastRenamed() {
XCTAssert(service.lastRenamed == nil)

service.lastRenamed = URL(string: "file:///tmp/foo")
XCTAssertEqual(service.lastRenamed?.absoluteString, URL(string: "file:///tmp/foo")?.absoluteString)

service.lastRenamed = nil
XCTAssertNil(service.lastRenamed)
}

func testFsUpdatesDisabled() {
XCTAssertFalse(service.fsUpdatesDisabled)

service.fsUpdatesDisabled = true
XCTAssertTrue(service.fsUpdatesDisabled)

service.fsUpdatesDisabled = false
XCTAssertFalse(service.fsUpdatesDisabled)
}

func testSkipListReload() {
XCTAssertFalse(service.skipListReload)

service.skipListReload = true
XCTAssertTrue(service.skipListReload)

service.skipListReload = false
XCTAssertFalse(service.skipListReload)
}
}

0 comments on commit 3c6bc78

Please sign in to comment.