Add Contribution view with review and donation buttons
This commit is contained in:
@@ -24,7 +24,25 @@ struct K {
|
||||
"oth": UIColor.systemBrown,
|
||||
"extra": UIColor.systemBrown
|
||||
]
|
||||
|
||||
|
||||
struct UserDefaults {
|
||||
static let donationHearts = "donationHearts"
|
||||
static let hasDonated = "hasDonated"
|
||||
}
|
||||
|
||||
struct ConsumableTransactions {
|
||||
static let TierOne = "SingleTimeTipTierOne"
|
||||
static let TierTwo = "SingleTimeTipTierTwo"
|
||||
static let TierThree = "SingleTimeTipTierThree"
|
||||
static let TierFour = "SingleTimeTipTierFour"
|
||||
}
|
||||
|
||||
struct MonthlyTransactions {
|
||||
static let TierOne = "TierOne"
|
||||
static let TierTwo = "TierTwo"
|
||||
static let TierThree = "TierThree"
|
||||
}
|
||||
|
||||
static var getFlashCardAnswersFetchRequest: NSFetchRequest<FlashCardAnswer> {
|
||||
let request: NSFetchRequest<FlashCardAnswer> = FlashCardAnswer.fetchRequest()
|
||||
request.sortDescriptors = []
|
||||
|
@@ -20,6 +20,12 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"%lld 💕" : {
|
||||
|
||||
},
|
||||
"Contribute" : {
|
||||
|
||||
},
|
||||
"Correct" : {
|
||||
"localizations" : {
|
||||
@@ -50,6 +56,12 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"Donate Monthly" : {
|
||||
|
||||
},
|
||||
"Donate Once" : {
|
||||
|
||||
},
|
||||
"Enter Toki Pona Word or Phrase" : {
|
||||
"localizations" : {
|
||||
@@ -110,6 +122,9 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"One-Time Donation" : {
|
||||
|
||||
},
|
||||
"Parts of Speech" : {
|
||||
"localizations" : {
|
||||
@@ -170,6 +185,9 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"Thank you for donating!" : {
|
||||
|
||||
},
|
||||
"Words" : {
|
||||
"localizations" : {
|
||||
@@ -180,6 +198,9 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"Write Review" : {
|
||||
|
||||
}
|
||||
},
|
||||
"version" : "1.0"
|
||||
|
98
Toki Trainer/Models/TransactionObserver.swift
Normal file
98
Toki Trainer/Models/TransactionObserver.swift
Normal file
@@ -0,0 +1,98 @@
|
||||
//
|
||||
// Store.swift
|
||||
// Toki Trainer
|
||||
//
|
||||
// Created by Madeline Pace on 12/18/23.
|
||||
//
|
||||
|
||||
import CoreData
|
||||
import StoreKit
|
||||
|
||||
@MainActor
|
||||
final class TransactionObserver: ObservableObject {
|
||||
|
||||
var updates: Task<Void, Never>? = nil
|
||||
|
||||
@Published var donationHearts: Int = 0
|
||||
@Published var hasDonated = false
|
||||
|
||||
|
||||
init() {
|
||||
updates = newTransactionListenerTask()
|
||||
donationHearts = UserDefaults.standard.integer(forKey: K.UserDefaults.donationHearts)
|
||||
hasDonated = UserDefaults.standard.bool(forKey: K.UserDefaults.hasDonated)
|
||||
}
|
||||
|
||||
deinit {
|
||||
updates?.cancel()
|
||||
}
|
||||
|
||||
func addDonationHearts(_ amount: Int) {
|
||||
if hasDonated == false {
|
||||
hasDonated = true
|
||||
UserDefaults.standard.set(hasDonated, forKey: K.UserDefaults.hasDonated)
|
||||
}
|
||||
|
||||
DispatchQueue.main.async {
|
||||
self.donationHearts += amount
|
||||
}
|
||||
UserDefaults.standard.set(donationHearts, forKey: K.UserDefaults.donationHearts)
|
||||
}
|
||||
|
||||
private func newTransactionListenerTask() -> Task<Void, Never> {
|
||||
Task(priority: .background) {
|
||||
for await verificationResult in Transaction.updates {
|
||||
await self.handle(updatedTransaction: verificationResult)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func handle(updatedTransaction verificationResult: VerificationResult<Transaction>) async {
|
||||
guard case .verified(let transaction) = verificationResult else {
|
||||
return
|
||||
}
|
||||
|
||||
switch transaction.productType {
|
||||
case Product.ProductType.consumable:
|
||||
processConsumable(transaction.productID)
|
||||
case Product.ProductType.nonRenewable, Product.ProductType.autoRenewable:
|
||||
processSubscription(transaction.productID)
|
||||
default:
|
||||
return
|
||||
}
|
||||
|
||||
print("Finishing transaction")
|
||||
await transaction.finish()
|
||||
}
|
||||
|
||||
func processConsumable(_ productID: String) {
|
||||
print("Consumable ID: \(productID)")
|
||||
switch productID {
|
||||
case K.ConsumableTransactions.TierOne:
|
||||
self.addDonationHearts(100)
|
||||
case K.ConsumableTransactions.TierTwo:
|
||||
self.addDonationHearts(500)
|
||||
case K.ConsumableTransactions.TierThree:
|
||||
self.addDonationHearts(1000)
|
||||
case K.ConsumableTransactions.TierFour:
|
||||
self.addDonationHearts(2000)
|
||||
default:
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func processSubscription(_ productID: String) {
|
||||
// TODO: Write function
|
||||
print("Subscription ID: \(productID)")
|
||||
switch productID {
|
||||
case K.MonthlyTransactions.TierOne:
|
||||
self.addDonationHearts(100)
|
||||
case K.MonthlyTransactions.TierTwo:
|
||||
self.addDonationHearts(500)
|
||||
case K.MonthlyTransactions.TierThree:
|
||||
self.addDonationHearts(1000)
|
||||
default:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
@@ -6,6 +6,7 @@
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import StoreKit
|
||||
import CoreData
|
||||
|
||||
extension String: Identifiable {
|
||||
@@ -14,7 +15,9 @@ extension String: Identifiable {
|
||||
|
||||
struct ContentView: View {
|
||||
@Environment(\.managedObjectContext) private var viewContext
|
||||
|
||||
|
||||
@StateObject var transactions: TransactionObserver = TransactionObserver()
|
||||
|
||||
var body: some View {
|
||||
TabView {
|
||||
DictionaryView()
|
||||
@@ -32,9 +35,15 @@ struct ContentView: View {
|
||||
Image(systemName: "character.textbox")
|
||||
Text("Flash Cards")
|
||||
}
|
||||
ContributeView()
|
||||
.tabItem {
|
||||
Image(systemName: "heart.circle.fill")
|
||||
Text("Contribute")
|
||||
}
|
||||
}
|
||||
.environmentObject(transactions)
|
||||
}
|
||||
|
||||
|
||||
func openPartsOfSpeechView() {
|
||||
print("Button pressed.")
|
||||
}
|
||||
|
263
Toki Trainer/Views/ContributeView.swift
Normal file
263
Toki Trainer/Views/ContributeView.swift
Normal file
@@ -0,0 +1,263 @@
|
||||
//
|
||||
// ContributeView.swift
|
||||
// Toki Trainer
|
||||
//
|
||||
// Created by Madeline Pace on 12/16/23.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import StoreKit
|
||||
|
||||
typealias Transaction = StoreKit.Transaction
|
||||
|
||||
public enum StoreError: Error {
|
||||
case failedVerification
|
||||
}
|
||||
|
||||
// MARK: ContributeView
|
||||
struct ContributeView: View {
|
||||
|
||||
private let supportString = """
|
||||
Hi 👋
|
||||
|
||||
I'm Maddie, the primary developer for Toki Trainer.
|
||||
|
||||
This app is free and open source. If you find it \
|
||||
useful, please consider supporting my development efforts.
|
||||
|
||||
I don't collect your data and am committed to providing \
|
||||
a high-quality, ad-free experience. Learning toki pona \
|
||||
easily and comfortably is the whole point, and ads would \
|
||||
ruin that!
|
||||
|
||||
Taking a moment to add an App Store Review helps a ton. \
|
||||
Please also consider donating financially if you can. 💕
|
||||
"""
|
||||
|
||||
|
||||
|
||||
private var recurringIAPs = ["TierThree"]
|
||||
|
||||
@EnvironmentObject var transactions: TransactionObserver
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
Spacer()
|
||||
if transactions.hasDonated {
|
||||
ThankYouBannerView(donationHearts: $transactions.donationHearts)
|
||||
}
|
||||
Text(supportString)
|
||||
.padding(16)
|
||||
Spacer()
|
||||
HStack {
|
||||
ReviewButton()
|
||||
SingleDonationButton()
|
||||
RecurringDonationButton()
|
||||
}
|
||||
Spacer()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: ThankYouBannerView
|
||||
struct ThankYouBannerView: View {
|
||||
|
||||
@Binding var donationHearts: Int
|
||||
|
||||
var body: some View {
|
||||
ZStack {
|
||||
Rectangle()
|
||||
.fill(.blue)
|
||||
.cornerRadius(15)
|
||||
VStack {
|
||||
Text("Thank you for donating!")
|
||||
.multilineTextAlignment(.center)
|
||||
.font(.title)
|
||||
Text("\(donationHearts) 💕")
|
||||
.padding(10)
|
||||
.font(.title3)
|
||||
}
|
||||
}
|
||||
.frame(width: 250, height: 140)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: ReviewButton
|
||||
struct ReviewButton: View {
|
||||
@Environment(\.requestReview) private var requestReview
|
||||
|
||||
var body: some View {
|
||||
Button(action: {
|
||||
print("Review requested")
|
||||
presentReviewRequest()
|
||||
}, label: {
|
||||
VStack {
|
||||
Image(systemName: "star.bubble")
|
||||
.font(.system(size: 24, weight: .regular))
|
||||
.padding(2)
|
||||
Text("Write Review")
|
||||
}
|
||||
})
|
||||
.frame(width: 80)
|
||||
.padding(8)
|
||||
}
|
||||
|
||||
func presentReviewRequest() {
|
||||
Task {
|
||||
await requestReview()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Workaround for .sheet(item:) expecting an Identifiable
|
||||
struct DonationProducts: Identifiable {
|
||||
let id = UUID()
|
||||
var products = [Product]()
|
||||
}
|
||||
|
||||
// MARK: SingleDonationButton
|
||||
struct SingleDonationButton: View {
|
||||
@Environment(\.purchase) private var purchase: PurchaseAction
|
||||
|
||||
@EnvironmentObject var transactions: TransactionObserver
|
||||
|
||||
@State private var IAPs: DonationProducts?
|
||||
@State private var productsFetched = false
|
||||
@State private var disabledPurchaseButtons = false
|
||||
|
||||
|
||||
private var singleIAPs = ["SingleTimeTipTierOne",
|
||||
"SingleTimeTipTierTwo",
|
||||
"SingleTimeTipTierThree",
|
||||
"SingleTimeTipTierFour"]
|
||||
|
||||
var body: some View {
|
||||
Button {
|
||||
Task {
|
||||
try await loadSingleProducts()
|
||||
}
|
||||
} label: {
|
||||
VStack {
|
||||
Image(systemName: "dollarsign.circle")
|
||||
.font(.system(size: 24, weight: .regular))
|
||||
.padding(2)
|
||||
Text("Donate Once")
|
||||
}
|
||||
}
|
||||
.frame(width: 80)
|
||||
.padding(8)
|
||||
.sheet(item: self.$IAPs) { IAPs in
|
||||
if(productsFetched) {
|
||||
Text("One-Time Donation")
|
||||
.font(.largeTitle)
|
||||
ForEach(IAPs.products) { product in
|
||||
HStack {
|
||||
VStack {
|
||||
Text(product.displayName)
|
||||
.font(.title)
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
Text(product.description)
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
}
|
||||
.padding(.leading, 12)
|
||||
Spacer()
|
||||
Button {
|
||||
print("Purchase this one: \(product)")
|
||||
disabledPurchaseButtons = true
|
||||
Task {
|
||||
let _ = try? await purchase(product)
|
||||
disabledPurchaseButtons = false
|
||||
}
|
||||
} label: {
|
||||
Text(product.displayPrice)
|
||||
.frame(minWidth: 50)
|
||||
.padding(8)
|
||||
.fontWeight(/*@START_MENU_TOKEN@*/.bold/*@END_MENU_TOKEN@*/)
|
||||
}
|
||||
.padding(.trailing, 12)
|
||||
.buttonStyle(.bordered)
|
||||
.disabled(disabledPurchaseButtons)
|
||||
}
|
||||
.padding(12)
|
||||
}
|
||||
} else {
|
||||
ProgressView()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func purchase(_ product: Product) async throws -> Transaction? {
|
||||
let result = try await product.purchase()
|
||||
|
||||
switch result {
|
||||
case .success(let verification):
|
||||
let transaction = try checkVerified(verification)
|
||||
await transaction.finish()
|
||||
print("Purchase success")
|
||||
transactions.processConsumable(transaction.productID)
|
||||
return transaction
|
||||
case .userCancelled, .pending:
|
||||
return nil
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
private func loadSingleProducts() async throws {
|
||||
self.IAPs = DonationProducts()
|
||||
self.IAPs?.products = try await Product.products(for: singleIAPs).sorted(by: { p1, p2 in
|
||||
p1.price < p2.price
|
||||
})
|
||||
self.productsFetched = true
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: RecurringDonationButton
|
||||
struct RecurringDonationButton: View {
|
||||
|
||||
@State var toggleSheet = false
|
||||
|
||||
private let groupID = "21424772"
|
||||
|
||||
var body: some View {
|
||||
Button {
|
||||
print("Subscription button pressed")
|
||||
toggleSheet.toggle()
|
||||
} label: {
|
||||
VStack {
|
||||
Image(systemName: "dollarsign.arrow.circlepath")
|
||||
.font(.system(size: 24, weight: .regular))
|
||||
.padding(2)
|
||||
Text("Donate Monthly")
|
||||
}
|
||||
}
|
||||
.frame(width: 80)
|
||||
.padding(8)
|
||||
.sheet(isPresented: $toggleSheet, content: {
|
||||
SubscriptionStoreView(groupID: self.groupID)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
ContributeView().environmentObject(TransactionObserver())
|
||||
}
|
||||
|
||||
#Preview {
|
||||
@State var donationHearts = 5000
|
||||
|
||||
return ThankYouBannerView(donationHearts: $donationHearts)
|
||||
}
|
Reference in New Issue
Block a user