예제 #1
0
        public Task <TabPerson> ToPerson()
        {
            if (person != null)
            {
                return(person.Task);
            }

            person = new TaskCompletionSource <TabPerson> ();
            var recipientQuery = new ParseQuery("Person");

            recipientQuery.SetCachePolicy(ParseQuery.CachePolicy.CacheElseNetwork);
            if (Emails.Any())
            {
                recipientQuery.WhereContainedIn("emails", Emails.Select(TabPerson.MD5Hash).ToArray());
            }
            else
            {
                recipientQuery.WhereEqualTo("displayName", DisplayName);
            }

            ParseObject recipient       = null;
            TabPerson   recipientPerson = null;

            recipientQuery.GetFirstInBackground(new TabGetCallback((o, e) => {
                if (o != null)
                {
                    recipient       = o;
                    recipientPerson = TabPerson.FromParse(recipient);
                    // TODO: add the emails that are in Emails and not in recipientPerson.Emails
                    person.SetResult(recipientPerson);
                }
                else
                {
                    recipientPerson = new TabPerson {
                        DisplayName = DisplayName,
                        Emails      = Emails,
                    };
                    recipient = recipientPerson.ToParse();
                    recipient.SaveInBackground(new TabSaveCallback(ex => {
                        if (ex == null)
                        {
                            person.SetResult(recipientPerson);
                        }
                        else
                        {
                            Android.Util.Log.Error("PersonCreator", ex.ToString());
                            person.SetException(ex);
                        }
                    }));
                    recipientQuery.ClearCachedResult();
                }
            }));

            return(person.Task);
        }
예제 #2
0
        void UpdateTabStatistics(TabPerson other, SelectedUserInfo selectedContact, bool force = false)
        {
            var p    = other.ToParse();
            var self = TabPerson.CurrentPerson.ToParse();

            var query = TabObject.CreateTabListQuery(self, p);

            if (force)
            {
                query.SetCachePolicy(ParseQuery.CachePolicy.NetworkElseCache);
            }
            else
            {
                query.SetCachePolicy(ParseQuery.CachePolicy.CacheElseNetwork);
                query.MaxCacheAge = 1000 * 60;                 // 1 minute
            }

            query.FindInBackground(new TabFindCallback((ps, ex) => {
                if (ex != null)
                {
                    Android.Util.Log.Error("TabStats", ex, "Error while retrieving tab list");
                    statsSpinner.Visibility = ViewStates.Invisible;
                    flashBarCtrl.ShowBar(() => UpdateTabStatistics(other, selectedContact, force),
                                         withMessageId: Resource.String.flashbar_stats_error);
                    return;
                }

                var tabs = ps.Select(TabObject.FromParse).ToArray();
                if (tabs.Length == 0)
                {
                    statsSpinner.Visibility = ViewStates.Invisible;
                    karmaBar.SetAnimatedVisibility(false);
                    return;
                }

                var counts        = tabs.GroupBy(tab => tab.Type.Name).ToArray();
                int totalPositive = 0, totalNegative = 0;
                int totalTypes    = counts.Length;

                foreach (var group in counts)
                {
                    var badge    = BadgeInstances[group.Key];
                    int positive = 0, negative = 0;

                    foreach (var tab in group)
                    {
                        if ((tab.Originator == other && tab.Direction == TabDirection.Receiving) ||
                            (tab.Originator == TabPerson.CurrentPerson && tab.Direction == TabDirection.Giving))
                        {
                            positive++;
                        }
                        if ((tab.Originator == TabPerson.CurrentPerson && tab.Direction == TabDirection.Receiving) ||
                            (tab.Originator == other && tab.Direction == TabDirection.Giving))
                        {
                            negative++;
                        }
                    }

                    totalPositive += positive;
                    totalNegative += negative;

                    Activity.RunOnUiThread(() => {
                        if (selectedContact.DisplayName != userBadge.SelectedUser.DisplayName)
                        {
                            return;
                        }
                        //badge.Count = positive - negative;
                        badge.SetCount(positive - negative, true);
                        if (--totalTypes == 0)
                        {
                            if (totalPositive == 0 && totalNegative == 0)
                            {
                                karmaBar.SetAnimatedVisibility(false);
                            }
                            else
                            {
                                karmaBar.SetAnimatedVisibility(true);
                                karmaBar.SetKarmaBasedOnValues(totalPositive, totalNegative);
                            }
                            statsSpinner.Visibility = ViewStates.Invisible;
                        }
                    });
                }
            }));
        }