/// <summary> /// Extends the amount of post shown. /// </summary> /// <param name="extendCount"></param> public void ExtendCollection(int extendCount = 50) { // #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 || m_state == CollectorState.FullyExtended) { return; } // Otherwise do the extension m_state = CollectorState.Extending; } // Fire this not under lock FireStateChanged(); // Kick off a new task to get the posts Task.Run(async() => { try { int previousCollectionSize = GetCurrentPostsInternal().Count; // Get the next elements // #todo make this lower when we have endless scrolling. List <T> posts = ParseElementList(await m_listHelper.FetchNext(extendCount)); // Fire the notification that the list has updated. FireCollectionUpdated(previousCollectionSize, posts, false, false); // Update the state lock (m_listHelper) { if (posts.Count == 0) { // If we don't get anything back we are fully extended. m_state = CollectorState.FullyExtended; } else { m_state = CollectorState.Idle; } } FireStateChanged(posts.Count); } catch (Exception e) { m_baconMan.MessageMan.DebugDia("Subreddit extension failed", e); // Update the state lock (m_listHelper) { m_state = CollectorState.Error; m_errorState = e is ServiceDownException ? CollectorErrorState.ServiceDown : CollectorErrorState.Unknown; } FireStateChanged(); } }); }
/// <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); }