display and update the ending time

This commit is contained in:
Lynne Megido 2020-09-04 20:48:47 +10:00
parent f252b52553
commit 0daccea9fd
Signed by: lynnesbian
GPG key ID: F0A184B5213D9F90

View file

@ -49,6 +49,12 @@ namespace Buypeeb {
// TODO: whenever we get something from the builder, cache it for later // TODO: whenever we get something from the builder, cache it for later
// that way we don't need to constantly do "builder.GetObject"s // 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); static SemaphoreSlim taskLimit = new SemaphoreSlim(4);
@ -99,6 +105,8 @@ namespace Buypeeb {
builder.Autoconnect(this); builder.Autoconnect(this);
this.statusLabel = (Label)builder.GetObject("LabelStatus"); 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 // bind treeview columns to watchlist instead of needing to manually sync its liststore
this.itemTreeView = (TreeView)builder.GetObject("TreeViewItems"); this.itemTreeView = (TreeView)builder.GetObject("TreeViewItems");
@ -120,6 +128,7 @@ namespace Buypeeb {
} }
this.UpdateItems(); this.UpdateItems();
GLib.Timeout.Add(1000, new GLib.TimeoutHandler(UpdateSelectionEndTime));
DeleteEvent += WindowShutdown; DeleteEvent += WindowShutdown;
} }
@ -182,8 +191,8 @@ namespace Buypeeb {
using (WebClient client = new WebClient()) { using (WebClient client = new WebClient()) {
// TODO: download should have timeout // TODO: download should have timeout
// item.Update(client.DownloadString(item.url)); item.Update(client.DownloadString(item.url));
item.Update(File.ReadAllText("yahoo.html")); // item.Update(File.ReadAllText("yahoo.html"));
} }
Gtk.Application.Invoke(delegate { Gtk.Application.Invoke(delegate {
@ -191,8 +200,7 @@ namespace Buypeeb {
this.items.EmitRowChanged(pathAndIter.path, pathAndIter.iter); this.items.EmitRowChanged(pathAndIter.path, pathAndIter.iter);
if (item == this.selectedItem) { if (item == this.selectedItem) {
// if the user has this item selected and it just became ready, enable the selection box // if the user has this item selected and it just became ready, enable the selection box
var s = (Box)this.builder.GetObject("SelectionViewBox"); this.selectionViewBox.Sensitive = true;
s.Sensitive = true;
} }
}); });
@ -215,8 +223,7 @@ namespace Buypeeb {
} }
private void UpdateItems() { private void UpdateItems() {
var s = (Box)this.builder.GetObject("SelectionViewBox"); this.selectionViewBox.Sensitive = false;
s.Sensitive = false;
var t = Task.Factory.StartNew(() => { var t = Task.Factory.StartNew(() => {
foreach (var item in this.settings.watchlist) { foreach (var item in this.settings.watchlist) {
@ -229,18 +236,17 @@ namespace Buypeeb {
private void UpdateSelectionView() { private void UpdateSelectionView() {
// get the currently selected item // get the currently selected item
var item = this.selectedItem; var item = this.selectedItem;
var s = (Box)this.builder.GetObject("SelectionViewBox");
var infobox = (Box)this.builder.GetObject("SelectionInfoBox"); var infobox = (Box)this.builder.GetObject("SelectionInfoBox");
if (item == null) { if (item == null) {
s.Sensitive = false; this.selectionViewBox.Sensitive = false;
infobox.Visible = false; infobox.Visible = false;
var l = (Label)this.builder.GetObject("LabelSelectedName"); var l = (Label)this.builder.GetObject("LabelSelectedName");
l.Text = "buypeeb"; l.Text = "buypeeb";
return; return;
} }
s.Sensitive = item.ready; this.selectionViewBox.Sensitive = item.ready;
infobox.Visible = true; infobox.Visible = true;
var info = new Dictionary<string, string>(); var info = new Dictionary<string, string>();
@ -248,7 +254,7 @@ namespace Buypeeb {
info.Add("YahooName", item.originalName); info.Add("YahooName", item.originalName);
info.Add("Price", item.PriceJPY()); info.Add("Price", item.PriceJPY());
info.Add("PriceAUD", item.PriceAUD()); info.Add("PriceAUD", item.PriceAUD());
info.Add("Ending", "whenever"); info.Add("Ending", "...");
info.Add("Bids", $"{item.bids}"); info.Add("Bids", $"{item.bids}");
info.Add("BuyItNow", item.winPrice == 0 ? "No" : $"¥{item.PriceJPY(true)} (${item.PriceAUD(true)})"); info.Add("BuyItNow", item.winPrice == 0 ? "No" : $"¥{item.PriceJPY(true)} (${item.PriceAUD(true)})");
info.Add("AutoExtension", item.autoExtension ? "Yes" : "No"); info.Add("AutoExtension", item.autoExtension ? "Yes" : "No");
@ -413,8 +419,7 @@ namespace Buypeeb {
} }
private void ButtonSelectedUpdateClicked(object sender, EventArgs args) { private void ButtonSelectedUpdateClicked(object sender, EventArgs args) {
var s = (Box)this.builder.GetObject("SelectionViewBox"); this.selectionViewBox.Sensitive = false;
s.Sensitive = false;
this.UpdateItem(this.selectedItem.id); this.UpdateItem(this.selectedItem.id);
} }
@ -455,5 +460,38 @@ namespace Buypeeb {
(cell as Gtk.CellRendererText).Text = item.ready ? ending : "..."; (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;
}
} }
} }