Skip to content

Global Settings

BarRaider edited this page Mar 24, 2021 · 2 revisions

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.

PluginBase class

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.

GlobalSettingsManager 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.

  1. 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; }
}
  1. 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 from PluginBase you can skip this step as you already have a OnReceivedGlobalSettings 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;
	}
}
  1. Use RequestGlobalSettings() method to request the Global Settings. You will then receive a callback in the OnReceivedGlobalSettings you set in step 2.
public class MyClass : IDisposable
{
	public MyClass()
	{
		GlobalSettingsManager.Instance.OnReceivedGlobalSettings += MyClass_OnReceivedGlobalSettings;
		GlobalSettingsManager.Instance.RequestGlobalSettings();
	}
}
  1. 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