| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.Helpers; |
| | 4 | |
|
| | 5 | | internal class UsersSearchFriendsHandler : IDisposable |
| | 6 | | { |
| | 7 | | public event Action<string> OnFriendRemoved; |
| 0 | 8 | | public bool isFriendlistDirty { private set; get; } |
| | 9 | |
|
| | 10 | | private readonly IFriendsController friendsController; |
| | 11 | |
|
| | 12 | | private bool waitingFriendsInitialize; |
| | 13 | | private Promise<Dictionary<string, FriendsController.UserStatus>> friendListPromise; |
| | 14 | |
|
| 17 | 15 | | public UsersSearchFriendsHandler(IFriendsController friendsController) |
| | 16 | | { |
| 17 | 17 | | this.friendsController = friendsController; |
| 17 | 18 | | isFriendlistDirty = true; |
| 17 | 19 | | waitingFriendsInitialize = true; |
| | 20 | |
|
| 17 | 21 | | if (friendsController != null) |
| | 22 | | { |
| 17 | 23 | | friendsController.OnUpdateFriendship += OnUpdateFriendship; |
| | 24 | | } |
| 17 | 25 | | } |
| | 26 | |
|
| | 27 | | public Promise<Dictionary<string, FriendsController.UserStatus>> GetFriendList() |
| | 28 | | { |
| 5 | 29 | | if (friendListPromise == null) |
| | 30 | | { |
| 5 | 31 | | friendListPromise = new Promise<Dictionary<string, FriendsController.UserStatus>>(); |
| | 32 | | } |
| | 33 | |
|
| 5 | 34 | | if (friendsController == null) |
| | 35 | | { |
| 0 | 36 | | return friendListPromise; |
| | 37 | | } |
| | 38 | |
|
| 5 | 39 | | if (isFriendlistDirty && friendsController.isInitialized) |
| | 40 | | { |
| 5 | 41 | | isFriendlistDirty = false; |
| 5 | 42 | | waitingFriendsInitialize = false; |
| 5 | 43 | | friendListPromise.Resolve(friendsController.GetFriends()); |
| | 44 | | } |
| | 45 | |
|
| 5 | 46 | | if (waitingFriendsInitialize) |
| | 47 | | { |
| 0 | 48 | | waitingFriendsInitialize = false; |
| 0 | 49 | | friendsController.OnInitialized += OnFriendsInitialized; |
| | 50 | | } |
| | 51 | |
|
| 5 | 52 | | return friendListPromise; |
| | 53 | | } |
| | 54 | |
|
| | 55 | | public void Dispose() |
| | 56 | | { |
| 17 | 57 | | if (friendsController != null) |
| | 58 | | { |
| 17 | 59 | | friendsController.OnUpdateFriendship -= OnUpdateFriendship; |
| 17 | 60 | | friendsController.OnInitialized -= OnFriendsInitialized; |
| | 61 | | } |
| | 62 | |
|
| 17 | 63 | | friendListPromise?.Dispose(); |
| 17 | 64 | | friendListPromise = null; |
| 17 | 65 | | } |
| | 66 | |
|
| | 67 | | private void OnFriendsInitialized() |
| | 68 | | { |
| 0 | 69 | | friendsController.OnInitialized -= OnFriendsInitialized; |
| 0 | 70 | | isFriendlistDirty = false; |
| 0 | 71 | | friendListPromise?.Resolve(friendsController.GetFriends()); |
| 0 | 72 | | } |
| | 73 | |
|
| | 74 | | private void OnUpdateFriendship(string userId, FriendshipAction friendshipAction) |
| | 75 | | { |
| 0 | 76 | | if (!friendsController.isInitialized) |
| 0 | 77 | | return; |
| | 78 | |
|
| 0 | 79 | | if (friendshipAction == FriendshipAction.APPROVED) |
| | 80 | | { |
| 0 | 81 | | isFriendlistDirty = true; |
| 0 | 82 | | return; |
| | 83 | | } |
| | 84 | |
|
| 0 | 85 | | if (friendshipAction == FriendshipAction.DELETED) |
| | 86 | | { |
| 0 | 87 | | isFriendlistDirty = true; |
| 0 | 88 | | OnFriendRemoved?.Invoke(userId); |
| | 89 | | } |
| 0 | 90 | | } |
| | 91 | | } |