Files
eqn.ios/Sources/Earthquake Network/Models/EQNInAppProducts.swift
T
2024-06-21 15:56:55 +02:00

179 lines
7.3 KiB
Swift

/// Copyright (c) 2018 Razeware LLC
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the "Software"), to deal
/// in the Software without restriction, including without limitation the rights
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
/// copies of the Software, and to permit persons to whom the Software is
/// furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in
/// all copies or substantial portions of the Software.
///
/// Notwithstanding the foregoing, you may not use, copy, modify, merge, publish,
/// distribute, sublicense, create a derivative work, and/or sell copies of the
/// Software in any work that is designed, intended, or marketed for pedagogical or
/// instructional purposes related to programming, coding, application development,
/// or information technology. Permission for such use, copying, modification,
/// merger, publication, distribution, sublicensing, creation of derivative works,
/// or sale is expressly withheld.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
/// THE SOFTWARE.
import Foundation
import StoreKit
public struct EQNInAppProducts {
enum Plan: CaseIterable {
case monthly
case yearly
case perpetual
var localizedTitle: String {
switch self {
case .monthly: NSLocalizedString("subscription_plan_monthly", comment: "")
case .yearly: NSLocalizedString("subscription_plan_yearly", comment: "")
case .perpetual: NSLocalizedString("subscription_plan_perpetual", comment: "")
}
}
}
enum Category {
case pro
case top10k
case top100k
var image: UIImage? {
switch self {
case .pro: nil
case .top10k: UIImage(named: "top_10k")
case .top100k: UIImage(named: "top_100k")
}
}
var localizedTitle: String {
switch self {
case .pro: return NSLocalizedString("network_pro", comment: "")
case .top10k: return "Top 10k"
case .top100k: return "Top 100k"
}
}
}
let plan: Plan
let category: Category
let isDiscounted: Bool
let product: SKProduct
// MARK: - Init
private init(plan: Plan, category: Category, isDiscounted: Bool, product: SKProduct) {
self.plan = plan
self.category = category
self.isDiscounted = isDiscounted
self.product = product
}
// MARK: - Accessories
var isTop10k: Bool {
category == .top10k
}
var isTop100k: Bool {
category == .top100k
}
var isSubscription: Bool {
category != .pro
}
var productIdentifier: String {
product.productIdentifier
}
// MARK: - Static
static func from(product: SKProduct) -> EQNInAppProducts? {
switch product.productIdentifier {
case Identifier.ProVersionFullPrice:
.init(plan: .perpetual, category: .pro, isDiscounted: false, product: product)
case Identifier.ProVersionDiscounted:
.init(plan: .perpetual, category: .pro, isDiscounted: true, product: product)
case Identifier.Subscription10kMonthly:
.init(plan: .monthly, category: .top10k, isDiscounted: false, product: product)
case Identifier.Subscription10kYearly:
.init(plan: .yearly, category: .top10k, isDiscounted: false, product: product)
case Identifier.Subscription10kYearlyDiscounted:
.init(plan: .yearly, category: .top10k, isDiscounted: true, product: product)
case Identifier.Subscription10kPerpetual:
.init(plan: .perpetual, category: .top10k, isDiscounted: false, product: product)
case Identifier.Subscription100kMonthly:
.init(plan: .monthly, category: .top100k, isDiscounted: false, product: product)
case Identifier.Subscription100kYearly:
.init(plan: .yearly, category: .top100k, isDiscounted: false, product: product)
case Identifier.Subscription100kYearlyDiscounted:
.init(plan: .yearly, category: .top100k, isDiscounted: true, product: product)
case Identifier.Subscription100kPerpetual:
.init(plan: .perpetual, category: .top100k, isDiscounted: false, product: product)
default:
nil
}
}
enum Identifier {
static let ProVersionFullPrice = "com.finazzi.distquake.ProPrezzoPieno"
static let ProVersionDiscounted = "com.finazzi.distquake.VersioneProScontata"
static let Subscription10kMonthly = "com.finazzi.distquake.Abbonamento10k.mensileAutomatico"
static let Subscription10kYearly = "com.finazzi.distquake.Abbonamento10k.annualeAutomatico"
static let Subscription10kYearlyDiscounted = "com.finazzi.distquake.Abbonamento10k.annualeAutomaticoScontato"
static let Subscription10kPerpetual = "com.finazzi.distquake.Abbonamento10k.perpetuo"
static let Subscription100kMonthly = "com.finazzi.distquake.Abbonamento100k.mensileAutomatico"
static let Subscription100kYearly = "com.finazzi.distquake.Abbonamento100k.annualeAutomatico"
static let Subscription100kYearlyDiscounted = "com.finazzi.distquake.Abbonamento100k.annualeAutomaticoscontato"
static let Subscription100kPerpetual = "com.finazzi.distquake.Abbonamento100k.perpetuo"
static let identifiers: Set<ProductIdentifier> = [
ProVersionFullPrice, ProVersionDiscounted,
Subscription10kMonthly, Subscription10kYearly, Subscription10kYearlyDiscounted,
Subscription100kMonthly, Subscription100kYearly, Subscription100kYearlyDiscounted,
Subscription10kPerpetual, Subscription100kPerpetual
]
static let identifierForProVersion: Set<ProductIdentifier> = [
ProVersionFullPrice, ProVersionDiscounted,
Subscription10kYearly, Subscription10kYearlyDiscounted,
Subscription100kYearly, Subscription100kYearlyDiscounted,
Subscription10kPerpetual, Subscription100kPerpetual
]
static let identifiersForTop10k: Set<ProductIdentifier> = [
Subscription10kMonthly, Subscription10kYearly, Subscription10kYearlyDiscounted, Subscription10kPerpetual
]
static let identifiersForTop100k: Set<ProductIdentifier> = [
Subscription100kMonthly, Subscription100kYearly, Subscription100kYearlyDiscounted, Subscription100kPerpetual
]
}
public static let store = IAPHelper(productIds: EQNInAppProducts.Identifier.identifiers)
}
func resourceNameForProductIdentifier(_ productIdentifier: String) -> String? {
return productIdentifier.components(separatedBy: ".").last
}