Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor FXIOS-5605 [v111] Remove Deferred from UIPasteboard+Extension #13000

Merged
merged 1 commit into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 5 additions & 18 deletions Client/Extensions/UIPasteboard+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,16 @@ extension UIPasteboard {
/// Preferred method to get strings out of the clipboard.
/// When iCloud pasteboards are enabled, the usually fast, synchronous calls
/// become slow and synchronous causing very slow start up times.
func asyncString() -> Deferred<Maybe<String?>> {
return fetchAsync {
return UIPasteboard.general.string
func asyncString(completionHandler: @escaping (String?) -> Void) {
DispatchQueue.global().async {
completionHandler(UIPasteboard.general.string)
}
}

/// Preferred method to get URLs out of the clipboard.
/// We use Deferred<Maybe<T?>> to fit in to the rest of the Deferred<Maybe> tools
/// we already use; but use optionals instead of errorTypes, because not having a URL
/// on the clipboard isn't an error.
func asyncURL() -> Deferred<Maybe<URL?>> {
return fetchAsync {
return self.syncURL
}
}

// Converts the potentially long running synchronous operation into an asynchronous one.
private func fetchAsync<T>(getter: @escaping () -> T) -> Deferred<Maybe<T>> {
let deferred = Deferred<Maybe<T>>()
func asyncURL(completionHandler: @escaping (URL?) -> Void) {
DispatchQueue.global().async {
let value = getter()
deferred.fill(Maybe(success: value))
completionHandler(self.syncURL)
}
return deferred
}
}
15 changes: 8 additions & 7 deletions Client/Frontend/Browser/ClipboardBarDisplayHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,17 @@ class ClipboardBarDisplayHandler: NSObject, URLChangeDelegate {
// UIPasteboardChanged gets triggered when calling UIPasteboard.general.
NotificationCenter.default.removeObserver(self, name: UIPasteboard.changedNotification, object: nil)

UIPasteboard.general.asyncURL().uponQueue(.main) { res in
defer {
NotificationCenter.default.addObserver(self, selector: #selector(self.UIPasteboardChanged), name: UIPasteboard.changedNotification, object: nil)
}
UIPasteboard.general.asyncURL { url in
ensureMainThread {
defer {
NotificationCenter.default.addObserver(self, selector: #selector(self.UIPasteboardChanged), name: UIPasteboard.changedNotification, object: nil)
}

guard let copiedURL: URL? = res.successValue,
let url = copiedURL else {
guard let url = url else {
return
}
self.lastDisplayedURL = url.absoluteString
}
self.lastDisplayedURL = url.absoluteString
}
}

Expand Down
14 changes: 8 additions & 6 deletions Extensions/Today/TodayViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ class TodayWidgetViewModel {

func updateCopiedLink() {
if UIPasteboard.general.hasURLs {
UIPasteboard.general.asyncURL().uponQueue(.main) { res in
guard let url: URL? = res.successValue else {
TodayModel.copiedURL = nil
return
UIPasteboard.general.asyncURL { url in
DispatchQueue.main.async {
guard let url: URL = url else {
TodayModel.copiedURL = nil
return
}
TodayModel.copiedURL = url
self.AppearanceDelegate?.openContainingApp("?url=\(TodayModel.copiedURL?.absoluteString.escape() ?? "")", query: "open-url")
}
TodayModel.copiedURL = url
self.AppearanceDelegate?.openContainingApp("?url=\(TodayModel.copiedURL?.absoluteString.escape() ?? "")", query: "open-url")
}
} else {
guard let searchText = UIPasteboard.general.string else {
Expand Down