saving and loading of settings
This commit is contained in:
parent
b9b4253a5d
commit
b20a2e29a0
4 changed files with 74 additions and 40 deletions
|
@ -22,6 +22,10 @@ namespace Buypeeb {
|
|||
this.ready = false;
|
||||
}
|
||||
|
||||
public Listing() {
|
||||
// parameterless constructur for deserialisation
|
||||
}
|
||||
|
||||
public void Update() {
|
||||
// use fake values for now
|
||||
var rnd = new Random();
|
||||
|
|
|
@ -18,8 +18,9 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text.RegularExpressions;
|
||||
// using System.Text.Json;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Gtk;
|
||||
|
@ -35,21 +36,50 @@ namespace Buypeeb {
|
|||
|
||||
class MainWindow : Window {
|
||||
|
||||
private string location;
|
||||
|
||||
private ListStore items;
|
||||
private Settings settings;
|
||||
private TreeView itemTreeView;
|
||||
private Label statusLabel;
|
||||
|
||||
static SemaphoreSlim tasklimit = new SemaphoreSlim(4);
|
||||
|
||||
public MainWindow() : this(new Builder("main.glade")) { }
|
||||
|
||||
private MainWindow(Builder builder) : base(builder.GetObject("wndMain").Handle) {
|
||||
if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
|
||||
// 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.settings.Save();
|
||||
}
|
||||
this.SaveSettings();
|
||||
this.Title = "Buypeeb";
|
||||
|
||||
builder.Autoconnect(this);
|
||||
|
||||
this.statusLabel = (Label)builder.GetObject("LabelStatus");
|
||||
|
||||
// bind treeview columns to watchlist instead of needing to manually sync its liststore
|
||||
this.itemTreeView = (TreeView)builder.GetObject("TreeViewItems");
|
||||
this.items = new ListStore(typeof(Listing));
|
||||
|
@ -58,27 +88,30 @@ namespace Buypeeb {
|
|||
this.itemTreeView.Model = this.items;
|
||||
for (int i = 0; i < this.itemTreeView.Columns.Length; i++) {
|
||||
var c = this.itemTreeView.Columns[i];
|
||||
var func = new Gtk.TreeCellDataFunc(this.RenderColumnName);
|
||||
TreeCellDataFunc func;
|
||||
|
||||
//TODO: get rid of this awful if statement
|
||||
|
||||
if (i == ItemColumns.PriceYen) {
|
||||
func = new Gtk.TreeCellDataFunc(this.RenderColumnPriceYen);
|
||||
if (i == ItemColumns.Name) {
|
||||
func = new TreeCellDataFunc(this.RenderColumnName);
|
||||
}
|
||||
else if (i == ItemColumns.PriceYen) {
|
||||
func = new TreeCellDataFunc(this.RenderColumnPriceYen);
|
||||
}
|
||||
else if (i == ItemColumns.PriceAUD) {
|
||||
func = new Gtk.TreeCellDataFunc(this.RenderColumnPriceAUD);
|
||||
func = new TreeCellDataFunc(this.RenderColumnPriceAUD);
|
||||
}
|
||||
else if (i == ItemColumns.Ending) {
|
||||
func = new Gtk.TreeCellDataFunc(this.RenderColumnEnding);
|
||||
func = new TreeCellDataFunc(this.RenderColumnEnding);
|
||||
}
|
||||
else {
|
||||
Console.WriteLine($"unexpected value {i}!");
|
||||
throw new IndexOutOfRangeException();
|
||||
}
|
||||
|
||||
c.SetCellDataFunc(c.Cells[0], func);
|
||||
}
|
||||
|
||||
|
||||
foreach (object[] row in this.items) {
|
||||
Console.WriteLine(row[0]);
|
||||
}
|
||||
|
@ -92,10 +125,23 @@ namespace Buypeeb {
|
|||
|
||||
// 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) {
|
||||
var item = this.settings.watchlist[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;
|
||||
// TODO: actually download the data
|
||||
Thread.Sleep(3000);
|
||||
string html = "Heeenlo";
|
||||
item.name = html;
|
||||
|
@ -120,6 +166,8 @@ namespace Buypeeb {
|
|||
}
|
||||
|
||||
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();
|
||||
var t = Task.Factory.StartNew(() => {
|
||||
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!") {
|
||||
Dialog ed = new Dialog(title, null, Gtk.DialogFlags.DestroyWithParent, "Cancel", ResponseType.Cancel, "OK", ResponseType.Ok);
|
||||
ed.DefaultResponse = ResponseType.Ok;
|
||||
|
@ -183,7 +232,6 @@ namespace Buypeeb {
|
|||
}
|
||||
|
||||
private void ButtonUpdateAllClicked(object sender, EventArgs a) {
|
||||
Console.WriteLine("ButtonUpdateAllClicked");
|
||||
this.UpdateItems();
|
||||
}
|
||||
|
||||
|
|
38
Settings.cs
38
Settings.cs
|
@ -1,13 +1,9 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Buypeeb {
|
||||
|
||||
class Settings {
|
||||
private string location;
|
||||
|
||||
public int updateInterval { get; set; } = 10 * 60;
|
||||
public int favouriteUpdateInterval { get; set; } = 5 * 60;
|
||||
public int updateIntervalCritical { get; set; } = 60;
|
||||
|
@ -18,23 +14,18 @@ namespace Buypeeb {
|
|||
}
|
||||
|
||||
public Settings() {
|
||||
if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
|
||||
// C:\Users\Beebus\AppData\Roaming\Lynnear Software\buypeeb
|
||||
this.location = Path.Combine(Environment.ExpandEnvironmentVariables("%APPDATA%"), "Lynnear Software", "buypeeb");
|
||||
}
|
||||
else {
|
||||
// ~/.config/Lynnear Software/buypeeb
|
||||
this.location = Path.Combine(Environment.GetEnvironmentVariable("HOME"), ".config", "Lynnear Software", "buypeeb");
|
||||
if (this.watchlist == null) {
|
||||
// either this is the first time the program has been run, or there's something wrong with userdata.json
|
||||
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();
|
||||
}
|
||||
|
||||
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) {
|
||||
|
@ -46,14 +37,5 @@ namespace Buypeeb {
|
|||
Console.WriteLine("{0} - {1}", entry.Value.name, entry.Value.price);
|
||||
}
|
||||
}
|
||||
|
||||
public void Save() {
|
||||
string j = JsonSerializer.Serialize(this);
|
||||
Console.WriteLine(j);
|
||||
}
|
||||
|
||||
// public void Load() {
|
||||
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -795,7 +795,7 @@
|
|||
<property name="margin_top">3</property>
|
||||
<property name="margin_bottom">3</property>
|
||||
<child>
|
||||
<object class="GtkLabel">
|
||||
<object class="GtkLabel" id="LabelStatus">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="halign">start</property>
|
||||
|
|
Loading…
Reference in a new issue