IceCubesApp/IceCubesApp/App/Tabs/Settings/SupportAppView.swift

284 lines
7.8 KiB
Swift
Raw Normal View History

2023-01-07 12:44:13 +00:00
import DesignSystem
2023-01-17 10:36:01 +00:00
import Env
2023-01-07 12:44:13 +00:00
import RevenueCat
2023-01-17 10:36:01 +00:00
import SwiftUI
2023-01-07 12:44:13 +00:00
2023-09-18 19:03:52 +00:00
@MainActor
2023-01-07 12:44:13 +00:00
struct SupportAppView: View {
2023-02-21 17:52:30 +00:00
enum Tip: String, CaseIterable {
case one, two, three, four, supporter
2023-01-17 10:36:01 +00:00
2023-01-07 12:44:13 +00:00
init(productId: String) {
self = .init(rawValue: String(productId.split(separator: ".")[2]))!
}
2023-01-17 10:36:01 +00:00
2023-01-07 12:44:13 +00:00
var productId: String {
"icecubes.tipjar.\(rawValue)"
}
2023-01-22 05:38:30 +00:00
var title: LocalizedStringKey {
2023-01-07 12:44:13 +00:00
switch self {
case .one:
2023-09-16 12:15:03 +00:00
"settings.support.one.title"
2023-01-07 12:44:13 +00:00
case .two:
2023-09-16 12:15:03 +00:00
"settings.support.two.title"
2023-01-07 12:44:13 +00:00
case .three:
2023-09-16 12:15:03 +00:00
"settings.support.three.title"
2023-01-22 11:27:00 +00:00
case .four:
2023-09-16 12:15:03 +00:00
"settings.support.four.title"
case .supporter:
2023-09-16 12:15:03 +00:00
"settings.support.supporter.title"
2023-01-07 12:44:13 +00:00
}
}
2023-01-22 05:38:30 +00:00
var subtitle: LocalizedStringKey {
2023-01-07 12:44:13 +00:00
switch self {
case .one:
2023-09-16 12:15:03 +00:00
"settings.support.one.subtitle"
2023-01-07 12:44:13 +00:00
case .two:
2023-09-16 12:15:03 +00:00
"settings.support.two.subtitle"
2023-01-07 12:44:13 +00:00
case .three:
2023-09-16 12:15:03 +00:00
"settings.support.three.subtitle"
2023-01-22 11:27:00 +00:00
case .four:
2023-09-16 12:15:03 +00:00
"settings.support.four.subtitle"
case .supporter:
2023-09-16 12:15:03 +00:00
"settings.support.supporter.subtitle"
2023-01-07 12:44:13 +00:00
}
}
}
2023-01-17 10:36:01 +00:00
2023-09-18 19:03:52 +00:00
@Environment(Theme.self) private var theme
2023-03-13 12:38:28 +00:00
@Environment(\.openURL) private var openURL
2023-01-17 10:36:01 +00:00
2023-01-07 12:44:13 +00:00
@State private var loadingProducts: Bool = false
@State private var products: [StoreProduct] = []
@State private var subscription: StoreProduct?
@State private var customerInfo: CustomerInfo?
2023-01-07 12:44:13 +00:00
@State private var isProcessingPurchase: Bool = false
@State private var purchaseSuccessDisplayed: Bool = false
@State private var purchaseErrorDisplayed: Bool = false
2023-01-17 10:36:01 +00:00
2023-01-07 12:44:13 +00:00
var body: some View {
Form {
aboutSection
subscriptionSection
tipsSection
restorePurchase
linksSection
2023-01-07 12:44:13 +00:00
}
.navigationTitle("settings.support.navigation-title")
#if !os(visionOS)
2024-02-14 11:48:14 +00:00
.scrollContentBackground(.hidden)
.background(theme.secondaryBackgroundColor)
#endif
2024-02-14 11:48:14 +00:00
.alert("settings.support.alert.title", isPresented: $purchaseSuccessDisplayed, actions: {
Button { purchaseSuccessDisplayed = false } label: { Text("alert.button.ok") }
}, message: {
Text("settings.support.alert.message")
})
.alert("alert.error", isPresented: $purchaseErrorDisplayed, actions: {
Button { purchaseErrorDisplayed = false } label: { Text("alert.button.ok") }
}, message: {
Text("settings.support.alert.error.message")
})
.onAppear {
loadingProducts = true
fetchStoreProducts()
refreshUserInfo()
}
}
2023-03-13 12:38:28 +00:00
private func purchase(product: StoreProduct) async {
if !isProcessingPurchase {
isProcessingPurchase = true
do {
let result = try await Purchases.shared.purchase(product: product)
if !result.userCancelled {
purchaseSuccessDisplayed = true
2023-01-10 07:24:05 +00:00
}
} catch {
purchaseErrorDisplayed = true
}
isProcessingPurchase = false
}
}
2023-03-13 12:38:28 +00:00
private func fetchStoreProducts() {
2023-09-16 12:15:03 +00:00
Purchases.shared.getProducts(Tip.allCases.map(\.productId)) { products in
subscription = products.first(where: { $0.productIdentifier == Tip.supporter.productId })
2023-03-13 12:38:28 +00:00
self.products = products.filter { $0.productIdentifier != Tip.supporter.productId }.sorted(by: { $0.price < $1.price })
withAnimation {
loadingProducts = false
}
}
}
2023-03-13 12:38:28 +00:00
private func refreshUserInfo() {
Purchases.shared.getCustomerInfo { info, _ in
2023-09-16 12:15:03 +00:00
customerInfo = info
}
}
2023-03-13 12:38:28 +00:00
private func makePurchaseButton(product: StoreProduct) -> some View {
Button {
Task {
await purchase(product: product)
refreshUserInfo()
}
} label: {
if isProcessingPurchase {
ProgressView()
} else {
Text(product.localizedPriceString)
}
}
.buttonStyle(.bordered)
}
2023-03-13 12:38:28 +00:00
private var aboutSection: some View {
Section {
HStack(alignment: .top, spacing: 12) {
VStack(spacing: 18) {
Image("avatar")
.resizable()
.frame(width: 50, height: 50)
.cornerRadius(4)
Image("icon0")
.resizable()
.frame(width: 50, height: 50)
.cornerRadius(4)
}
Text("settings.support.message-from-dev")
}
}
#if !os(visionOS)
.listRowBackground(theme.primaryBackgroundColor)
#endif
}
2023-03-13 12:38:28 +00:00
private var subscriptionSection: some View {
Section {
if loadingProducts {
loadingPlaceholder
} else if let subscription {
HStack {
2023-03-01 20:14:26 +00:00
if customerInfo?.entitlements["Supporter"]?.isActive == true {
Text(Image(systemName: "checkmark.seal.fill"))
.foregroundColor(theme.tintColor)
.baselineOffset(-1) +
2023-03-13 12:38:28 +00:00
Text("settings.support.supporter.subscribed")
.font(.scaledSubheadline)
} else {
VStack(alignment: .leading) {
Text(Image(systemName: "checkmark.seal.fill"))
.foregroundColor(theme.tintColor)
.baselineOffset(-1) +
2023-03-13 12:38:28 +00:00
Text(Tip.supporter.title)
.font(.scaledSubheadline)
Text(Tip.supporter.subtitle)
.font(.scaledFootnote)
2023-12-04 14:49:44 +00:00
.foregroundStyle(.secondary)
}
Spacer()
makePurchaseButton(product: subscription)
}
}
.padding(.vertical, 8)
}
} footer: {
if customerInfo?.entitlements.active.isEmpty == true {
Text("settings.support.supporter.subscription-info")
}
}
#if !os(visionOS)
.listRowBackground(theme.primaryBackgroundColor)
#endif
}
2023-03-13 12:38:28 +00:00
private var tipsSection: some View {
Section {
if loadingProducts {
loadingPlaceholder
} else {
ForEach(products, id: \.productIdentifier) { product in
let tip = Tip(productId: product.productIdentifier)
HStack {
VStack(alignment: .leading) {
Text(tip.title)
.font(.scaledSubheadline)
Text(tip.subtitle)
.font(.scaledFootnote)
2023-12-04 14:49:44 +00:00
.foregroundStyle(.secondary)
}
Spacer()
makePurchaseButton(product: product)
}
.padding(.vertical, 8)
}
}
}
#if !os(visionOS)
.listRowBackground(theme.primaryBackgroundColor)
#endif
}
2023-03-13 12:38:28 +00:00
private var restorePurchase: some View {
Section {
HStack {
Spacer()
Button {
Purchases.shared.restorePurchases { info, _ in
2023-09-16 12:15:03 +00:00
customerInfo = info
}
} label: {
Text("settings.support.restore-purchase.button")
}.buttonStyle(.bordered)
Spacer()
}
} footer: {
Text("settings.support.restore-purchase.explanation")
}
#if !os(visionOS)
.listRowBackground(theme.secondaryBackgroundColor)
#endif
}
2023-03-13 12:38:28 +00:00
private var linksSection: some View {
Section {
VStack(alignment: .leading, spacing: 16) {
Button {
openURL(URL(string: "https://github.com/Dimillian/IceCubesApp/blob/main/PRIVACY.MD")!)
} label: {
Text("settings.support.privacy-policy")
}
.buttonStyle(.borderless)
Button {
openURL(URL(string: "https://github.com/Dimillian/IceCubesApp/blob/main/TERMS.MD")!)
} label: {
Text("settings.support.terms-of-use")
}
.buttonStyle(.borderless)
}
}
#if !os(visionOS)
.listRowBackground(theme.secondaryBackgroundColor)
#endif
}
2023-03-13 12:38:28 +00:00
private var loadingPlaceholder: some View {
HStack {
VStack(alignment: .leading) {
2023-10-05 08:28:39 +00:00
Text("placeholder.loading.short")
.font(.scaledSubheadline)
Text("settings.support.placeholder.loading-subtitle")
.font(.scaledFootnote)
2023-12-04 14:49:44 +00:00
.foregroundStyle(.secondary)
2023-01-07 12:44:13 +00:00
}
.padding(.vertical, 8)
2023-01-07 12:44:13 +00:00
}
.redacted(reason: .placeholder)
2023-09-18 16:55:11 +00:00
.allowsHitTesting(false)
2023-01-07 12:44:13 +00:00
}
}