Supports “custom” pledge amounts for Patreon apps

This commit is contained in:
Riley Testut
2024-02-16 14:21:06 -06:00
committed by Magesh K
parent ac62612a18
commit 2f603778d6
4 changed files with 47 additions and 13 deletions

View File

@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="22225" systemVersion="22G91" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="22522" systemVersion="23D56" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<entity name="Account" representedClassName="Account" syncable="YES">
<attribute name="appleID" attributeType="String"/>
<attribute name="firstName" attributeType="String"/>
@@ -253,6 +253,7 @@
<attribute name="name" attributeType="String"/>
<attribute name="pledgeAmount" optional="YES" attributeType="Decimal"/>
<attribute name="pledgeCurrency" optional="YES" attributeType="String"/>
<attribute name="prefersCustomPledge" optional="YES" attributeType="Boolean" usesScalarValueType="YES"/>
<attribute name="screenshotURLs" attributeType="Transformable" valueTransformerName="ALTSecureValueTransformer"/>
<attribute name="size" attributeType="Integer 32" defaultValueString="0" usesScalarValueType="YES"/>
<attribute name="sortIndex" attributeType="Integer 32" defaultValueString="0" usesScalarValueType="YES"/>

View File

@@ -91,16 +91,36 @@ extension PlatformURL: Comparable {
public typealias PlatformURLs = [PlatformURL]
extension StoreApp
private struct PatreonParameters: Decodable
{
private struct PatreonParameters: Decodable
struct Pledge: Decodable
{
var pledge: Decimal?
var currency: String?
var tiers: Set<String>?
var benefit: String?
var hidden: Bool?
var amount: Decimal
var isCustom: Bool
init(from decoder: Decoder) throws
{
let container = try decoder.singleValueContainer()
if let stringValue = try? container.decode(String.self), stringValue == "custom"
{
self.amount = 0 // Use 0 as amount internally to simplify logic.
self.isCustom = true
}
else
{
// Unless the value is "custom", throw error if value is not Decimal.
self.amount = try container.decode(Decimal.self)
self.isCustom = false
}
}
}
var pledge: Pledge?
var currency: String?
var tiers: Set<String>?
var benefit: String?
var hidden: Bool?
}
@objc(StoreApp)
@@ -136,6 +156,7 @@ public class StoreApp: NSManagedObject, Decodable, Fetchable
@NSManaged public private(set) var isPledgeRequired: Bool
@NSManaged public private(set) var isHiddenWithoutPledge: Bool
@NSManaged public private(set) var pledgeCurrency: String?
@NSManaged public private(set) var prefersCustomPledge: Bool
@nonobjc public var pledgeAmount: Decimal? { _pledgeAmount as? Decimal }
@NSManaged @objc(pledgeAmount) private var _pledgeAmount: NSDecimalNumber?
@@ -423,6 +444,7 @@ public class StoreApp: NSManagedObject, Decodable, Fetchable
// Must _explicitly_ set to false to ensure it updates cached database value.
self.isPledged = false
self.prefersCustomPledge = false
if let patreon = try container.decodeIfPresent(PatreonParameters.self, forKey: .patreon)
{
@@ -431,8 +453,9 @@ public class StoreApp: NSManagedObject, Decodable, Fetchable
if let pledge = patreon.pledge
{
self._pledgeAmount = pledge as NSDecimalNumber
self._pledgeAmount = pledge.amount as NSDecimalNumber
self.pledgeCurrency = patreon.currency ?? "USD" // Only set pledge currency if explicitly given pledge.
self.prefersCustomPledge = pledge.isCustom
}
else if patreon.pledge == nil && patreon.tiers == nil && patreon.benefit == nil
{