Files
purchases-ios/Examples/MagicWeatherSwiftUI/Shared/Sources/Helpers/Extensions.swift
T
Joseph Kokenge b91dd19c92 SwiftUI Sample Project: Refactor Package terms method to a computed property (#2405)
Note: Not sure if PRs are wanted on Example code.

### Checklist
- [x] If applicable, unit tests
- [ ] If applicable, create follow-up issues for `purchases-android` and
hybrids

### Motivation
Noticed that the terms func was just passing the same `package`.

### Description
Testing: Looks like there are no tests for sample code.

This pull request refactors the terms(for package: Package) method in
the Package extension to a computed property terms. The change not only
improves readability and simplifies access to the package terms, but
also eliminates the need to pass a Package instance to a function that
belongs to the same Package type, which didn't make sense.
By making these changes, we provide a more streamlined and intuitive
approach for developers to access package terms, and eliminate the need
for an explicit method call.
2023-04-13 09:43:10 -03:00

45 lines
1.1 KiB
Swift

//
// Extensions.swift
// Magic Weather SwiftUI
//
// Created by Cody Kerns on 1/26/21.
//
import Foundation
import RevenueCat
import StoreKit
/* Some methods to make displaying subscription terms easier */
extension Package {
var terms: String {
if let intro = self.storeProduct.introductoryDiscount {
if intro.price == 0 {
return "\(intro.subscriptionPeriod.periodTitle) free trial"
} else {
return "\(self.localizedIntroductoryPriceString!) for \(intro.subscriptionPeriod.periodTitle)"
}
} else {
return "Unlocks Premium"
}
}
}
extension SubscriptionPeriod {
var durationTitle: String {
switch self.unit {
case .day: return "day"
case .week: return "week"
case .month: return "month"
case .year: return "year"
@unknown default: return "Unknown"
}
}
var periodTitle: String {
let periodString = "\(self.value) \(self.durationTitle)"
let pluralized = self.value > 1 ? periodString + "s" : periodString
return pluralized
}
}