-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a2cdd96
commit bca9342
Showing
10 changed files
with
223 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...ityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/RouteCollections/Purchase/Models/Offering.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// | ||
// Offering.swift | ||
// SOLARdVPNCommunityCoreiOS | ||
// | ||
// Created by Viktoriia Kostyleva on 11.10.2022. | ||
// | ||
|
||
import Foundation | ||
import RevenueCat | ||
|
||
struct Offering { | ||
let identifier: String | ||
let serverDescription: String | ||
let availablePackages: [Package] | ||
let lifetime: Package? | ||
let annual: Package? | ||
let sixMonth: Package? | ||
let threeMonth: Package? | ||
let twoMonth: Package? | ||
let monthly: Package? | ||
let weekly: Package? | ||
} | ||
|
||
extension Offering { | ||
init(from model: RevenueCat.Offering) { | ||
self.identifier = model.identifier | ||
self.serverDescription = model.serverDescription | ||
self.availablePackages = model.availablePackages.map { Package(from: $0) } | ||
self.lifetime = Package(from: model.lifetime) | ||
self.annual = Package(from: model.annual) | ||
self.sixMonth = Package(from: model.sixMonth) | ||
self.threeMonth = Package(from: model.threeMonth) | ||
self.twoMonth = Package(from: model.twoMonth) | ||
self.monthly = Package(from: model.monthly) | ||
self.weekly = Package(from: model.weekly) | ||
} | ||
} | ||
|
||
extension Offering: Codable {} |
39 changes: 39 additions & 0 deletions
39
...nityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/RouteCollections/Purchase/Models/Package.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// | ||
// Package.swift | ||
// SOLARdVPNCommunityCoreiOS | ||
// | ||
// Created by Viktoriia Kostyleva on 11.10.2022. | ||
// | ||
|
||
import Foundation | ||
import RevenueCat | ||
|
||
struct Package { | ||
let identifier: String | ||
let packageType: PackageType | ||
let storeProduct: StoreProduct | ||
let offeringIdentifier: String | ||
let localizedPriceString: String | ||
let localizedIntroductoryPriceString: String? | ||
} | ||
|
||
extension Package { | ||
init?(from optionalModel: RevenueCat.Package?) { | ||
guard let model = optionalModel else { | ||
return nil | ||
} | ||
|
||
self.init(from: model) | ||
} | ||
|
||
init(from model: RevenueCat.Package) { | ||
self.identifier = model.identifier | ||
self.packageType = PackageType.init(rawValue: model.packageType.rawValue) ?? .unknown | ||
self.storeProduct = StoreProduct.init(from: model.storeProduct) | ||
self.offeringIdentifier = model.offeringIdentifier | ||
self.localizedPriceString = model.localizedPriceString | ||
self.localizedIntroductoryPriceString = model.localizedIntroductoryPriceString | ||
} | ||
} | ||
|
||
extension Package: Codable {} |
22 changes: 22 additions & 0 deletions
22
...CoreiOS/SOLARdVPNCommunityCoreiOS/Root/RouteCollections/Purchase/Models/PackageType.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// PackageType.swift | ||
// SOLARdVPNCommunityCoreiOS | ||
// | ||
// Created by Viktoriia Kostyleva on 11.10.2022. | ||
// | ||
|
||
import Foundation | ||
|
||
enum PackageType: Int { | ||
case unknown = -2 | ||
case custom | ||
case lifetime | ||
case annual | ||
case sixMonth | ||
case threeMonth | ||
case twoMonth | ||
case monthly | ||
case weekly | ||
} | ||
|
||
extension PackageType: Codable {} |
23 changes: 23 additions & 0 deletions
23
...oreiOS/SOLARdVPNCommunityCoreiOS/Root/RouteCollections/Purchase/Models/StoreProduct.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// | ||
// StoreProduct.swift | ||
// SOLARdVPNCommunityCoreiOS | ||
// | ||
// Created by Viktoriia Kostyleva on 11.10.2022. | ||
// | ||
|
||
import Foundation | ||
import RevenueCat | ||
|
||
struct StoreProduct { | ||
let price: Decimal | ||
let currency: String | ||
} | ||
|
||
extension StoreProduct { | ||
init(from model: RevenueCat.StoreProduct) { | ||
self.price = model.price | ||
self.currency = model.priceFormatter?.currencySymbol ?? "$" | ||
} | ||
} | ||
|
||
extension StoreProduct: Codable {} |
53 changes: 53 additions & 0 deletions
53
...OS/SOLARdVPNCommunityCoreiOS/Root/RouteCollections/Purchase/PurchaseRouteCollection.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// | ||
// PurchaseRouteCollection.swift | ||
// SOLARdVPNCommunityCoreiOS | ||
// | ||
// Created by Viktoriia Kostyleva on 10.10.2022. | ||
// | ||
|
||
import Vapor | ||
import RevenueCat | ||
|
||
struct PurchaseRouteCollection: RouteCollection { | ||
let context: NoContext | ||
|
||
func boot(routes: RoutesBuilder) throws { | ||
routes.get("offerings", use: getOfferings) | ||
} | ||
} | ||
|
||
extension PurchaseRouteCollection { | ||
func getOfferings(_ req: Request) async throws -> String { | ||
try await withCheckedThrowingContinuation({ (continuation: CheckedContinuation<String, Error>) in | ||
getOfferings() { result in | ||
switch result { | ||
case let .failure(error): | ||
continuation.resume(throwing: error.encodedError()) | ||
case let .success(offerings): | ||
Encoder.encode(model: offerings, continuation: continuation) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// MARK: - Private | ||
|
||
extension PurchaseRouteCollection { | ||
private func getOfferings(completion: @escaping (Result<[Offering], Error>) -> Void) { | ||
Purchases.shared.getOfferings { offerings, error in | ||
guard let offerings = offerings else { | ||
if let error = error { | ||
log.error(error) | ||
completion(.failure(error)) | ||
return | ||
} | ||
|
||
completion(.success([])) | ||
return | ||
} | ||
|
||
completion(.success(offerings.all.values.map { Offering(from: $0) })) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters