| | 1 | | using System; |
| | 2 | |
|
| | 3 | | namespace DCL.Components |
| | 4 | | { |
| | 5 | | internal class GetAnchorPointsHandler : IDisposable |
| | 6 | | { |
| | 7 | | public event Action OnAvatarRemoved; |
| | 8 | |
|
| | 9 | | private Action<IAvatarAnchorPoints> onAvatarFound; |
| | 10 | |
|
| 10 | 11 | | private string ownPlayerId => UserProfile.GetOwnUserProfile().userId; |
| | 12 | |
|
| 0 | 13 | | private IAnchorPointsGetterHandler ownPlayerAnchorPointsGetterHandler => new OwnPlayerGetAnchorPointsHandler(); |
| 15 | 14 | | private IAnchorPointsGetterHandler otherPlayerAnchorPointsGetterHandler => new OtherPlayerGetAnchorPointsHandler |
| | 15 | | private IAnchorPointsGetterHandler currentAnchorPointsGetterHandler; |
| | 16 | |
|
| | 17 | | /// <summary> |
| | 18 | | /// Search for an `avatarId` that could be the own player or any other player. |
| | 19 | | /// If that `avatarId` is loaded it will call `onSuccess`, otherwise it will wait |
| | 20 | | /// for that `avatarId` to be loaded |
| | 21 | | /// </summary> |
| | 22 | | public void SearchAnchorPoints(string avatarId, Action<IAvatarAnchorPoints> onSuccess) |
| | 23 | | { |
| 10 | 24 | | CleanUp(); |
| | 25 | |
|
| 10 | 26 | | if (string.IsNullOrEmpty(avatarId)) |
| 0 | 27 | | return; |
| | 28 | |
|
| 10 | 29 | | onAvatarFound = onSuccess; |
| | 30 | |
|
| 10 | 31 | | currentAnchorPointsGetterHandler = GetHandler(avatarId); |
| 10 | 32 | | currentAnchorPointsGetterHandler.OnAvatarFound += OnAvatarFoundEvent; |
| 10 | 33 | | currentAnchorPointsGetterHandler.OnAvatarRemoved += OnAvatarRemovedEvent; |
| 10 | 34 | | currentAnchorPointsGetterHandler.GetAnchorPoints(avatarId); |
| 10 | 35 | | } |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Cancel the active search/waiting of the `avatarId` |
| | 39 | | /// </summary> |
| | 40 | | public void CancelCurrentSearch() |
| | 41 | | { |
| 13 | 42 | | CleanUp(); |
| 13 | 43 | | } |
| | 44 | |
|
| | 45 | | public void Dispose() |
| | 46 | | { |
| 7 | 47 | | CleanUp(); |
| 7 | 48 | | ownPlayerAnchorPointsGetterHandler.Dispose(); |
| 7 | 49 | | otherPlayerAnchorPointsGetterHandler.Dispose(); |
| 7 | 50 | | } |
| | 51 | |
|
| | 52 | | private void CleanUp() |
| | 53 | | { |
| 30 | 54 | | onAvatarFound = null; |
| | 55 | |
|
| 30 | 56 | | if (currentAnchorPointsGetterHandler != null) |
| | 57 | | { |
| 10 | 58 | | currentAnchorPointsGetterHandler.CleanUp(); |
| 10 | 59 | | currentAnchorPointsGetterHandler.OnAvatarFound -= OnAvatarFoundEvent; |
| 10 | 60 | | currentAnchorPointsGetterHandler.OnAvatarRemoved -= OnAvatarRemovedEvent; |
| | 61 | | } |
| 30 | 62 | | } |
| | 63 | |
|
| | 64 | | private IAnchorPointsGetterHandler GetHandler(string id) |
| | 65 | | { |
| 10 | 66 | | return id == ownPlayerId ? ownPlayerAnchorPointsGetterHandler : otherPlayerAnchorPointsGetterHandler; |
| | 67 | | } |
| | 68 | |
|
| | 69 | | private void OnAvatarFoundEvent(string id, IAvatarAnchorPoints anchorPoints) |
| | 70 | | { |
| 9 | 71 | | onAvatarFound?.Invoke(anchorPoints); |
| 8 | 72 | | } |
| | 73 | |
|
| | 74 | | private void OnAvatarRemovedEvent(string id) |
| | 75 | | { |
| 2 | 76 | | OnAvatarRemoved?.Invoke(); |
| 1 | 77 | | } |
| | 78 | | } |
| | 79 | | } |