2023-11-22 17:37:06 -08:00
|
|
|
//
|
|
|
|
|
// 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
|
|
|
|
|
//
|
|
|
|
|
// TemplateViewConfiguration+Extensions.swift
|
|
|
|
|
//
|
|
|
|
|
// Created by Nacho Soto on 11/19/23.
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
import RevenueCat
|
|
|
|
|
|
|
|
|
|
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
|
|
|
|
|
extension TemplateViewConfiguration.PackageConfiguration {
|
|
|
|
|
|
|
|
|
|
/// - Returns: `true` if the packages contain more than one different text
|
|
|
|
|
/// for the given `display`.
|
|
|
|
|
/// This allows determining whether animations are required when transitioning between
|
|
|
|
|
/// packages.
|
|
|
|
|
@MainActor
|
|
|
|
|
func packagesProduceDifferentLabels(
|
|
|
|
|
for display: IntroEligibilityStateView.Display,
|
|
|
|
|
eligibility: IntroEligibilityViewModel
|
|
|
|
|
) -> Bool {
|
|
|
|
|
return self.packagesProduceDifferentLabels(
|
|
|
|
|
for: display,
|
|
|
|
|
eligibility: eligibility.allEligibility
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-22 18:43:07 -08:00
|
|
|
/// - Returns: `true` if any of the packages produce text for the given `display`.
|
|
|
|
|
/// This allows determining whether a label is required to be rendered.
|
|
|
|
|
@MainActor
|
|
|
|
|
func packagesProduceAnyLabel(
|
|
|
|
|
for display: IntroEligibilityStateView.Display,
|
|
|
|
|
eligibility: IntroEligibilityViewModel
|
|
|
|
|
) -> Bool {
|
|
|
|
|
return self.packagesProduceAnyLabel(
|
|
|
|
|
for: display,
|
|
|
|
|
eligibility: eligibility.allEligibility
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-22 17:37:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: - Implementation
|
|
|
|
|
|
|
|
|
|
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
|
|
|
|
|
extension TemplateViewConfiguration.PackageConfiguration {
|
|
|
|
|
|
2023-11-22 18:43:07 -08:00
|
|
|
typealias Eligibility = [Package: IntroEligibilityStatus]
|
|
|
|
|
|
2023-11-22 17:37:06 -08:00
|
|
|
func packagesProduceDifferentLabels(
|
|
|
|
|
for display: IntroEligibilityStateView.Display,
|
2023-11-22 18:43:07 -08:00
|
|
|
eligibility: Eligibility
|
2023-11-22 17:37:06 -08:00
|
|
|
) -> Bool {
|
2023-11-22 18:43:07 -08:00
|
|
|
return self.labels(for: display, eligibility: eligibility).count > 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func packagesProduceAnyLabel(
|
|
|
|
|
for display: IntroEligibilityStateView.Display,
|
|
|
|
|
eligibility: Eligibility
|
|
|
|
|
) -> Bool {
|
|
|
|
|
return self
|
|
|
|
|
.labels(for: display, eligibility: eligibility)
|
|
|
|
|
.contains { !$0.isEmpty }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
|
|
|
|
|
private extension TemplateViewConfiguration.PackageConfiguration {
|
|
|
|
|
|
|
|
|
|
private func labels(
|
|
|
|
|
for display: IntroEligibilityStateView.Display,
|
|
|
|
|
eligibility: Eligibility
|
|
|
|
|
) -> Set<String> {
|
2023-11-22 17:37:06 -08:00
|
|
|
return Set(
|
|
|
|
|
self.all
|
2023-11-22 18:43:07 -08:00
|
|
|
.lazy
|
|
|
|
|
.map {
|
|
|
|
|
IntroEligibilityStateView.text(
|
|
|
|
|
for: display,
|
|
|
|
|
localization: $0.localization,
|
|
|
|
|
introEligibility: eligibility[$0.content]
|
|
|
|
|
)
|
|
|
|
|
}
|
2023-11-22 17:37:06 -08:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|