91 lines
3.1 KiB
Swift
91 lines
3.1 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:CONTEGGIO_APERTURE_PER_SCONTO)
|
|
if appOpenCounter < Self.AppOpenCountForDiscount {
|
|
completion(0)
|
|
return
|
|
}
|
|
|
|
let discountExpired = UserDefaults.standard.bool(forKey: PREZZO_SCONTATO_SCADUTO)
|
|
if discountExpired {
|
|
completion(0)
|
|
return
|
|
}
|
|
|
|
EQNUser.default().downloadOfferTimeRemaining { (timeOffer) in
|
|
if timeOffer == 0 {
|
|
UserDefaults.standard.set(true, forKey: PREZZO_SCONTATO_SCADUTO)
|
|
}
|
|
|
|
let timeInHours = timeOffer / 60
|
|
completion(timeInHours)
|
|
}
|
|
}
|
|
|
|
/// Returns availabilities for active subscriptions
|
|
/// - Parameter completion: Completion
|
|
static func availableSubscriptions(completion: @escaping (_ availability: EQNPurchaseAvailability?) -> Void) {
|
|
guard let url = URL(string: EQNServerUrlAvailableSubscriptionsCounter) else {
|
|
completion(nil)
|
|
return
|
|
}
|
|
|
|
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
|
|
guard let data = data else {
|
|
completion(nil)
|
|
return
|
|
}
|
|
|
|
let availability = EQNPurchaseAvailability(data: data)
|
|
completion(availability)
|
|
}
|
|
task.resume()
|
|
}
|
|
|
|
/// 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 {
|
|
var hasProVersion = false
|
|
VersioneProProducts.Identifier.identifierForProVersion.forEach { (identifier) in
|
|
hasProVersion = hasProVersion || UserDefaults.standard.bool(forKey: identifier)
|
|
}
|
|
return hasProVersion
|
|
}
|
|
|
|
/// Remove saved in-app purchases flags.
|
|
/// Used only during development
|
|
@objc public static func resetInAppPurchases() {
|
|
VersioneProProducts.Identifier.identifiers.forEach { (identifier) in
|
|
UserDefaults.standard.removeObject(forKey: identifier)
|
|
}
|
|
NotificationCenter.default.post(name: .IAPHelperPurchaseNotification, 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: .IAPHelperPurchaseNotification, object: identifier)
|
|
}
|
|
}
|