protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            string id = string.Empty;

            try
            {
                if (NavigationContext.QueryString.TryGetValue("id", out id))
                {
                    _id = Convert.ToInt32(id);

                    // From friend requests.
                    var friend = App.Current.EntityService.FriendsRequests.FirstOrDefault(x => x.Uid == _id);

                    // From mutual friends.
                    if (friend == null)
                    {
                        _isMutual = true;
                        friend = App.Current.EntityService.FriendsMutual.FirstOrDefault(x => x.Uid == _id);
                        DeleteButton.IsEnabled = false;
                    }

                    if (friend != null)
                    {
                        _friend = friend;
                        this.DataContext = (object)_friend;
                    }
                }
                else
                {
                    // From global search page.
                    if (App.Current.EntityService.FoundGlobalUser != null)
                    {
                        _friend = App.Current.EntityService.FoundGlobalUser;
                        _id = _friend.Uid;
                        this.DataContext = (object)_friend;
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("OnNavigatedTo in FriendRequestPage failed: " + ex.Message);
            }
        }
        private void SearchTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (_userTypingTimer != null)
            {
                _userTypingTimer.Dispose();
                _userTypingTimer = null;
            }

            if (!string.IsNullOrEmpty(SearchTextBox.Text))
            {
                _workQ = SearchTextBox.Text;

                _userTypingTimer = new Timer(state =>
                {
                    Dispatcher.BeginInvoke(() =>
                    {
                        // Start search after 1 second of silence.
                        var op = new UsersSearch(_workQ, results =>
                        {
                            Dispatcher.BeginInvoke(() =>
                            {
                                try
                                {
                                    _results.Clear();
                                    _photosToLoad.Clear();

                                    foreach (var result in results)
                                    {
                                        try
                                        {
                                            var model = new FriendViewModel(result.Uid,
                                                result.FullName, string.Empty, result.FirstName, result.LastName,
                                                result.IsOnline, -1, result.PhotoBig, App.Current.EntityService.DefaultAvatar);
                                            _results.Add(model);

                                            // Try to load image from cache.
                                            string filename = CommonHelper.DoDigest(result.PhotoBig);
                                            BitmapImage image = _imageCache.GetItem(filename);
                                            if (image != null)
                                                model.Photo = image;
                                            else // ...if it doesn't exists - load from web.
                                                _photosToLoad.Add(model, new AvatarLoadItem(_photosToLoad.Count, result.PhotoBig, filename));
                                        }
                                        catch (Exception ex)
                                        {
                                            Debug.WriteLine("UsersSearch model creating failed: " + ex.Message);
                                            // If some fields is empty =).
                                        }
                                    }

                                    if (results.Count > 0)
                                        SearchDescription.Visibility = System.Windows.Visibility.Collapsed;
                                    else
                                        SearchDescription.Visibility = System.Windows.Visibility.Visible;

                                    _LoadPhotos();
                                }
                                catch (Exception ex)
                                {
                                    Debug.WriteLine("UsersSearch failed: " + ex.Message);
                                    // If some inter-process error
                                }
                            });
                        });
                        op.Execute();
                    });
                }, null, 1000, -1);
            }
            else
            {
                SearchDescription.Visibility = System.Windows.Visibility.Visible;
            }
        }
Example #3
0
        public void GetFriendRequests(Action finishedCallback)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                try
                {
                    var op = new FriendsGetRequests(1, friends =>
                    {
                        try
                        {
                            _stateCounter.CountOfRequests = friends.Count;

                            // Collect all uids to load info.
                            var dict = new Dictionary<int, string>();
                            string uids = string.Empty;
                            string mutualUids = string.Empty;
                            foreach (var request in friends)
                            {
                                uids += request.Uid.ToString() + ",";

                                dict.Add(request.Uid, request.Message == null ? string.Empty : request.Message);

                                if (request.MutualFriends != null)
                                {
                                    foreach (var mutual in request.MutualFriends.Uids)
                                    {
                                        mutualUids += mutual.ToString() + ",";
                                    }
                                }
                            }

                            // Get friends requests.
                            if (!string.IsNullOrEmpty(uids))
                            {
                                UsersGet op1 = new UsersGet(uids, info =>
                                {
                                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                                    {
                                        try
                                        {
                                            FriendsRequests.Clear();

                                            foreach (var user in info)
                                            {
                                                var model = new FriendViewModel(user.Uid, user.FullName, string.Empty,
                                                    user.FirstName, user.LastName, user.IsOnline, -1, user.PhotoBig, DefaultAvatar);

                                                string message = string.Empty;
                                                dict.TryGetValue(user.Uid, out message);
                                                model.Message = message;
                                                FriendsRequests.Add(model);//new FriendRequestViewModel(model, user.Photo, message));
                                            }

                                            if (string.IsNullOrEmpty(mutualUids))
                                                finishedCallback();
                                        }
                                        catch (Exception ex)
                                        {
                                            Debug.WriteLine("UsersGet in GetFriendRequests failed: " + ex.Message);
                                            finishedCallback();
                                        }
                                    });
                                });
                                op1.Execute();
                            }

                            // Get mutual friends.
                            if (!string.IsNullOrEmpty(mutualUids))
                            {
                                UsersGet op2 = new UsersGet(mutualUids, info =>
                                {
                                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                                    {
                                        try
                                        {
                                            FriendsMutual.Clear();

                                            foreach (var user in info)
                                            {
                                                var inFriends = Friends.FirstOrDefault(x => x.Uid == user.Uid);

                                                if (inFriends == null)
                                                    FriendsMutual.Add(new FriendViewModel(user.Uid, user.FullName, string.Empty,
                                                        user.FirstName, user.LastName, user.IsOnline, -1, user.PhotoBig, DefaultAvatar));
                                            }

                                            finishedCallback();
                                        }
                                        catch (Exception ex)
                                        {
                                            Debug.WriteLine("UsersGet in GetFriendRequests failed: " + ex.Message);
                                            finishedCallback();
                                        }
                                    });
                                });
                                op2.Execute();
                            }
                            else
                            {
                                if (string.IsNullOrEmpty(uids))
                                    finishedCallback();
                            }
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine("GetFriendRequests failed in EntityService: " + ex.Message);
                            finishedCallback();
                        }
                    });
                    op.Execute();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("GetFriendRequests failed: " + ex.Message);
                }
            });
        }