Skip to content

Commit

Permalink
more docs, theming
Browse files Browse the repository at this point in the history
  • Loading branch information
armcknight committed Nov 14, 2024
1 parent ed478f5 commit f24bd66
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 49 deletions.
12 changes: 7 additions & 5 deletions Samples/iOS-Swift/iOS-Swift/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
uiForm.messagePlaceholder = "Describe the nature of the jank. Its essence, if you will."
}
config.configureTheme = { theme in
let fontSize: CGFloat = 25

let fontFamily: String
if Locale.current.languageCode == "ar" { // arabic; ar_EG
fontFamily = "Damascus"
Expand All @@ -218,15 +216,19 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
} else {
fontFamily = "ChalkboardSE-Regular"
}
theme.font = UIFont(name: fontFamily, size: fontSize) ?? UIFont.systemFont(ofSize: fontSize)
theme.fontFamily = fontFamily
theme.outlineStyle = .init(outlineColor: .purple)
theme.foreground = .purple
theme.background = .purple.withAlphaComponent(0.1)
theme.background = .init(red: 0.95, green: 0.9, blue: 0.95, alpha: 1)
theme.submitBackground = .orange
theme.submitForeground = .purple
theme.buttonBackground = .purple
theme.buttonForeground = .white
}
config.onSubmitSuccess = { info in
let name = info["name"] ?? "$shakespearean_insult_name"
let alert = UIAlertController(title: "Thanks?", message: "We have enough jank of our own, we really didn't need yours too, \(name).", preferredStyle: .alert)
alert.addAction(.init(title: "Derp", style: .default))
alert.addAction(.init(title: "Deal with it 🕶️", style: .default))
self.window?.rootViewController?.present(alert, animated: true)
}
config.onSubmitError = { error in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,47 @@ import UIKit
@available(iOS 13.0, *)
@objcMembers
public class SentryUserFeedbackThemeConfiguration: NSObject {
lazy var defaultFont = UIFont.preferredFont(forTextStyle: .callout)

lazy var defaultTitleFont = UIFont.preferredFont(forTextStyle: .title1)
/**
* The font family to use for form text elements.
* - note: Defaults to the system default, if this property is `nil`.
*/
public lazy var fontFamily: String? = nil

lazy var defaultHeadingFont = UIFont.preferredFont(forTextStyle: .headline)
/**
* Font for form input elements.
* - note: Defaults to `UIFont.TextStyle.callout`.
*/
lazy var font = scaledFont(style: .callout)

/**
* The default font to use.
* - note: Defaults to the current system default.
* Font for main header title of the feedback form.
* - note: Defaults to `UIFont.TextStyle.title1`.
*/
public lazy var font = defaultFont
lazy var headerFont = scaledFont(style: .title1)

public lazy var titleFont = defaultTitleFont
/**
* Font for titles of text fields and buttons in the form.
* - note: Defaults to `UIFont.TextStyle.headline`.
*/
lazy var titleFont = scaledFont(style: .headline)

public lazy var headingFont = defaultHeadingFont
/**
* Return a scaled font for the given style, using the configured font family.
*/
func scaledFont(style: UIFont.TextStyle) -> UIFont {
guard let fontFamily = fontFamily, let font = UIFont(name: fontFamily, size: UIFont.systemFontSize) else {
return UIFont.preferredFont(forTextStyle: style)

Check warning on line 41 in Sources/Swift/Integrations/UserFeedback/Configuration/SentryUserFeedbackThemeConfiguration.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Swift/Integrations/UserFeedback/Configuration/SentryUserFeedbackThemeConfiguration.swift#L41

Added line #L41 was not covered by tests
}
return UIFontMetrics(forTextStyle: style).scaledFont(for: font)
}

Check warning on line 44 in Sources/Swift/Integrations/UserFeedback/Configuration/SentryUserFeedbackThemeConfiguration.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Swift/Integrations/UserFeedback/Configuration/SentryUserFeedbackThemeConfiguration.swift#L44

Added line #L44 was not covered by tests

/**
* Helps respond to dynamic font size changes when the app is in the background, and then comes back to the foreground.
*/
func updateDefaultFonts() {
defaultFont = UIFont.preferredFont(forTextStyle: .callout)
defaultTitleFont = UIFont.preferredFont(forTextStyle: .title1)
defaultHeadingFont = UIFont.preferredFont(forTextStyle: .headline)
font = defaultFont
titleFont = defaultTitleFont
headingFont = defaultHeadingFont
font = scaledFont(style: .callout)
headerFont = scaledFont(style: .title1)
titleFont = scaledFont(style: .headline)
}

Check warning on line 53 in Sources/Swift/Integrations/UserFeedback/Configuration/SentryUserFeedbackThemeConfiguration.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Swift/Integrations/UserFeedback/Configuration/SentryUserFeedbackThemeConfiguration.swift#L50-L53

Added lines #L50 - L53 were not covered by tests

/**
Expand Down Expand Up @@ -108,13 +126,19 @@ public class SentryUserFeedbackThemeConfiguration: NSObject {
}

Check warning on line 126 in Sources/Swift/Integrations/UserFeedback/Configuration/SentryUserFeedbackThemeConfiguration.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Swift/Integrations/UserFeedback/Configuration/SentryUserFeedbackThemeConfiguration.swift#L123-L126

Added lines #L123 - L126 were not covered by tests
}

// We need to keep a reference to a default instance of this for comparison purposes later. We don't use the default to give UITextFields a default style, instead, we use `UITextField.BorderStyle.roundedRect` if `SentryUserFeedbackThemeConfiguration.outlineStyle == defaultOutlineStyle`.
/**
* - note: We need to keep a reference to a default instance of this for comparison purposes later. We don't use the default to give UITextFields a default style, instead, we use `UITextField.BorderStyle.roundedRect` if `SentryUserFeedbackThemeConfiguration.outlineStyle == defaultOutlineStyle`.
*/
let defaultOutlineStyle = OutlineStyle()

// Options for styling the outline of input elements and buttons in the feedback form.
/**
* Options for styling the outline of input elements and buttons in the feedback form.
*/
public lazy var outlineStyle: OutlineStyle = defaultOutlineStyle

// The background color to use for text inputs in the feedback form.
/**
* Background color to use for text inputs in the feedback form.
*/
public var inputBackground: UIColor = UIColor.secondarySystemBackground
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,16 @@ class SentryUserFeedbackForm: UIViewController {
weak var delegate: (any SentryUserFeedbackFormDelegate)?

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
if config.theme.font == config.theme.defaultFont {
config.theme.updateDefaultFonts()
config.recalculateScaleFactors()
}

config.theme.updateDefaultFonts()
config.recalculateScaleFactors()
updateLayout()
}

Check warning on line 22 in Sources/Swift/Integrations/UserFeedback/SentryUserFeedbackForm.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Swift/Integrations/UserFeedback/SentryUserFeedbackForm.swift#L19-L22

Added lines #L19 - L22 were not covered by tests

let formElementHeight: CGFloat = 40
let logoWidth: CGFloat = 47
lazy var messageTextViewHeightConstraint = messageTextView.heightAnchor.constraint(equalToConstant: config.theme.font.lineHeight * 5)
lazy var logoViewWidthConstraint = sentryLogoView.widthAnchor.constraint(equalToConstant: logoWidth * config.scaleFactor)
lazy var messagePlaceholderLeadingConstraint = messageTextViewPlaceholder.leadingAnchor.constraint(equalTo: messageTextView.leadingAnchor, constant: messageTextView.textContainerInset.left + 5)
lazy var messagePlaceholderTopConstraint = messageTextViewPlaceholder.topAnchor.constraint(equalTo: messageTextView.topAnchor, constant: messageTextView.textContainerInset.top)
lazy var fullNameTextFieldHeightConstraint = fullNameTextField.heightAnchor.constraint(equalToConstant: formElementHeight * config.scaleFactor)
lazy var emailTextFieldHeightConstraint = emailTextField.heightAnchor.constraint(equalToConstant: formElementHeight * config.scaleFactor)
lazy var addScreenshotButtonHeightConstraint = addScreenshotButton.heightAnchor.constraint(equalToConstant: formElementHeight * config.scaleFactor)
lazy var removeScreenshotButtonHeightConstraint = removeScreenshotButton.heightAnchor.constraint(equalToConstant: formElementHeight * config.scaleFactor)
lazy var submitButtonHeightConstraint = submitButton.heightAnchor.constraint(equalToConstant: formElementHeight * config.scaleFactor)
lazy var cancelButtonHeightConstraint = cancelButton.heightAnchor.constraint(equalToConstant: formElementHeight * config.scaleFactor)

init(config: SentryUserFeedbackConfiguration, delegate: any SentryUserFeedbackFormDelegate) {
self.config = config
self.delegate = delegate
super.init(nibName: nil, bundle: nil)
view.backgroundColor = .systemBackground

view.backgroundColor = config.theme.background

NSLayoutConstraint.activate([
scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: config.margin),
Expand Down Expand Up @@ -90,12 +73,12 @@ class SentryUserFeedbackForm: UIViewController {
}

Check warning on line 74 in Sources/Swift/Integrations/UserFeedback/SentryUserFeedbackForm.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Swift/Integrations/UserFeedback/SentryUserFeedbackForm.swift#L74

Added line #L74 was not covered by tests
[fullNameLabel, emailLabel, messageLabel].forEach {
$0.font = config.theme.headingFont
$0.font = config.theme.titleFont
$0.adjustsFontForContentSizeCategory = true

Check warning on line 77 in Sources/Swift/Integrations/UserFeedback/SentryUserFeedbackForm.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Swift/Integrations/UserFeedback/SentryUserFeedbackForm.swift#L76-L77

Added lines #L76 - L77 were not covered by tests
}

Check warning on line 79 in Sources/Swift/Integrations/UserFeedback/SentryUserFeedbackForm.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Swift/Integrations/UserFeedback/SentryUserFeedbackForm.swift#L79

Added line #L79 was not covered by tests
[submitButton, addScreenshotButton, removeScreenshotButton, cancelButton].forEach {
$0.titleLabel?.font = config.theme.headingFont
$0.titleLabel?.font = config.theme.titleFont
$0.titleLabel?.adjustsFontForContentSizeCategory = true

Check warning on line 82 in Sources/Swift/Integrations/UserFeedback/SentryUserFeedbackForm.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Swift/Integrations/UserFeedback/SentryUserFeedbackForm.swift#L81-L82

Added lines #L81 - L82 were not covered by tests
}

Check warning on line 84 in Sources/Swift/Integrations/UserFeedback/SentryUserFeedbackForm.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Swift/Integrations/UserFeedback/SentryUserFeedbackForm.swift#L84

Added line #L84 was not covered by tests
Expand Down Expand Up @@ -134,7 +117,20 @@ class SentryUserFeedbackForm: UIViewController {
delegate?.cancelled()
}

Check warning on line 118 in Sources/Swift/Integrations/UserFeedback/SentryUserFeedbackForm.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Swift/Integrations/UserFeedback/SentryUserFeedbackForm.swift#L117-L118

Added lines #L117 - L118 were not covered by tests

// MARK: UI
// MARK: Layout

let formElementHeight: CGFloat = 40
let logoWidth: CGFloat = 47
lazy var messageTextViewHeightConstraint = messageTextView.heightAnchor.constraint(equalToConstant: config.theme.font.lineHeight * 5)
lazy var logoViewWidthConstraint = sentryLogoView.widthAnchor.constraint(equalToConstant: logoWidth * config.scaleFactor)
lazy var messagePlaceholderLeadingConstraint = messageTextViewPlaceholder.leadingAnchor.constraint(equalTo: messageTextView.leadingAnchor, constant: messageTextView.textContainerInset.left + 5)
lazy var messagePlaceholderTopConstraint = messageTextViewPlaceholder.topAnchor.constraint(equalTo: messageTextView.topAnchor, constant: messageTextView.textContainerInset.top)
lazy var fullNameTextFieldHeightConstraint = fullNameTextField.heightAnchor.constraint(equalToConstant: formElementHeight * config.scaleFactor)
lazy var emailTextFieldHeightConstraint = emailTextField.heightAnchor.constraint(equalToConstant: formElementHeight * config.scaleFactor)
lazy var addScreenshotButtonHeightConstraint = addScreenshotButton.heightAnchor.constraint(equalToConstant: formElementHeight * config.scaleFactor)
lazy var removeScreenshotButtonHeightConstraint = removeScreenshotButton.heightAnchor.constraint(equalToConstant: formElementHeight * config.scaleFactor)
lazy var submitButtonHeightConstraint = submitButton.heightAnchor.constraint(equalToConstant: formElementHeight * config.scaleFactor)
lazy var cancelButtonHeightConstraint = cancelButton.heightAnchor.constraint(equalToConstant: formElementHeight * config.scaleFactor)

func updateLayout() {
let verticalPadding: CGFloat = 8
Expand All @@ -150,14 +146,14 @@ class SentryUserFeedbackForm: UIViewController {
removeScreenshotButtonHeightConstraint.constant = formElementHeight * config.scaleFactor
submitButtonHeightConstraint.constant = formElementHeight * config.scaleFactor
cancelButtonHeightConstraint.constant = formElementHeight * config.scaleFactor


}

Check warning on line 149 in Sources/Swift/Integrations/UserFeedback/SentryUserFeedbackForm.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Swift/Integrations/UserFeedback/SentryUserFeedbackForm.swift#L136-L149

Added lines #L136 - L149 were not covered by tests

// MARK: UI Elements

lazy var formTitleLabel = {
let label = UILabel(frame: .zero)
label.text = config.formConfig.formTitle
label.font = config.theme.titleFont
label.font = config.theme.headerFont
label.setContentCompressionResistancePriority(.required, for: .horizontal)
label.adjustsFontForContentSizeCategory = true
return label

Check warning on line 159 in Sources/Swift/Integrations/UserFeedback/SentryUserFeedbackForm.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Swift/Integrations/UserFeedback/SentryUserFeedbackForm.swift#L154-L159

Added lines #L154 - L159 were not covered by tests
Expand Down Expand Up @@ -321,6 +317,7 @@ class SentryUserFeedbackForm: UIViewController {
}()
}

// MARK: UITextViewDelegate
@available(iOS 13.0, *)
extension SentryUserFeedbackForm: UITextViewDelegate {
func textViewDidChange(_ textView: UITextView) {
Expand Down

0 comments on commit f24bd66

Please sign in to comment.