diff --git a/VisualStudioDiscordRPC.Shared/Plugs/TextPlugs/CustomTextPlug.cs b/VisualStudioDiscordRPC.Shared/Plugs/TextPlugs/CustomTextPlug.cs index fdf792d..f0593d5 100644 --- a/VisualStudioDiscordRPC.Shared/Plugs/TextPlugs/CustomTextPlug.cs +++ b/VisualStudioDiscordRPC.Shared/Plugs/TextPlugs/CustomTextPlug.cs @@ -24,37 +24,41 @@ public CustomTextPlug(string id, string name) _id = id; _name = name; } + public void SetPattern(string pattern) { - _stringObserver?.Dispose(); + if (_stringObserver == null) + { + _stringObserver = new StringObserver(); + } + + _stringObserver.Clear(); var parser = new ObservableStringParser(); var entries = parser.Parse(pattern); - var stringObserver = new StringObserver(); foreach (var entry in entries) { switch (entry.Type) { case ObservableStringParser.EntryType.Text: - stringObserver.AddText(entry.Value); + _stringObserver.AddText(entry.Value); break; case ObservableStringParser.EntryType.Keyword: var variable = _variableService.GetVariableByName(entry.Value); if (variable != null) - stringObserver.AddText(new ObservableVariable(variable)); + _stringObserver.AddText(new ObservableVariable(variable)); break; } } - _stringObserver = stringObserver; _pattern = pattern; } public void ClearObserver() { - _stringObserver?.Dispose(); + _stringObserver?.Clear(); } public override void Enable() diff --git a/VisualStudioDiscordRPC.Shared/Plugs/TextPlugs/StringObserver.cs b/VisualStudioDiscordRPC.Shared/Plugs/TextPlugs/StringObserver.cs index 2da397d..345c671 100644 --- a/VisualStudioDiscordRPC.Shared/Plugs/TextPlugs/StringObserver.cs +++ b/VisualStudioDiscordRPC.Shared/Plugs/TextPlugs/StringObserver.cs @@ -4,7 +4,7 @@ namespace VisualStudioDiscordRPC.Shared.Plugs.TextPlugs { - public class StringObserver : IDisposable + public class StringObserver { public event Action Changed; @@ -23,10 +23,12 @@ public void AddText(string staticText) AddText(staticTextSource); } - public void Dispose() + public void Clear() { foreach (IObservableString observableString in _observableStrings) observableString.Changed -= OnAnyTextSourceChanged; + + _observableStrings.Clear(); } private void OnAnyTextSourceChanged()