-
-
Notifications
You must be signed in to change notification settings - Fork 70
Global Settings
Global Settings are useful for storing global data that is relevant for your entire plugin (vs only the current action). All data stored in the Global Settings is encrypted and saved securely by the Stream Deck app.
By implementing the PluginBase class you automatically get notified when the Global Settings change through the ReceivedGlobalSettings
method. The Connection
object (which is part of the PluginBasee class) has the SetGlobalSettingsAsync and GetGlobalSettingsAsync used to read/write from the Global Settings. While the detailed example below shows how to modify the settings from the GlobalSettingsMaanger, you can do the same via the Connection object if you implement the PluginBase class.
Using the GlobalSettingsManager
you can get access to the plugin's global settings from anywhere in your code.
Below is an example of how to read and write to the Global Settings.
- Create a class that will store the fields of your Global Settings:
public class GlobalSettings
{
[JsonProperty(PropertyName = "myFirstField")]
public String MyFirstField { get; set; }
[JsonProperty(PropertyName = "mySecondFile")]
public bool MySecondField { get; set; }
}
- In the class you want to read/write the settings, subscribe to the
OnReceivedGlobalSettings
event. Remember: If you subscribe to an event, you must also unsubscribe to it. So make sure your class has a Dispose function (inherits from IDisposable).
NOTE: If this is in your action where you inherit fromPluginBase
you can skip this step as you already have aOnReceivedGlobalSettings
function as part of the PluginBase implementation
public class MyClass : IDisposable
{
public MyClass()
{
GlobalSettingsManager.Instance.OnReceivedGlobalSettings += MyClass_OnReceivedGlobalSettings;
}
public override void Dispose()
{
GlobalSettingsManager.Instance.OnReceivedGlobalSettings -= MyClass_OnReceivedGlobalSettings;
}
}
- Use
RequestGlobalSettings()
method to request the Global Settings. You will then receive a callback in theOnReceivedGlobalSettings
you set in step 2.
public class MyClass : IDisposable
{
public MyClass()
{
GlobalSettingsManager.Instance.OnReceivedGlobalSettings += MyClass_OnReceivedGlobalSettings;
GlobalSettingsManager.Instance.RequestGlobalSettings();
}
}
- Example of reading and saving settings
private void MyClass_OnReceivedGlobalSettings(object sender, ReceivedGlobalSettingsPayload payload)
{
// Global Settings exist
if (payload?.Settings != null && payload.Settings.Count > 0)
{
global = payload.Settings.ToObject<GlobalSettings>();
// global now has all the settings
// Console.Writeline(global.MyFirstField);
}
else // Global settings do not exist, create new one and SAVE it
{
Logger.Instance.LogMessage(TracingLevel.WARN, $"No global settings found, creating new object");
global = new GlobalSettings();
SetGlobalSettings();
}
}
// Saves the global object back the global settings
private void SetGlobalSettings()
{
Connection.SetGlobalSettingsAsync(JObject.FromObject(global));
}
Next Topic: User Settings
© Copyright 2021 By BarRaider