- Move Master.txt translation file to Translations/en/Translation.txt
- Threadsafe MatterControlTranslationMap initialization
- Refactor UserSettings:
- Threadsafe initialization
- Concise initialization
- UseTryGetValue for dictionary lookups
- Use backing fields
- Cache frequently accessed Language value as local property
89 lines
No EOL
1.9 KiB
C#
89 lines
No EOL
1.9 KiB
C#
using MatterHackers.MatterControl.DataStorage;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace MatterHackers.MatterControl
|
|
{
|
|
public class UserSettings
|
|
{
|
|
private static UserSettings globalInstance = null;
|
|
|
|
private static readonly object syncRoot = new object();
|
|
|
|
private Dictionary<string, UserSetting> settingsDictionary;
|
|
|
|
private UserSettings()
|
|
{
|
|
// Load the UserSettings from the database
|
|
settingsDictionary = (from setting in Datastore.Instance.dbSQLite.Query<UserSetting>("SELECT * FROM UserSetting;")
|
|
select setting).ToDictionary(s => s.Name, s => s);
|
|
|
|
// Set English as default language if unset
|
|
if (string.IsNullOrEmpty(this.get("Language")))
|
|
{
|
|
UserSettings.Instance.set("Language", "en");
|
|
}
|
|
|
|
// Propagate Language to local property
|
|
this.Language = this.get("Language");
|
|
}
|
|
|
|
public static UserSettings Instance
|
|
{
|
|
get
|
|
{
|
|
if (globalInstance == null)
|
|
{
|
|
lock(syncRoot)
|
|
{
|
|
if (globalInstance == null)
|
|
{
|
|
globalInstance = new UserSettings();
|
|
}
|
|
}
|
|
}
|
|
|
|
return globalInstance;
|
|
}
|
|
}
|
|
|
|
public string Language { get; private set; }
|
|
|
|
public UserSettingsFields Fields { get; private set; } = new UserSettingsFields();
|
|
|
|
public string get(string key)
|
|
{
|
|
UserSetting userSetting;
|
|
if (settingsDictionary.TryGetValue(key, out userSetting))
|
|
{
|
|
return userSetting.Value;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public void set(string key, string value)
|
|
{
|
|
UserSetting setting;
|
|
|
|
if(!settingsDictionary.TryGetValue(key, out setting))
|
|
{
|
|
// If the setting for the given key doesn't exist, create it
|
|
setting = new UserSetting()
|
|
{
|
|
Name = key
|
|
};
|
|
settingsDictionary[key] = setting;
|
|
}
|
|
|
|
// Special case to propagate Language to local property on assignment
|
|
if(key == "Language")
|
|
{
|
|
this.Language = value;
|
|
}
|
|
|
|
setting.Value = value;
|
|
setting.Commit();
|
|
}
|
|
}
|
|
} |