mirror of
https://github.com/leonboe1/QuickDrop.git
synced 2026-11-19 21:06:10 +00:00
New Alert for receiving files, rename to QuickDrop
This commit is contained in:
1 parent
79b359fb64
commit
4929a96134
71 files changed
+1521
-413
No files matched your search
@@ -0,0 +1,245 @@
|
||||
//
|
||||
// GetPlusView.swift
|
||||
// QuickDrop
|
||||
//
|
||||
// Created by Leon Böttger on 23.02.25.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import StoreKit
|
||||
|
||||
struct GetPlusView: View {
|
||||
|
||||
@AppStorage(UserDefaultsKeys.plusVersion.rawValue) var isPlusVersion = false
|
||||
@ObservedObject private var iaphelper = IAPManager.sharedInstance
|
||||
let closeView: () -> Void
|
||||
|
||||
@State private var restoring = false
|
||||
@State private var showRestoreError = false
|
||||
@State private var restoreError = ""
|
||||
|
||||
@Environment(\.colorScheme) var colorScheme
|
||||
@State var products = [SKProduct]()
|
||||
@State var price: String = ""
|
||||
@State var warning = false
|
||||
@State var buying = false
|
||||
@State var errorText = ""
|
||||
|
||||
@State var boughtSuccessAlert = false
|
||||
|
||||
|
||||
var body: some View {
|
||||
|
||||
ZStack {
|
||||
Color.defaultBackground
|
||||
.edgesIgnoringSafeArea(.all)
|
||||
|
||||
let validPrice = price != ""
|
||||
|
||||
VStack(spacing: 12) {
|
||||
Text("plusview_title")
|
||||
.font(.title)
|
||||
.bold()
|
||||
|
||||
Text("plusview_description")
|
||||
.multilineTextAlignment(.center)
|
||||
.padding()
|
||||
.padding(.top, -10)
|
||||
.alert(isPresented: $boughtSuccessAlert, content: {
|
||||
Alert(title: Text("plusview_success_title"), message: Text("plusview_success_description"), dismissButton: .default(Text("plusview_success_button"), action: {
|
||||
closeView()
|
||||
}))
|
||||
})
|
||||
|
||||
HStack(spacing: 10) {
|
||||
Button("plusview_restorepurchase") {
|
||||
restorePurchases()
|
||||
}
|
||||
.keyboardShortcut(.cancelAction)
|
||||
.alert(isPresented: $showRestoreError, content: {
|
||||
Alert(title: Text("plusview_restorefailed"), message: Text(restoreError), dismissButton: .default(Text("plusview_restorefailed_proceed")))
|
||||
})
|
||||
.opacity(restoring ? 0 : 1)
|
||||
.overlay(
|
||||
ProgressView()
|
||||
.scaleEffect(0.5)
|
||||
.opacity(restoring ? 1 : 0)
|
||||
)
|
||||
|
||||
let priceString = validPrice ? " (\(price))" : ""
|
||||
|
||||
Button("plusview_unlock_label".localized() + priceString) {
|
||||
buttonAction()
|
||||
}
|
||||
.keyboardShortcut(.defaultAction)
|
||||
.alert(isPresented: $warning, content: {
|
||||
Alert(title: Text("plusview_purchasefailed"), message: Text("plusview_purchasefaileddescription".localized() + (errorText != "" ? " Error: " : "") + errorText), dismissButton: .default(Text("plusview_restorefailed_proceed")))
|
||||
})
|
||||
}
|
||||
.opacity(buying || restoring ? 0 : 1)
|
||||
.overlay(
|
||||
ProgressView()
|
||||
.scaleEffect(0.5)
|
||||
.opacity(buying || restoring ? 1 : 0)
|
||||
)
|
||||
}
|
||||
.onAppear {
|
||||
loadProducts(shouldBuy: false)
|
||||
}
|
||||
.animation(.default, value: validPrice)
|
||||
.padding()
|
||||
}
|
||||
.frame(width: 400, height: 200)
|
||||
}
|
||||
|
||||
|
||||
func restorePurchases() {
|
||||
|
||||
withAnimation {
|
||||
restoring = true
|
||||
}
|
||||
|
||||
iaphelper.restorePurchases { result in
|
||||
DispatchQueue.main.async {
|
||||
|
||||
switch result {
|
||||
case .success(let success):
|
||||
if success {
|
||||
withAnimation {
|
||||
isPlusVersion = true
|
||||
}
|
||||
boughtSuccessAlert = true
|
||||
} else {
|
||||
//nothing to restore
|
||||
restoreFailed(message: "plusview_nothingtorestore")
|
||||
}
|
||||
|
||||
case .failure(let error):
|
||||
log(error.localizedDescription)
|
||||
restoreFailed(message: error.localizedDescription)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func restoreFailed(message: String) {
|
||||
|
||||
#if targetEnvironment(simulator)
|
||||
isPlusVersion = true
|
||||
dismissView()
|
||||
#else
|
||||
|
||||
// now, try to convert from old version
|
||||
IAPConverter.shared.restoreIAPfromPreviousPurchase { success in
|
||||
|
||||
DispatchQueue.main.async {
|
||||
|
||||
if success {
|
||||
withAnimation {
|
||||
isPlusVersion = true
|
||||
}
|
||||
|
||||
boughtSuccessAlert = true
|
||||
}
|
||||
else {
|
||||
restoreError = message
|
||||
showRestoreError = true
|
||||
|
||||
withAnimation {
|
||||
restoring = false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
func buttonAction() {
|
||||
if let product = products.first {
|
||||
purchase(product: product)
|
||||
}
|
||||
else {
|
||||
loadProducts(shouldBuy: true)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func purchase(product: SKProduct) {
|
||||
if !iaphelper.canMakePayments() {
|
||||
warning = true
|
||||
} else {
|
||||
withAnimation{
|
||||
buying = true
|
||||
}
|
||||
|
||||
iaphelper.buy(product: product) { (result) in
|
||||
DispatchQueue.main.async {
|
||||
withAnimation{
|
||||
buying = false
|
||||
}
|
||||
|
||||
switch result {
|
||||
case .success(_):
|
||||
|
||||
withAnimation {
|
||||
isPlusVersion = true
|
||||
boughtSuccessAlert = true
|
||||
warning = false
|
||||
}
|
||||
|
||||
case .failure(let error):
|
||||
warning = true
|
||||
errorText = error.localizedDescription
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func loadProducts(shouldBuy: Bool) {
|
||||
iaphelper.getProducts { (result) in
|
||||
DispatchQueue.main.async {
|
||||
|
||||
switch result {
|
||||
case .success(let products):
|
||||
self.products = products
|
||||
price = products.first?.localizedPrice ?? ""
|
||||
|
||||
if let product = products.first, shouldBuy {
|
||||
purchase(product: product)
|
||||
}
|
||||
|
||||
case .failure(let error):
|
||||
errorText = error.localizedDescription
|
||||
|
||||
if(shouldBuy){
|
||||
warning = true
|
||||
}
|
||||
|
||||
withAnimation{
|
||||
buying = false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
extension SKProduct {
|
||||
var localizedPrice: String {
|
||||
let formatter = NumberFormatter()
|
||||
formatter.numberStyle = .currency
|
||||
formatter.locale = priceLocale
|
||||
if let price = formatter.string(from: price) {
|
||||
return price
|
||||
}
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#Preview {
|
||||
GetPlusView(closeView: {})
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
//
|
||||
// StoreChecker.swift
|
||||
// LUI
|
||||
//
|
||||
// Created by Leon Böttger on 19.08.24.
|
||||
//
|
||||
import StoreKit
|
||||
|
||||
class IAPConverter: NSObject, SKRequestDelegate {
|
||||
|
||||
let switchToIAPAfterBundleVersion = "6"
|
||||
|
||||
static let shared = IAPConverter()
|
||||
|
||||
private override init() {
|
||||
super.init()
|
||||
}
|
||||
|
||||
func restoreIAPfromPreviousPurchase(success: ((Bool) -> ())? = nil) {
|
||||
Task {
|
||||
do {
|
||||
// Get the appTransaction.
|
||||
|
||||
if #available(macOS 13.0, *) {
|
||||
let shared = try await AppTransaction.shared
|
||||
|
||||
if case .verified(let appTransaction) = shared {
|
||||
|
||||
let originalVersion = appTransaction.originalAppVersion
|
||||
let newModelVersion = switchToIAPAfterBundleVersion
|
||||
log("[LUI] Original Purchased Version: \(originalVersion) | New Model Version: \(newModelVersion)")
|
||||
|
||||
if originalVersion.compare(newModelVersion, options: .numeric) == .orderedAscending {
|
||||
grant(success: success)
|
||||
return
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Fallback on earlier versions
|
||||
// Grant IAP, as we cannot check the original purchase date.
|
||||
DispatchQueue.main.async {
|
||||
log("[LUI] Cannot check version - Plus Version Granted!")
|
||||
success?(true)
|
||||
}
|
||||
}
|
||||
}
|
||||
catch {
|
||||
// Handle errors.
|
||||
log("[LUI] Error: \(error.localizedDescription)")
|
||||
}
|
||||
|
||||
DispatchQueue.main.async {
|
||||
log("[LUI] Plus Version Denied!")
|
||||
success?(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func grant(success: ((Bool) -> ())? = nil) {
|
||||
log("[LUI] Existing User - Plus Version Granted!")
|
||||
|
||||
DispatchQueue.main.async {
|
||||
success?(true)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,195 @@
|
||||
//
|
||||
// PurchaseHelper.swift
|
||||
// Loop
|
||||
//
|
||||
// Created by Leon Böttger on 26.10.20.
|
||||
// Copyright © 2020 Leon Böttger. All rights reserved.
|
||||
//
|
||||
|
||||
import StoreKit
|
||||
|
||||
let iapName = "plusversion"
|
||||
|
||||
class IAPManager: NSObject, ObservableObject {
|
||||
|
||||
enum IAPManagerError: Error {
|
||||
case noProductIDsFound
|
||||
case noProductsFound
|
||||
case paymentWasCancelled
|
||||
case productRequestFailed
|
||||
}
|
||||
|
||||
static let plusVersionID = iapName
|
||||
static let sharedInstance = IAPManager()
|
||||
var onReceiveProductsHandler: ((Result<[SKProduct], IAPManagerError>) -> Void)?
|
||||
var onBuyProductHandler: ((Result<Bool, Error>) -> Void)?
|
||||
var totalRestoredPurchases = 0
|
||||
var isActive = false
|
||||
|
||||
private override init() {
|
||||
super.init()
|
||||
getProducts { _ in }
|
||||
}
|
||||
|
||||
fileprivate func getProductIDs() -> [String]? {
|
||||
return [IAPManager.plusVersionID]
|
||||
}
|
||||
|
||||
func startObserving() {
|
||||
SKPaymentQueue.default().add(self)
|
||||
}
|
||||
|
||||
|
||||
func stopObserving() {
|
||||
SKPaymentQueue.default().remove(self)
|
||||
}
|
||||
|
||||
|
||||
func canMakePayments() -> Bool {
|
||||
return SKPaymentQueue.canMakePayments()
|
||||
}
|
||||
|
||||
func getProducts(withHandler productsReceiveHandler: @escaping (_ result: Result<[SKProduct], IAPManagerError>) -> Void) {
|
||||
// Keep the handler (closure) that will be called when requesting for
|
||||
// products on the App Store is finished.
|
||||
onReceiveProductsHandler = productsReceiveHandler
|
||||
|
||||
// Get the product identifiers.
|
||||
guard let productIDs = getProductIDs() else {
|
||||
productsReceiveHandler(.failure(.noProductIDsFound))
|
||||
return
|
||||
}
|
||||
|
||||
// Initialize a product request.
|
||||
let request = SKProductsRequest(productIdentifiers: Set(productIDs))
|
||||
|
||||
// Set self as the its delegate.
|
||||
request.delegate = self
|
||||
|
||||
// Make the request.
|
||||
request.start()
|
||||
}
|
||||
|
||||
func buy(product: SKProduct, withHandler handler: @escaping ((_ result: Result<Bool, Error>) -> Void)) {
|
||||
let payment = SKPayment(product: product)
|
||||
SKPaymentQueue.default().add(payment)
|
||||
|
||||
// Keep the completion handler.
|
||||
onBuyProductHandler = handler
|
||||
}
|
||||
|
||||
|
||||
func restorePurchases(withHandler handler: @escaping ((_ result: Result<Bool, Error>) -> Void)) {
|
||||
onBuyProductHandler = handler
|
||||
totalRestoredPurchases = 0
|
||||
SKPaymentQueue.default().restoreCompletedTransactions()
|
||||
}
|
||||
}
|
||||
|
||||
extension IAPManager: SKPaymentTransactionObserver {
|
||||
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
|
||||
transactions.forEach { (transaction) in
|
||||
|
||||
switch transaction.transactionState {
|
||||
case .purchased:
|
||||
onBuyProductHandler?(.success(true))
|
||||
SKPaymentQueue.default().finishTransaction(transaction)
|
||||
|
||||
case .restored:
|
||||
totalRestoredPurchases += 1
|
||||
onBuyProductHandler?(.success(true))
|
||||
SKPaymentQueue.default().finishTransaction(transaction)
|
||||
|
||||
case .failed:
|
||||
log("[LUI] IAP failed")
|
||||
if let error = transaction.error as? SKError {
|
||||
if error.code != .paymentCancelled {
|
||||
onBuyProductHandler?(.failure(error))
|
||||
} else {
|
||||
onBuyProductHandler?(.failure(IAPManagerError.paymentWasCancelled))
|
||||
}
|
||||
log("[LUI] IAP Error:" + error.localizedDescription)
|
||||
}
|
||||
onBuyProductHandler?(.failure(transaction.error ?? IAPManagerError.productRequestFailed))
|
||||
SKPaymentQueue.default().finishTransaction(transaction)
|
||||
|
||||
case .deferred, .purchasing: log("[LUI] other cases")
|
||||
@unknown default: log("[LUI] other default cases")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {
|
||||
if totalRestoredPurchases != 0 {
|
||||
onBuyProductHandler?(.success(true))
|
||||
} else {
|
||||
log("[LUI] IAP: No purchases to restore!")
|
||||
onBuyProductHandler?(.success(false))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func paymentQueue(_ queue: SKPaymentQueue, restoreCompletedTransactionsFailedWithError error: Error) {
|
||||
if let error = error as? SKError {
|
||||
if error.code != .paymentCancelled {
|
||||
log("[LUI] IAP Restore Error:" + error.localizedDescription)
|
||||
onBuyProductHandler?(.failure(error))
|
||||
} else {
|
||||
onBuyProductHandler?(.failure(IAPManagerError.paymentWasCancelled))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension IAPManager: SKProductsRequestDelegate {
|
||||
func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
|
||||
// Get the available products contained in the response.
|
||||
let products = response.products
|
||||
|
||||
// Check if there are any products available.
|
||||
if products.count > 0 {
|
||||
// Call the following handler passing the received products.
|
||||
onReceiveProductsHandler?(.success(products))
|
||||
} else {
|
||||
// No products were found.
|
||||
onReceiveProductsHandler?(.failure(.noProductsFound))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func paymentQueue(_ queue: SKPaymentQueue, shouldAddStorePayment payment: SKPayment, for product: SKProduct) -> Bool {
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
func request(_ request: SKRequest, didFailWithError error: Error) {
|
||||
onReceiveProductsHandler?(.failure(.productRequestFailed))
|
||||
}
|
||||
|
||||
|
||||
func requestDidFinish(_ request: SKRequest) {
|
||||
// Implement this method OPTIONALLY and add any custom logic
|
||||
// you want to apply when a product request is finished.
|
||||
}
|
||||
}
|
||||
|
||||
extension IAPManager.IAPManagerError: LocalizedError {
|
||||
var errorDescription: String? {
|
||||
switch self {
|
||||
case .noProductIDsFound: return "No In-App Purchase product identifiers were found."
|
||||
case .noProductsFound: return "No In-App Purchases were found."
|
||||
case .productRequestFailed: return "plusview_productRequestFailed".localized()
|
||||
case .paymentWasCancelled: return "In-App Purchase process was cancelled."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public extension String {
|
||||
func localized() -> String {
|
||||
let localizedString = NSLocalizedString(self, comment: "")
|
||||
return localizedString
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user