Exemple #1
0
        public void SetFavorite(string redditId, bool isAdding)
        {
            if (isAdding && FavoriteSubreddits.ContainsKey(redditId))
            {
                // We already have it
                return;
            }

            if (!isAdding && !FavoriteSubreddits.ContainsKey(redditId))
            {
                // We already don't have it
                return;
            }

            if (isAdding)
            {
                // Add it to the map
                FavoriteSubreddits[redditId] = true;
            }
            else
            {
                FavoriteSubreddits.Remove(redditId);
            }

            // Force the settings to save
            SaveSettings();

            SetSubreddits(SubredditList);
        }
Exemple #2
0
        /// <summary>
        /// Used to sort and add fix up the subreddits before they are saved.
        /// </summary>
        /// <param name="subreddits"></param>
        private void SetSubreddits(List <Subreddit> subreddits)
        {
            List <Subreddit> newSubredditList = new List <Subreddit>();

            foreach (Subreddit subreddit in subreddits)
            {
                // Mark if it is a favorite
                subreddit.IsFavorite = FavoriteSubreddits.ContainsKey(subreddit.Id);

                // Escape the description, we need to do it twice because sometimes it is double escaped.
                subreddit.Description = WebUtility.HtmlDecode(subreddit.Description);

                // Do a simple inert sort, account for favorites
                bool wasAdded = false;
                for (int i = 0; i < newSubredditList.Count; i++)
                {
                    bool addHere = false;
                    // Is this list item a favorite
                    if (newSubredditList[i].IsFavorite)
                    {
                        // If the new item isn't then continue.
                        if (!subreddit.IsFavorite)
                        {
                            continue;
                        }

                        // If they are both favorites compare them.
                        if (newSubredditList[i].DisplayName.CompareTo(subreddit.DisplayName) > 0)
                        {
                            addHere = true;
                        }
                    }
                    // Or if the new one is a favorite only
                    else if (subreddit.IsFavorite)
                    {
                        addHere = true;
                    }
                    // If neither of them are favorites.
                    else
                    {
                        if (newSubredditList[i].DisplayName.CompareTo(subreddit.DisplayName) > 0)
                        {
                            addHere = true;
                        }
                    }

                    if (addHere)
                    {
                        newSubredditList.Insert(i, subreddit);
                        wasAdded = true;
                        break;
                    }
                }

                // If we didn't add it add it to the end.
                if (!wasAdded)
                {
                    newSubredditList.Add(subreddit);
                }
            }

            // Set the list
            SubredditList = newSubredditList;

            // Save the list
            SaveSettings();

            // Fire the callback for listeners
            m_onSubredditsUpdated.Raise(this, new OnSubredditsUpdatedArgs()
            {
                NewSubreddits = SubredditList
            });
        }