Skip to content

Commit

Permalink
fix(settings): refresh overlay on saved settings
Browse files Browse the repository at this point in the history
  • Loading branch information
DevilPepper committed May 17, 2021
1 parent 6e61cd2 commit 8cd667f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
16 changes: 14 additions & 2 deletions ItemBoxTracker/src/Plugin/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ public class Main : IPlugin, ISettingsOwner {
private InventoryService Inventory;
private InventoryView GUI;

private SettingsView settings;
private SettingsView Settings {
get {
lock (Config) {
if (settings == null) {
settings = new(Config);
}
}
return settings;
}
}

// Really bad singleton, but I think it's fine considering
// the plugin gets instantiated by HunterPie
// and everywhere that uses the singleton is part of this plugin
Expand All @@ -37,7 +49,7 @@ public void Initialize(Game context) {

Dispatcher.Dispatch(async () => {
GUI = new(await Inventory.LoadAsync());
Events = new(Context, GUI, Inventory);
Events = new(Context, GUI, Inventory, Settings);
Events.Subscribe();
Overlay.RegisterWidget(GUI);
await GUI.Initialize();
Expand All @@ -53,7 +65,7 @@ public void Unload() {
}

public IEnumerable<ISettingsTab> GetSettings(ISettingsBuilder builder) {
builder.AddTab(new SettingsView(Config));
builder.AddTab(Settings);
return builder.Value();
}
}
Expand Down
7 changes: 5 additions & 2 deletions ItemBoxTracker/src/Plugin/Service/EventService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ public class EventService {
private Game Context;
InventoryView GUI;
InventoryService Inventory;
public EventService(Game Context, InventoryView GUI, InventoryService Inventory) {
SettingsView Settings;
public EventService(Game Context, InventoryView GUI, InventoryService Inventory, SettingsView Settings) {
this.Context = Context;
this.GUI = GUI;
this.Inventory = Inventory;
this.Settings = Settings;
}

public void Subscribe() {
Expand All @@ -22,7 +24,7 @@ public void Subscribe() {
player.Inventory.OnInventoryUpdate += Refresh;
player.OnCharacterLogin += ShowWidget;
player.OnCharacterLogout += HideWidget;
// TODO: subscribe to save event?
Settings.SavedSettings += Refresh;
}

public void Unsubscribe() {
Expand All @@ -33,6 +35,7 @@ public void Unsubscribe() {
player.Inventory.OnInventoryUpdate -= Refresh;
player.OnCharacterLogin -= ShowWidget;
player.OnCharacterLogout -= HideWidget;
Settings.SavedSettings -= Refresh;
}

public void Refresh(object source, EventArgs e) {
Expand Down
5 changes: 4 additions & 1 deletion ItemBoxTracker/src/Plugin/Views/SettingsView.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Windows.Controls;
using HunterPie.Settings;
using MHWItemBoxTracker.Service;
Expand All @@ -24,11 +25,13 @@ public async void LoadSettings() {

public async void SaveSettings() {
await Config.SaveAsync();
// TODO: Inventory.Refresh()
SavedSettings?.Invoke(null, null);
}

public string ValidateSettings() {
return null;
}

public event EventHandler SavedSettings;
}
}

0 comments on commit 8cd667f

Please sign in to comment.