public void LocalRefresh()
        {
            if (App.CurrentAthlete == null)
            {
                return;
            }

            var comparer = new LeagueComparer();
            var toJoin   = DataManager.Instance.Leagues.Where(k => !App.CurrentAthlete.Memberships.Select(m => m.LeagueId).Contains(k.Key))
                           .Select(k => k.Value).ToList();

            var toRemove = Leagues.Where(vm => vm.League != null).Select(vm => vm.League).Except(toJoin, comparer).ToList();
            var toAdd    = toJoin.Except(Leagues.Select(vm => vm.League), comparer).OrderBy(r => r.Name).Select(l => new LeagueViewModel {
                LeagueId = l.Id
            }).ToList();

            toRemove.ForEach(l => Leagues.Remove(Leagues.Single(vm => vm.League == l)));

            if (Leagues.Count == 0 && toAdd.Count == 0)
            {
                if (_empty == null)
                {
                    _empty = new LeagueViewModel {
                        EmptyMessage = "There are no available leagues to join."
                    }
                }
                ;

                if (!Leagues.Contains(_empty))
                {
                    Leagues.Add(_empty);
                }
            }

            var compare = new LeagueSortComparer();

            foreach (var lv in toAdd)
            {
                int index = 0;
                foreach (var l in Leagues.ToList())
                {
                    if (compare.Compare(lv, l) < 0)
                    {
                        break;
                    }

                    index++;
                }
                Leagues.Insert(index, lv);
            }

            if (toAdd.Count > 0 || toRemove.Count > 0)
            {
                var last = Leagues.LastOrDefault();
                foreach (var l in Leagues)
                {
                    l.IsLast = l == last;
                }
            }

            if (Leagues.Count > 0 && Leagues.Contains(_empty) && Leagues.First() != _empty)
            {
                Leagues.Remove(_empty);
            }
        }

        async public Task GetAvailableLeagues(bool forceRefresh = false)
Esempio n. 2
0
        public void LocalRefresh()
        {
            if (Athlete == null)
            {
                return;
            }

            if (Leagues == null)
            {
                Leagues = new ObservableCollection <LeagueViewModel>();
            }

            var comparer = new LeagueIdComparer();
            var toRemove = Leagues.Where(vm => vm.League != null).Select(vm => vm.League).Except(Athlete.Leagues, comparer).ToList();
            var toAdd    = Athlete.Leagues.Except(Leagues.Select(vm => vm.League), comparer).OrderBy(r => r.Name).Select(l => new LeagueViewModel {
                LeagueId = l.Id
            }).ToList();

            toRemove.ForEach(l => Leagues.Remove(Leagues.Single(vm => vm.League == l)));
            foreach (var l in Leagues)
            {
                l.NotifyPropertiesChanged();
            }

            if (Leagues.Count == 0 && toAdd.Count == 0)
            {
                if (_empty == null)
                {
                    _empty = new LeagueViewModel {
                        EmptyMessage = "You don't belong to any leagues yet.\n\nYou can join leagues by tapping the + button above."
                    }
                }
                ;

                if (!Leagues.Contains(_empty))
                {
                    Leagues.Add(_empty);
                }
            }

            var compare = new LeagueSortComparer();

            foreach (var lv in toAdd)
            {
                int index = 0;
                foreach (var l in Leagues.ToList())
                {
                    if (compare.Compare(lv, l) < 0)
                    {
                        break;
                    }

                    index++;
                }
                Leagues.Insert(index, lv);
            }

            if (toAdd.Count > 0 || toRemove.Count > 0)
            {
                var last = Leagues.LastOrDefault();
                foreach (var l in Leagues)
                {
                    l.IsLast = l == last;
                }
            }

            if (Leagues.Count > 0 && Leagues.Contains(_empty) && Leagues.First() != _empty)
            {
                Leagues.Remove(_empty);
            }
        }
    }