From 8a691b5e07e159dd29eb5ac0bc9a53ecce327e47 Mon Sep 17 00:00:00 2001 From: lumaa-dev Date: Mon, 23 Sep 2024 18:30:45 +0200 Subject: [PATCH] Advanced premium managing --- Bubble/AppDelegate.swift | 41 +++++++++++++----------- Bubble/BubbleApp.swift | 4 +-- Bubble/Data/HapticManager.swift | 5 +++ Bubble/Views/Post/PostingView.swift | 3 +- Bubble/Views/Profile/ProfileView.swift | 3 ++ Bubble/Views/Settings/AboutView.swift | 4 +-- Bubble/Views/Settings/SettingsView.swift | 3 ++ 7 files changed, 39 insertions(+), 24 deletions(-) diff --git a/Bubble/AppDelegate.swift b/Bubble/AppDelegate.swift index c57c30d..2ad4fe3 100644 --- a/Bubble/AppDelegate.swift +++ b/Bubble/AppDelegate.swift @@ -29,9 +29,11 @@ public class AppDelegate: NSObject, UIWindowSceneDelegate, Sendable, UIApplicati } } - let foundPremium = AppDelegate.hasPlus() - print("User has \(!foundPremium ? "no-" : "")access to Plus") - + AppDelegate.hasPlus { subscribed in + print("User has \(!subscribed ? "no-" : "")access to Plus") + Self.premium = subscribed + } + windowWidth = window?.bounds.size.width ?? UIScreen.main.bounds.size.width windowHeight = window?.bounds.size.height ?? UIScreen.main.bounds.size.height Self.observedSceneDelegate.insert(self) @@ -51,23 +53,26 @@ public class AppDelegate: NSObject, UIWindowSceneDelegate, Sendable, UIApplicati } /// This function uses the REAL customer info to access the premium state -// static func hasPlus() -> Bool { -// #if DEBUG -// self.premium = true -// return true -// #else -// Purchases.shared.getCustomerInfo { (customerInfo, error) in -// self.premium = hasActuallyPlus(customerInfo: customerInfo) -// } -// return self.premium -// #endif -// } + static func hasPlus(completionHandler: @escaping (Bool) -> Void) { + #if targetEnvironment(simulator) + completionHandler(true) + #else + Purchases.shared.getCustomerInfo { (customerInfo, error) in + guard let error else { + let hasPrem: Bool = hasActuallyPlus(customerInfo: customerInfo) + completionHandler(hasPrem) + return + } + fatalError(error.localizedDescription) + } + #endif + } /// This function returns a fake "true" value every time whatever the customer info is - static func hasPlus() -> Bool { - self.premium = true - return true - } +// static func hasPlus() -> Bool { +// self.premium = true +// return true +// } private static func hasActuallyPlus(customerInfo: CustomerInfo?) -> Bool { return customerInfo?.entitlements[PlusEntitlements.lifetime.getEntitlementId()]?.isActive == true || customerInfo?.entitlements[PlusEntitlements.monthly.getEntitlementId()]?.isActive == true || customerInfo?.entitlements[PlusEntitlements.yearly.getEntitlementId()]?.isActive == true diff --git a/Bubble/BubbleApp.swift b/Bubble/BubbleApp.swift index fe50873..a7f015c 100644 --- a/Bubble/BubbleApp.swift +++ b/Bubble/BubbleApp.swift @@ -13,9 +13,7 @@ struct BubbleApp: App { #if DEBUG Purchases.logLevel = .debug #endif - if #available(iOS 18.0, *) { - Purchases.configure(withAPIKey: apiKey, appUserID: deviceId) - } + Purchases.configure(withAPIKey: apiKey, appUserID: deviceId) } BubbleShortcuts.updateAppShortcutParameters() //might not work? diff --git a/Bubble/Data/HapticManager.swift b/Bubble/Data/HapticManager.swift index 79a8874..83b413a 100644 --- a/Bubble/Data/HapticManager.swift +++ b/Bubble/Data/HapticManager.swift @@ -17,6 +17,11 @@ struct Haptic: Hashable { Haptic(intensity: 1.0, sharpness: 0.7, interval: 0.0), Haptic(intensity: 1.0, sharpness: 0.3, interval: 0.2) ] + static let lock: [Haptic] = [ + Haptic(intensity: 1.0, sharpness: 0.7, interval: 0.0), + Haptic(intensity: 0.55, sharpness: 0.55, interval: 0.1), + Haptic(intensity: 0.35, sharpness: 0.35, interval: 0.1) + ] } class HapticManager { diff --git a/Bubble/Views/Post/PostingView.swift b/Bubble/Views/Post/PostingView.swift index bd7710c..471209c 100644 --- a/Bubble/Views/Post/PostingView.swift +++ b/Bubble/Views/Post/PostingView.swift @@ -575,7 +575,7 @@ struct PostingView: View { //MARK: Post buttons HStack(spacing: 18) { actionMenu("plus.square.dashed") { - let addDisabled: Bool = self.drafts.count >= 3 && !AppDelegate.hasPlus() + let addDisabled: Bool = self.drafts.count >= 3 && !AppDelegate.premium Button { selectingDrafts.toggle() @@ -599,6 +599,7 @@ struct PostingView: View { HapticManager.playHaptics(haptics: Haptic.success) } else { + HapticManager.playHaptics(haptics: Haptic.lock) UniversalNavigator.static.presentedSheet = .lockedFeature(.drafts) } } label: { diff --git a/Bubble/Views/Profile/ProfileView.swift b/Bubble/Views/Profile/ProfileView.swift index 9fa2a96..ee9a3d5 100644 --- a/Bubble/Views/Profile/ProfileView.swift +++ b/Bubble/Views/Profile/ProfileView.swift @@ -560,6 +560,9 @@ struct ProfileView: View { .multilineTextAlignment(.leading) Button { + let about: [Haptic] = Haptic.lock.reversed() + HapticManager.playHaptics(haptics: about) + uniNav.presentedSheet = .aboutSubclub } label: { Text("\(client?.server ?? "???")") diff --git a/Bubble/Views/Settings/AboutView.swift b/Bubble/Views/Settings/AboutView.swift index e7cc55f..41198be 100644 --- a/Bubble/Views/Settings/AboutView.swift +++ b/Bubble/Views/Settings/AboutView.swift @@ -32,12 +32,12 @@ struct AboutView: View { Toggle("setting.experimental.activate", isOn: $userPreferences.showExperimental) .listRowThreaded() .tint(Color(uiColor: UIColor.label)) - .disabled(!AppDelegate.hasPlus()) + .disabled(!AppDelegate.premium) .onAppear { do { let oldPreferences = try UserPreferences.loadAsCurrent() ?? UserPreferences.defaultPreferences - userPreferences.showExperimental = oldPreferences.showExperimental && AppDelegate.hasPlus() + userPreferences.showExperimental = oldPreferences.showExperimental && AppDelegate.premium } catch { print(error) } diff --git a/Bubble/Views/Settings/SettingsView.swift b/Bubble/Views/Settings/SettingsView.swift index 64acd7c..fe09044 100644 --- a/Bubble/Views/Settings/SettingsView.swift +++ b/Bubble/Views/Settings/SettingsView.swift @@ -28,6 +28,7 @@ struct SettingsView: View { if AppDelegate.premium || loggedAccounts.count < 3 { uniNav.presentedSheet = .mastodonLogin(logged: $switched) } else { + HapticManager.playHaptics(haptics: Haptic.lock) uniNav.presentedSheet = .lockedFeature(.moreAccounts) } } label: { @@ -103,12 +104,14 @@ struct SettingsView: View { } .listRowThreaded() + #if !targetEnvironment(simulator) Button { openURL(URL(string: "https://apps.apple.com/app/id6477757490?action=write-review")!) } label: { Label("setting.review", systemImage: "star.fill") } .listRowThreaded() + #endif Button { navigator.navigate(to: .support)