95 lines
3.7 KiB
Swift
95 lines
3.7 KiB
Swift
//
|
|
// EQNPurchaseUtility.swift
|
|
// Earthquake Network
|
|
//
|
|
// Created by Busi Andrea on 30/07/2020.
|
|
// Copyright © 2020 Earthquake Network. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
|
|
@objc
|
|
public class EQNPurchaseUtility: NSObject {
|
|
|
|
private static let AppOpenCountForDiscount = 15
|
|
|
|
// MARK: - Public
|
|
|
|
/// Check if a discounted price is available and returns the remaining time to get the offer.
|
|
/// If zero, no discounted price is available
|
|
/// - Parameter completion: Completion
|
|
static func offerTimeRemaining(completion: @escaping (_ timeRemaining: Int) -> Void) {
|
|
let appOpenCounter = UserDefaults.standard.integer(forKey: UserDefaults.UserDataProDiscountOpenCounter)
|
|
if appOpenCounter < Self.AppOpenCountForDiscount {
|
|
completion(0)
|
|
return
|
|
}
|
|
|
|
let discountExpired = UserDefaults.standard.bool(forKey: UserDefaults.UserDataProDiscountExpired)
|
|
if discountExpired {
|
|
completion(0)
|
|
return
|
|
}
|
|
|
|
EQNUser.default().downloadOfferTimeRemaining { (timeOffer) in
|
|
if timeOffer == 0 {
|
|
UserDefaults.standard.set(true, forKey: UserDefaults.UserDataProDiscountExpired)
|
|
}
|
|
|
|
let timeInHours = timeOffer / 60
|
|
completion(timeInHours)
|
|
}
|
|
}
|
|
|
|
/// Returns availabilities for active subscriptions
|
|
/// - Parameter completion: Completion
|
|
static func availableSubscriptions(completion: @escaping (_ availability: EQNPurchaseAvailability?) -> Void) {
|
|
// previously, to get this data a separated call was required
|
|
// starting from March 2023, the values are available from the EQNServerUrlDownloadSmartphoneNetwork request
|
|
|
|
let reteSmartPhone = EQNManager.manager().rete_smartphone
|
|
let availability = EQNPurchaseAvailability(top10kAvailable: reteSmartPhone?.top10kAvailable ?? 0,
|
|
top100kAvailable: reteSmartPhone?.top100kAvailable ?? 0)
|
|
completion(availability)
|
|
}
|
|
|
|
/// Check if user has bought pro app version
|
|
/// Pro version is enabled also if a yearly subscription is enabled
|
|
@objc public static func isProVersionEnabled() -> Bool {
|
|
EQNInAppProducts.Identifier.identifierForProVersion.reduce(false) { (result, identifier) -> Bool in
|
|
return result || UserDefaults.standard.bool(forKey: identifier)
|
|
}
|
|
}
|
|
|
|
/// Check if user has bought Top 10k subscription
|
|
@objc public static func isTop10kEnabled() -> Bool {
|
|
EQNInAppProducts.Identifier.identifiersForTop10k.reduce(false) { (result, identifier) -> Bool in
|
|
return result || UserDefaults.standard.bool(forKey: identifier)
|
|
}
|
|
}
|
|
|
|
/// Check if user has bought Top 100k subscription
|
|
@objc public static func isTop100kEnabled() -> Bool {
|
|
EQNInAppProducts.Identifier.identifiersForTop100k.reduce(false) { (result, identifier) -> Bool in
|
|
return result || UserDefaults.standard.bool(forKey: identifier)
|
|
}
|
|
}
|
|
|
|
/// Remove saved in-app purchases flags.
|
|
/// Used only during development
|
|
@objc public static func resetInAppPurchases() {
|
|
EQNInAppProducts.Identifier.identifiers.forEach { (identifier) in
|
|
UserDefaults.standard.removeObject(forKey: identifier)
|
|
}
|
|
NotificationCenter.default.post(name: .EQNInAppPurchaseDidComplete, object: nil)
|
|
}
|
|
|
|
/// Use to simulate the purchase of the Pro version
|
|
@objc public static func simulateProPurchase(identifier: String) {
|
|
// store and notify
|
|
UserDefaults.standard.set(true, forKey: identifier)
|
|
NotificationCenter.default.post(name: .EQNInAppPurchaseDidComplete, object: identifier)
|
|
}
|
|
}
|