saving and loading of settings

This commit is contained in:
Lynne Megido 2020-09-03 23:47:36 +10:00
parent b9b4253a5d
commit b20a2e29a0
Signed by: lynnesbian
GPG key ID: F0A184B5213D9F90
4 changed files with 74 additions and 40 deletions

View file

@ -22,6 +22,10 @@ namespace Buypeeb {
this.ready = false; this.ready = false;
} }
public Listing() {
// parameterless constructur for deserialisation
}
public void Update() { public void Update() {
// use fake values for now // use fake values for now
var rnd = new Random(); var rnd = new Random();

View file

@ -18,8 +18,9 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
// using System.Text.Json; using System.Text.Json;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Gtk; using Gtk;
@ -35,21 +36,50 @@ namespace Buypeeb {
class MainWindow : Window { class MainWindow : Window {
private string location;
private ListStore items; private ListStore items;
private Settings settings; private Settings settings;
private TreeView itemTreeView; private TreeView itemTreeView;
private Label statusLabel;
static SemaphoreSlim tasklimit = new SemaphoreSlim(4); static SemaphoreSlim tasklimit = new SemaphoreSlim(4);
public MainWindow() : this(new Builder("main.glade")) { } public MainWindow() : this(new Builder("main.glade")) { }
private MainWindow(Builder builder) : base(builder.GetObject("wndMain").Handle) { private MainWindow(Builder builder) : base(builder.GetObject("wndMain").Handle) {
this.settings = new Settings(); if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
this.settings.Save(); // C:\Users\Beebus\AppData\Roaming\Lynnear Software\buypeeb
this.location = System.IO.Path.Combine(Environment.ExpandEnvironmentVariables("%APPDATA%"), "Lynnear Software", "buypeeb");
}
else {
// ~/.config/Lynnear Software/buypeeb
this.location = System.IO.Path.Combine(Environment.GetEnvironmentVariable("HOME"), ".config", "Lynnear Software", "buypeeb");
}
string userdata = System.IO.Path.Combine(location, "userdata.json");
if (File.Exists(userdata)) {
try {
string j = File.ReadAllText(userdata);
this.settings = JsonSerializer.Deserialize<Settings>(j);
}
catch {
// ???
Console.WriteLine("oops");
Application.Quit();
}
}
else {
this.settings = new Settings();
}
this.SaveSettings();
this.Title = "Buypeeb"; this.Title = "Buypeeb";
builder.Autoconnect(this); builder.Autoconnect(this);
this.statusLabel = (Label)builder.GetObject("LabelStatus");
// 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");
this.items = new ListStore(typeof(Listing)); this.items = new ListStore(typeof(Listing));
@ -58,27 +88,30 @@ namespace Buypeeb {
this.itemTreeView.Model = this.items; this.itemTreeView.Model = this.items;
for (int i = 0; i < this.itemTreeView.Columns.Length; i++) { for (int i = 0; i < this.itemTreeView.Columns.Length; i++) {
var c = this.itemTreeView.Columns[i]; var c = this.itemTreeView.Columns[i];
var func = new Gtk.TreeCellDataFunc(this.RenderColumnName); TreeCellDataFunc func;
//TODO: get rid of this awful if statement //TODO: get rid of this awful if statement
if (i == ItemColumns.PriceYen) { if (i == ItemColumns.Name) {
func = new Gtk.TreeCellDataFunc(this.RenderColumnPriceYen); func = new TreeCellDataFunc(this.RenderColumnName);
}
else if (i == ItemColumns.PriceYen) {
func = new TreeCellDataFunc(this.RenderColumnPriceYen);
} }
else if (i == ItemColumns.PriceAUD) { else if (i == ItemColumns.PriceAUD) {
func = new Gtk.TreeCellDataFunc(this.RenderColumnPriceAUD); func = new TreeCellDataFunc(this.RenderColumnPriceAUD);
} }
else if (i == ItemColumns.Ending) { else if (i == ItemColumns.Ending) {
func = new Gtk.TreeCellDataFunc(this.RenderColumnEnding); func = new TreeCellDataFunc(this.RenderColumnEnding);
} }
else { else {
Console.WriteLine($"unexpected value {i}!");
throw new IndexOutOfRangeException(); throw new IndexOutOfRangeException();
} }
c.SetCellDataFunc(c.Cells[0], func); c.SetCellDataFunc(c.Cells[0], func);
} }
foreach (object[] row in this.items) { foreach (object[] row in this.items) {
Console.WriteLine(row[0]); Console.WriteLine(row[0]);
} }
@ -92,17 +125,30 @@ namespace Buypeeb {
// general behaviour // general behaviour
private void SetStatus(string status) {
this.statusLabel.Text = status ?? "Buypeeb";
}
private void SaveSettings() {
string j = JsonSerializer.Serialize(this.settings);
Console.WriteLine(j);
File.WriteAllText(System.IO.Path.Combine(this.location, "userdata.json"), j);
}
private void UpdateThread(string id) { private void UpdateThread(string id) {
var item = this.settings.watchlist[id]; var item = this.settings.watchlist[id];
Console.WriteLine($"Updating {id}..."); Console.WriteLine($"Updating {id}...");
// set item.ready to false to show that it's still being updated
// this changes a few behaviours, such as displaying the price as "..." instead of whatever's currently stored
item.ready = false; item.ready = false;
// TODO: actually download the data
Thread.Sleep(3000); Thread.Sleep(3000);
string html = "Heeenlo"; string html = "Heeenlo";
item.name = html; item.name = html;
item.ready = true; item.ready = true;
Console.WriteLine($"{id} updated."); Console.WriteLine($"{id} updated.");
Gtk.Application.Invoke(delegate { Gtk.Application.Invoke(delegate {
//TODO: surely there's a better way to do this // TODO: surely there's a better way to do this
TreeIter iter; TreeIter iter;
this.itemTreeView.Model.GetIterFirst(out iter); this.itemTreeView.Model.GetIterFirst(out iter);
@ -120,6 +166,8 @@ namespace Buypeeb {
} }
private void UpdateItem(string id) { private void UpdateItem(string id) {
// don't start a new task if there are more than [tasklimit] tasks currently running
// this makes sure we don't make 1000 simultaneous requests to yahoo auctions if there are 1000 items on the watchlist
tasklimit.Wait(); tasklimit.Wait();
var t = Task.Factory.StartNew(() => { var t = Task.Factory.StartNew(() => {
this.UpdateThread(id); this.UpdateThread(id);
@ -134,6 +182,7 @@ namespace Buypeeb {
}); });
} }
// show a simple entry dialogue that allows the user to enter text and either cancel or submit it
private (Boolean accepted, string response) EntryDialogue(string title = "Buypeeb", string message = "Hi there!") { private (Boolean accepted, string response) EntryDialogue(string title = "Buypeeb", string message = "Hi there!") {
Dialog ed = new Dialog(title, null, Gtk.DialogFlags.DestroyWithParent, "Cancel", ResponseType.Cancel, "OK", ResponseType.Ok); Dialog ed = new Dialog(title, null, Gtk.DialogFlags.DestroyWithParent, "Cancel", ResponseType.Cancel, "OK", ResponseType.Ok);
ed.DefaultResponse = ResponseType.Ok; ed.DefaultResponse = ResponseType.Ok;
@ -183,7 +232,6 @@ namespace Buypeeb {
} }
private void ButtonUpdateAllClicked(object sender, EventArgs a) { private void ButtonUpdateAllClicked(object sender, EventArgs a) {
Console.WriteLine("ButtonUpdateAllClicked");
this.UpdateItems(); this.UpdateItems();
} }

View file

@ -1,13 +1,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Text.Json;
namespace Buypeeb { namespace Buypeeb {
class Settings { class Settings {
private string location;
public int updateInterval { get; set; } = 10 * 60; public int updateInterval { get; set; } = 10 * 60;
public int favouriteUpdateInterval { get; set; } = 5 * 60; public int favouriteUpdateInterval { get; set; } = 5 * 60;
public int updateIntervalCritical { get; set; } = 60; public int updateIntervalCritical { get; set; } = 60;
@ -18,23 +14,18 @@ namespace Buypeeb {
} }
public Settings() { public Settings() {
if (Environment.OSVersion.Platform == PlatformID.Win32NT) { if (this.watchlist == null) {
// C:\Users\Beebus\AppData\Roaming\Lynnear Software\buypeeb // either this is the first time the program has been run, or there's something wrong with userdata.json
this.location = Path.Combine(Environment.ExpandEnvironmentVariables("%APPDATA%"), "Lynnear Software", "buypeeb"); this.watchlist = new Dictionary<string, Listing>();
} // this.Watch("https://buypeeb.biz/whatever/k12345", "my thingy");
else { // this.Watch("https://buypeeb.biz/whatever/z09876", "your thingy");
// ~/.config/Lynnear Software/buypeeb // this.Watch("https://buypeeb.biz/whatever/h55555", "our thingy");
this.location = Path.Combine(Environment.GetEnvironmentVariable("HOME"), ".config", "Lynnear Software", "buypeeb"); // for (int i = 0; i < 10; i++) {
// this.Watch($"https://buypeeb.biz/whatever/x{i * 123}", $"filler {i}");
// }
// this.watchlist["k12345"].Update();
} }
this.watchlist = new Dictionary<string, Listing>();
this.Watch("https://buypeeb.biz/whatever/k12345", "my thingy");
this.Watch("https://buypeeb.biz/whatever/z09876", "your thingy");
this.Watch("https://buypeeb.biz/whatever/h55555", "our thingy");
for (int i = 0; i < 10; i++) {
this.Watch($"https://buypeeb.biz/whatever/x{i * 123}", $"filler {i}");
}
this.watchlist["k12345"].Update();
} }
public void Watch(string url, string name) { public void Watch(string url, string name) {
@ -46,14 +37,5 @@ namespace Buypeeb {
Console.WriteLine("{0} - {1}", entry.Value.name, entry.Value.price); Console.WriteLine("{0} - {1}", entry.Value.name, entry.Value.price);
} }
} }
public void Save() {
string j = JsonSerializer.Serialize(this);
Console.WriteLine(j);
}
// public void Load() {
// }
} }
} }

View file

@ -795,7 +795,7 @@
<property name="margin_top">3</property> <property name="margin_top">3</property>
<property name="margin_bottom">3</property> <property name="margin_bottom">3</property>
<child> <child>
<object class="GtkLabel"> <object class="GtkLabel" id="LabelStatus">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="halign">start</property> <property name="halign">start</property>