Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
Merge branch 'master' of github.com:appcraftstudio/buymeacoffee
Browse files Browse the repository at this point in the history
  • Loading branch information
frboulais committed Sep 27, 2021
2 parents a020813 + eacbf9c commit 20c574a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ BMCManager.shared.thankYouMessage = "Thank you for supporting 🎉 App Craft Stu
4. Add a `BMCButton` to your storyboard, XIB file, or instantiate it programmatically. To add the button to your storyboard or XIB file, add a View and set its custom class to `BMCButton`.
5. **Optional**: If you want to customize the button, do the following:
```swift
let button = BMCButton(configuration: .default)
button.configuraton = .init(color: .orange, font: .cookie)
let configuration = BMCButton.Configuration(color: .orange, font: .cookie)
let button = BMCButton(configuration: configuration)
// or set the burtton configuration later
button.configure(with: configuration)
```

<p align="center">
Expand Down
46 changes: 22 additions & 24 deletions Sources/BuyMeACoffee/BMCButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ public class BMCButton: UIButton {
return numberFormatter
}()

public var configuration: Configuration = .default {
didSet {
configure(with: configuration)
}
}
private var _configuration: Configuration = .default

public override var isHighlighted: Bool {
didSet {
Expand All @@ -45,7 +41,7 @@ public class BMCButton: UIButton {
public convenience init(configuration: Configuration) {
self.init(type: .custom)

self.configuration = configuration
self._configuration = configuration
}

override init(frame: CGRect) {
Expand All @@ -68,6 +64,25 @@ public class BMCButton: UIButton {
setup()
}

// MARK: - Public functions

public func configure(with configuration: Configuration) {
titleLabel?.font = configuration.font.value
setTitleColor(configuration.color.title, for: .normal)
setImage(configuration.color.cup, for: .normal)
backgroundColor = configuration.color.background

var title = configuration.title
if let product = BMCManager.shared.product {
numberFormatter.locale = product.priceLocale
if let price = numberFormatter.string(from: product.price) {
title.append(" (\(price))")
}
}

setTitle(title, for: .normal)
}

// MARK: - Private functions

private func setup() {
Expand All @@ -89,24 +104,7 @@ public class BMCButton: UIButton {

addTarget(BMCManager.shared, action: #selector(BMCManager.shared.start), for: .touchUpInside)

configure(with: configuration)
}

private func configure(with configuration: Configuration) {
titleLabel?.font = configuration.font.value
setTitleColor(configuration.color.title, for: .normal)
setImage(configuration.color.cup, for: .normal)
backgroundColor = configuration.color.background

var title = configuration.title
if let product = BMCManager.shared.product {
numberFormatter.locale = product.priceLocale
if let price = numberFormatter.string(from: product.price) {
title.append(" (\(price))")
}
}

setTitle(title, for: .normal)
configure(with: _configuration)
}

private func registerFonts() {
Expand Down
2 changes: 1 addition & 1 deletion Tests/buymeacoffeeTests/BuyMeACoffeeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class BuyMeACoffeeTests: XCTestCase {
}

let button = BMCButton(frame: .init(x: 0, y: 0, width: 200, height: 50))
button.configuration = .default
button.configure(with: .default)

if let data = button.snapshot().pngData() {
let url = url.appendingPathComponent("snapshot-bmc-button").appendingPathExtension("png")
Expand Down

0 comments on commit 20c574a

Please sign in to comment.