| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Tasks; |
| | 3 | | using System; |
| | 4 | | using System.Threading; |
| | 5 | |
|
| | 6 | | namespace DCL.Social.Friends |
| | 7 | | { |
| | 8 | | public class ProxySocialApiBridge : ISocialApiBridge |
| | 9 | | { |
| | 10 | | private readonly RPCSocialApiBridge socialApiBridge; |
| | 11 | | private readonly DataStore dataStore; |
| | 12 | |
|
| 423 | 13 | | private CancellationTokenSource lifeCycleCancellationToken = new (); |
| | 14 | | private UniTaskCompletionSource featureFlagsInitializedTask; |
| | 15 | |
|
| 865 | 16 | | private FeatureFlag featureFlags => dataStore.featureFlags.flags.Get(); |
| | 17 | |
|
| 442 | 18 | | private bool useSocialApiBridge => featureFlags.IsFeatureEnabled("use-social-client"); |
| | 19 | |
|
| | 20 | | public event Action<FriendRequest> OnIncomingFriendRequestAdded |
| | 21 | | { |
| 0 | 22 | | add => socialApiBridge.OnIncomingFriendRequestAdded += value; |
| 423 | 23 | | remove => socialApiBridge.OnIncomingFriendRequestAdded -= value; |
| | 24 | | } |
| | 25 | |
|
| | 26 | | public event Action<FriendRequest> OnOutgoingFriendRequestAdded |
| | 27 | | { |
| 0 | 28 | | add => socialApiBridge.OnOutgoingFriendRequestAdded += value; |
| 423 | 29 | | remove => socialApiBridge.OnOutgoingFriendRequestAdded -= value; |
| | 30 | | } |
| | 31 | |
|
| | 32 | | public event Action<string> OnFriendRequestAccepted |
| | 33 | | { |
| 0 | 34 | | add => socialApiBridge.OnFriendRequestAccepted += value; |
| 423 | 35 | | remove => socialApiBridge.OnFriendRequestAccepted -= value; |
| | 36 | | } |
| | 37 | |
|
| | 38 | | public event Action<string> OnFriendRequestRejected |
| | 39 | | { |
| 0 | 40 | | add => socialApiBridge.OnFriendRequestRejected += value; |
| 423 | 41 | | remove => socialApiBridge.OnFriendRequestRejected -= value; |
| | 42 | | } |
| | 43 | |
|
| | 44 | | public event Action<string> OnFriendRequestCanceled |
| | 45 | | { |
| 0 | 46 | | add => socialApiBridge.OnFriendRequestCanceled += value; |
| 423 | 47 | | remove => socialApiBridge.OnFriendRequestCanceled -= value; |
| | 48 | | } |
| | 49 | |
|
| | 50 | | public event Action<string> OnDeletedByFriend |
| | 51 | | { |
| 0 | 52 | | add => socialApiBridge.OnDeletedByFriend += value; |
| 423 | 53 | | remove => socialApiBridge.OnDeletedByFriend -= value; |
| | 54 | | } |
| | 55 | |
|
| 423 | 56 | | public ProxySocialApiBridge(RPCSocialApiBridge socialApiBridge, |
| | 57 | | DataStore dataStore) |
| | 58 | | { |
| 423 | 59 | | this.socialApiBridge = socialApiBridge; |
| 423 | 60 | | this.dataStore = dataStore; |
| 423 | 61 | | } |
| | 62 | |
|
| | 63 | | public void Dispose() |
| | 64 | | { |
| 423 | 65 | | lifeCycleCancellationToken.SafeCancelAndDispose(); |
| | 66 | |
|
| 423 | 67 | | if (useSocialApiBridge) |
| 0 | 68 | | socialApiBridge.Dispose(); |
| 423 | 69 | | } |
| | 70 | |
|
| | 71 | | public void Initialize() |
| | 72 | | { |
| 423 | 73 | | lifeCycleCancellationToken = lifeCycleCancellationToken.SafeRestart(); |
| | 74 | |
|
| | 75 | | void InitializeIfEnabled() |
| | 76 | | { |
| 19 | 77 | | if (useSocialApiBridge) |
| 0 | 78 | | socialApiBridge.Initialize(); |
| 19 | 79 | | } |
| | 80 | |
|
| 423 | 81 | | if (featureFlags.IsInitialized) |
| 0 | 82 | | InitializeIfEnabled(); |
| | 83 | | else |
| 423 | 84 | | WaitForFeatureFlagsToBeInitialized(lifeCycleCancellationToken.Token) |
| | 85 | | .ContinueWith(InitializeIfEnabled) |
| | 86 | | .Forget(); |
| 423 | 87 | | } |
| | 88 | |
|
| | 89 | | public UniTask<AllFriendsInitializationMessage> GetInitializationInformationAsync(CancellationToken cancellation |
| | 90 | | { |
| 0 | 91 | | if (useSocialApiBridge) |
| 0 | 92 | | return socialApiBridge.GetInitializationInformationAsync(cancellationToken); |
| | 93 | |
|
| 0 | 94 | | return UniTask.Never<AllFriendsInitializationMessage>(cancellationToken); |
| | 95 | | } |
| | 96 | |
|
| | 97 | | public UniTask RejectFriendshipAsync(string friendId, CancellationToken cancellationToken = default) |
| | 98 | | { |
| 0 | 99 | | if (useSocialApiBridge) |
| 0 | 100 | | return socialApiBridge.RejectFriendshipAsync(friendId, cancellationToken); |
| | 101 | |
|
| 0 | 102 | | return UniTask.Never(cancellationToken); |
| | 103 | | } |
| | 104 | |
|
| | 105 | | public UniTask<FriendRequest> RequestFriendshipAsync(string friendId, string messageBody, CancellationToken canc |
| | 106 | | { |
| 0 | 107 | | if (useSocialApiBridge) |
| 0 | 108 | | return socialApiBridge.RequestFriendshipAsync(friendId, messageBody, cancellationToken); |
| | 109 | |
|
| 0 | 110 | | return UniTask.Never<FriendRequest>(cancellationToken); |
| | 111 | | } |
| | 112 | |
|
| | 113 | | public UniTask CancelFriendshipAsync(string friendId, CancellationToken cancellationToken = default) |
| | 114 | | { |
| 0 | 115 | | if (useSocialApiBridge) |
| 0 | 116 | | return socialApiBridge.CancelFriendshipAsync(friendId, cancellationToken); |
| | 117 | |
|
| 0 | 118 | | return UniTask.Never(cancellationToken); |
| | 119 | | } |
| | 120 | |
|
| | 121 | | public UniTask AcceptFriendshipAsync(string friendId, CancellationToken cancellationToken = default) |
| | 122 | | { |
| 0 | 123 | | if (useSocialApiBridge) |
| 0 | 124 | | return socialApiBridge.AcceptFriendshipAsync(friendId, cancellationToken); |
| | 125 | |
|
| 0 | 126 | | return UniTask.Never(cancellationToken); |
| | 127 | | } |
| | 128 | |
|
| | 129 | | public UniTask DeleteFriendshipAsync(string friendId, CancellationToken cancellationToken = default) |
| | 130 | | { |
| 0 | 131 | | if (useSocialApiBridge) |
| 0 | 132 | | return socialApiBridge.DeleteFriendshipAsync(friendId, cancellationToken); |
| | 133 | |
|
| 0 | 134 | | return UniTask.Never(cancellationToken); |
| | 135 | | } |
| | 136 | |
|
| | 137 | | private async UniTask WaitForFeatureFlagsToBeInitialized(CancellationToken cancellationToken) |
| | 138 | | { |
| 423 | 139 | | if (featureFlagsInitializedTask == null) |
| | 140 | | { |
| 423 | 141 | | featureFlagsInitializedTask = new UniTaskCompletionSource(); |
| | 142 | |
|
| | 143 | | void CompleteTaskAndUnsubscribe(FeatureFlag current, FeatureFlag previous) |
| | 144 | | { |
| 59 | 145 | | dataStore.featureFlags.flags.OnChange -= CompleteTaskAndUnsubscribe; |
| 59 | 146 | | featureFlagsInitializedTask.TrySetResult(); |
| 59 | 147 | | } |
| | 148 | |
|
| 423 | 149 | | dataStore.featureFlags.flags.OnChange += CompleteTaskAndUnsubscribe; |
| | 150 | | } |
| | 151 | |
|
| 1269 | 152 | | await featureFlagsInitializedTask.Task.AttachExternalCancellation(cancellationToken); |
| 57 | 153 | | await UniTask.NextFrame(cancellationToken); |
| 19 | 154 | | await UniTask.SwitchToMainThread(cancellationToken); |
| 19 | 155 | | } |
| | 156 | | } |
| | 157 | | } |