Esempio n. 1
0
        private async void acceptRequestButton_Click(object sender, RoutedEventArgs e)
        {
            FriendRequest    selectedRequest = null;
            FriendsListEntry newFriend       = null;
            int i = 0;

            // Find the request which has it's buttons showing, this must be the one that has been pressed
            foreach (FriendRequest friendRequest in AppServices.friendsData.friendRequests)
            {
                if (friendRequest.showRequestButtons == Windows.UI.Xaml.Visibility.Visible)
                {
                    selectedRequest = friendRequest;
                    break;
                }
                i++;
            }
            // If a request exists then we accept it
            if (selectedRequest != null)
            {
                this.StartAsyncOperations("Accepting request...");
                AppServices.operationComplete = false;
                AppServices.buddyService.AcceptFriendRequest(AppServices.localUsernameEncrypted, selectedRequest.encryptedUsername, new AcceptRequestCallback());

                await Task.Run(() =>
                {
                    while (AppServices.operationComplete == false)
                    {
                        ;
                    }
                });

                // If the accept was successful we now remove them from the request list and add them to the friends list
                if (AppServices.operationSuccessful)
                {
                    AppServices.friendsData.friendsList.Add(new FriendsListEntry {
                        username = selectedRequest.username, isOnline = false
                    });
                    newFriend = AppServices.friendsData.friendsList.ElementAt(AppServices.friendsData.friendsList.Count);
                    AppServices.friendsData.friendRequests.RemoveAt(i);

                    // Update the friends list XAML in case this was the last request
                    this.DisplayFriendRequestsList();

                    // Now check if the new friend is online
                    this.StartAsyncOperations("Checking if new friend is online...");
                    AppServices.operationComplete = false;
                    AppServices.isFriendOnline    = false;
                    AppServices.sessionService.GetSession(selectedRequest.encryptedUsername, false, new GetFriendSessionCallback(selectedRequest.encryptedUsername));

                    await Task.Run(() =>
                    {
                        while (AppServices.operationComplete == false)
                        {
                            ;
                        }
                    });

                    // An error occured and the friend may or may not be online. This is needed because the App42 callback will call the onException callback
                    //	even if no error occured but the friend is offline. We just use isFriendOnline to either mark an error occuring if the operation
                    //	wasn't successful. Or the friend's online status if the operation was successful
                    if (AppServices.operationSuccessful == false && AppServices.isFriendOnline == true)
                    {
                        this.EndAsyncOperationsError();
                        return;
                    }
                    // The friend is online
                    if (AppServices.operationSuccessful && AppServices.isFriendOnline)
                    {
                        newFriend.isOnline = true;
                        AppServices.appWarpLastSentToUsername = newFriend.username;
                        AppServices.warpClient.sendPrivateUpdate(newFriend.username, MessageBuilder.buildAcceptedFriendRequestBytes(AppServices.defaultEncryptionKey));

                        //bool sendSuccessful = await AppServices.SendDataToUser(selectedRequest.username, MessageBuilder.buildAcceptedFriendRequestBytes());

                        //if (sendSuccessful == false)
                        //{
                        //	this.EndAsyncOperationsError();
                        //	return;
                        //}
                    }
                    // The friend is offline
                    this.EndAsyncOperationsSuccess();
                    return;
                }
                else
                {
                    this.EndAsyncOperationsError();
                    return;
                }
            }
        }