| | 1 | | using System; |
| | 2 | |
|
| | 3 | | namespace DCL.Components |
| | 4 | | { |
| | 5 | | public class GetAnchorPointsHandler : IDisposable |
| | 6 | | { |
| | 7 | | public event Action OnAvatarRemoved; |
| | 8 | |
|
| | 9 | | private Action<IAvatarAnchorPoints> onAvatarFound; |
| | 10 | | private bool cleaned = false; |
| | 11 | |
|
| 42 | 12 | | private UserProfile ownPlayerProfile => UserProfile.GetOwnUserProfile(); |
| | 13 | |
|
| 15 | 14 | | private IAnchorPointsGetterHandler ownPlayerAnchorPointsGetterHandler => new OwnPlayerGetAnchorPointsHandler(); |
| 25 | 15 | | private IAnchorPointsGetterHandler otherPlayerAnchorPointsGetterHandler => new OtherPlayerGetAnchorPointsHandler |
| | 16 | | private IAnchorPointsGetterHandler currentAnchorPointsGetterHandler; |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// Search for an `avatarId` that could be the own player or any other player. |
| | 20 | | /// If that `avatarId` is loaded it will call `onSuccess`, otherwise it will wait |
| | 21 | | /// for that `avatarId` to be loaded |
| | 22 | | /// </summary> |
| | 23 | | public void SearchAnchorPoints(string avatarId, Action<IAvatarAnchorPoints> onSuccess, bool supportNullId = fals |
| | 24 | | { |
| 14 | 25 | | CleanUp(); |
| 14 | 26 | | cleaned = false; |
| | 27 | |
|
| 14 | 28 | | if (string.IsNullOrEmpty(avatarId) && !supportNullId) |
| 0 | 29 | | return; |
| | 30 | |
|
| 14 | 31 | | string ownUserId = ownPlayerProfile.userId; |
| | 32 | |
|
| 14 | 33 | | onAvatarFound = onSuccess; |
| | 34 | |
|
| | 35 | | void GetOwnProfileUpdated(UserProfile profile) |
| | 36 | | { |
| 14 | 37 | | ownPlayerProfile.OnUpdate -= GetOwnProfileUpdated; |
| 14 | 38 | | ownUserId = profile.userId; |
| 14 | 39 | | string targetId = string.IsNullOrEmpty(avatarId) ? ownUserId : avatarId; |
| | 40 | |
|
| 14 | 41 | | if (cleaned) |
| | 42 | | { |
| 0 | 43 | | return; |
| | 44 | | } |
| | 45 | |
|
| 14 | 46 | | currentAnchorPointsGetterHandler = GetHandler(targetId, ownUserId); |
| 14 | 47 | | currentAnchorPointsGetterHandler.OnAvatarFound += OnAvatarFoundEvent; |
| 14 | 48 | | currentAnchorPointsGetterHandler.OnAvatarRemoved += OnAvatarRemovedEvent; |
| 14 | 49 | | currentAnchorPointsGetterHandler.GetAnchorPoints(targetId); |
| 14 | 50 | | } |
| | 51 | |
|
| 14 | 52 | | if (string.IsNullOrEmpty(ownUserId)) |
| | 53 | | { |
| | 54 | | // Unsubscribe first in case of multiple calls. |
| 0 | 55 | | ownPlayerProfile.OnUpdate -= GetOwnProfileUpdated; |
| 0 | 56 | | ownPlayerProfile.OnUpdate += GetOwnProfileUpdated; |
| | 57 | | } |
| | 58 | | else |
| | 59 | | { |
| 14 | 60 | | GetOwnProfileUpdated(ownPlayerProfile); |
| | 61 | | } |
| 14 | 62 | | } |
| | 63 | |
|
| | 64 | | /// <summary> |
| | 65 | | /// Cancel the active search/waiting of the `avatarId` |
| | 66 | | /// </summary> |
| | 67 | | public void CancelCurrentSearch() |
| | 68 | | { |
| 24 | 69 | | CleanUp(); |
| 24 | 70 | | } |
| | 71 | |
|
| | 72 | | public void Dispose() |
| | 73 | | { |
| 13 | 74 | | CleanUp(); |
| 13 | 75 | | ownPlayerAnchorPointsGetterHandler.Dispose(); |
| 13 | 76 | | otherPlayerAnchorPointsGetterHandler.Dispose(); |
| 13 | 77 | | } |
| | 78 | |
|
| | 79 | | private void CleanUp() |
| | 80 | | { |
| 51 | 81 | | onAvatarFound = null; |
| 51 | 82 | | cleaned = true; |
| | 83 | |
|
| 51 | 84 | | if (currentAnchorPointsGetterHandler != null) |
| | 85 | | { |
| 19 | 86 | | currentAnchorPointsGetterHandler.CleanUp(); |
| 19 | 87 | | currentAnchorPointsGetterHandler.OnAvatarFound -= OnAvatarFoundEvent; |
| 19 | 88 | | currentAnchorPointsGetterHandler.OnAvatarRemoved -= OnAvatarRemovedEvent; |
| | 89 | | } |
| 51 | 90 | | } |
| | 91 | |
|
| | 92 | | private IAnchorPointsGetterHandler GetHandler(string id, string ownPlayerId) |
| | 93 | | { |
| 14 | 94 | | return id == ownPlayerId ? ownPlayerAnchorPointsGetterHandler : otherPlayerAnchorPointsGetterHandler; |
| | 95 | | } |
| | 96 | |
|
| | 97 | | private void OnAvatarFoundEvent(string id, IAvatarAnchorPoints anchorPoints) |
| | 98 | | { |
| 12 | 99 | | onAvatarFound?.Invoke(anchorPoints); |
| 11 | 100 | | } |
| | 101 | |
|
| | 102 | | private void OnAvatarRemovedEvent(string id) |
| | 103 | | { |
| 3 | 104 | | OnAvatarRemoved?.Invoke(); |
| 2 | 105 | | } |
| | 106 | | } |
| | 107 | | } |