Skip to content

Commit

Permalink
Merge pull request #67 from GitHawkApp/structs
Browse files Browse the repository at this point in the history
Make some types structs, more auto eq/hash
  • Loading branch information
rnystrom committed Jun 9, 2019
2 parents 160e418 + 0d103f0 commit bfd0f4f
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 62 deletions.
26 changes: 26 additions & 0 deletions Pods/Target Support Files/Pods-Tests/Pods-Tests-Info.plist

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions Pods/Target Support Files/Snap.swift/Snap.swift-Info.plist

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Source/NSAttributedStringAttributesType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// NSAttributedStringAttributesType.swift
// StyledTextKit
//
// Created by Ryan Nystrom on 6/9/19.
// Copyright © 2019 Ryan Nystrom. All rights reserved.
//

import Foundation

public typealias NSAttributedStringAttributesType = [NSAttributedString.Key: AnyHashable]
11 changes: 2 additions & 9 deletions Source/StyledText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,6 @@ public class StyledText: Hashable, Equatable {
}
}

public var hashValue: Int {
switch self {
case .text(let text): return text.hashValue
case .attributedText(let text): return text.hashValue
case .image(let image, _): return image.hashValue
}
}

// MARK: Equatable

public static func ==(lhs: Storage, rhs: Storage) -> Bool {
Expand Down Expand Up @@ -152,7 +144,8 @@ public class StyledText: Hashable, Equatable {
// MARK: Equatable

public static func ==(lhs: StyledText, rhs: StyledText) -> Bool {
return lhs.storage == rhs.storage
return lhs === rhs
|| lhs.storage == rhs.storage
&& lhs.style == rhs.style
}

Expand Down
14 changes: 7 additions & 7 deletions Source/StyledTextBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public final class StyledTextBuilder: Hashable, Equatable {
self.styledTexts = styledTexts
}

public var tipAttributes: [NSAttributedString.Key: Any]? {
public var tipAttributes: NSAttributedStringAttributesType? {
return styledTexts.last?.style.attributes
}

Expand Down Expand Up @@ -73,7 +73,7 @@ public final class StyledTextBuilder: Hashable, Equatable {
public func add(
text: String,
traits: UIFontDescriptor.SymbolicTraits? = nil,
attributes: [NSAttributedString.Key: Any]? = nil
attributes: NSAttributedStringAttributesType? = nil
) -> StyledTextBuilder {
return add(storage: .text(text), traits: traits, attributes: attributes)
}
Expand All @@ -82,7 +82,7 @@ public final class StyledTextBuilder: Hashable, Equatable {
public func add(
attributedText: NSAttributedString,
traits: UIFontDescriptor.SymbolicTraits? = nil,
attributes: [NSAttributedString.Key: Any]? = nil
attributes: NSAttributedStringAttributesType? = nil
) -> StyledTextBuilder {
return add(storage: .attributedText(attributedText), traits: traits, attributes: attributes)
}
Expand All @@ -91,7 +91,7 @@ public final class StyledTextBuilder: Hashable, Equatable {
public func add(
storage: StyledText.Storage = .text(""),
traits: UIFontDescriptor.SymbolicTraits? = nil,
attributes: [NSAttributedString.Key: Any]? = nil
attributes: NSAttributedStringAttributesType? = nil
) -> StyledTextBuilder {
guard let tip = styledTexts.last else { return self }

Expand Down Expand Up @@ -135,7 +135,7 @@ public final class StyledTextBuilder: Hashable, Equatable {
public func add(
image: UIImage,
options: [StyledText.ImageFitOptions] = [.fit, .center],
attributes: [NSAttributedString.Key: Any]? = nil
attributes: NSAttributedStringAttributesType? = nil
) -> StyledTextBuilder {
return add(storage: .image(image, options), attributes: attributes)
}
Expand All @@ -162,8 +162,8 @@ public final class StyledTextBuilder: Hashable, Equatable {
// MARK: Equatable

public static func ==(lhs: StyledTextBuilder, rhs: StyledTextBuilder) -> Bool {
if lhs === rhs { return true }
return lhs.styledTexts == rhs.styledTexts
return lhs === rhs
|| lhs.styledTexts == rhs.styledTexts
}

}
4 changes: 2 additions & 2 deletions Source/StyledTextRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public final class StyledTextRenderer {
return (contents, size)
}

public func attributes(at point: CGPoint) -> (attributes: [NSAttributedString.Key: Any], index: Int)? {
public func attributes(at point: CGPoint) -> (attributes: NSAttributedStringAttributesType, index: Int)? {
os_unfair_lock_lock(&lock)
defer { os_unfair_lock_unlock(&lock) }
var fractionDistance: CGFloat = 1.0
Expand All @@ -170,7 +170,7 @@ public final class StyledTextRenderer {
)
if index != NSNotFound,
fractionDistance < 1.0,
let attributes = layoutManager.textStorage?.attributes(at: index, effectiveRange: nil) {
let attributes = layoutManager.textStorage?.attributes(at: index, effectiveRange: nil) as? NSAttributedStringAttributesType {
return (attributes, index)
}
return nil
Expand Down
17 changes: 1 addition & 16 deletions Source/StyledTextString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation
import UIKit

public final class StyledTextString: Hashable, Equatable {
public struct StyledTextString: Hashable, Equatable {

public enum RenderMode {
case trimWhitespaceAndNewlines
Expand Down Expand Up @@ -39,19 +39,4 @@ public final class StyledTextString: Hashable, Equatable {
}
}

// MARK: Hashable

public func hash(into hasher: inout Hasher) {
styledTexts.forEach {
hasher.combine($0)
}
}

// MARK: Equatable

public static func ==(lhs: StyledTextString, rhs: StyledTextString) -> Bool {
if lhs === rhs { return true }
return lhs.styledTexts == rhs.styledTexts
}

}
4 changes: 2 additions & 2 deletions Source/StyledTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import UIKit

public protocol StyledTextViewDelegate: class {
func didTap(view: StyledTextView, attributes: [NSAttributedString.Key: Any], point: CGPoint)
func didLongPress(view: StyledTextView, attributes: [NSAttributedString.Key: Any], point: CGPoint)
func didTap(view: StyledTextView, attributes: NSAttributedStringAttributesType, point: CGPoint)
func didLongPress(view: StyledTextView, attributes: NSAttributedStringAttributesType, point: CGPoint)
}

open class StyledTextView: UIView {
Expand Down
26 changes: 2 additions & 24 deletions Source/TextStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ public struct TextStyle: Hashable, Equatable {

public let font: Font
public let size: CGFloat
public let attributes: [NSAttributedString.Key: Any]
public let attributes: NSAttributedStringAttributesType
public let minSize: CGFloat
public let maxSize: CGFloat
public let scalingTextStyle: UIFont.TextStyle

public init(
font: Font = .system(.default),
size: CGFloat = UIFont.systemFontSize,
attributes: [NSAttributedString.Key: Any] = [:],
attributes: NSAttributedStringAttributesType = [:],
minSize: CGFloat = 0,
maxSize: CGFloat = .greatestFiniteMagnitude,
scalingTextStyle: UIFont.TextStyle = .body
Expand Down Expand Up @@ -64,26 +64,4 @@ public struct TextStyle: Hashable, Equatable {
}
}

// MARK: Hashable

public func hash(into hasher: inout Hasher) {
hasher.combine(font)
hasher.combine(size)
hasher.combine(attributes.count)
hasher.combine(minSize)
hasher.combine(maxSize)
hasher.combine(scalingTextStyle)
}

// MARK: Equatable

public static func ==(lhs: TextStyle, rhs: TextStyle) -> Bool {
return lhs.size == rhs.size
&& lhs.minSize == rhs.minSize
&& rhs.maxSize == rhs.maxSize
&& lhs.font == rhs.font
&& lhs.scalingTextStyle == rhs.scalingTextStyle
&& NSDictionary(dictionary: lhs.attributes).isEqual(to: rhs.attributes)
}

}
4 changes: 4 additions & 0 deletions StyledTextKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
29BAEE4920C339D100F283EA /* StyledTextBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29BAEE3420C339D100F283EA /* StyledTextBuilder.swift */; };
29BAEE4B20C339D100F283EA /* StyledText.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29BAEE3620C339D100F283EA /* StyledText.swift */; };
29BAEE4C20C339D100F283EA /* StyledTextView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29BAEE3720C339D100F283EA /* StyledTextView.swift */; };
29D7C5E822AD797E005BDC17 /* NSAttributedStringAttributesType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29D7C5E722AD797E005BDC17 /* NSAttributedStringAttributesType.swift */; };
3D4040F520F40F2B00ED4B3D /* StyledTextBuilderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29BAEE1220C339C800F283EA /* StyledTextBuilderTests.swift */; };
3D931C8520F412FC0090FFFB /* StyledTextRendererSnapshotTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29BAEE0F20C339C800F283EA /* StyledTextRendererSnapshotTests.swift */; };
3D931C8620F412FF0090FFFB /* StyledTextTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29BAEE0E20C339C800F283EA /* StyledTextTests.swift */; };
Expand Down Expand Up @@ -125,6 +126,7 @@
29BAEE3620C339D100F283EA /* StyledText.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StyledText.swift; sourceTree = "<group>"; };
29BAEE3720C339D100F283EA /* StyledTextView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StyledTextView.swift; sourceTree = "<group>"; };
29BAEE4D20C33A3F00F283EA /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
29D7C5E722AD797E005BDC17 /* NSAttributedStringAttributesType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSAttributedStringAttributesType.swift; sourceTree = "<group>"; };
5DA9D30284894EF619A71075 /* Pods-Tests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Tests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Tests/Pods-Tests.debug.xcconfig"; sourceTree = "<group>"; };
660B43ECDF42D84142029C1F /* Pods-Tests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Tests.release.xcconfig"; path = "Pods/Target Support Files/Pods-Tests/Pods-Tests.release.xcconfig"; sourceTree = "<group>"; };
73D1CEE14004B5BBA25A669D /* Pods_Tests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Tests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -311,6 +313,7 @@
29BAEE2D20C339D100F283EA /* TextStyle.swift */,
29BAEE2620C339D100F283EA /* UIContentSizeCategory+Scaling.swift */,
29BAEE2C20C339D100F283EA /* UIScreen+Static.swift */,
29D7C5E722AD797E005BDC17 /* NSAttributedStringAttributesType.swift */,
);
path = Source;
sourceTree = "<group>";
Expand Down Expand Up @@ -595,6 +598,7 @@
29BAEE4C20C339D100F283EA /* StyledTextView.swift in Sources */,
29BAEE3F20C339D100F283EA /* NSLayoutManager+Render.swift in Sources */,
29BAEE4020C339D100F283EA /* CGSize+Utility.swift in Sources */,
29D7C5E822AD797E005BDC17 /* NSAttributedStringAttributesType.swift in Sources */,
29BAEE3920C339D100F283EA /* CGSize+LRUCachable.swift in Sources */,
29BAEE4120C339D100F283EA /* UIScreen+Static.swift in Sources */,
29BAEE3A20C339D100F283EA /* NSAttributedStringKey+StyledText.swift in Sources */,
Expand Down
5 changes: 3 additions & 2 deletions UITestsApp/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ class ViewController: UIViewController {

extension ViewController: StyledTextViewDelegate {

func didTap(view: StyledTextView, attributes: [NSAttributedString.Key : Any], point: CGPoint) {
func didTap(view: StyledTextView, attributes: NSAttributedStringAttributesType, point: CGPoint) {
guard let linkContent = attributes[.highlight] as? String else { return }
stateLabel.text = "didTap: \(linkContent)"
}

func didLongPress(view: StyledTextView, attributes: [NSAttributedString.Key : Any], point: CGPoint) {
func didLongPress(view: StyledTextView, attributes: NSAttributedStringAttributesType, point: CGPoint) {
guard let linkContent = attributes[.highlight] as? String else { return }
stateLabel.text = "didLongPress: \(linkContent)"
}

}

0 comments on commit bfd0f4f

Please sign in to comment.