FacebookDialogManager.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using UnityEngine;
  2. using System.Collections;
  3. public class FacebookDialogManager : MonoBehaviour
  4. {
  5. public UIElementTransition noInternetDialog;
  6. public UIElementTransition loginDialog;
  7. public UIElementTransition likeDialog;
  8. void OnEnable()
  9. {
  10. FacebookHelper.AddSuccessfulLoginWithPermissionsListener(OnSuccessfulLoginWithPermissions);
  11. FacebookHelper.AddQueryHasLikedPageCompleteListener(OnQueryHasLikedPageComplete);
  12. DebugViewManager.OnDebugView += OnDebugView;
  13. CoreNotificationCenter.AddListener(OnConnectivityChange, CoreNotificationType.ConnectivityChange);
  14. ShowAppropriateDialog();
  15. }
  16. void OnDisable()
  17. {
  18. FacebookHelper.RemoveSuccessfulLoginWithPermissionsListener(OnSuccessfulLoginWithPermissions);
  19. FacebookHelper.RemoveQueryHasLikedPageCompleteListener(OnQueryHasLikedPageComplete);
  20. DebugViewManager.OnDebugView -= OnDebugView;
  21. CoreNotificationCenter.RemoveListener(OnConnectivityChange, CoreNotificationType.ConnectivityChange);
  22. }
  23. void OnApplicationPause()
  24. {
  25. AVDebug.Log("OnApplicationPause");
  26. //when switching back to app, and if the like dialog is active,
  27. //recheck if he has really liked the page
  28. StartCoroutine(RecheckPageLikeCoroutine());
  29. }
  30. void ShowAppropriateDialog()
  31. {
  32. if (!ConnectivityPollManager.HasInternet)
  33. {
  34. Debug.Log("ShowAppropriateDialog");
  35. noInternetDialog.Show();
  36. loginDialog.Hide();
  37. likeDialog.Hide();
  38. } else
  39. //if (!FacebookHelper.Instance.IsLoggedIn())
  40. //{
  41. // noInternetDialog.Hide();
  42. // loginDialog.Show();
  43. // likeDialog.Hide();
  44. //} else
  45. {
  46. noInternetDialog.Hide();
  47. loginDialog.Hide();
  48. }
  49. }
  50. IEnumerator RecheckPageLikeCoroutine()
  51. {
  52. if (likeDialog.gameObject.activeSelf)
  53. {
  54. yield return new WaitForSeconds(0.1f);
  55. FacebookHelper.Instance.QueryHasLikedPage();
  56. }
  57. }
  58. void OnDebugView()
  59. {
  60. if (GUILayout.Button("Reset facebook dummy stuff"))
  61. {
  62. FacebookHelper.Instance.Reset();
  63. }
  64. }
  65. void OnLoginButtonMsg()
  66. {
  67. Logger.LogFlurryEvent("Facebook Login from Scores screen");
  68. FacebookHelper.Instance.LogIn();
  69. }
  70. void OnLikeButtonMsg()
  71. {
  72. Logger.LogFlurryEvent("Facebook Like button pressed");
  73. FacebookHelper.Instance.LikePage();
  74. }
  75. void OnSuccessfulLoginWithPermissions()
  76. {
  77. loginDialog.Hide();
  78. //Danish Requirement: Only check for like on game over
  79. if (MenuManager.CurrentState == MenuManager.MenuState.ViewingScoresAfterGame)
  80. {
  81. FacebookHelper.Instance.QueryHasLikedPage();
  82. }
  83. }
  84. void OnQueryHasLikedPageComplete(bool isLiked)
  85. {
  86. AVDebug.Log("OnQueryHasLikedPageComplete " + isLiked);
  87. if (isLiked)
  88. {
  89. likeDialog.Hide();
  90. } else
  91. //Ask for like page after game over
  92. if (MenuManager.CurrentState == MenuManager.MenuState.ViewingScoresAfterGame)
  93. {
  94. likeDialog.Show();
  95. }
  96. }
  97. void OnConnectivityChange(CoreNotification note)
  98. {
  99. Debug.Log("ShowAppropriateDialog");
  100. ShowAppropriateDialog();
  101. }
  102. }