@@ -35,6 +35,9 @@ import com.kharagedition.tibetankeyboard.R
3535import com.kharagedition.tibetankeyboard.UserPreferences
3636import com.kharagedition.tibetankeyboard.repo.UserRepository
3737import com.kharagedition.tibetankeyboard.ui.ChatActivity
38+ import com.kharagedition.tibetankeyboard.subscription.RevenueCatManager
39+ import com.kharagedition.tibetankeyboard.auth.AuthManager
40+ import android.util.Log
3841import kotlinx.coroutines.launch
3942
4043class LoginActivity : AppCompatActivity () {
@@ -46,6 +49,7 @@ class LoginActivity : AppCompatActivity() {
4649 private lateinit var userPreferences: UserPreferences
4750 private lateinit var textViewPrivacyPolicy: TextView
4851 private lateinit var userRepository: UserRepository
52+ private lateinit var revenueCatManager: RevenueCatManager
4953
5054 private val googleSignInLauncher = registerForActivityResult(
5155 ActivityResultContracts .StartActivityForResult ()
@@ -68,10 +72,12 @@ class LoginActivity : AppCompatActivity() {
6872 auth = Firebase .auth
6973 userRepository = UserRepository ()
7074 userPreferences = UserPreferences (this )
75+ revenueCatManager = RevenueCatManager .getInstance()
7176
7277 // Check if user is already signed in
7378 if (auth.currentUser != null && userPreferences.isUserLoggedIn()) {
74- navigateToChatActivity()
79+ Log .d(" LoginActivity" , " User already logged in, initializing RevenueCat..." )
80+ initializeRevenueCatAndNavigate()
7581 return
7682 }
7783
@@ -237,15 +243,33 @@ class LoginActivity : AppCompatActivity() {
237243 )
238244 )
239245
240- navigateToChatActivity()
246+ // CRITICAL: Initialize RevenueCat immediately after successful login
247+ // This ensures purchases can be acknowledged with Google Play
248+ Log .d(" LoginActivity" , " Initializing RevenueCat after successful login..." )
249+ revenueCatManager.initialize(this @LoginActivity, auth, object : RevenueCatManager .SubscriptionCallback {
250+ override fun onSuccess (message : String ) {
251+ Log .d(" LoginActivity" , " ✅ RevenueCat initialized successfully: $message " )
252+ navigateToChatActivity()
253+ }
254+
255+ override fun onError (error : String ) {
256+ Log .e(" LoginActivity" , " ❌ RevenueCat initialization failed: $error " )
257+ // Still navigate even if RevenueCat fails
258+ navigateToChatActivity()
259+ }
260+
261+ override fun onUserCancelled () {
262+ // Not applicable here
263+ }
264+ })
241265 } else {
242266 // Even if Firestore fails, continue with login but show warning
243267 Toast .makeText(
244268 this @LoginActivity,
245269 " Welcome ${firebaseUser.displayName} ! (Profile sync pending)" ,
246270 Toast .LENGTH_SHORT
247271 ).show()
248- navigateToChatActivity ()
272+ initializeRevenueCatAndNavigate ()
249273 }
250274 } catch (e: Exception ) {
251275 hideLoading()
@@ -255,10 +279,30 @@ class LoginActivity : AppCompatActivity() {
255279 " Welcome ${firebaseUser.displayName} !" ,
256280 Toast .LENGTH_SHORT
257281 ).show()
258- navigateToChatActivity ()
282+ initializeRevenueCatAndNavigate ()
259283 }
260284 }
261285 }
286+
287+ /* *
288+ * Initialize RevenueCat after login and navigate to next screen
289+ */
290+ private fun initializeRevenueCatAndNavigate () {
291+ Log .d(" LoginActivity" , " Initializing RevenueCat after login..." )
292+ revenueCatManager.initialize(this , auth, object : RevenueCatManager .SubscriptionCallback {
293+ override fun onSuccess (message : String ) {
294+ Log .d(" LoginActivity" , " ✅ RevenueCat initialized: $message " )
295+ navigateToChatActivity()
296+ }
297+
298+ override fun onError (error : String ) {
299+ Log .e(" LoginActivity" , " ❌ RevenueCat init failed: $error " )
300+ navigateToChatActivity()
301+ }
302+
303+ override fun onUserCancelled () {}
304+ })
305+ }
262306 private fun handleLoginError (exception : Exception ? ) {
263307 val errorMessage = when (exception) {
264308 is FirebaseAuthUserCollisionException -> " An account already exists with this email"
0 commit comments