-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for async text resolver (#176)
- Loading branch information
Showing
10 changed files
with
204 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
Proton/Sources/Swift/AsyncTextResolver/AsyncTextResolver.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// | ||
// AsyncTextResolver.swift | ||
// Proton | ||
// | ||
// Created by Rajdeep Kwatra on 31/5/2023. | ||
// Copyright © 2023 Rajdeep Kwatra. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
|
||
import Foundation | ||
|
||
/// Result type for async text resolution | ||
public enum AsyncTextResolvingResult { | ||
case apply(NSAttributedString, range: NSRange) | ||
case discard | ||
} | ||
|
||
/// An object capable of resolving text asynchronously to another representation. New representation may contain change in attributes or the string itself. | ||
public protocol AsyncTextResolving { | ||
/// Name of the Resolver. This name must be applied to the range of text that requires async resolution with attribute key: `.asyncTextResolver` | ||
var name: String { get } | ||
|
||
/// Resolves the string to a different representation | ||
/// - Parameters: | ||
/// - editor: Editor containing the attributed string | ||
/// - range: Range of attributesString containing `.asyncTextResolver` attribute | ||
/// - string: Substring of attributesString containing `.asyncTextResolver` attribute | ||
/// - completion: Transformed result to be applied. `.apply` will replace the `range` in `Editor` with provided `attributedString`. | ||
/// .`.discard` discards the operation. | ||
/// - Important: As part of resolution, `.asyncTextResolver` is removed from original range. If the content in original range has changed in | ||
/// the time it took for resolution, it is responsibility of consumer to cleanup dangling attribute, if any. | ||
func resolve(using editor: EditorView, range: NSRange, string: NSAttributedString, completion: @escaping (AsyncTextResolvingResult) -> Void) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
74 changes: 74 additions & 0 deletions
74
Proton/Tests/AsyncTextResolver/AsyncTextResolverSnapshotTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// | ||
// AsyncTextResolverSnapshotTests.swift | ||
// Proton | ||
// | ||
// Created by Rajdeep Kwatra on 4/6/2023. | ||
// Copyright © 2023 Rajdeep Kwatra. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
import XCTest | ||
import SnapshotTesting | ||
|
||
@testable import Proton | ||
|
||
class AsyncTextResolverSnapshotTests: SnapshotTestCase { | ||
func testIgnoresTextAsyncAttributeUntilInvoked() { | ||
let expectation = functionExpectation() | ||
expectation.expectedFulfillmentCount = 2 | ||
let viewController = EditorTestViewController(height: 80) | ||
let editor = viewController.editor | ||
editor.asyncTextResolvers = [DummyResolver()] | ||
editor.attributedText = NSAttributedString(string: "This is some text") | ||
editor.addAttribute(.asyncTextResolver, value: "dummy", at: NSRange(location: 8, length: 4)) | ||
|
||
viewController.render(size: CGSize(width: 300, height: 150)) | ||
assertSnapshot(matching: viewController.view, as: .image, record: recordMode) | ||
|
||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { | ||
viewController.render(size: CGSize(width: 300, height: 150)) | ||
assertSnapshot(matching: viewController.view, as: Snapshotting.image, record: self.recordMode) | ||
expectation.fulfill() | ||
// Invoke async text resolution | ||
editor.resolveAsyncTextIfNeeded() | ||
} | ||
|
||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { | ||
viewController.render(size: CGSize(width: 300, height: 150)) | ||
assertSnapshot(matching: viewController.view, as: Snapshotting.image, record: self.recordMode) | ||
expectation.fulfill() | ||
} | ||
|
||
waitForExpectations(timeout: 1.0) | ||
} | ||
} | ||
|
||
class DummyResolver: AsyncTextResolving { | ||
var name: String { "dummy" } | ||
|
||
func resolve(using editor: EditorView, range: NSRange, string: NSAttributedString, completion: @escaping (AsyncTextResolvingResult) -> Void) { | ||
let mutableString = NSMutableAttributedString(attributedString: string) | ||
|
||
mutableString.addAttribute(.foregroundColor, value: UIColor.red, range: mutableString.fullRange) | ||
mutableString.removeAttribute(.asyncTextResolver, range: mutableString.fullRange) | ||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { | ||
if editor.attributedText.attributedSubstring(from: range).string == string.string { | ||
completion(.apply(mutableString, range: range)) | ||
} else { | ||
completion(.discard) | ||
} | ||
} | ||
} | ||
} |
Binary file added
BIN
+15.2 KB
.../AsyncTextResolverSnapshotTests/testIgnoresTextAsyncAttributeUntilInvoked.1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+15.2 KB
.../AsyncTextResolverSnapshotTests/testIgnoresTextAsyncAttributeUntilInvoked.2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+16.2 KB
.../AsyncTextResolverSnapshotTests/testIgnoresTextAsyncAttributeUntilInvoked.3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.