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