iap - wip

This commit is contained in:
Trey t
2022-07-17 10:26:00 -05:00
parent 6c239c5e26
commit bd238e5743
15 changed files with 897 additions and 22 deletions

View File

@@ -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
)

View File

@@ -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)

View 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())
}
}

View File

@@ -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 {

View 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
}
}

View File

@@ -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())