From 0daccea9fde2711665ad3796cbdfa7710aa5ce6c Mon Sep 17 00:00:00 2001 From: Lynnesbian Date: Fri, 4 Sep 2020 20:48:47 +1000 Subject: [PATCH] display and update the ending time --- MainWindow.cs | 62 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 50 insertions(+), 12 deletions(-) diff --git a/MainWindow.cs b/MainWindow.cs index 241fd39..e84e8e1 100755 --- a/MainWindow.cs +++ b/MainWindow.cs @@ -49,6 +49,12 @@ namespace Buypeeb { // TODO: whenever we get something from the builder, cache it for later // that way we don't need to constantly do "builder.GetObject"s + // when that is done, you can use the cache array to replace everything from here... + + private Box selectionViewBox; + private Label endingLabel; + + // ...to here. static SemaphoreSlim taskLimit = new SemaphoreSlim(4); @@ -99,6 +105,8 @@ namespace Buypeeb { builder.Autoconnect(this); this.statusLabel = (Label)builder.GetObject("LabelStatus"); + this.selectionViewBox = (Box)builder.GetObject("SelectionViewBox"); + this.endingLabel = (Label)builder.GetObject("LabelSelectedEnding"); // bind treeview columns to watchlist instead of needing to manually sync its liststore this.itemTreeView = (TreeView)builder.GetObject("TreeViewItems"); @@ -120,6 +128,7 @@ namespace Buypeeb { } this.UpdateItems(); + GLib.Timeout.Add(1000, new GLib.TimeoutHandler(UpdateSelectionEndTime)); DeleteEvent += WindowShutdown; } @@ -182,8 +191,8 @@ namespace Buypeeb { using (WebClient client = new WebClient()) { // TODO: download should have timeout - // item.Update(client.DownloadString(item.url)); - item.Update(File.ReadAllText("yahoo.html")); + item.Update(client.DownloadString(item.url)); + // item.Update(File.ReadAllText("yahoo.html")); } Gtk.Application.Invoke(delegate { @@ -191,8 +200,7 @@ namespace Buypeeb { this.items.EmitRowChanged(pathAndIter.path, pathAndIter.iter); if (item == this.selectedItem) { // if the user has this item selected and it just became ready, enable the selection box - var s = (Box)this.builder.GetObject("SelectionViewBox"); - s.Sensitive = true; + this.selectionViewBox.Sensitive = true; } }); @@ -215,8 +223,7 @@ namespace Buypeeb { } private void UpdateItems() { - var s = (Box)this.builder.GetObject("SelectionViewBox"); - s.Sensitive = false; + this.selectionViewBox.Sensitive = false; var t = Task.Factory.StartNew(() => { foreach (var item in this.settings.watchlist) { @@ -229,18 +236,17 @@ namespace Buypeeb { private void UpdateSelectionView() { // get the currently selected item var item = this.selectedItem; - var s = (Box)this.builder.GetObject("SelectionViewBox"); var infobox = (Box)this.builder.GetObject("SelectionInfoBox"); if (item == null) { - s.Sensitive = false; + this.selectionViewBox.Sensitive = false; infobox.Visible = false; var l = (Label)this.builder.GetObject("LabelSelectedName"); l.Text = "buypeeb"; return; } - s.Sensitive = item.ready; + this.selectionViewBox.Sensitive = item.ready; infobox.Visible = true; var info = new Dictionary(); @@ -248,7 +254,7 @@ namespace Buypeeb { info.Add("YahooName", item.originalName); info.Add("Price", item.PriceJPY()); info.Add("PriceAUD", item.PriceAUD()); - info.Add("Ending", "whenever"); + info.Add("Ending", "..."); info.Add("Bids", $"{item.bids}"); info.Add("BuyItNow", item.winPrice == 0 ? "No" : $"¥{item.PriceJPY(true)} (${item.PriceAUD(true)})"); info.Add("AutoExtension", item.autoExtension ? "Yes" : "No"); @@ -413,8 +419,7 @@ namespace Buypeeb { } private void ButtonSelectedUpdateClicked(object sender, EventArgs args) { - var s = (Box)this.builder.GetObject("SelectionViewBox"); - s.Sensitive = false; + this.selectionViewBox.Sensitive = false; this.UpdateItem(this.selectedItem.id); } @@ -455,5 +460,38 @@ namespace Buypeeb { (cell as Gtk.CellRendererText).Text = item.ready ? ending : "..."; } + // timers + + private bool UpdateSelectionEndTime() { + if (!this.selectionViewBox.IsSensitive) { + return true; + } + + var item = this.selectedItem; + if (!item.ready) { + return true; + } + + string ending = ""; + if (item.available) { + var now = DateTime.Now; + var end = item.endDate.ToLocalTime(); + var span = end.Subtract(now); + + if (span.Days > 0) { + ending += span.ToString("dd' days, '"); // will format twelve days as "12 days, " + } + // timespan objects don't contain definitions for the time or date separators, so the colons need to be escaped + // `HH` doesn't exist, but `hh` behaves identically + // see https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-timespan-format-strings + ending += span.ToString(@"hh\:mm\:ss"); + } + else { + ending = "Auction has ended"; + } + + this.endingLabel.Text = ending; + return true; + } } }