Skip to content

Commit

Permalink
Read iCloud snippets in background if they not downloaded yet. Refs #…
Browse files Browse the repository at this point in the history
  • Loading branch information
yury committed Jul 27, 2023
1 parent 70e4d92 commit 3b8c125
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions BlinkSnippets/iCloudSnippets.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,32 @@
import Foundation

public class iCloudSnippets: LocalSnippets {
private let _fileManager: FileManager
private let _downloadQueue: DispatchQueue

public override init(from sourcePathURL: URL) {
self._fileManager = FileManager.default
self._downloadQueue = DispatchQueue.global()
super.init(from: sourcePathURL)
}

public override func listSnippets(forceUpdate: Bool = false) async throws -> [Snippet] {
try FileManager.default.startDownloadingUbiquitousItem(at: self.sourcePathURL)
try _fileManager.startDownloadingUbiquitousItem(at: self.sourcePathURL)
return try await super.listSnippets(forceUpdate: forceUpdate)
}

public override func readDescription(folder: String, name: String) throws -> String {
let url = snippetLocation(folder: folder, name: name)
let iCloudUrl = url.appendingPathExtension("icloud")
if _fileManager.fileExists(atPath: iCloudUrl.path) {
_downloadQueue.async {
// NOTE: if we try to read first line, .icloud will be still there
_ = try? String(contentsOf: url)
}
return ""
}
return url.readFirstLineOfContent() ?? ""
}

}

// iCloudSnippets can handle the iCloud interface to track changes to files.

0 comments on commit 3b8c125

Please sign in to comment.