diff --git a/MainWindow.cs b/MainWindow.cs index 653f710..ab3715c 100755 --- a/MainWindow.cs +++ b/MainWindow.cs @@ -55,6 +55,7 @@ namespace Buypeeb { static SemaphoreSlim taskLimit = new SemaphoreSlim(6); private Queue updateQueue = new Queue(); private IEnumerable filterQuery; + private IEnumerable outdatedItemQuery; private YahooAuctionsItem selectedItem { get { @@ -115,6 +116,14 @@ namespace Buypeeb { this.filterChecks[name].Active = false; } + // only returns items that meet all of the following: + // - marked as "ready", as in, they aren't in the process of updating + // - not updated since the interval + this.outdatedItemQuery = + from item in this.settings.watchlist.Values.ToList() + where item.ready && this.settings.ItemNotUpdatedSinceInterval(item) + select item; + // father forgive me for i have lynned this.filterQuery = from item in this.settings.watchlist.Values.ToList() @@ -155,6 +164,7 @@ namespace Buypeeb { this.RenderList(); this.UpdateItems(); GLib.Timeout.Add(1000, new GLib.TimeoutHandler(UpdateSelectionEndTime)); + GLib.Timeout.Add(10000, new GLib.TimeoutHandler(AutoUpdateItems)); DeleteEvent += WindowShutdown; } @@ -674,6 +684,24 @@ namespace Buypeeb { return true; } + private bool AutoUpdateItems() { + if (this.queueActive) { + // don't autoupdate if the queue is active + return true; + } + + foreach (var item in this.outdatedItemQuery) { + updateQueue.Enqueue(item.id); + } + + if (updateQueue.TryPeek(out string _)) { + // there's at least one item in the queue + this.ProcessUpdateQueue(); + } + + return true; + } + // column renderers private void RenderColumnFavourite(Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.ITreeModel model, Gtk.TreeIter iter) { diff --git a/Settings.cs b/Settings.cs index ba9933f..def2f87 100644 --- a/Settings.cs +++ b/Settings.cs @@ -27,5 +27,29 @@ namespace Buypeeb { this.watchlist[id] = new YahooAuctionsItem(id, name); return this.watchlist[id]; } + + // TRUE if the item hasn't been updated for at least interval seconds + // for example, if the interval is 10, and the item hasn't been updated since 20 seconds ago, this will be TRUE + public bool ItemNotUpdatedSinceInterval(YahooAuctionsItem item) { + int seconds = 1000; + if (item.favourite) { + if (item.endingSoon) { + seconds = this.favouriteUpdateIntervalCritical; + } + else { + seconds = this.favouriteUpdateInterval; + } + } + else { + if (item.endingSoon) { + seconds = this.updateIntervalCritical; + } + else { + seconds = this.updateInterval; + } + } + var later = item.lastUpdated.AddSeconds(seconds); + return DateTime.Compare(later, DateTime.UtcNow) < 0; + } } } diff --git a/YahooAuctionsItem.cs b/YahooAuctionsItem.cs index 5fd7031..d0ae293 100644 --- a/YahooAuctionsItem.cs +++ b/YahooAuctionsItem.cs @@ -53,6 +53,7 @@ namespace Buypeeb { public bool endingToday { get { return this.endDate.DayOfYear == DateTime.UtcNow.DayOfYear; } } public bool hasWinPrice { get { return this.winPrice != 0; } } private bool success { get; set; } // TODO: custom setter that throws an exception if set to false or something idk + public bool endingSoon { get { return DateTime.Compare(DateTime.UtcNow.AddMinutes(10), this.endDate) > 0; } } public YahooAuctionsItem(string id, string name) { this.id = id;