Example #1
0
        public void SetStarred(SetViewModel set, bool starred, Action completed, Action <Exception> errorHandler)
        {
            if (starred == set.Starred)
            {
                return;
            }

            api.SetStarred(
                set.ID,
                starred,
                delegate {
                cache.SetStarred(set.ID, starred);

                if (starred)
                {
                    FavouriteSets.Add(set);
                }
                else
                {
                    FavouriteSets.Remove(set);
                }

                set.Starred = starred;
                completed();
            },
                errorHandler,
                new CancellationToken());
        }
Example #2
0
        public SetViewModel GetSet(long id, bool loadTerms)
        {
            // Try to return an instance we already have, if possible.

            var existing = MySets.FirstOrDefault(s => s.ID == id)
                           ?? FavouriteSets.FirstOrDefault(s => s.ID == id)
                           ?? Groups.SelectMany(g => g.Sets).FirstOrDefault(s => s.ID == id);

            if (existing != null)
            {
                if (loadTerms && !existing.TermsLoaded)
                {
                    LoadTerms(existing);
                }
                return(existing);
            }

            var si = cache.GetSet(id, loadTerms);

            if (si == null)
            {
                return(null);
            }
            bool starred = cache.GetFavourites().Any(x => x.ID == si.Value.ID);

            return(new SetViewModel(this, si.Value)
            {
                Starred = starred
            });
        }
Example #3
0
        public void Delete(SetViewModel set, Action success, Action <Exception> errorHandler)
        {
            Action completion = delegate {
                MySets.Remove(s => s.ID == set.ID);
                FavouriteSets.Remove(s => s.ID == set.ID);
                foreach (var g in Groups)
                {
                    g.Sets.Remove(s => s.ID == set.ID);
                }

                cache.RemoveSet(set.ID);

                success();
            };

            api.DeleteSet(
                set.ID,
                completion,
                err => {
                if (err is ItemDeletedException)
                {
                    completion();                             // This case can hardly be considered an error.
                }
                else
                {
                    errorHandler(err);
                }
            },
                new CancellationToken());
        }
Example #4
0
        public void LoadData()
        {
            if (IsDataLoaded)
            {
                return;
            }

            try {
                Metrics.Measure("MainViewModel.LoadData()", delegate {
                    Metrics.Measure("Load ModelCache", delegate {
                        cache = api.Credentials != null
                                                        ? new ModelCache(api.Credentials)
                                                        : new ModelCache();
                    });

                    if (cache.Credentials == null)
                    {
                        return;
                    }

                    if (!api.Authenticate(cache.Credentials))
                    {
                        cache.LogOut();
                        LogOut();
                        return;
                    }

                    UserName = cache.Credentials.UserName;

                    Metrics.Measure("Create group view models", delegate {
                        foreach (var si in cache.GetMySets())
                        {
                            MySets.Add(new SetViewModel(this, si));
                        }

                        foreach (var si in cache.GetRecentSets().Take(8))
                        {
                            RecentSets.Add(new SetViewModel(this, si));
                        }

                        foreach (var si in cache.GetFavourites())
                        {
                            FavouriteSets.Add(new SetViewModel(this, si)
                            {
                                Starred = true
                            });
                        }
                    });

                    Metrics.Measure("Create group view models", delegate {
                        foreach (var gi in cache.GetGroups())
                        {
                            Groups.Add(new GroupViewModel(this, gi));
                        }
                    });

                    Metrics.Measure("Load profile image", LoadProfileImage);

                    IsDataLoaded = true;
                });
            } catch (CacheIndexMissingException) {
                // XXX This will never happen
            }
        }