| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using MainScripts.DCL.Controllers.HUD.CharacterPreview; |
| | 4 | | using SocialFeaturesAnalytics; |
| | 5 | | using System; |
| | 6 | | using System.Threading; |
| | 7 | |
|
| | 8 | | namespace DCL.Social.Passports |
| | 9 | | { |
| | 10 | | public class PassportPlayerPreviewComponentController : IDisposable |
| | 11 | | { |
| | 12 | | private const string TUTORIAL_ENABLED_KEY = "PassportPreviewRotationHintEnabled"; |
| | 13 | |
|
| | 14 | | private readonly Service<ICharacterPreviewFactory> characterPreviewFactory; |
| | 15 | |
|
| | 16 | | private readonly ICharacterPreviewController previewController; |
| | 17 | | private readonly ISocialAnalytics socialAnalytics; |
| | 18 | | private CancellationTokenSource cancellationTokenSource; |
| | 19 | | private readonly IPassportPlayerPreviewComponentView view; |
| | 20 | |
|
| 0 | 21 | | public PassportPlayerPreviewComponentController( |
| | 22 | | IPassportPlayerPreviewComponentView view, |
| | 23 | | ISocialAnalytics socialAnalytics) |
| | 24 | | { |
| 0 | 25 | | this.view = view; |
| 0 | 26 | | this.socialAnalytics = socialAnalytics; |
| | 27 | |
|
| 0 | 28 | | if (view.PreviewCameraRotationController != null) |
| 0 | 29 | | view.PreviewCameraRotationController.OnHorizontalRotation += RotateCharacterPreview; |
| 0 | 30 | | cancellationTokenSource = new CancellationTokenSource(); |
| | 31 | |
|
| 0 | 32 | | previewController = characterPreviewFactory.Ref.Create(CharacterPreviewMode.WithoutHologram, |
| | 33 | | view.CharacterPreviewTexture, true, PreviewCameraFocus.Preview); |
| | 34 | |
|
| 0 | 35 | | view.SetModel(new (TutorialEnabled)); |
| 0 | 36 | | view.OnEndDragEvent += EndPreviewDrag; |
| 0 | 37 | | } |
| | 38 | |
|
| | 39 | | private void EndPreviewDrag(double timeSpendDragging) |
| | 40 | | { |
| 0 | 41 | | socialAnalytics.SendInspectAvatar(timeSpendDragging); |
| 0 | 42 | | } |
| | 43 | |
|
| | 44 | | public void SetPassportPanelVisibility(bool visible) |
| | 45 | | { |
| 0 | 46 | | previewController.SetEnabled(visible); |
| 0 | 47 | | if (visible) |
| 0 | 48 | | previewController.ResetRotation(); |
| 0 | 49 | | } |
| | 50 | |
|
| 0 | 51 | | public void SetAsLoading(bool isLoading) => view.SetAsLoading(isLoading); |
| | 52 | |
|
| 0 | 53 | | private bool TutorialEnabled => PlayerPrefsBridge.GetBool(TUTORIAL_ENABLED_KEY, true); |
| | 54 | |
|
| | 55 | | private void RotateCharacterPreview(float angularVelocity) |
| | 56 | | { |
| 0 | 57 | | previewController.Rotate(angularVelocity); |
| 0 | 58 | | SetRotationTutorialCompleted(); |
| 0 | 59 | | } |
| | 60 | |
|
| | 61 | | private void SetRotationTutorialCompleted() |
| | 62 | | { |
| 0 | 63 | | if (!TutorialEnabled) |
| 0 | 64 | | return; |
| | 65 | |
|
| 0 | 66 | | PlayerPrefsBridge.SetBool(TUTORIAL_ENABLED_KEY, false); |
| 0 | 67 | | PlayerPrefsBridge.Save(); |
| 0 | 68 | | view.HideTutorial(); |
| | 69 | |
|
| 0 | 70 | | view.SetModel(new (false)); |
| 0 | 71 | | } |
| | 72 | |
|
| | 73 | | public void UpdateWithUserProfile(UserProfile userProfile, bool activateLoading) |
| | 74 | | { |
| | 75 | | async UniTask UpdateWithUserProfileAsync() |
| | 76 | | { |
| 0 | 77 | | if (activateLoading) |
| 0 | 78 | | SetAsLoading(true); |
| | 79 | |
|
| 0 | 80 | | await previewController.TryUpdateModelAsync(userProfile.avatar, cancellationTokenSource.Token) |
| | 81 | | .SuppressCancellationThrow(); |
| | 82 | |
|
| 0 | 83 | | SetAsLoading(false); |
| 0 | 84 | | } |
| | 85 | |
|
| 0 | 86 | | cancellationTokenSource?.Cancel(); |
| 0 | 87 | | cancellationTokenSource?.Dispose(); |
| 0 | 88 | | cancellationTokenSource = new CancellationTokenSource(); |
| 0 | 89 | | UpdateWithUserProfileAsync().Forget(); |
| 0 | 90 | | } |
| | 91 | |
|
| | 92 | | public void Dispose() |
| | 93 | | { |
| 0 | 94 | | if (view.PreviewCameraRotationController != null) |
| 0 | 95 | | view.PreviewCameraRotationController.OnHorizontalRotation -= RotateCharacterPreview; |
| | 96 | |
|
| 0 | 97 | | cancellationTokenSource?.Cancel(); |
| 0 | 98 | | cancellationTokenSource?.Dispose(); |
| 0 | 99 | | cancellationTokenSource = null; |
| 0 | 100 | | } |
| | 101 | | } |
| | 102 | | } |