Skip to content

Commit

Permalink
Deprecate many attribute reader functions
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsaidi committed Feb 14, 2024
1 parent f4b6ae8 commit 410e223
Show file tree
Hide file tree
Showing 18 changed files with 50 additions and 45 deletions.
10 changes: 5 additions & 5 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ Until then, minor updates may remove deprecated features and introduce breaking

This release adds a lot of new `RichTextAction` cases and adjusts the context and coordinator subscription.

This release also deprecates `RichTextAttributeReader` and `RichTextAttributeWriter` functionality that are not used by the library, in favor of `RichTextViewComponent`. This is done to reduce the complexity of the library.
This release also deprecates `RichTextAttributeReader` and `RichTextAttributeWriter` functionality that are not used by the library, in favor of `RichTextViewComponent`, to reduce the complexity of the library.

Until now, the functions mostly did the same things, but sometimes the `RichTextViewComponent` have to use its text storage or layout manager, or update the typing attributes. Since these are not present in the reader and writer protocols, the code diverged and mostly identical copies had to co-exist. And since the reader and writer versions are not used by the library, they may not work as well as intended.
Until now, these functions mostly did the same thing, but sometimes the `RichTextViewComponent` have to use its text storage or layout manager, or update the typing attributes. This caused the code to diverge and mostly identical copies had co-exist, where the reader and writer versions were not even used by the library.

This change will hopefully make it possible to simplify the library in the 1.0 release, and focus more on unifying the different TextView platform implementations (`UITextView` in iOS and `NSTextView` in macOS), rather than providing a low level string handling interface.
By deprecating these functions, we can simplify the library in 1.0, and focus more on unifying the different `RichTextViewComponent` implementations (`UITextView` in iOS and `NSTextView` in macOS), rather than providing a low level string handling interface.

### ✨ Features

Expand All @@ -39,10 +39,10 @@ This change will hopefully make it possible to simplify the library in the 1.0 r
* `RichTextAlignment` now behaves better.
* `RichTextCoordinator` now handles indentation changes.


### 🗑️ Deprecations

* `RichTextAttributeWriter` deprecates many functions in favor of `RichTextViewComponent`.
* `RichTextAttributeReader` has deprecated a lot of functions.
* `RichTextAttributeWriter` has deprecated a lot of functions.
* `RichTextCoordinator` functions that simply triggered `handle(_:)` have been deprecated.


Expand Down
6 changes: 6 additions & 0 deletions Sources/RichTextKit/Attributes/RichTextAttributeReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ import Foundation
The protocol is implemented by `NSAttributedString` as well
as other types in the library.
Note that this protocol used to have a lot of functionality
for getting various attributes, styles, etc. However, since
``RichTextViewComponent`` needs more capabilities, we ended
up with duplicated code where the reader had functions that
weren't even used within the library.
*/
public protocol RichTextAttributeReader: RichTextReader {}

