Skip to content

Commit

Permalink
- Fixes scheduled backups
Browse files Browse the repository at this point in the history
  • Loading branch information
glushchenko committed Jan 11, 2025
1 parent ba33879 commit 3d6e0d8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion FSNotes/EditorViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class EditorViewController: NSViewController, NSTextViewDelegate, NSMenuItemVali

// git
public var snapshotsTimer = Timer()
public var lastSnapshot: Int = 0
public var lastSnapshot: Int?
public var pullTimer = Timer()

public var encPassword: NSSecureTextField?
Expand Down
18 changes: 12 additions & 6 deletions FSNotes/ViewController+Git.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,23 @@ extension EditorViewController {
let hour = cal.component(.hour, from: Date())
let minute = cal.component(.minute, from: Date())

if minute == lastSnapshot {
return
if let lastSnapshot = self.lastSnapshot {
if minute == lastSnapshot {
return
} else {
self.lastSnapshot = nil
}
}

lastSnapshot = minute

guard UserDefaultsManagement.snapshotsInterval != 0 && (
hour == UserDefaultsManagement.snapshotsInterval || (
hour != 0 && hour % UserDefaultsManagement.snapshotsInterval == 0
)
) else { return }

guard UserDefaultsManagement.snapshotsIntervalMinutes == minute else { return }

lastSnapshot = minute

ViewController.gitQueue.addOperation({
ViewController.gitQueueOperationDate = Date()
Expand Down Expand Up @@ -186,8 +190,10 @@ extension EditorViewController {
public func scheduleSnapshots() {
guard !UserDefaultsManagement.backupManually else { return }

snapshotsTimer.invalidate()
snapshotsTimer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(makeFullSnapshot), userInfo: nil, repeats: true)
DispatchQueue.main.async {
self.snapshotsTimer.invalidate()
self.snapshotsTimer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(self.makeFullSnapshot), userInfo: nil, repeats: true)
}
}

public func schedulePull() {
Expand Down

0 comments on commit 3d6e0d8

Please sign in to comment.