Эх сурвалжийг харах

Ask for app rating after meaningful engagement.

Prompt for a rating once after 5 minutes of app usage or immediately after a successful premium purchase.

Made-with: Cursor
huzaifahayat12 3 сар өмнө
parent
commit
a90a38791e

+ 12 - 0
google_apps/LauncherRootView.swift

@@ -16,6 +16,7 @@ struct LauncherRootView: View {
     @State private var showingCreateAppSheet = false
     @State private var previouslyPremiumUnlocked = false
     @State private var startupPaywallTask: Task<Void, Never>?
+    @State private var ratingPromptTask: Task<Void, Never>?
     @State private var customApps: [LauncherApp] = []
     @State private var appOrder: [UUID] = []
     @State private var draggedAppID: UUID?
@@ -198,10 +199,21 @@ struct LauncherRootView: View {
                 guard !showingPremiumScreen else { return }
                 showingPremiumScreen = true
             }
+
+            ratingPromptTask?.cancel()
+            ratingPromptTask = Task {
+                try? await Task.sleep(nanoseconds: 5 * 60 * 1_000_000_000)
+                guard !Task.isCancelled else { return }
+                await MainActor.run {
+                    RatingPromptManager.shared.requestRatingIfNeeded()
+                }
+            }
         }
         .onDisappear {
             startupPaywallTask?.cancel()
             startupPaywallTask = nil
+            ratingPromptTask?.cancel()
+            ratingPromptTask = nil
         }
     }
 

+ 1 - 0
google_apps/PremiumStore.swift

@@ -79,6 +79,7 @@ final class PremiumStore: ObservableObject {
                 case .verified(let transaction):
                     if PremiumProductID(rawValue: transaction.productID) != nil {
                         isPremiumUnlocked = true
+                        RatingPromptManager.shared.requestRatingIfNeeded()
                     }
                     await transaction.finish()
                     await refreshEntitlements()

+ 20 - 0
google_apps/RatingPromptManager.swift

@@ -0,0 +1,20 @@
+import Foundation
+import StoreKit
+
+@MainActor
+final class RatingPromptManager {
+    static let shared = RatingPromptManager()
+
+    private let hasRequestedKey = "hasRequestedAppRating"
+    private let defaults: UserDefaults
+
+    private init(defaults: UserDefaults = .standard) {
+        self.defaults = defaults
+    }
+
+    func requestRatingIfNeeded() {
+        guard !defaults.bool(forKey: hasRequestedKey) else { return }
+        SKStoreReviewController.requestReview()
+        defaults.set(true, forKey: hasRequestedKey)
+    }
+}