Compare commits

..

2 Commits

Author SHA1 Message Date
Shadowfacts 3eeffada1f Add tip jar link to push notifications settings 2024-05-20 12:49:26 -04:00
Shadowfacts 0499255be7 Add tip jar subscription 2024-05-20 12:49:20 -04:00
3 changed files with 307 additions and 52 deletions

View File

@ -17,6 +17,15 @@ struct NotificationsPrefsView: View {
var body: some View { var body: some View {
List { List {
NavigationLink {
TipJarView()
} label: {
Text("Push notifications are available for free to all users, but providing them has an ongoing cost. If you like the app, please consider \(Text("supporting Tusker").foregroundColor(.accentColor)).")
.font(.callout)
.foregroundStyle(.secondary)
}
.listRowBackground(Color.accentColor.opacity(0.1))
Section { Section {
ForEach(userAccounts.accounts) { account in ForEach(userAccounts.accounts) { account in
PushInstanceSettingsView(account: account) PushInstanceSettingsView(account: account)

View File

@ -11,18 +11,24 @@ import StoreKit
import Combine import Combine
struct TipJarView: View { struct TipJarView: View {
private static let productIDs = [ private static let tipProductIDs = [
"tusker.tip.small", "tusker.tip.small",
"tusker.tip.medium", "tusker.tip.medium",
"tusker.tip.large", "tusker.tip.large",
] ]
private static let supporterProductIDs = [
"tusker.supporter.regular",
]
@State private var isLoaded = false @State private var isLoaded = false
@State private var products: [(Product, Bool)] = [] @State private var tipProducts: [(Product, Bool)] = []
@State private var supporterProducts: [(Product, Bool)] = []
@State private var error: Error? @State private var error: Error?
@State private var showConfetti = false @State private var showConfetti = false
@State private var updatesObserver: Task<Void, Never>? @State private var updatesObserver: Task<Void, Never>?
@State private var buttonWidth: CGFloat? @State private var tipButtonWidth: CGFloat?
@State private var supporterButtonWidth: CGFloat?
@State private var supporterStartDate: Date?
@StateObject private var observer = UbiquitousKeyValueStoreObserver() @StateObject private var observer = UbiquitousKeyValueStoreObserver()
var body: some View { var body: some View {
@ -47,9 +53,20 @@ struct TipJarView: View {
updatesObserver = Task.detached { @MainActor in updatesObserver = Task.detached { @MainActor in
await observeTransactionUpdates() await observeTransactionUpdates()
} }
for await verificationResult in Transaction.currentEntitlements {
if case .verified(let transaction) = verificationResult,
Self.supporterProductIDs.contains(transaction.productID),
await transaction.subscriptionStatus?.state == .subscribed {
supporterStartDate = transaction.originalPurchaseDate
break
}
}
do { do {
products = try await Product.products(for: Self.productIDs).map { ($0, false) } let allProducts = try await Product.products(for: Self.tipProductIDs + Self.supporterProductIDs).map { ($0, false) }
products.sort(by: { $0.0.price < $1.0.price }) tipProducts = allProducts.filter { Self.tipProductIDs.contains($0.0.id) }
tipProducts.sort(by: { $0.0.price < $1.0.price })
supporterProducts = allProducts.filter { Self.supporterProductIDs.contains($0.0.id) }
supporterProducts.sort(by: { $0.0.price < $1.0.price })
isLoaded = true isLoaded = true
} catch { } catch {
self.error = .fetchingProducts(error) self.error = .fetchingProducts(error)
@ -67,25 +84,15 @@ struct TipJarView: View {
private var productsView: some View { private var productsView: some View {
if isLoaded { if isLoaded {
VStack { VStack {
Text("If you're enjoying using Tusker and want to show your gratitude or help support its development, it is greatly appreciated!") supporterSubscriptions
.multilineTextAlignment(.center)
.padding(.horizontal)
VStack(alignment: .myAlignment) { tipPurchases
ForEach($products, id: \.0.id) { $productAndPurchasing in
TipRow(product: productAndPurchasing.0, buttonWidth: buttonWidth, isPurchasing: $productAndPurchasing.1, showConfetti: $showConfetti)
}
}
.onPreferenceChange(ButtonWidthKey.self) { newValue in
if let buttonWidth {
self.buttonWidth = max(buttonWidth, newValue)
} else {
self.buttonWidth = newValue
}
}
if let total = getTotalTips(), total > 0 { if let tipStatus {
Text("You've tipped a total of \(Text(total, format: products[0].0.priceFormatStyle)) 😍") tipStatus
.multilineTextAlignment(.center)
.padding(.horizontal)
.padding(.top, 16)
Text("Thank you!") Text("Thank you!")
} }
} }
@ -95,19 +102,92 @@ struct TipJarView: View {
} }
} }
@ViewBuilder
private var supporterSubscriptions: some View {
Text("If you want to contribute Tusker's continued development, you can become a supporter. Supporting Tusker is an auto-renewable monthly subscription.")
.multilineTextAlignment(.center)
.padding(.horizontal)
VStack(alignment: .myAlignment) {
ForEach($supporterProducts, id: \.0.id) { $productAndPurchasing in
TipRow(product: productAndPurchasing.0, buttonWidth: supporterButtonWidth, isPurchasing: $productAndPurchasing.1, showConfetti: $showConfetti)
}
}
.onPreferenceChange(ButtonWidthKey.self) { newValue in
if let supporterButtonWidth {
self.supporterButtonWidth = max(supporterButtonWidth, newValue)
} else {
self.supporterButtonWidth = newValue
}
}
}
@ViewBuilder
private var tipPurchases: some View {
Text("Or, you can choose to make a one-time tip to show your gratitutde or help support the app's development. It is greatly appreciated!")
.multilineTextAlignment(.center)
.padding(.horizontal)
.padding(.top, 16)
VStack(alignment: .myAlignment) {
ForEach($tipProducts, id: \.0.id) { $productAndPurchasing in
TipRow(product: productAndPurchasing.0, buttonWidth: tipButtonWidth, isPurchasing: $productAndPurchasing.1, showConfetti: $showConfetti)
}
}
.onPreferenceChange(ButtonWidthKey.self) { newValue in
if let tipButtonWidth {
self.tipButtonWidth = max(tipButtonWidth, newValue)
} else {
self.tipButtonWidth = newValue
}
}
}
private var tipStatus: Text? {
var text: Text?
if let supporterStartDate {
var months = Calendar.current.dateComponents([.month], from: supporterStartDate, to: Date()).month!
// the user has already paid for n months before the nth month has finished, so reflect that
months += 1
text = Text("You've been a supporter for ^[\(months) months](inflect: true)")
}
if let total = getTotalTips(),
total > 0 {
if let t = text {
text = Text("\(t) and tipped \(total.formatted(tipProducts[0].0.priceFormatStyle))")
} else {
text = Text("You've tipped \(total.formatted(tipProducts[0].0.priceFormatStyle))")
}
}
if let text {
return Text("\(text) 😍")
} else {
return nil
}
}
@MainActor @MainActor
private func observeTransactionUpdates() async { private func observeTransactionUpdates() async {
for await verificationResult in StoreKit.Transaction.updates { for await verificationResult in StoreKit.Transaction.updates {
guard let index = products.firstIndex(where: { $0.0.id == verificationResult.unsafePayloadValue.productID }) else { if let index = tipProducts.firstIndex(where: { $0.0.id == verificationResult.unsafePayloadValue.productID }) {
continue switch verificationResult {
} case .verified(let transaction):
switch verificationResult { await transaction.finish()
case .verified(let transaction): self.tipProducts[index].1 = false
await transaction.finish() self.showConfetti = true
self.products[index].1 = false case .unverified(_, let error):
self.showConfetti = true self.error = .verifyingTransaction(error)
case .unverified(_, let error): }
self.error = .verifyingTransaction(error) } else if let index = supporterProducts.firstIndex(where: { $0.0.id == verificationResult.unsafePayloadValue.productID }) {
switch verificationResult {
case .verified(let transaction):
await transaction.finish()
self.supporterProducts[index].1 = false
self.showConfetti = true
self.supporterStartDate = transaction.originalPurchaseDate
case .unverified(_, let error):
self.error = .verifyingTransaction(error)
}
} }
} }
} }
@ -149,25 +229,11 @@ private struct TipRow: View {
Text(product.displayName) Text(product.displayName)
.alignmentGuide(.myAlignment, computeValue: { context in context[.trailing] }) .alignmentGuide(.myAlignment, computeValue: { context in context[.trailing] })
Button { if let subscription = product.subscription {
Task { SubscriptionButton(product: product, subscriptionInfo: subscription, isPurchasing: $isPurchasing, buttonWidth: buttonWidth, purchase: purchase)
await self.purchase() } else {
} tipButton
} label: {
if isPurchasing {
ProgressView()
.progressViewStyle(.circular)
.frame(width: buttonWidth, alignment: .center)
} else {
Text(product.displayPrice)
.background(GeometryReader { proxy in
Color.clear
.preference(key: ButtonWidthKey.self, value: proxy.size.width)
})
.frame(width: buttonWidth)
}
} }
.buttonStyle(.borderedProminent)
} }
.alertWithData("Error", data: $error) { _ in .alertWithData("Error", data: $error) { _ in
Button("OK") {} Button("OK") {}
@ -176,6 +242,28 @@ private struct TipRow: View {
} }
} }
private var tipButton: some View {
Button {
Task {
await self.purchase()
}
} label: {
if isPurchasing {
ProgressView()
.progressViewStyle(.circular)
.frame(width: buttonWidth, alignment: .center)
} else {
Text(product.displayPrice)
.background(GeometryReader { proxy in
Color.clear
.preference(key: ButtonWidthKey.self, value: proxy.size.width)
})
.frame(width: buttonWidth)
}
}
.buttonStyle(.borderedProminent)
}
@MainActor @MainActor
private func purchase() async { private func purchase() async {
isPurchasing = true isPurchasing = true
@ -221,6 +309,71 @@ private struct TipRow: View {
} }
} }
private struct SubscriptionButton: View {
let product: Product
let subscriptionInfo: Product.SubscriptionInfo
@Binding var isPurchasing: Bool
let buttonWidth: CGFloat?
let purchase: () async -> Void
@State private var hasPurchased = false
@State private var showManageSheet = false
var body: some View {
Button {
if #available(iOS 17.0, *), hasPurchased {
showManageSheet = true
} else {
Task {
await purchase()
await updateHasPurchased()
}
}
} label: {
if #available(iOS 17.0, *), hasPurchased {
Text("Manage")
} else if isPurchasing {
ProgressView()
.progressViewStyle(.circular)
.frame(width: buttonWidth, alignment: .center)
} else {
let per: String = if subscriptionInfo.subscriptionPeriod.value == 1, subscriptionInfo.subscriptionPeriod.unit == .month {
"mo"
} else {
subscriptionInfo.subscriptionPeriod.formatted(product.subscriptionPeriodFormatStyle)
}
Text("\(product.displayPrice)/\(per)")
.background(GeometryReader { proxy in
Color.clear
.preference(key: ButtonWidthKey.self, value: proxy.size.width)
})
.frame(width: buttonWidth)
}
}
.buttonStyle(.borderedProminent)
.task {
await updateHasPurchased()
}
.onChange(of: showManageSheet) {
if !$0 {
Task {
await updateHasPurchased()
}
}
}
.manageSubscriptionsSheetIfAvailable(isPresented: $showManageSheet, subscriptionGroupID: subscriptionInfo.subscriptionGroupID)
}
private func updateHasPurchased() async {
switch await Transaction.currentEntitlement(for: product.id) {
case .verified(let transaction):
let state = await transaction.subscriptionStatus?.state
hasPurchased = state == .subscribed
default:
break
}
}
}
extension HorizontalAlignment { extension HorizontalAlignment {
private enum MyTrailing: AlignmentID { private enum MyTrailing: AlignmentID {
static func defaultValue(in context: ViewDimensions) -> CGFloat { static func defaultValue(in context: ViewDimensions) -> CGFloat {
@ -263,3 +416,15 @@ private class UbiquitousKeyValueStoreObserver: ObservableObject {
} }
} }
} }
private extension View {
@available(iOS, obsoleted: 17.0)
@ViewBuilder
func manageSubscriptionsSheetIfAvailable(isPresented: Binding<Bool>, subscriptionGroupID: String) -> some View {
if #available(iOS 17.0, *) {
self.manageSubscriptionsSheet(isPresented: isPresented, subscriptionGroupID: subscriptionGroupID)
} else {
self
}
}
}

View File

@ -53,13 +53,94 @@
"settings" : { "settings" : {
"_applicationInternalID" : "1498334597", "_applicationInternalID" : "1498334597",
"_developerTeamID" : "V4WK9KR9U2", "_developerTeamID" : "V4WK9KR9U2",
"_lastSynchronizedDate" : 696310076.23998904 "_failTransactionsEnabled" : false,
"_lastSynchronizedDate" : 737914663.21114194,
"_storeKitErrors" : [
{
"current" : null,
"enabled" : false,
"name" : "Load Products"
},
{
"current" : null,
"enabled" : false,
"name" : "Purchase"
},
{
"current" : null,
"enabled" : false,
"name" : "Verification"
},
{
"current" : null,
"enabled" : false,
"name" : "App Store Sync"
},
{
"current" : null,
"enabled" : false,
"name" : "Subscription Status"
},
{
"current" : null,
"enabled" : false,
"name" : "App Transaction"
},
{
"current" : null,
"enabled" : false,
"name" : "Manage Subscriptions Sheet"
},
{
"current" : null,
"enabled" : false,
"name" : "Refund Request Sheet"
},
{
"current" : null,
"enabled" : false,
"name" : "Offer Code Redeem Sheet"
}
]
}, },
"subscriptionGroups" : [ "subscriptionGroups" : [
{
"id" : "21490109",
"localizations" : [
],
"name" : "Tip Jar",
"subscriptions" : [
{
"adHocOffers" : [
],
"codeOffers" : [
],
"displayPrice" : "1.99",
"familyShareable" : false,
"groupNumber" : 1,
"internalID" : "6502909920",
"introductoryOffer" : null,
"localizations" : [
{
"description" : "Support the continued development of Tusker!",
"displayName" : "Tusker Supporter",
"locale" : "en_US"
}
],
"productID" : "tusker.supporter.regular",
"recurringSubscriptionPeriod" : "P1M",
"referenceName" : "tusker.supporter.regular",
"subscriptionGroupID" : "21490109",
"type" : "RecurringSubscription"
}
]
}
], ],
"version" : { "version" : {
"major" : 2, "major" : 3,
"minor" : 0 "minor" : 0
} }
} }