|
2 | 2 | using UnityEngine.UI;
|
3 | 3 | using UnityEngine.EventSystems;
|
4 | 4 | using TMPro;
|
| 5 | +using Immutable.Passport.Model; |
5 | 6 |
|
6 | 7 | namespace Immutable.Passport
|
7 | 8 | {
|
@@ -346,20 +347,166 @@ private void WireUpPassportManager()
|
346 | 347 | if (passportManager == null) return;
|
347 | 348 |
|
348 | 349 | // Set UI references using the public method
|
349 |
| - passportManager.SetUIReferences( |
350 |
| - loginButton, |
351 |
| - googleLoginButton, |
352 |
| - appleLoginButton, |
353 |
| - facebookLoginButton, |
354 |
| - logoutButton, |
355 |
| - statusText, |
356 |
| - userInfoText |
357 |
| - ); |
| 350 | + // Wire up generated UI elements to PassportManager directly |
| 351 | + ConfigureUIButtons(); |
358 | 352 |
|
359 | 353 | Debug.Log("[PassportUIBuilder] UI wired to PassportManager successfully!");
|
360 | 354 | Debug.Log($"[PassportUIBuilder] Button click listeners: Google={googleLoginButton.onClick.GetPersistentEventCount()}");
|
361 | 355 | }
|
362 | 356 |
|
| 357 | + /// <summary> |
| 358 | + /// Configure UI button listeners to call PassportManager methods |
| 359 | + /// </summary> |
| 360 | + private void ConfigureUIButtons() |
| 361 | + { |
| 362 | + if (passportManager == null) return; |
| 363 | + |
| 364 | + // Clear existing listeners to prevent duplicates |
| 365 | + if (loginButton != null) |
| 366 | + { |
| 367 | + loginButton.onClick.RemoveAllListeners(); |
| 368 | + loginButton.onClick.AddListener(() => passportManager.Login()); |
| 369 | + Debug.Log("[PassportUIBuilder] Configured login button"); |
| 370 | + } |
| 371 | + |
| 372 | + if (googleLoginButton != null) |
| 373 | + { |
| 374 | + googleLoginButton.onClick.RemoveAllListeners(); |
| 375 | + googleLoginButton.onClick.AddListener(() => passportManager.Login(new DirectLoginOptions(DirectLoginMethod.Google))); |
| 376 | + Debug.Log("[PassportUIBuilder] Configured Google login button"); |
| 377 | + } |
| 378 | + |
| 379 | + if (appleLoginButton != null) |
| 380 | + { |
| 381 | + appleLoginButton.onClick.RemoveAllListeners(); |
| 382 | + appleLoginButton.onClick.AddListener(() => passportManager.Login(new DirectLoginOptions(DirectLoginMethod.Apple))); |
| 383 | + Debug.Log("[PassportUIBuilder] Configured Apple login button"); |
| 384 | + } |
| 385 | + |
| 386 | + if (facebookLoginButton != null) |
| 387 | + { |
| 388 | + facebookLoginButton.onClick.RemoveAllListeners(); |
| 389 | + facebookLoginButton.onClick.AddListener(() => passportManager.Login(new DirectLoginOptions(DirectLoginMethod.Facebook))); |
| 390 | + Debug.Log("[PassportUIBuilder] Configured Facebook login button"); |
| 391 | + } |
| 392 | + |
| 393 | + if (logoutButton != null) |
| 394 | + { |
| 395 | + logoutButton.onClick.RemoveAllListeners(); |
| 396 | + logoutButton.onClick.AddListener(() => passportManager.Logout()); |
| 397 | + Debug.Log("[PassportUIBuilder] Configured logout button"); |
| 398 | + } |
| 399 | + |
| 400 | + // Subscribe to PassportManager events for UI state management |
| 401 | + if (passportManager.OnPassportInitialized != null) |
| 402 | + passportManager.OnPassportInitialized.AddListener(UpdateUIState); |
| 403 | + if (passportManager.OnLoginSucceeded != null) |
| 404 | + passportManager.OnLoginSucceeded.AddListener(OnLoginSuccess); |
| 405 | + if (passportManager.OnLogoutSucceeded != null) |
| 406 | + passportManager.OnLogoutSucceeded.AddListener(OnLogoutSuccess); |
| 407 | + |
| 408 | + // Initial UI state update |
| 409 | + UpdateUIState(); |
| 410 | + } |
| 411 | + |
| 412 | + /// <summary> |
| 413 | + /// Handle successful login |
| 414 | + /// </summary> |
| 415 | + private void OnLoginSuccess() |
| 416 | + { |
| 417 | + ShowLoggedInPanel(); |
| 418 | + UpdateUIState(); |
| 419 | + } |
| 420 | + |
| 421 | + /// <summary> |
| 422 | + /// Handle successful logout |
| 423 | + /// </summary> |
| 424 | + private void OnLogoutSuccess() |
| 425 | + { |
| 426 | + ShowLoginPanel(); |
| 427 | + UpdateUIState(); |
| 428 | + } |
| 429 | + |
| 430 | + /// <summary> |
| 431 | + /// Update UI state based on PassportManager status |
| 432 | + /// </summary> |
| 433 | + private void UpdateUIState() |
| 434 | + { |
| 435 | + if (passportManager == null) return; |
| 436 | + |
| 437 | + bool isInitialized = passportManager.IsInitialized; |
| 438 | + bool isLoggedIn = passportManager.IsLoggedIn; |
| 439 | + |
| 440 | + // Update button states |
| 441 | + if (loginButton != null) |
| 442 | + loginButton.interactable = isInitialized && !isLoggedIn; |
| 443 | + if (googleLoginButton != null) |
| 444 | + googleLoginButton.interactable = isInitialized && !isLoggedIn; |
| 445 | + if (appleLoginButton != null) |
| 446 | + appleLoginButton.interactable = isInitialized && !isLoggedIn; |
| 447 | + if (facebookLoginButton != null) |
| 448 | + facebookLoginButton.interactable = isInitialized && !isLoggedIn; |
| 449 | + if (logoutButton != null) |
| 450 | + logoutButton.interactable = isInitialized && isLoggedIn; |
| 451 | + |
| 452 | + // Update status text |
| 453 | + string statusMessage; |
| 454 | + Color statusColor; |
| 455 | + |
| 456 | + if (!isInitialized) |
| 457 | + { |
| 458 | + statusMessage = "Initializing Passport..."; |
| 459 | + statusColor = Color.yellow; |
| 460 | + } |
| 461 | + else if (isLoggedIn) |
| 462 | + { |
| 463 | + statusMessage = "✅ Logged In Successfully"; |
| 464 | + statusColor = Color.green; |
| 465 | + } |
| 466 | + else |
| 467 | + { |
| 468 | + statusMessage = "Ready to login"; |
| 469 | + statusColor = Color.white; |
| 470 | + } |
| 471 | + |
| 472 | + if (statusText != null) |
| 473 | + { |
| 474 | + statusText.text = statusMessage; |
| 475 | + statusText.color = statusColor; |
| 476 | + } |
| 477 | + |
| 478 | + // Update user info |
| 479 | + if (isLoggedIn) |
| 480 | + { |
| 481 | + UpdateUserInfoDisplay(); |
| 482 | + } |
| 483 | + else if (userInfoText != null) |
| 484 | + { |
| 485 | + userInfoText.text = ""; |
| 486 | + } |
| 487 | + } |
| 488 | + |
| 489 | + /// <summary> |
| 490 | + /// Update user info display with token preview |
| 491 | + /// </summary> |
| 492 | + private async void UpdateUserInfoDisplay() |
| 493 | + { |
| 494 | + if (userInfoText != null && passportManager != null && passportManager.PassportInstance != null) |
| 495 | + { |
| 496 | + try |
| 497 | + { |
| 498 | + string accessToken = await passportManager.PassportInstance.GetAccessToken(); |
| 499 | + string tokenPreview = accessToken.Length > 20 ? accessToken.Substring(0, 20) + "..." : accessToken; |
| 500 | + userInfoText.text = $"Token: {tokenPreview}"; |
| 501 | + } |
| 502 | + catch (System.Exception ex) |
| 503 | + { |
| 504 | + userInfoText.text = $"Error: {ex.Message}"; |
| 505 | + Debug.LogWarning($"[PassportUIBuilder] Failed to load user info: {ex.Message}"); |
| 506 | + } |
| 507 | + } |
| 508 | + } |
| 509 | + |
363 | 510 | /// <summary>
|
364 | 511 | /// Show the login panel and hide the logged-in panel
|
365 | 512 | /// </summary>
|
|
0 commit comments