Skip to content

Commit

Permalink
Merge pull request #796 from kiwicom/release/rc
Browse files Browse the repository at this point in the history
Release fixes
  • Loading branch information
PavelHolec authored Apr 16, 2024
2 parents c9c93d6 + 4857049 commit e979822
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 69 deletions.
Binary file modified Snapshots/iPhone/SwitchTests/testSwitches.1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 17 additions & 15 deletions Sources/Orbit/Components/Heading.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@ public struct Heading: View, FormattedTextBuildable, PotentiallyEmptyView {
public var isEmpty: Bool {
content.isEmpty
}

private init(
text: Text,
style: Style
) {
self.content = text
self.style = style

// Set default weight
self.fontWeight = style.weight
}
}

// MARK: - Inits
Expand All @@ -117,11 +128,7 @@ public extension Heading {
_ content: some StringProtocol = String(""),
style: Style
) {
self.content = Text(content)
self.style = style

// Set default weight
self.fontWeight = style.weight
self.init(text: Text(content), style: style)
}

/// Creates Orbit ``Heading`` component using localizable key.
Expand All @@ -133,11 +140,7 @@ public extension Heading {
bundle: Bundle? = nil,
comment: StaticString? = nil
) {
self.content = Text(keyAndValue, tableName: tableName, bundle: bundle)
self.style = style

// Set default weight
self.fontWeight = style.weight
self.init(text: Text(keyAndValue, tableName: tableName, bundle: bundle), style: style)
}
}

