mirror of
https://github.com/RevenueCat/purchases-ios
synced 2026-11-11 22:55:55 +05:45
61 lines
1.9 KiB
Swift
61 lines
1.9 KiB
Swift
//
|
|
// Copyright RevenueCat Inc. All Rights Reserved.
|
|
//
|
|
// Licensed under the MIT License (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// https://opensource.org/licenses/MIT
|
|
//
|
|
// IntroEligibilityViewModel.swift
|
|
//
|
|
// Created by Nacho Soto on 7/26/23.
|
|
|
|
import RevenueCat
|
|
import SwiftUI
|
|
|
|
/// Holds the state for dynamically computed `IntroEligibilityStatus`
|
|
/// for single or multi-package templates, depending on `PackageConfiguration`.
|
|
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
|
|
@MainActor
|
|
final class IntroEligibilityViewModel: ObservableObject {
|
|
|
|
typealias PackageConfiguration = TemplateViewConfiguration.PackageConfiguration
|
|
|
|
private let introEligibilityChecker: TrialOrIntroEligibilityChecker
|
|
|
|
init(introEligibilityChecker: TrialOrIntroEligibilityChecker) {
|
|
self.introEligibilityChecker = introEligibilityChecker
|
|
}
|
|
|
|
@Published
|
|
private(set) var allEligibility: [Package: IntroEligibilityStatus] = [:]
|
|
@Published
|
|
private(set) var singleEligibility: IntroEligibilityStatus?
|
|
|
|
}
|
|
|
|
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
|
|
extension IntroEligibilityViewModel {
|
|
|
|
func computeEligibility(for packages: PackageConfiguration) async {
|
|
switch packages {
|
|
case let .single(package):
|
|
self.singleEligibility = await self.introEligibilityChecker.eligibility(for: package.content)
|
|
self.allEligibility[package.content] = self.singleEligibility
|
|
|
|
case let .multiple(packages):
|
|
self.allEligibility = await self.introEligibilityChecker.eligibility(for: packages.all.map(\.content))
|
|
|
|
case let .multiTier(_, tiers, _):
|
|
let packages = tiers
|
|
.values
|
|
.flatMap { $0.all }
|
|
.map(\.content)
|
|
|
|
self.allEligibility = await self.introEligibilityChecker.eligibility(for: packages)
|
|
}
|
|
}
|
|
|
|
}
|