-
-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1bf09db
commit a0783c9
Showing
15 changed files
with
401 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// | ||
// TipJarView.swift | ||
// BookPlayer | ||
// | ||
// Created by Gianni Carlo on 2/2/25. | ||
// Copyright © 2025 BookPlayer LLC. All rights reserved. | ||
// | ||
|
||
import BookPlayerKit | ||
import SwiftUI | ||
|
||
struct TipJarView: View { | ||
@ObservedObject var viewModel: TipJarViewModel | ||
@StateObject var themeViewModel = ThemeViewModel() | ||
@State var selected: TipOption = TipOption.excellent | ||
@State var error: Error? | ||
|
||
var body: some View { | ||
NavigationView { | ||
VStack { | ||
if let disclaimer = viewModel.disclaimer { | ||
Text(disclaimer) | ||
.foregroundColor(themeViewModel.primaryColor) | ||
.font(Font(Fonts.titleRegular)) | ||
.padding() | ||
} | ||
|
||
HStack(spacing: Spacing.S1) { | ||
Spacer() | ||
ForEach(TipOption.allCases) { option in | ||
TipOptionView( | ||
title: .constant(option.title), | ||
price: .constant(option.price), | ||
isSelected: .constant(selected == option) | ||
) | ||
.onTapGesture { | ||
selected = option | ||
} | ||
} | ||
Spacer() | ||
} | ||
Button(action: { | ||
Task { @MainActor in | ||
await viewModel.donate(selected) | ||
} | ||
}) { | ||
Text("Donate") | ||
.contentShape(Rectangle()) | ||
.font(Font(Fonts.headline)) | ||
.frame(height: 45) | ||
.frame(maxWidth: .infinity) | ||
.foregroundColor(.white) | ||
.background(Color(UIColor(hex: "687AB7"))) | ||
.cornerRadius(6) | ||
.padding(.top, Spacing.S1) | ||
} | ||
Spacer() | ||
} | ||
.padding(.horizontal, Spacing.M) | ||
.background(themeViewModel.systemGroupedBackgroundColor) | ||
.toolbar { | ||
ToolbarItem(placement: .cancellationAction) { | ||
Button { | ||
viewModel.dismiss() | ||
} label: { | ||
Image(systemName: "xmark") | ||
} | ||
} | ||
|
||
ToolbarItem(placement: .confirmationAction) { | ||
Button("Restore") { | ||
Task { @MainActor in | ||
await viewModel.restorePurchases() | ||
} | ||
} | ||
} | ||
} | ||
.navigationTitle("Tip Jar") | ||
.navigationBarTitleDisplayMode(.inline) | ||
} | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
BookPlayer/SecondOnboarding/Support/Tips/TipJarViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// | ||
// TipJarViewModel.swift | ||
// BookPlayer | ||
// | ||
// Created by Gianni Carlo on 3/2/25. | ||
// Copyright © 2025 BookPlayer LLC. All rights reserved. | ||
// | ||
|
||
import BookPlayerKit | ||
import Foundation | ||
import RevenueCat | ||
|
||
@MainActor | ||
public final class TipJarViewModel: ObservableObject { | ||
enum Routes { | ||
case showLoader(Bool) | ||
case showAlert(BPAlertContent) | ||
case success(message: String) | ||
case dismiss | ||
} | ||
|
||
let disclaimer: String? | ||
let accountService: AccountServiceProtocol | ||
/// Callback to handle actions on this screen | ||
var onTransition: BPTransition<Routes>? | ||
|
||
init( | ||
disclaimer: String?, | ||
accountService: AccountServiceProtocol | ||
) { | ||
self.disclaimer = disclaimer | ||
self.accountService = accountService | ||
} | ||
|
||
func donate(_ tip: TipOption) async { | ||
onTransition?(.showLoader(true)) | ||
do { | ||
let product = await Purchases.shared.products([tip.rawValue]).first! | ||
let result = try await Purchases.shared.purchase(product: product) | ||
onTransition?(.showLoader(false)) | ||
if !result.userCancelled { | ||
accountService.updateAccount( | ||
id: nil, | ||
email: nil, | ||
donationMade: true, | ||
hasSubscription: nil | ||
) | ||
onTransition?(.success(message: "thanks_amazing_title".localized)) | ||
} | ||
} catch { | ||
onTransition?(.showLoader(false)) | ||
onTransition?( | ||
.showAlert( | ||
BPAlertContent.errorAlert(message: error.localizedDescription) | ||
) | ||
) | ||
} | ||
} | ||
|
||
func restorePurchases() async { | ||
onTransition?(.showLoader(true)) | ||
do { | ||
let customerInfo = try await Purchases.shared.restorePurchases() | ||
onTransition?(.showLoader(false)) | ||
if customerInfo.nonSubscriptions.isEmpty { | ||
onTransition?( | ||
.showAlert( | ||
BPAlertContent.errorAlert(message: "tip_missing_title".localized) | ||
) | ||
) | ||
} else { | ||
accountService.updateAccount( | ||
id: nil, | ||
email: nil, | ||
donationMade: true, | ||
hasSubscription: nil | ||
) | ||
onTransition?(.success(message: "purchases_restored_title".localized)) | ||
} | ||
} catch { | ||
onTransition?(.showLoader(false)) | ||
onTransition?( | ||
.showAlert( | ||
BPAlertContent.errorAlert(message: error.localizedDescription) | ||
) | ||
) | ||
} | ||
} | ||
|
||
func dismiss() { | ||
onTransition?(.dismiss) | ||
} | ||
} |
Oops, something went wrong.