Expand Down
9 changes: 3 additions & 6 deletions Sources/RichTextKit/Attributes/RichTextAttributeWriter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ import Foundation
Note that this protocol used to have a lot of functionality
for setting various attributes, styles, etc. However, since
``RichTextViewComponent`` needs to perform changes in other
ways, we ended up with duplicated code where the writer had
functions that may have worked, but weren't used within the
library. As such, the ``RichTextViewComponent`` will be the
primary component for modifying rich text, while the writer
functionality is removed. This will help to avoid confusion.
``RichTextViewComponent`` needs more capabilities, we ended
up with duplicated code where the writer had functions that
weren't even used within the library.
*/
public protocol RichTextAttributeWriter: RichTextWriter, RichTextAttributeReader {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ public extension RichTextViewComponent {
guard let attribute = color.attribute else { return nil }
return richTextAttribute(attribute)
}

/// Get a certain rich text color at a certain range.
func richTextColor(
_ color: RichTextColor,
at range: NSRange
) -> ColorRepresentable? {
guard let attribute = color.attribute else { return nil }
return richTextAttribute(attribute, at: range)
}

/// Set a certain rich text color at current range.
func setRichTextColor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import Foundation

@available(*, deprecated, message: "Use RichTextViewComponent instead.")
public extension RichTextAttributeReader {

/// Get the rich text alignment at a certain range.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@

import Foundation

@available(*, deprecated, message: "Use RichTextViewComponent instead")
public extension RichTextAttributeReader {

/// Get a certain rich text color at a certain range.
func richTextColor(
func richTextColorOld(
_ color: RichTextColor,
at range: NSRange
) -> ColorRepresentable? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import Foundation

@available(*, deprecated, message: "Use RichTextViewComponent instead.")
public extension RichTextAttributeReader {

/// Get the rich text font at a certain range.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import Foundation

@available(*, deprecated, message: "Use RichTextViewComponent instead")
public extension RichTextAttributeReader {

/// Get the rich text indent at a certain range.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import UIKit
import AppKit
#endif

@available(*, deprecated, message: "Use RichTextViewComponent instead.")
public extension RichTextAttributeReader {

/// Get the rich text paragraph style at a certain range.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import Foundation

@available(*, deprecated, message: "Use RichTextViewComponent instead")
public extension RichTextAttributeReader {

/// Get all rich text styles at a certain range.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#if macOS
import Foundation

@available(*, deprecated, message: "Use RichTextViewComponent instead.")
public extension RichTextAttributeReader {

/// Get the superscript level at a certain range.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import Foundation
public extension RichTextAttributeWriter {

/// Set a certain rich text color at a certain range.
func setRichTextColor(
func setRichTextColorOld(
_ color: RichTextColor,
to val: ColorRepresentable,
at range: NSRange
) {
guard let attribute = color.attribute else { return }
if richTextColor(color, at: range) == val { return }
if richTextColorOld(color, at: range) == val { return }
setRichTextAttribute(attribute, to: val, at: range)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ final class RichTextCoordinatorIntegrationTests: XCTestCase {
XCTAssertEqual(textView.richTextAttributes[.font] as? FontRepresentable, FontRepresentable.systemFont(ofSize: 16))
XCTAssertEqual(textView.richTextAttributes[.foregroundColor] as? ColorRepresentable, ColorRepresentable.red)

textView.setRichTextStyle(.strikethrough, to: true, at: textView.selectedRange)
textView.setRichTextStyle(.strikethrough, to: true)
XCTAssertEqual(textView.richTextAttributes[.strikethroughStyle] as? Int, 1)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ final class RichTextViewComponent_AttributesTests: XCTestCase {
textView.setRichTextAttribute(.font, to: font)
assertEqualAttributes(textView.richTextAttributes)
assertEqualAttributes(textView.richTextAttributes(at: selectedRange))
#if iOS
assertEqualAttributes(textView.typingAttributes)
#endif
}

func testTextAttributesIsValidForNoSelectedRange() {
Expand All @@ -65,7 +67,9 @@ final class RichTextViewComponent_AttributesTests: XCTestCase {
textView.setRichTextAttribute(.font, to: font)
assertEqualAttribute(textView.richTextAttribute(.font))
assertEqualAttribute(textView.richTextAttribute(.font, at: selectedRange))
#if iOS
assertEqualAttribute(textView.typingAttributes[.font])
#endif
}

func testTextAttributeValueForKeyIsValidForNoSelectedRange() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ final class RichTextViewComponent_ColorTests: XCTestCase {
XCTAssertEqual(textView.richTextColor(.background), color)
assertEqualColor(textView.richTextAttributes[.backgroundColor])
assertEqualColor(textView.richTextAttributes(at: selectedRange)[.backgroundColor])
#if iOS || os(tvOS)
assertEqualColor(textView.typingAttributes[.backgroundColor])
#endif
}

func testRichTextBackgroundColorWorksForNoSelectedRange() {
Expand All @@ -76,7 +78,9 @@ final class RichTextViewComponent_ColorTests: XCTestCase {
XCTAssertEqual(textView.richTextColor(.foreground), color)
assertEqualColor(textView.richTextAttributes[.foregroundColor])
assertEqualColor(textView.richTextAttributes(at: selectedRange)[.foregroundColor])
#if iOS || os(tvOS)
assertEqualColor(textView.typingAttributes[.foregroundColor])
#endif
}

func testRichTextForegroundColorWorksForNoSelectedRange() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ final class RichTextViewComponent_FontSizeTests: XCTestCase {
XCTAssertEqual(textView.richTextFont?.fontName, font.fontName)
assertEqualFont(textView.richTextAttribute(.font))
assertEqualFont(textView.richTextAttributes(at: selectedRange)[.font])
#if iOS || os(tvOS)
assertEqualFont(textView.typingAttributes[.font])
#endif
}

func testRichTextFontWorksForNoSelectedRange() {
Expand Down
9 changes: 6 additions & 3 deletions Tests/RichTextKitTests/RichTextViewRepresentableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,14 @@ final class RichTextViewComponentTests: XCTestCase {
#endif
XCTAssertEqual(view.backgroundColor, .clear)
XCTAssertEqual(view.contentCompressionResistancePriority(for: .horizontal), .defaultLow)
#if iOS || os(tvOS)
#if iOS
XCTAssertEqual(view.spellCheckingType, .no)
XCTAssertNil(view.textColor)
#elseif os(tvOS)
XCTAssertEqual(view.spellCheckingType, .no)
XCTAssertEqual(view.textColor, nil)
XCTAssertNotNil(view.textColor)
#elseif macOS
XCTAssertEqual(view.textColor, nil)
XCTAssertNotNil(view.textColor)
#endif
}
}
Expand Down

0 comments on commit 410e223

Please sign in to comment.