mattercontrol/SettingsManagement/UserSettings.cs

112 lines
2.4 KiB
C#
Raw Normal View History

using MatterHackers.MatterControl.DataStorage;
using System.Collections.Generic;
using System.Linq;
2014-01-29 19:09:30 -08:00
namespace MatterHackers.MatterControl
{
2016-04-29 07:45:15 -07:00
public enum ApplicationDisplayType { Responsive, Touchscreen };
2015-04-08 15:20:10 -07:00
public class UserSettings
{
private static UserSettings globalInstance = null;
private static readonly object syncRoot = new object();
2014-01-29 19:09:30 -08:00
private Dictionary<string, UserSetting> settingsDictionary;
private UserSettings()
2015-04-08 15:20:10 -07:00
{
// Load the UserSettings from the database
2016-01-05 18:55:34 -08:00
settingsDictionary = new Dictionary<string, UserSetting>();
foreach(var userSetting in Datastore.Instance.dbSQLite.Query<UserSetting>("SELECT * FROM UserSetting;"))
{
// Allow for duplicate entries in the database with the same .Name value
settingsDictionary[userSetting.Name] = userSetting;
}
// Set English as default language if unset
if (string.IsNullOrEmpty(this.get("Language")))
{
2016-01-06 10:41:18 -08:00
this.set("Language", "en");
}
// Propagate Language to local property
this.Language = this.get("Language");
2015-04-08 15:20:10 -07:00
}
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
public static UserSettings Instance
{
get
{
if (globalInstance == null)
{
lock(syncRoot)
{
if (globalInstance == null)
{
globalInstance = new UserSettings();
}
}
2015-04-08 15:20:10 -07:00
}
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
return globalInstance;
}
}
2014-01-29 19:09:30 -08:00
public string Language { get; private set; }
public UserSettingsFields Fields { get; private set; } = new UserSettingsFields();
2015-04-08 15:20:10 -07:00
public string get(string key)
{
UserSetting userSetting;
if (settingsDictionary.TryGetValue(key, out userSetting))
{
return userSetting.Value;
2015-04-08 15:20:10 -07:00
}
return null;
2015-04-08 15:20:10 -07:00
}
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
public void set(string key, string value)
{
UserSetting setting;
2014-01-29 19:09:30 -08:00
if(!settingsDictionary.TryGetValue(key, out setting))
{
// If the setting for the given key doesn't exist, create it
setting = new UserSetting()
{
Name = key
};
2015-04-08 15:20:10 -07:00
settingsDictionary[key] = setting;
}
2014-01-29 19:09:30 -08:00
// Special case to propagate Language to local property on assignment
if(key == "Language")
2015-04-08 15:20:10 -07:00
{
this.Language = value;
2015-04-08 15:20:10 -07:00
}
2014-01-29 19:09:30 -08:00
setting.Value = value;
setting.Commit();
2015-04-08 15:20:10 -07:00
}
2016-04-29 07:45:15 -07:00
public ApplicationDisplayType DisplayMode
{
get
{
if (this.get("ApplicationDisplayMode") == "touchscreen")
{
return ApplicationDisplayType.Touchscreen;
}
else
{
return ApplicationDisplayType.Responsive;
}
}
}
public bool IsTouchScreen => this.get("ApplicationDisplayMode") == "touchscreen";
2015-04-08 15:20:10 -07:00
}
}