2020-09-24 04:20:32 +08:00
|
|
|
|
// Copyright (c) Microsoft Corporation
|
|
|
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
|
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
|
|
|
|
|
|
using System;
|
2020-10-23 00:45:48 +08:00
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
|
2020-09-24 04:20:32 +08:00
|
|
|
|
|
2020-10-23 00:45:48 +08:00
|
|
|
|
namespace Microsoft.PowerToys.Settings.UI.Library
|
2020-09-24 04:20:32 +08:00
|
|
|
|
{
|
|
|
|
|
// This Singleton class is a wrapper around the settings configurations that are accessed by viewmodels.
|
|
|
|
|
// This class can have only one instance and therefore the settings configurations are common to all.
|
|
|
|
|
public class SettingsRepository<T> : ISettingsRepository<T>
|
|
|
|
|
where T : class, ISettingsConfig, new()
|
|
|
|
|
{
|
|
|
|
|
private static readonly object _SettingsRepoLock = new object();
|
|
|
|
|
|
|
|
|
|
private static ISettingsUtils _settingsUtils;
|
|
|
|
|
|
|
|
|
|
private static SettingsRepository<T> settingsRepository;
|
|
|
|
|
|
|
|
|
|
private T settingsConfig;
|
|
|
|
|
|
2020-10-20 04:32:05 +08:00
|
|
|
|
// Suppressing the warning as this is a singleton class and this method is
|
|
|
|
|
// necessarily static
|
|
|
|
|
#pragma warning disable CA1000 // Do not declare static members on generic types
|
2020-09-24 04:20:32 +08:00
|
|
|
|
public static SettingsRepository<T> GetInstance(ISettingsUtils settingsUtils)
|
2020-10-20 04:32:05 +08:00
|
|
|
|
#pragma warning restore CA1000 // Do not declare static members on generic types
|
2020-09-24 04:20:32 +08:00
|
|
|
|
{
|
|
|
|
|
// To ensure that only one instance of Settings Repository is created in a multi-threaded environment.
|
|
|
|
|
lock (_SettingsRepoLock)
|
|
|
|
|
{
|
|
|
|
|
if (settingsRepository == null)
|
|
|
|
|
{
|
|
|
|
|
settingsRepository = new SettingsRepository<T>();
|
|
|
|
|
_settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return settingsRepository;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The Singleton class must have a private constructor so that it cannot be instantiated by any other object other than itself.
|
|
|
|
|
private SettingsRepository()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Settings configurations shared across all viewmodels
|
|
|
|
|
public T SettingsConfig
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (settingsConfig == null)
|
|
|
|
|
{
|
|
|
|
|
T settingsItem = new T();
|
2021-01-14 20:14:29 +08:00
|
|
|
|
settingsConfig = _settingsUtils.GetSettingsOrDefault<T>(settingsItem.GetModuleName());
|
2020-09-24 04:20:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return settingsConfig;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value != null)
|
|
|
|
|
{
|
|
|
|
|
settingsConfig = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|