iap - wip
This commit is contained in:
@@ -94,4 +94,10 @@ extension Date: RawRepresentable {
|
||||
|
||||
return (startDate, endDate)
|
||||
}
|
||||
|
||||
func formattedDate() -> String {
|
||||
let dateFormatter = DateFormatter()
|
||||
dateFormatter.dateFormat = "MMM dd, yyyy"
|
||||
return dateFormatter.string(from: self)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,8 @@ struct FeelsApp: App {
|
||||
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
|
||||
|
||||
let persistenceController = PersistenceController.shared
|
||||
|
||||
@StateObject var iapManager = IAPManager()
|
||||
|
||||
init() {
|
||||
BGTaskScheduler.shared.cancelAllTaskRequests()
|
||||
BGTaskScheduler.shared.register(forTaskWithIdentifier: BGTask.updateDBMissingID, using: nil) { (task) in
|
||||
@@ -33,6 +34,7 @@ struct FeelsApp: App {
|
||||
yearView: YearView(viewModel: YearViewModel()),
|
||||
customizeView: CustomizeView())
|
||||
.environment(\.managedObjectContext, persistenceController.viewContext)
|
||||
.environmentObject(iapManager)
|
||||
}.onChange(of: scenePhase) { phase in
|
||||
if phase == .background {
|
||||
//BGTask.scheduleBackgroundProcessing()
|
||||
@@ -41,6 +43,7 @@ struct FeelsApp: App {
|
||||
|
||||
if phase == .active {
|
||||
UIApplication.shared.applicationIconBadgeNumber = 0
|
||||
iapManager.refresh()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
296
Shared/IAPManager.swift
Normal file
296
Shared/IAPManager.swift
Normal file
@@ -0,0 +1,296 @@
|
||||
/*
|
||||
See LICENSE folder for this sample’s licensing information.
|
||||
|
||||
Abstract:
|
||||
The store class is responsible for requesting products from the App Store and starting purchases.
|
||||
*/
|
||||
|
||||
import Foundation
|
||||
import StoreKit
|
||||
import SwiftUI
|
||||
|
||||
typealias Transaction = StoreKit.Transaction
|
||||
typealias RenewalInfo = StoreKit.Product.SubscriptionInfo.RenewalInfo
|
||||
typealias RenewalState = StoreKit.Product.SubscriptionInfo.RenewalState
|
||||
|
||||
public enum StoreError: Error {
|
||||
case failedVerification
|
||||
}
|
||||
|
||||
class IAPManager: ObservableObject {
|
||||
@Published private(set) var showIAP = false
|
||||
@Published private(set) var subscriptions = [Product: (status: [Product.SubscriptionInfo.Status], renewalInfo: RenewalInfo)?]()
|
||||
|
||||
public var sortedSubscriptionKeysByPriceOptions: [Product] {
|
||||
subscriptions.keys.sorted(by: {
|
||||
$0.price < $1.price
|
||||
})
|
||||
}
|
||||
|
||||
public var currentSubscription: Product? {
|
||||
let sortedProducts = subscriptions.keys.sorted(by: {
|
||||
$0.price > $1.price
|
||||
})
|
||||
|
||||
// first see if we have a product + sub that is set to autorenew
|
||||
for product in sortedProducts {
|
||||
if let _value = subscriptions[product]??.renewalInfo {
|
||||
if _value.willAutoRenew {
|
||||
return product
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// if no auto renew then return
|
||||
// highest priced that has a sub status
|
||||
for product in sortedProducts {
|
||||
if let _ = subscriptions[product]??.status {
|
||||
return product
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// for all products return the one that is set
|
||||
// to auto renew
|
||||
public var nextRenewllOption: Product? {
|
||||
if let currentSubscription = currentSubscription,
|
||||
let info = subscriptions[currentSubscription],
|
||||
let status = info?.status {
|
||||
for aStatus in status {
|
||||
if let renewalInfo = try? checkVerified(aStatus.renewalInfo),
|
||||
renewalInfo.willAutoRenew {
|
||||
if let renewToProduct = subscriptions.first(where: {
|
||||
$0.key.id == renewalInfo.autoRenewPreference
|
||||
})?.key {
|
||||
return renewToProduct
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var updateListenerTask: Task<Void, Error>? = nil
|
||||
|
||||
private let iapIdentifiers = Set([
|
||||
"com.88oakapps.ifeel.IAP.subscription.weekly",
|
||||
"com.88oakapps.ifeel.IAP.subscription.monthly",
|
||||
"com.88oakapps.ifeel.IAP.subscription.yearly"
|
||||
])
|
||||
|
||||
init() {
|
||||
//Start a transaction listener as close to app launch as possible so you don't miss any transactions.
|
||||
updateListenerTask = listenForTransactions()
|
||||
|
||||
refresh()
|
||||
}
|
||||
|
||||
deinit {
|
||||
updateListenerTask?.cancel()
|
||||
}
|
||||
|
||||
func refresh() {
|
||||
Task {
|
||||
//During store initialization, request products from the App Store.
|
||||
await requestProducts()
|
||||
|
||||
//Deliver products that the customer purchases.
|
||||
await updateCustomerProductStatus()
|
||||
|
||||
decideShowIAP()
|
||||
}
|
||||
}
|
||||
|
||||
func decideShowIAP() {
|
||||
guard !subscriptions.isEmpty else {
|
||||
return
|
||||
}
|
||||
var tmpShowIAP = true
|
||||
for (_, value) in self.subscriptions {
|
||||
if value != nil {
|
||||
tmpShowIAP = false
|
||||
return
|
||||
}
|
||||
}
|
||||
DispatchQueue.main.async {
|
||||
self.showIAP = tmpShowIAP
|
||||
}
|
||||
}
|
||||
|
||||
func listenForTransactions() -> Task<Void, Error> {
|
||||
return Task.detached {
|
||||
//Iterate through any transactions that don't come from a direct call to `purchase()`.
|
||||
for await result in Transaction.updates {
|
||||
do {
|
||||
let transaction = try self.checkVerified(result)
|
||||
|
||||
//Deliver products to the user.
|
||||
await self.updateCustomerProductStatus()
|
||||
|
||||
//Always finish a transaction.
|
||||
await transaction.finish()
|
||||
} catch {
|
||||
//StoreKit has a transaction that fails verification. Don't deliver content to the user.
|
||||
print("Transaction failed verification")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// fetch all available iap from remote and store locally
|
||||
// in subscriptions
|
||||
@MainActor
|
||||
func requestProducts() async {
|
||||
do {
|
||||
subscriptions.removeAll()
|
||||
|
||||
//Request products from the App Store using the identifiers that the Products.plist file defines.
|
||||
let storeProducts = try await Product.products(for: iapIdentifiers)
|
||||
|
||||
//Filter the products into categories based on their type.
|
||||
for product in storeProducts {
|
||||
switch product.type {
|
||||
case .consumable:
|
||||
break
|
||||
case .nonConsumable:
|
||||
break
|
||||
case .autoRenewable:
|
||||
subscriptions.updateValue(nil, forKey: product)
|
||||
case .nonRenewable:
|
||||
break
|
||||
default:
|
||||
//Ignore this product.
|
||||
print("Unknown product")
|
||||
}
|
||||
}
|
||||
|
||||
} catch {
|
||||
print("Failed product request from the App Store server: \(error)")
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
func updateCustomerProductStatus() async {
|
||||
var purchasedSubscriptions: [Product] = []
|
||||
|
||||
// Iterate through all of the user's purchased products.
|
||||
for await result in Transaction.currentEntitlements {
|
||||
do {
|
||||
//Check whether the transaction is verified. If it isn’t, catch `failedVerification` error.
|
||||
let transaction = try checkVerified(result)
|
||||
|
||||
//Check the `productType` of the transaction and get the corresponding product from the store.
|
||||
switch transaction.productType {
|
||||
case .nonConsumable:
|
||||
break
|
||||
case .nonRenewable:
|
||||
break
|
||||
case .autoRenewable:
|
||||
print("subscriptions2 \(subscriptions)")
|
||||
if let subscription = subscriptions.first(where: {
|
||||
$0.key.id == transaction.productID
|
||||
}) {
|
||||
purchasedSubscriptions.append(subscription.key)
|
||||
}
|
||||
default:
|
||||
break
|
||||
}
|
||||
} catch {
|
||||
print()
|
||||
}
|
||||
}
|
||||
print("purchasedSubscriptions \(purchasedSubscriptions)")
|
||||
if !purchasedSubscriptions.isEmpty {
|
||||
self.showIAP = false
|
||||
}
|
||||
|
||||
for sub in purchasedSubscriptions {
|
||||
guard let statuses = try? await sub.subscription?.status else {
|
||||
return
|
||||
}
|
||||
|
||||
for status in statuses {
|
||||
switch status.state {
|
||||
case .expired, .revoked:
|
||||
continue
|
||||
default:
|
||||
if let renewalInfo = try? checkVerified(status.renewalInfo) {
|
||||
subscriptions.updateValue((statuses, renewalInfo), forKey: sub)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func purchase(_ product: Product) async throws -> Transaction? {
|
||||
//Begin purchasing the `Product` the user selects.
|
||||
let result = try await product.purchase()
|
||||
|
||||
switch result {
|
||||
case .success(let verification):
|
||||
//Check whether the transaction is verified. If it isn't,
|
||||
//this function rethrows the verification error.
|
||||
let transaction = try checkVerified(verification)
|
||||
|
||||
//The transaction is verified. Deliver content to the user.
|
||||
await updateCustomerProductStatus()
|
||||
|
||||
//Always finish a transaction.
|
||||
await transaction.finish()
|
||||
|
||||
decideShowIAP()
|
||||
|
||||
return transaction
|
||||
case .userCancelled, .pending:
|
||||
return nil
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func isPurchased(_ product: Product) async throws -> Bool {
|
||||
//Determine whether the user purchases a given product.
|
||||
switch product.type {
|
||||
case .nonRenewable:
|
||||
return false
|
||||
case .nonConsumable:
|
||||
return false
|
||||
case .autoRenewable:
|
||||
return subscriptions.keys.contains(product)
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func checkVerified<T>(_ result: VerificationResult<T>) throws -> T {
|
||||
//Check whether the JWS passes StoreKit verification.
|
||||
switch result {
|
||||
case .unverified:
|
||||
//StoreKit parses the JWS, but it fails verification.
|
||||
throw StoreError.failedVerification
|
||||
case .verified(let safe):
|
||||
//The result is verified. Return the unwrapped value.
|
||||
return safe
|
||||
}
|
||||
}
|
||||
|
||||
func sortByPrice(_ products: [Product]) -> [Product] {
|
||||
products.sorted(by: { return $0.price < $1.price })
|
||||
}
|
||||
|
||||
func colorForIAPButton(iapIdentifier: String) -> Color {
|
||||
if iapIdentifier == "com.88oakapps.ifeel.IAP.subscription.weekly" {
|
||||
return DefaultMoodTint.color(forMood: .horrible)
|
||||
}
|
||||
else if iapIdentifier == "com.88oakapps.ifeel.IAP.subscription.monthly" {
|
||||
return DefaultMoodTint.color(forMood: .average)
|
||||
}
|
||||
else if iapIdentifier == "com.88oakapps.ifeel.IAP.subscription.yearly" {
|
||||
return DefaultMoodTint.color(forMood: .great)
|
||||
}
|
||||
|
||||
return .blue
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
//
|
||||
// IAPManager.swift
|
||||
// Feels
|
||||
//
|
||||
// Created by Trey Tartt on 7/7/22.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
class IAPManager {
|
||||
public private(set) var isSubscribed = true
|
||||
}
|
||||
16
Shared/View+Extensions.swift
Normal file
16
Shared/View+Extensions.swift
Normal file
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// View+Extensions.swift
|
||||
// Feels
|
||||
//
|
||||
// Created by Trey Tartt on 7/7/22.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
extension View {
|
||||
func hasScrollEnabled(_ value: Bool) -> some View {
|
||||
self.onAppear {
|
||||
UITableView.appearance().isScrollEnabled = value
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -38,6 +38,7 @@ struct DayView: View {
|
||||
@State private var showUpdateEntryAlert = false
|
||||
@StateObject private var onboardingData = OnboardingDataDataManager.shared
|
||||
@StateObject private var filteredDays = DaysFilterClass.shared
|
||||
@EnvironmentObject var iapManager: IAPManager
|
||||
|
||||
@ObservedObject var viewModel: DayViewViewModel
|
||||
|
||||
@@ -79,6 +80,13 @@ struct DayView: View {
|
||||
showUpdateEntryAlert = false
|
||||
})
|
||||
}
|
||||
|
||||
if iapManager.showIAP {
|
||||
VStack {
|
||||
Spacer()
|
||||
PurchaseButtonView(height: 175, iapManager: iapManager)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,7 +109,7 @@ struct DayView: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding([.leading, .trailing, .bottom])
|
||||
.padding([.leading, .trailing])
|
||||
.background(
|
||||
theme.currentTheme.bg
|
||||
.edgesIgnoringSafeArea(.all)
|
||||
@@ -161,6 +169,7 @@ struct DayView: View {
|
||||
}
|
||||
)
|
||||
}
|
||||
.disabled(iapManager.showIAP)
|
||||
.background(
|
||||
theme.currentTheme.secondaryBGColor
|
||||
)
|
||||
|
||||
@@ -22,7 +22,7 @@ struct MonthView: View {
|
||||
// store a value that gets changed when user updates custom colors to update the view since the moodTint doesn't change
|
||||
@AppStorage(UserDefaultsStore.Keys.customMoodTintUpdateNumber.rawValue, store: GroupUserDefaults.groupDefaults) private var customMoodTintUpdateNumber: Int = 0
|
||||
|
||||
|
||||
@EnvironmentObject var iapManager: IAPManager
|
||||
@StateObject private var selectedDetail = StupidAssDetailViewObservableObject()
|
||||
@State private var showingSheet = false
|
||||
@StateObject private var onboardingData = OnboardingDataDataManager.shared
|
||||
@@ -73,12 +73,20 @@ struct MonthView: View {
|
||||
}
|
||||
.padding([.leading, .trailing])
|
||||
}
|
||||
.disabled(iapManager.showIAP)
|
||||
}
|
||||
|
||||
if iapManager.showIAP {
|
||||
VStack {
|
||||
Spacer()
|
||||
PurchaseButtonView(height: 175, iapManager: iapManager)
|
||||
}
|
||||
}
|
||||
}
|
||||
.onAppear(perform: {
|
||||
EventLogger.log(event: "show_month_view")
|
||||
})
|
||||
.padding([.top, .bottom])
|
||||
.padding([.top])
|
||||
.background(
|
||||
theme.currentTheme.bg
|
||||
.edgesIgnoringSafeArea(.all)
|
||||
|
||||
236
Shared/views/PurchaseButtonView.swift
Normal file
236
Shared/views/PurchaseButtonView.swift
Normal file
@@ -0,0 +1,236 @@
|
||||
//
|
||||
// PurchaseButtonView.swift
|
||||
// Feels
|
||||
//
|
||||
// Created by Trey Tartt on 7/7/22.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import StoreKit
|
||||
|
||||
struct PurchaseButtonView: View {
|
||||
enum ViewStatus: String {
|
||||
case needToBuy
|
||||
case error
|
||||
case success
|
||||
case subscribed
|
||||
}
|
||||
@AppStorage(UserDefaultsStore.Keys.theme.rawValue, store: GroupUserDefaults.groupDefaults) private var theme: Theme = .system
|
||||
@AppStorage(UserDefaultsStore.Keys.textColor.rawValue, store: GroupUserDefaults.groupDefaults) private var textColor: Color = DefaultTextColor.textColor
|
||||
var iapManager: IAPManager
|
||||
|
||||
private let height: Float
|
||||
private let showManageSubClosure: (() -> Void)?
|
||||
|
||||
@State var isPurchasing: Bool = false
|
||||
|
||||
@State private var viewStatus: ViewStatus = .needToBuy {
|
||||
didSet {
|
||||
isPurchasing = false
|
||||
}
|
||||
}
|
||||
|
||||
public init(height: Float, iapManager: IAPManager, showManageSubClosure: (() -> Void)? = nil) {
|
||||
self.height = height
|
||||
self.showManageSubClosure = showManageSubClosure
|
||||
self.iapManager = iapManager
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
ZStack {
|
||||
switch viewStatus {
|
||||
case .needToBuy, .error:
|
||||
buyOptionsView
|
||||
.frame(height: CGFloat(height))
|
||||
.background(theme.currentTheme.secondaryBGColor)
|
||||
case .success:
|
||||
subscribedView
|
||||
.frame(height: CGFloat(height))
|
||||
.background(theme.currentTheme.secondaryBGColor)
|
||||
case .subscribed:
|
||||
subscribedView
|
||||
.frame(height: CGFloat(height))
|
||||
.background(theme.currentTheme.secondaryBGColor)
|
||||
}
|
||||
}
|
||||
.onAppear{
|
||||
if let _ = iapManager.currentSubscription {
|
||||
viewStatus = .subscribed
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var buyOptionsSetingsView: some View {
|
||||
GeometryReader { metrics in
|
||||
VStack(spacing: 20) {
|
||||
Text(String(localized: "purchase_view_title"))
|
||||
.foregroundColor(textColor)
|
||||
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
|
||||
.frame(height: 50)
|
||||
.padding([.leading, .trailing])
|
||||
VStack(alignment: .leading) {
|
||||
ForEach(iapManager.sortedSubscriptionKeysByPriceOptions, id: \.self) { product in
|
||||
HStack {
|
||||
Button(action: {
|
||||
purchase(product: product)
|
||||
}, label: {
|
||||
Text("\(product.displayPrice)\n\(product.displayName)")
|
||||
.foregroundColor(.white)
|
||||
.bold()
|
||||
.frame(maxWidth: .infinity)
|
||||
.contentShape(Rectangle())
|
||||
})
|
||||
.frame(maxWidth: .infinity)
|
||||
.frame(height: 50)
|
||||
.padding()
|
||||
.background(RoundedRectangle(cornerRadius: 10).fill(iapManager.colorForIAPButton(iapIdentifier: product.id)))
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding([.leading, .leading])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var buyOptionsView: some View {
|
||||
VStack {
|
||||
ZStack {
|
||||
VStack(spacing: 20) {
|
||||
Text(String(localized: "purchase_view_title"))
|
||||
.font(.body)
|
||||
.bold()
|
||||
.foregroundColor(textColor)
|
||||
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
|
||||
|
||||
HStack {
|
||||
ForEach(iapManager.sortedSubscriptionKeysByPriceOptions) { product in
|
||||
Button(action: {
|
||||
purchase(product: product)
|
||||
}, label: {
|
||||
Text("\(product.displayPrice)\n\(product.displayName)")
|
||||
.foregroundColor(.white)
|
||||
.bold()
|
||||
.frame(maxWidth: .infinity)
|
||||
.contentShape(Rectangle())
|
||||
})
|
||||
.padding()
|
||||
.frame(maxWidth: .infinity)
|
||||
.background(RoundedRectangle(cornerRadius: 10).fill(iapManager.colorForIAPButton(iapIdentifier: product.id)))
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding([.leading, .trailing])
|
||||
.frame(minWidth: 0, maxWidth: .infinity)
|
||||
|
||||
}
|
||||
.background(.ultraThinMaterial)
|
||||
.frame(minWidth: 0, maxWidth: .infinity)
|
||||
}
|
||||
.background(.clear)
|
||||
}
|
||||
|
||||
private var successView: some View {
|
||||
HStack {
|
||||
Text("it worked")
|
||||
}
|
||||
.background(.green)
|
||||
}
|
||||
|
||||
private var errorView: some View {
|
||||
HStack {
|
||||
Text("something broke")
|
||||
}
|
||||
.background(.red)
|
||||
}
|
||||
|
||||
private var subscribedView: some View {
|
||||
VStack(alignment: .leading) {
|
||||
Text(String(localized: "purchase_view_current_subscription"))
|
||||
.font(.title3)
|
||||
.padding(.leading)
|
||||
|
||||
Divider()
|
||||
|
||||
if let currentProduct = iapManager.currentSubscription,
|
||||
let value = iapManager.subscriptions[currentProduct] {
|
||||
HStack {
|
||||
VStack (alignment: .leading, spacing: 10) {
|
||||
Text(currentProduct.displayName)
|
||||
.font(.title3)
|
||||
Text(currentProduct.displayPrice)
|
||||
.font(.title3)
|
||||
}.padding([.leading, .trailing])
|
||||
|
||||
ForEach(value!.status, id: \.self) { singleStatus in
|
||||
StatusInfoView(product: currentProduct, status: singleStatus)
|
||||
.padding([.leading])
|
||||
.font(.body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Button(action: {
|
||||
showManageSubClosure?()
|
||||
}, label: {
|
||||
Text(String(localized: "purchase_view_cancel"))
|
||||
.foregroundColor(.red)
|
||||
})
|
||||
.frame(maxWidth: .infinity)
|
||||
.padding([.bottom])
|
||||
.multilineTextAlignment(.center)
|
||||
|
||||
Divider()
|
||||
|
||||
showOtherSubOptions
|
||||
}
|
||||
}
|
||||
|
||||
private var showOtherSubOptions: some View {
|
||||
VStack (spacing: 10) {
|
||||
HStack {
|
||||
ForEach(iapManager.sortedSubscriptionKeysByPriceOptions, id: \.self) { product in
|
||||
if product.id != iapManager.nextRenewllOption?.id {
|
||||
Button(action: {
|
||||
purchase(product: product)
|
||||
}, label: {
|
||||
Text("\(product.displayPrice)\n\(product.displayName)")
|
||||
.foregroundColor(.white)
|
||||
.font(.headline)
|
||||
})
|
||||
.contentShape(Rectangle())
|
||||
.padding()
|
||||
.frame(maxWidth: .infinity)
|
||||
.background(RoundedRectangle(cornerRadius: 10).fill(iapManager.colorForIAPButton(iapIdentifier: product.id)))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding([.leading, .trailing])
|
||||
}
|
||||
|
||||
private var purchasingView: some View {
|
||||
HStack {
|
||||
Text("purcasing")
|
||||
}
|
||||
.background(.yellow)
|
||||
}
|
||||
|
||||
private func purchase(product: Product) {
|
||||
isPurchasing = true
|
||||
Task {
|
||||
do {
|
||||
if let _ = try await iapManager.purchase(product) {
|
||||
viewStatus = .success
|
||||
}
|
||||
} catch {
|
||||
viewStatus = .error
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct PurchaseButtonView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
PurchaseButtonView(height: 175, iapManager: IAPManager())
|
||||
}
|
||||
}
|
||||
@@ -8,10 +8,12 @@
|
||||
import SwiftUI
|
||||
import CloudKitSyncMonitor
|
||||
import UniformTypeIdentifiers
|
||||
import StoreKit
|
||||
|
||||
struct SettingsView: View {
|
||||
@Environment(\.dismiss) var dismiss
|
||||
|
||||
@EnvironmentObject var iapManager: IAPManager
|
||||
|
||||
@State private var showingExporter = false
|
||||
@State private var showingImporter = false
|
||||
@State private var importContent = ""
|
||||
@@ -35,6 +37,7 @@ struct SettingsView: View {
|
||||
.padding()
|
||||
|
||||
// cloudKitEnable
|
||||
subscriptionInfoView
|
||||
canDelete
|
||||
showOnboardingButton
|
||||
specialThanksCell
|
||||
@@ -136,6 +139,19 @@ struct SettingsView: View {
|
||||
}
|
||||
}
|
||||
|
||||
private var subscriptionInfoView: some View {
|
||||
ZStack {
|
||||
theme.currentTheme.secondaryBGColor
|
||||
PurchaseButtonView(height: iapManager.currentSubscription != nil ? 300 : 175, iapManager: iapManager, showManageSubClosure: {
|
||||
Task {
|
||||
await
|
||||
self.showManageSubscription()
|
||||
}
|
||||
})
|
||||
}
|
||||
.cornerRadius(Constants.viewsCornerRaidus, corners: [.topLeft, .topRight, .bottomLeft, .bottomRight])
|
||||
}
|
||||
|
||||
private var closeButtonView: some View {
|
||||
HStack{
|
||||
Spacer()
|
||||
@@ -483,6 +499,17 @@ struct SettingsView: View {
|
||||
.fixedSize(horizontal: false, vertical: true)
|
||||
.cornerRadius(Constants.viewsCornerRaidus, corners: [.topLeft, .topRight, .bottomLeft, .bottomRight])
|
||||
}
|
||||
|
||||
func showManageSubscription() async {
|
||||
if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene{
|
||||
do {
|
||||
try await StoreKit.AppStore.showManageSubscriptions(in: windowScene)
|
||||
iapManager.refresh()
|
||||
} catch {
|
||||
print("Sheet can not be opened")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct TextFile: FileDocument {
|
||||
|
||||
143
Shared/views/StatusInfoView.swift
Normal file
143
Shared/views/StatusInfoView.swift
Normal file
@@ -0,0 +1,143 @@
|
||||
//
|
||||
// StatusInfoView.swift
|
||||
// Feels
|
||||
//
|
||||
// Created by Trey Tartt on 7/8/22.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import StoreKit
|
||||
|
||||
struct StatusInfoView: View {
|
||||
@EnvironmentObject var iapManager: IAPManager
|
||||
|
||||
let product: Product
|
||||
let status: Product.SubscriptionInfo.Status
|
||||
|
||||
var body: some View {
|
||||
ScrollView {
|
||||
Text(statusDescription())
|
||||
.frame(maxWidth: .infinity, alignment: .center)
|
||||
.font(.body)
|
||||
}
|
||||
.frame(maxWidth: .infinity)
|
||||
.frame(height: 75)
|
||||
}
|
||||
|
||||
//Build a string description of the subscription status to display to the user.
|
||||
fileprivate func statusDescription() -> String {
|
||||
guard case .verified(let renewalInfo) = status.renewalInfo,
|
||||
case .verified(let transaction) = status.transaction else {
|
||||
return "The App Store could not verify your subscription status."
|
||||
}
|
||||
|
||||
var description = ""
|
||||
|
||||
switch status.state {
|
||||
case .subscribed:
|
||||
let renewToProduct: Product?
|
||||
let renewalInfo: RenewalInfo?
|
||||
|
||||
renewalInfo = try? iapManager.checkVerified(status.renewalInfo)
|
||||
renewToProduct = iapManager.subscriptions.first(where: {
|
||||
$0.key.id == renewalInfo?.autoRenewPreference
|
||||
})?.key
|
||||
|
||||
description = subscribedDescription(expirationDate: transaction.expirationDate,
|
||||
willRenew: renewalInfo?.willAutoRenew ?? false,
|
||||
willRenewTo: renewToProduct)
|
||||
case .expired:
|
||||
if let expirationDate = transaction.expirationDate,
|
||||
let expirationReason = renewalInfo.expirationReason {
|
||||
description = expirationDescription(expirationReason, expirationDate: expirationDate)
|
||||
}
|
||||
case .revoked:
|
||||
if let revokedDate = transaction.revocationDate {
|
||||
description = "The App Store refunded your subscription to \(product.displayName) on \(revokedDate.formattedDate())."
|
||||
}
|
||||
case .inGracePeriod:
|
||||
description = gracePeriodDescription(renewalInfo)
|
||||
case .inBillingRetryPeriod:
|
||||
description = billingRetryDescription()
|
||||
default:
|
||||
break
|
||||
}
|
||||
return description
|
||||
}
|
||||
|
||||
fileprivate func billingRetryDescription() -> String {
|
||||
var description = "The App Store could not confirm your billing information for \(product.displayName)."
|
||||
description += " Please verify your billing information to resume service."
|
||||
return description
|
||||
}
|
||||
|
||||
fileprivate func gracePeriodDescription(_ renewalInfo: RenewalInfo) -> String {
|
||||
var description = "The App Store could not confirm your billing information for \(product.displayName)."
|
||||
if let untilDate = renewalInfo.gracePeriodExpirationDate {
|
||||
description += " Please verify your billing information to continue service after \(untilDate.formattedDate())"
|
||||
}
|
||||
|
||||
return description
|
||||
}
|
||||
|
||||
fileprivate func subscribedDescription(expirationDate: Date?, willRenew: Bool, willRenewTo: Product?) -> String {
|
||||
var description = "You are currently subscribed to \(product.displayName)"
|
||||
|
||||
if let expirationDate = expirationDate {
|
||||
description += ", which will expire on \(expirationDate.formattedDate()),"
|
||||
}
|
||||
|
||||
if willRenew {
|
||||
if let willRenewTo = willRenewTo {
|
||||
if willRenewTo == product {
|
||||
description += " and will auto renew."
|
||||
} else {
|
||||
description += " and will auto renew to \(willRenewTo.displayName) at \(willRenewTo.displayPrice)."
|
||||
}
|
||||
}
|
||||
} else {
|
||||
description += " and will NOT auto renew."
|
||||
}
|
||||
|
||||
return description
|
||||
}
|
||||
|
||||
fileprivate func renewalDescription(_ renewalInfo: RenewalInfo, _ expirationDate: Date) -> String {
|
||||
var description = ""
|
||||
|
||||
if let newProductID = renewalInfo.autoRenewPreference {
|
||||
if let newProduct = iapManager.subscriptions.first(where: { $0.key.id == newProductID }) {
|
||||
description += "\nYour subscription to \(newProduct.key.displayName)"
|
||||
description += " will begin when your current subscription expires on \(expirationDate.formattedDate())."
|
||||
}
|
||||
} else if renewalInfo.willAutoRenew {
|
||||
description += "\nWill auto renew on: \(expirationDate.formattedDate())."
|
||||
}
|
||||
|
||||
return description
|
||||
}
|
||||
|
||||
//Build a string description of the `expirationReason` to display to the user.
|
||||
fileprivate func expirationDescription(_ expirationReason: RenewalInfo.ExpirationReason, expirationDate: Date) -> String {
|
||||
var description = ""
|
||||
|
||||
switch expirationReason {
|
||||
case .autoRenewDisabled:
|
||||
if expirationDate > Date() {
|
||||
description += "Your subscription to \(product.displayName) will expire on \(expirationDate.formattedDate())."
|
||||
} else {
|
||||
description += "Your subscription to \(product.displayName) expired on \(expirationDate.formattedDate())."
|
||||
}
|
||||
case .billingError:
|
||||
description = "Your subscription to \(product.displayName) was not renewed due to a billing error."
|
||||
case .didNotConsentToPriceIncrease:
|
||||
description = "Your subscription to \(product.displayName) was not renewed due to a price increase that you disapproved."
|
||||
case .productUnavailable:
|
||||
description = "Your subscription to \(product.displayName) was not renewed because the product is no longer available."
|
||||
default:
|
||||
description = "Your subscription to \(product.displayName) was not renewed."
|
||||
}
|
||||
|
||||
return description
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@ struct YearView: View {
|
||||
@AppStorage(UserDefaultsStore.Keys.moodTint.rawValue, store: GroupUserDefaults.groupDefaults) private var moodTint: MoodTints = .Default
|
||||
@AppStorage(UserDefaultsStore.Keys.textColor.rawValue, store: GroupUserDefaults.groupDefaults) private var textColor: Color = DefaultTextColor.textColor
|
||||
|
||||
@EnvironmentObject var iapManager: IAPManager
|
||||
@StateObject public var viewModel: YearViewModel
|
||||
@StateObject private var filteredDays = DaysFilterClass.shared
|
||||
//[
|
||||
@@ -52,8 +53,16 @@ struct YearView: View {
|
||||
ScrollView {
|
||||
gridView
|
||||
}
|
||||
.disabled(iapManager.showIAP)
|
||||
.padding(.bottom, 5)
|
||||
}
|
||||
|
||||
if iapManager.showIAP {
|
||||
VStack {
|
||||
Spacer()
|
||||
PurchaseButtonView(height: 175, iapManager: iapManager)
|
||||
}
|
||||
}
|
||||
}
|
||||
.onAppear(perform: {
|
||||
self.viewModel.filterEntries(startDate: Date(timeIntervalSince1970: 0), endDate: Date())
|
||||
|
||||
Reference in New Issue
Block a user