| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using MainScripts.DCL.Controllers.HUD.CharacterPreview; |
| | 4 | | using System; |
| | 5 | | using System.Threading; |
| | 6 | | using UnityEngine; |
| | 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 CancellationTokenSource cancellationTokenSource; |
| | 18 | | private readonly IPassportPlayerPreviewComponentView view; |
| | 19 | |
|
| 0 | 20 | | public PassportPlayerPreviewComponentController(IPassportPlayerPreviewComponentView view) |
| | 21 | | { |
| 0 | 22 | | this.view = view; |
| 0 | 23 | | view.PreviewCameraRotation.OnHorizontalRotation += RotateCharacterPreview; |
| 0 | 24 | | cancellationTokenSource = new CancellationTokenSource(); |
| | 25 | |
|
| 0 | 26 | | previewController = characterPreviewFactory.Ref.Create(CharacterPreviewMode.WithHologram, |
| | 27 | | view.CharacterPreviewTexture, true, CharacterPreviewController.CameraFocus.Preview); |
| | 28 | |
|
| 0 | 29 | | view.SetModel(new (TutorialEnabled)); |
| 0 | 30 | | } |
| | 31 | |
|
| | 32 | | public void SetPassportPanelVisibility(bool visible) |
| | 33 | | { |
| 0 | 34 | | previewController.SetEnabled(visible); |
| 0 | 35 | | if (visible) |
| 0 | 36 | | previewController.ResetRotation(); |
| 0 | 37 | | } |
| | 38 | |
|
| 0 | 39 | | private bool TutorialEnabled => PlayerPrefsUtils.GetBool(TUTORIAL_ENABLED_KEY, true); |
| | 40 | |
|
| | 41 | | private void RotateCharacterPreview(float angularVelocity) |
| | 42 | | { |
| 0 | 43 | | previewController.Rotate(angularVelocity); |
| 0 | 44 | | SetRotationTutorialCompleted(); |
| 0 | 45 | | } |
| | 46 | |
|
| | 47 | | private void SetRotationTutorialCompleted() |
| | 48 | | { |
| 0 | 49 | | if (!TutorialEnabled) |
| 0 | 50 | | return; |
| | 51 | |
|
| 0 | 52 | | PlayerPrefsUtils.SetBool(TUTORIAL_ENABLED_KEY, false); |
| 0 | 53 | | PlayerPrefsUtils.Save(); |
| | 54 | |
|
| 0 | 55 | | view.SetModel(new (false)); |
| 0 | 56 | | } |
| | 57 | |
|
| | 58 | | public void UpdateWithUserProfile(UserProfile userProfile) |
| | 59 | | { |
| 0 | 60 | | previewController.TryUpdateModelAsync(userProfile.avatar, cancellationTokenSource.Token) |
| | 61 | | .SuppressCancellationThrow() |
| | 62 | | .Forget(); |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | public void Dispose() |
| | 66 | | { |
| 0 | 67 | | cancellationTokenSource?.Cancel(); |
| 0 | 68 | | cancellationTokenSource?.Dispose(); |
| 0 | 69 | | cancellationTokenSource = null; |
| 0 | 70 | | } |
| | 71 | | } |
| | 72 | | } |