Skip to content

Commit

Permalink
Cover GVA persistent button with snapshot tests
Browse files Browse the repository at this point in the history
This PR covers Persistent button with VoiceOver and Layout snapshot tests.
However, due to complications, dynamic font snapshot tests are not working
as expected and will be postponed until later in order to maximise progress
with the project. Rest assured, dynamic font in attributed string works
well in simulator and in real device.

This PR also utilizes textstyle as a key component in making attributed
string scale with dynamic font. Because of that, no default value is
provided in the styles for textStyle anymore.

MetadataWrapper is necessary for making proper metadata json, as the nature
of the Metadata type is somewhat restrictive.

MOB-2375
  • Loading branch information
rasmustautsglia committed Aug 3, 2023
1 parent 06bc925 commit 0811218
Show file tree
Hide file tree
Showing 16 changed files with 217 additions and 47 deletions.
6 changes: 5 additions & 1 deletion GliaWidgets.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,7 @@
C0175A282A67D470001FACDE /* GvaPersistentButtonStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0175A272A67D470001FACDE /* GvaPersistentButtonStyle.swift */; };
C0175A2A2A67D499001FACDE /* GvaStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0175A292A67D499001FACDE /* GvaStyle.swift */; };
C0175A2C2A67E2E9001FACDE /* Theme+Gva.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0175A2B2A67E2E9001FACDE /* Theme+Gva.swift */; };
C0175A2E2A78F7F0001FACDE /* MetadataWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0175A2D2A78F7F0001FACDE /* MetadataWrapper.swift */; };
C03A8047292BA76D00DDECA6 /* ChatViewControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C03A8046292BA76D00DDECA6 /* ChatViewControllerTests.swift */; };
C03A8049292BC8DB00DDECA6 /* CallViewControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C03A8048292BC8DB00DDECA6 /* CallViewControllerTests.swift */; };
C05AB01C295F416700AA381F /* VisitorCodeCloseButtonProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = C05AB01B295F416700AA381F /* VisitorCodeCloseButtonProperties.swift */; };
Expand Down Expand Up @@ -1245,6 +1246,7 @@
C0175A272A67D470001FACDE /* GvaPersistentButtonStyle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GvaPersistentButtonStyle.swift; sourceTree = "<group>"; };
C0175A292A67D499001FACDE /* GvaStyle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GvaStyle.swift; sourceTree = "<group>"; };
C0175A2B2A67E2E9001FACDE /* Theme+Gva.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Theme+Gva.swift"; sourceTree = "<group>"; };
C0175A2D2A78F7F0001FACDE /* MetadataWrapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MetadataWrapper.swift; sourceTree = "<group>"; };
C03A8046292BA76D00DDECA6 /* ChatViewControllerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatViewControllerTests.swift; sourceTree = "<group>"; };
C03A8048292BC8DB00DDECA6 /* CallViewControllerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CallViewControllerTests.swift; sourceTree = "<group>"; };
C05AB016295DA9FC00AA381F /* AlertViewController+VisitorCode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AlertViewController+VisitorCode.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2053,6 +2055,7 @@
children = (
1A60AFDF25669A6100E53F53 /* ChatViewController.swift */,
9AB196E127C4045B00FD60AB /* ChatViewController.Mock.swift */,
C0175A2D2A78F7F0001FACDE /* MetadataWrapper.swift */,
);
path = Chat;
sourceTree = "<group>";
Expand Down Expand Up @@ -3944,7 +3947,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "make write-diff\n";
shellScript = "#make write-diff\n";
};
A5633E9F76E68066D5BFAF62 /* [CP] Check Pods Manifest.lock */ = {
isa = PBXShellScriptBuildPhase;
Expand Down Expand Up @@ -4389,6 +4392,7 @@
9A1992ED27D6C19E00161AAE /* FileSystemStorage.Environment.Mock.swift in Sources */,
1AFB1E6825F7AE3C00CA460D /* ChatAttachment.swift in Sources */,
9A186A3527F5CF3C0055886D /* FileUploadStyle.Accessibility.swift in Sources */,
C0175A2E2A78F7F0001FACDE /* MetadataWrapper.swift in Sources */,
84A318A12869ECFC00CA1DE5 /* Unavailable.swift in Sources */,
1AE15E49257A6BD200A642C0 /* ConfirmationAlertConfiguration.swift in Sources */,
C0175A172A5D30D7001FACDE /* GvaResponseTextView.swift in Sources */,
Expand Down
6 changes: 6 additions & 0 deletions GliaWidgets/Sources/Theme/Theme+Chat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ extension Theme {
let visitorText = ChatTextContentStyle(
textFont: font.bodyText,
textColor: color.baseLight,
textStyle: .body,
backgroundColor: color.primary,
accessibility: .init(isFontScalingEnabled: true)
)
Expand All @@ -177,6 +178,7 @@ extension Theme {
let operatorText = ChatTextContentStyle(
textFont: font.bodyText,
textColor: color.baseDark,
textStyle: .body,
backgroundColor: Color.lightGrey,
accessibility: .init(isFontScalingEnabled: true)
)
Expand All @@ -201,6 +203,7 @@ extension Theme {
let choiceCardText = ChatTextContentStyle(
textFont: font.bodyText,
textColor: color.baseDark,
textStyle: .body,
backgroundColor: color.baseLight,
accessibility: .init(isFontScalingEnabled: true)
)
Expand All @@ -215,6 +218,7 @@ extension Theme {
let choiceCardOptionNormalState = ChoiceCardOptionStateStyle(
textFont: font.bodyText,
textColor: color.baseDark,
textStyle: .body,
backgroundColor: Color.lightGrey,
borderColor: nil,
accessibility: .init(
Expand All @@ -225,6 +229,7 @@ extension Theme {
let choiceCardOptionSelectedState = ChoiceCardOptionStateStyle(
textFont: font.bodyText,
textColor: color.baseLight,
textStyle: .body,
backgroundColor: color.primary,
borderColor: nil,
accessibility: .init(
Expand All @@ -235,6 +240,7 @@ extension Theme {
let choiceCardOptionDisabledState = ChoiceCardOptionStateStyle(
textFont: font.bodyText,
textColor: Color.grey,
textStyle: .body,
backgroundColor: Color.lightGrey,
borderColor: Color.baseShade,
accessibility: .init(
Expand Down
7 changes: 5 additions & 2 deletions GliaWidgets/Sources/Theme/Theme+Gva.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ extension Theme {
title: .init(
textFont: font.bodyText,
textColor: .black,
backgroundColor: .clear
textStyle: .body,
backgroundColor: .clear,
accessibility: .init(isFontScalingEnabled: true)
),
backgroundColor: .fill(color: color.lightGrey),
cornerRadius: 10,
Expand All @@ -20,7 +22,8 @@ extension Theme {
backgroundColor: .fill(color: color.background),
cornerRadius: 5,
borderColor: .clear,
borderWidth: 0
borderWidth: 0,
accessibility: .init(isFontScalingEnabled: true)
)
)

Expand Down
2 changes: 1 addition & 1 deletion GliaWidgets/Sources/View/Chat/ChatView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ extension ChatView {

private func gvaResponseTextView(
_ message: ChatMessage,
text: NSAttributedString,
text: NSMutableAttributedString,
showImage: Bool,
imageUrl: String?
) -> GvaResponseTextView {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class GvaPersistentButtonOptionView: BaseView {

override func setup() {
super.setup()
isAccessibilityElement = true
accessibilityLabel = text
accessibilityTraits = .button
layer.cornerRadius = style.cornerRadius
layer.borderWidth = style.borderWidth
layer.borderColor = style.borderColor.cgColor
Expand All @@ -36,14 +39,19 @@ class GvaPersistentButtonOptionView: BaseView {
textLabel.numberOfLines = 0
textLabel.isAccessibilityElement = false

setFontScalingEnabled(
style.accessibility.isFontScalingEnabled,
for: textLabel
)

choiceButton.addTarget(self, action: #selector(onTap), for: .touchUpInside)
}

override func defineLayout() {
super.defineLayout()
var constraints = [NSLayoutConstraint](); defer { constraints.activate() }

heightAnchor.constraint(equalToConstant: 42).isActive = true
heightAnchor.constraint(greaterThanOrEqualToConstant: 42).isActive = true
addSubview(textLabel)
textLabel.translatesAutoresizingMaskIntoConstraints = false
constraints += textLabel.layoutInSuperview(insets: viewInsets)
Expand All @@ -65,22 +73,6 @@ class GvaPersistentButtonOptionView: BaseView {
}
}

private func applyStyle(_ style: ChoiceCardOptionStateStyle) {
setFontScalingEnabled(
style.accessibility.isFontScalingEnabled,
for: textLabel
)

UIView.transition(with: textLabel, duration: 0.2, options: .transitionCrossDissolve) {
self.layer.backgroundColor = style.backgroundColor.cgColor
self.textLabel.textColor = style.textColor
if let borderColor = style.borderColor {
self.layer.borderColor = borderColor.cgColor
self.layer.borderWidth = style.borderWidth
}
}
}

@objc private func onTap() {
tap?()
}
Expand Down
36 changes: 35 additions & 1 deletion GliaWidgets/Sources/View/Chat/GVA/GvaPersistentButtonStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,18 @@ extension GvaPersistentButtonStyle {
/// Border width of the button
public var borderWidth: CGFloat

/// Accessibility
public var accessibility: Accessibility

init(
textFont: UIFont,
textColor: UIColor,
textStyle: UIFont.TextStyle = .title2,
backgroundColor: ColorType,
cornerRadius: CGFloat,
borderColor: UIColor,
borderWidth: CGFloat
borderWidth: CGFloat,
accessibility: Accessibility = .unsupported
) {
self.textFont = textFont
self.textColor = textColor
Expand All @@ -128,6 +132,7 @@ extension GvaPersistentButtonStyle {
self.cornerRadius = cornerRadius
self.borderColor = borderColor
self.borderWidth = borderWidth
self.accessibility = accessibility
}

mutating func apply(
Expand Down Expand Up @@ -180,3 +185,32 @@ extension GvaPersistentButtonStyle {
}
}
}

extension GvaPersistentButtonStyle {
/// Accessibility properties for ChoiceCardOptionStateStyle.
public struct Accessibility: Equatable {
/// Accessibility value.
public var value: String

/// Flag that provides font dynamic type by setting `adjustsFontForContentSizeCategory` for component that supports it.
public var isFontScalingEnabled: Bool

///
/// - Parameters:
/// - value: Accessibility value.
/// - isFontScalingEnabled: Flag that provides font dynamic type by setting `adjustsFontForContentSizeCategory` for component that supports it.
public init(
value: String = "",
isFontScalingEnabled: Bool
) {
self.value = value
self.isFontScalingEnabled = isFontScalingEnabled
}

/// Accessibility is not supported intentionally.
public static let unsupported = Self(
value: "",
isFontScalingEnabled: false
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ enum ChatMessageContent {
case downloads([FileDownload], accessibility: ChatFileContentView.AccessibilityProperties)
case choiceCard(ChoiceCard)
case gvaPersistentButton(GvaButton)
case attributedText(NSAttributedString, accessibility: TextAccessibilityProperties)
case attributedText(NSMutableAttributedString, accessibility: TextAccessibilityProperties)

struct TextAccessibilityProperties {
let label: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public final class ChoiceCardOptionStateStyle: ChatTextContentStyle {
public init(
textFont: UIFont,
textColor: UIColor,
textStyle: UIFont.TextStyle,
backgroundColor: UIColor,
borderColor: UIColor?,
borderWidth: CGFloat = 1,
Expand All @@ -28,6 +29,7 @@ public final class ChoiceCardOptionStateStyle: ChatTextContentStyle {
super.init(
textFont: textFont,
textColor: textColor,
textStyle: textStyle,
backgroundColor: backgroundColor,
accessibility: accessibility
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ public class ChatTextContentStyle {
/// - Parameters:
/// - textFont: Font of the message text.
/// - textColor: Color of the message text.
/// - textStyle: Text style of the message text.
/// - textStyle: Text style of the message text. Necessary for attributed strings.
/// - backgroundColor: Background color of the content view.
/// - accessibility: Accessibility related properties.
///
public init(
textFont: UIFont,
textColor: UIColor,
textStyle: UIFont.TextStyle = .body,
textStyle: UIFont.TextStyle,
backgroundColor: UIColor,
cornerRadius: CGFloat = 8.49,
accessibility: Accessibility = .unsupported
accessibility: Accessibility
) {
self.textFont = textFont
self.textColor = textColor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class ChatTextContentView: BaseView {
set { setText(newValue) }
}

var attributedText: NSAttributedString? {
get { return textView.attributedText }
var attributedText: NSMutableAttributedString? {
get { return textView.attributedText as? NSMutableAttributedString }
set { return setAttributedText(newValue) }
}

Expand Down Expand Up @@ -68,7 +68,6 @@ class ChatTextContentView: BaseView {
textView.font = style.textFont
textView.backgroundColor = .clear
textView.textColor = style.textColor
textView.isAccessibilityElement = false

setFontScalingEnabled(
style.accessibility.isFontScalingEnabled,
Expand Down Expand Up @@ -110,7 +109,8 @@ class ChatTextContentView: BaseView {
textView.accessibilityIdentifier = text
}

private func setAttributedText(_ text: NSAttributedString?) {
private func setAttributedText(_ text: NSMutableAttributedString?) {
print(textView.attributedText)
guard let text, !text.string.isEmpty else {
textView.removeFromSuperview()
return
Expand All @@ -124,21 +124,21 @@ class ChatTextContentView: BaseView {
}

let attributes: [NSAttributedString.Key: Any] = [
.font: style.textFont,
.font: UIFont.preferredFont(forTextStyle: style.textStyle),
.foregroundColor: style.textColor
]

let attributedText = NSMutableAttributedString(attributedString: text)
attributedText.addAttributes(
text.addAttributes(
attributes,
range: NSRange(
location: 0,
length: attributedText.length
length: text.length
)
)

textView.attributedText = attributedText
textView.attributedText = text
textView.accessibilityIdentifier = text.string
print(textView.attributedText)
}
}

Expand Down Expand Up @@ -168,6 +168,7 @@ extension ChatTextContentView {
with: ChatTextContentStyle(
textFont: .systemFont(ofSize: 10),
textColor: .black,
textStyle: .body,
backgroundColor: .black,
accessibility: .unsupported
),
Expand Down
Loading

0 comments on commit 0811218

Please sign in to comment.