コード例 #1
0
ファイル: SubredditManager.cs プロジェクト: nhabash/Baconit
        /// <summary>
        /// Called when the reddit subreddit list should be updated.
        /// </summary>
        /// <param name="force">Forces the update</param>
        public bool Update(bool force = false)
        {
            TimeSpan timeSinceLastUpdate = DateTime.Now - LastUpdate;

            if (!force && timeSinceLastUpdate.TotalMinutes < 300 && SubredditList.Count > 0)
            {
                return(false);
            }

            lock (objectLock)
            {
                if (m_isUpdateRunning)
                {
                    return(false);
                }
                m_isUpdateRunning = true;
            }

            // Kick off an new task
            new Task(async() =>
            {
                try
                {
                    // Get the entire list of subreddits. We will give the helper a super high limit so it
                    // will return all it can find.
                    string baseUrl = m_baconMan.UserMan.IsUserSignedIn ? "/subreddits/mine.json" : "/subreddits/default.json";
                    int maxLimit   = m_baconMan.UserMan.IsUserSignedIn ? 99999 : 100;
                    RedditListHelper <Subreddit> listHelper = new RedditListHelper <Subreddit>(baseUrl, m_baconMan.NetworkMan);

                    // Get the list
                    List <Element <Subreddit> > elements = await listHelper.FetchElements(0, maxLimit);

                    // Create a json list from the wrappers.
                    List <Subreddit> subredditList = new List <Subreddit>();
                    foreach (Element <Subreddit> element in elements)
                    {
                        subredditList.Add(element.Data);
                    }

                    // Update the subreddit list
                    HandleSubredditsFromWeb(subredditList);
                    LastUpdate = DateTime.Now;
                }
                catch (Exception e)
                {
                    m_baconMan.MessageMan.DebugDia("Failed to get subreddit list", e);
                }

                // Indicate we aren't running anymore
                m_isUpdateRunning = false;
            }).Start();
            return(true);
        }
コード例 #2
0
ファイル: SubredditManager.cs プロジェクト: maly7/Baconit
        /// <summary>
        /// Called when the reddit subreddit list should be updated.
        /// </summary>
        /// <param name="force">Forces the update</param>
        public bool Update(bool force = false)
        {
            TimeSpan timeSinceLastUpdate = DateTime.Now - LastUpdate;
            if (!force && timeSinceLastUpdate.TotalMinutes < 300 && SubredditList.Count > 0)
            {
               return false;
            }

            lock(objectLock)
            {
                if(m_isUpdateRunning)
                {
                    return false;
                }
                m_isUpdateRunning = true;
            }

            // Kick off an new task
            new Task(async () =>
            {
                try
                {
                    // Get the entire list of subreddits. We will give the helper a super high limit so it
                    // will return all it can find.
                    string baseUrl = m_baconMan.UserMan.IsUserSignedIn ? "/subreddits/mine.json" : "/subreddits/default.json";
                    int maxLimit = m_baconMan.UserMan.IsUserSignedIn ? 99999 : 100;
                    RedditListHelper <Subreddit> listHelper = new RedditListHelper<Subreddit>(baseUrl, m_baconMan.NetworkMan);

                    // Get the list
                    List<Element<Subreddit>> elements = await listHelper.FetchElements(0, maxLimit);

                    // Create a json list from the wrappers.
                    List<Subreddit> subredditList = new List<Subreddit>();
                    foreach(Element<Subreddit> element in elements)
                    {
                        subredditList.Add(element.Data);
                    }

                    // Update the subreddit list
                    HandleSubredditsFromWeb(subredditList);
                    LastUpdate = DateTime.Now;
                }
                catch(Exception e)
                {
                    m_baconMan.MessageMan.DebugDia("Failed to get subreddit list", e);
                }

                // Indicate we aren't running anymore
                m_isUpdateRunning = false;
            }).Start();
            return true;
        }
コード例 #3
0
        /// <summary>
        /// Called by consumers when the collection should be updated. If the force flag is set we should always do it.
        /// </summary>
        /// <param name="force"></param>
        /// <returns>If an update was kicked off or not</returns>
        public virtual bool Update(bool force = false, int updateCount = 50)
        {
            // #todo add caching
            // #todo #bug If we are refreshing we will grab 50 new post but listeners might already have 100
            // we need to indicate to them they should remove the rest of the posts that are old.
            lock (m_listHelper)
            {
                if (m_state == CollectorState.Updating || m_state == CollectorState.Extending)
                {
                    return(false);
                }

                TimeSpan timeSinceLastUpdate = DateTime.Now - LastUpdateTime;
                if (timeSinceLastUpdate.TotalHours < 2 &&             // Check the time has been longer than 2 hours
                    m_listHelper.GetCurrentElements().Count != 0 &&   // Check that we have elements
                    !force)                                           // Check that it isn't a force
                {
                    return(false);
                }

                // Otherwise do the update
                m_state = CollectorState.Updating;
            }

            // Fire this not under lock
            FireStateChanged();

            // Kick off a new task to get the posts
            new Task(async() =>
            {
                try
                {
                    // First clear out the helper to remove any current posts.
                    m_listHelper.Clear();

                    // Get the next elements
                    // #todo make this lower when we have endless scrolling.
                    List <T> posts = ParseElementList(await m_listHelper.FetchElements(0, updateCount));

                    // Fire the notification that the list has updated.
                    FireCollectionUpdated(0, posts, true, false);

                    // Update the update time
                    LastUpdateTime = DateTime.Now;

                    // Update the state
                    lock (m_listHelper)
                    {
                        m_state = CollectorState.Idle;
                    }

                    FireStateChanged(posts.Count);
                }
                catch (Exception e)
                {
                    m_baconMan.MessageMan.DebugDia("Collector failed to update id:" + m_uniqueId, e);

                    // Update the state
                    lock (m_listHelper)
                    {
                        m_state      = CollectorState.Error;
                        m_errorState = e is ServiceDownException ? CollectorErrorState.ServiceDown : CollectorErrorState.Unknown;
                    }
                    FireStateChanged();
                }
            }).Start();

            return(true);
        }