Expand Down Expand Up @@ -196,11 +199,10 @@ public extension Heading {
// MARK: - TextRepresentable
extension Heading: TextRepresentable {

public func swiftUIText(textRepresentableEnvironment: TextRepresentableEnvironment) -> SwiftUI.Text? {
if content.isEmpty { return nil }

return textContent
.text(textRepresentableEnvironment: textRepresentableEnvironment)
public func text(environment: TextRepresentableEnvironment) -> SwiftUI.Text? {
content.isEmpty
? nil
: textContent.text(environment: environment)
}
}

Expand Down
8 changes: 5 additions & 3 deletions Sources/Orbit/Components/Icon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public struct Icon: View, TextBuildable, PotentiallyEmptyView {
@Environment(\.iconColor) private var iconColor
@Environment(\.iconSize) private var iconSize
@Environment(\.locale) private var locale
@Environment(\.localizationBundle) private var localizationBundle
@Environment(\.textColor) private var textColor
@Environment(\.textFontWeight) private var textFontWeight
@Environment(\.textSize) private var textSize
Expand Down Expand Up @@ -112,6 +113,7 @@ public struct Icon: View, TextBuildable, PotentiallyEmptyView {
iconColor: iconColor,
iconSize: iconSize,
locale: locale,
localizationBundle: localizationBundle,
textAccentColor: nil,
textColor: textColor,
textFontWeight: textFontWeight,
Expand Down Expand Up @@ -224,11 +226,11 @@ private extension Icon {
// MARK: - TextRepresentable
extension Icon: TextRepresentable {

public func swiftUIText(textRepresentableEnvironment: TextRepresentableEnvironment) -> SwiftUI.Text? {
public func text(environment: TextRepresentableEnvironment) -> SwiftUI.Text? {
if #available(iOS 14.0, *) {
return text(textRepresentableEnvironment: textRepresentableEnvironment)
return text(textRepresentableEnvironment: environment)
} else {
return textFallback(textRepresentableEnvironment: textRepresentableEnvironment)
return textFallback(textRepresentableEnvironment: environment)
}
}

Expand Down
3 changes: 2 additions & 1 deletion Sources/Orbit/Components/Switch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ public struct Switch<Icon: View>: View {
Rectangle()
.frame(width: 50, height: 32)
.foregroundColor(tint)
.opacity(isEnabled ? 1 : 0.3)
.overlay(
knob,
alignment: isOn ? .trailing : .leading
)
.opacity(isEnabled ? 1 : 0.3)
.animation(.easeInOut(duration: 0.15), value: isOn)
// FIXME: Replace with a SwitchButtonStyle
.accessibility(addTraits: [.isButton])
Expand Down Expand Up @@ -72,6 +72,7 @@ public struct Switch<Icon: View>: View {
icon
.iconSize(.small)
.iconColor(iconTint)
.opacity(isEnabled ? 1 : 0.3)
.environment(\.sizeCategory, .large)
.animation(.easeIn(duration: 0.15), value: isOn)
}
Expand Down
100 changes: 57 additions & 43 deletions Sources/Orbit/Components/Text.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ import SwiftUI
/// When the provided content is empty, the component results in `EmptyView` so that it does not take up any space in the layout.
///
/// - Note: [Orbit.kiwi documentation](https://orbit.kiwi/components/text/)
public struct Text: View, FormattedTextBuildable, PotentiallyEmptyView {
public struct Text: View, FormattedTextBuildable, PotentiallyEmptyView, CustomStringConvertible {

@Environment(\.multilineTextAlignment) private var multilineTextAlignment
@Environment(\.lineSpacing) private var lineSpacing
Expand All @@ -99,7 +99,7 @@ public struct Text: View, FormattedTextBuildable, PotentiallyEmptyView {
@Environment(\.textSize) private var textSize
@Environment(\.sizeCategory) private var sizeCategory

private let verbatimContent: String
public let description: String

// Localization
private let localization: TextLocalization?
Expand All @@ -120,66 +120,66 @@ public struct Text: View, FormattedTextBuildable, PotentiallyEmptyView {

public var body: some View {
if isEmpty == false {
text(textRepresentableEnvironment: textRepresentableEnvironment)
text(environment: textRepresentableEnvironment, showTextLinks: false)
.lineSpacing(lineSpacingAdjusted)
.overlay(copyableText)
// If the text contains links, the TextLink overlay takes accessibility priority
.accessibility(hidden: content(textRepresentableEnvironment.locale).containsTextLinks)
.accessibility(hidden: content(textRepresentableEnvironment).containsTextLinks)
.overlay(textLinks)
.padding(.vertical, textRepresentableEnvironment.lineHeightPadding(lineHeight: lineHeight, size: size))
.fixedSize(horizontal: false, vertical: true)
}
}

@ViewBuilder private var textLinks: some View {
if content(locale).containsTextLinks {
TextLink(textLinkAttributedString(textRepresentableEnvironment: textRepresentableEnvironment))
if content(textRepresentableEnvironment).containsTextLinks {
TextLink(textLinkAttributedString(environment: textRepresentableEnvironment))
}
}

@ViewBuilder private var copyableText: some View {
if textIsCopyable {
CopyableText(
attributedString(textRepresentableEnvironment: textRepresentableEnvironment).string
attributedString(environment: textRepresentableEnvironment).string
)
}
}

var isEmpty: Bool {
verbatimContent.isEmpty
description.isEmpty
}

func text(textRepresentableEnvironment: TextRepresentableEnvironment, isConcatenated: Bool = false) -> SwiftUI.Text {
if content(textRepresentableEnvironment.locale).containsHtmlFormatting {
func text(environment: TextRepresentableEnvironment, showTextLinks: Bool = false) -> SwiftUI.Text {
if content(environment).containsHtmlFormatting {
return modifierWrapper(
SwiftUI.Text(
attributedString(
textRepresentableEnvironment: textRepresentableEnvironment,
isConcatenated: isConcatenated
environment: environment,
showTextLinks: showTextLinks
)
)
)
} else {
return modifierWrapper(
fontWeightWrapper(
boldWrapper(
SwiftUI.Text(verbatim: content(textRepresentableEnvironment.locale))
.foregroundColor(textRepresentableEnvironment.resolvedColor(color))
SwiftUI.Text(verbatim: content(environment))
.foregroundColor(environment.resolvedColor(color))
)
)
)
.font(textRepresentableEnvironment.font(size: size, weight: fontWeight, isBold: isBold))
.font(environment.font(size: size, weight: fontWeight, isBold: isBold))
}
}

private func content(_ locale: Locale) -> String {
private func content(_ environment: TextRepresentableEnvironment) -> String {
switch localization {
case .key(let localizedStringKey, let bundle, let tableName, let explicitKey):
localizedStringKey.localized(locale: locale, bundle: bundle ?? localizationBundle, tableName: tableName, explicitKey: explicitKey) ?? verbatimContent
localizedStringKey.localized(locale: environment.locale, bundle: bundle ?? environment.localizationBundle, tableName: tableName, explicitKey: explicitKey) ?? description
case .resource(let resource):
resolvedLocalizedStringResource(resource, locale: locale) ?? verbatimContent
resolvedLocalizedStringResource(resource, locale: environment.locale) ?? description
case nil:
verbatimContent
description
}
}

Expand All @@ -200,6 +200,7 @@ public struct Text: View, FormattedTextBuildable, PotentiallyEmptyView {
iconColor: nil,
iconSize: nil,
locale: locale,
localizationBundle: localizationBundle,
textAccentColor: textAccentColor,
textColor: textColor,
textFontWeight: textFontWeight,
Expand Down Expand Up @@ -308,13 +309,13 @@ public struct Text: View, FormattedTextBuildable, PotentiallyEmptyView {
}

private func textLinkAttributedString(
textRepresentableEnvironment: TextRepresentableEnvironment
environment: TextRepresentableEnvironment
) -> NSAttributedString {
TagAttributedStringBuilder.all.attributedString(
content(textRepresentableEnvironment.locale),
content(environment),
alignment: multilineTextAlignment,
fontSize: textRepresentableEnvironment.scaledSize(size),
fontWeight: textRepresentableEnvironment.resolvedFontWeight(fontWeight, isBold: isBold),
fontSize: environment.scaledSize(size),
fontWeight: environment.resolvedFontWeight(fontWeight, isBold: isBold),
lineSpacing: lineSpacingAdjusted,
kerning: kerning,
strikethrough: strikethrough ?? false,
Expand All @@ -324,25 +325,30 @@ public struct Text: View, FormattedTextBuildable, PotentiallyEmptyView {
}

private func attributedString(
textRepresentableEnvironment: TextRepresentableEnvironment,
isConcatenated: Bool = false
environment: TextRepresentableEnvironment,
showTextLinks: Bool = false
) -> NSAttributedString {
TagAttributedStringBuilder.all.attributedString(
content(textRepresentableEnvironment.locale),
content(environment),
alignment: multilineTextAlignment,
fontSize: textRepresentableEnvironment.scaledSize(size),
fontWeight: textRepresentableEnvironment.resolvedFontWeight(fontWeight, isBold: isBold),
fontSize: environment.scaledSize(size),
fontWeight: environment.resolvedFontWeight(fontWeight, isBold: isBold),
lineSpacing: lineSpacingAdjusted,
kerning: kerning,
color: textRepresentableEnvironment.resolvedColor(color).uiColor,
linkColor: isConcatenated ? nil : .clear,
accentColor: textRepresentableEnvironment.resolvedAccentColor(accentColor, color: color).uiColor
color: environment.resolvedColor(color).uiColor,
linkColor: showTextLinks ? nil : .clear,
accentColor: environment.resolvedAccentColor(accentColor, color: color).uiColor
)
}

private var lineSpacingAdjusted: CGFloat {
textRepresentableEnvironment.lineSpacingAdjusted(lineSpacing, lineHeight: lineHeight, size: size)
}

private init(localization: TextLocalization?, description: String) {
self.localization = localization
self.description = description
}
}

// MARK: - Inits (Localized)
Expand All @@ -366,8 +372,10 @@ public extension Text {
bundle: Bundle? = nil,
comment: StaticString? = nil
) {
self.localization = .key(value, bundle: bundle, tableName: tableName, explicitKey: key.description)
self.verbatimContent = String(describing: key)
self.init(
localization: .key(value, bundle: bundle, tableName: tableName, explicitKey: key.description),
description: String(describing: key)
)
}

/// Creates Orbit ``Text`` component that displays a localized content identified by a key.
Expand All @@ -385,8 +393,10 @@ public extension Text {
bundle: Bundle? = nil,
comment: StaticString? = nil
) {
self.localization = .key(keyAndValue, bundle: bundle, tableName: tableName)
self.verbatimContent = keyAndValue.key ?? ""
self.init(
localization: .key(keyAndValue, bundle: bundle, tableName: tableName),
description: keyAndValue.key ?? ""
)
}
}

Expand All @@ -400,8 +410,10 @@ public extension Text {
@_disfavoredOverload
@_semantics("swiftui.init_with_localization")
init(_ resource: LocalizedStringResource) {
self.localization = .resource(resource)
self.verbatimContent = resource.key
self.init(
localization: .resource(resource),
description: resource.key
)
}
}

Expand All @@ -420,8 +432,10 @@ public extension Text {
///
/// - Parameter resource: A string to display without localization.
init(verbatim content: String) {
self.localization = nil
self.verbatimContent = content
self.init(
localization: nil,
description: content
)
}
}

Expand Down Expand Up @@ -479,10 +493,10 @@ enum TextLocalization {
// MARK: - TextRepresentable
extension Text: TextRepresentable {

public func swiftUIText(textRepresentableEnvironment: TextRepresentableEnvironment) -> SwiftUI.Text? {
if isEmpty { return nil }

return text(textRepresentableEnvironment: textRepresentableEnvironment, isConcatenated: true)
public func text(environment: TextRepresentableEnvironment) -> SwiftUI.Text? {
isEmpty
? nil
: text(environment: environment, showTextLinks: true)
}
}

Expand Down
1 change: 1 addition & 0 deletions Sources/Orbit/Support/ButtonStyles/TagButtonStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public struct TagButtonStyle: ButtonStyle {
}
.iconColor(resolvedIconColor(isPressed: configuration.isPressed))
.iconSize(.small)
.accessibility(addTraits: .isButton)
}
}
.textFontWeight(.medium)
Expand Down
12 changes: 7 additions & 5 deletions Sources/Orbit/Support/Text/ConcatenatedText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ struct ConcatenatedText: View {
@Environment(\.iconSize) var iconSize
@Environment(\.lineSpacing) var lineSpacing
@Environment(\.locale) var locale
@Environment(\.localizationBundle) var localizationBundle
@Environment(\.sizeCategory) var sizeCategory
@Environment(\.textAccentColor) var textAccentColor
@Environment(\.textColor) var textColor
Expand Down Expand Up @@ -34,6 +35,7 @@ struct ConcatenatedText: View {
iconColor: iconColor,
iconSize: iconSize,
locale: locale,
localizationBundle: localizationBundle,
textAccentColor: textAccentColor,
textColor: textColor,
textFontWeight: textFontWeight,
Expand All @@ -46,18 +48,18 @@ struct ConcatenatedText: View {

extension ConcatenatedText: TextRepresentable {

func swiftUIText(textRepresentableEnvironment: TextRepresentableEnvironment) -> SwiftUI.Text? {
content(textRepresentableEnvironment)
func text(environment: TextRepresentableEnvironment) -> SwiftUI.Text? {
content(environment)
}
}

extension ConcatenatedText {

init(_ TextRepresentable: TextRepresentable) {
if let concatenatedText = TextRepresentable as? ConcatenatedText {
init(_ textRepresentable: TextRepresentable) {
if let concatenatedText = textRepresentable as? ConcatenatedText {
self = concatenatedText
} else {
self.init(content: TextRepresentable.swiftUIText)
self.init(content: textRepresentable.text)
}
}
}
Expand Down
Loading

0 comments on commit e979822

Please sign in to comment.