c58d846057
Related Work Items: #416, #422, #423, #424
101 lines
2.4 KiB
C#
101 lines
2.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Threading.Tasks;
|
|
using Windows.Storage;
|
|
using Windows.UI.Xaml;
|
|
|
|
namespace FoxTube.Services
|
|
{
|
|
public static class Storage
|
|
{
|
|
public static event EventHandler<Settings> SettingsChanged;
|
|
|
|
public static StorageFolder Folder => ApplicationData.Current.RoamingFolder;
|
|
public static ApplicationDataContainer Registry { get; } = ApplicationData.Current.RoamingSettings;
|
|
|
|
private static readonly Dictionary<Settings, object> _defaultSettings = new Dictionary<Settings, object>
|
|
{
|
|
{ Settings.Theme, ElementTheme.Default },
|
|
{ Settings.UILanguage, GetDefaultLanguage() },
|
|
{ Settings.RelevanceLanguage, GetDefaultLanguage() },
|
|
{ Settings.PromptFeedback, true },
|
|
{ Settings.PromptReview, true },
|
|
{ Settings.AllowAnalytics, true }
|
|
};
|
|
|
|
public enum Settings
|
|
{
|
|
/// <summary>
|
|
/// ElementTheme
|
|
/// </summary>
|
|
Theme,
|
|
/// <summary>
|
|
/// string
|
|
/// </summary>
|
|
UILanguage,
|
|
/// <summary>
|
|
/// string
|
|
/// </summary>
|
|
RelevanceLanguage,
|
|
/// <summary>
|
|
/// string
|
|
/// </summary>
|
|
DefaultDownloadsFolder,
|
|
/// <summary>
|
|
/// bool
|
|
/// </summary>
|
|
PromptFeedback,
|
|
/// <summary>
|
|
/// bool
|
|
/// </summary>
|
|
PromptReview,
|
|
/// <summary>
|
|
/// bool
|
|
/// </summary>
|
|
AllowAnalytics,
|
|
/// <summary>
|
|
/// string
|
|
/// </summary>
|
|
Region
|
|
}
|
|
|
|
public enum Metrics
|
|
{
|
|
/// <summary>
|
|
/// TimeSpan
|
|
/// </summary>
|
|
Uptime
|
|
}
|
|
|
|
|
|
public static void SetValue(Settings key, object value)
|
|
{
|
|
Registry.Values[$"{key.GetType().Name}.{key}"] = value;
|
|
SettingsChanged?.Invoke(value, key);
|
|
}
|
|
public static void SetValue(Metrics key, object value) =>
|
|
Registry.Values[$"{key.GetType().Name}.{key}"] = value;
|
|
|
|
public static T GetValue<T>(Settings key) =>
|
|
(T)(Registry.Values[$"{key.GetType().Name}.{key}"] ?? (_defaultSettings.ContainsKey(key) ? _defaultSettings[key] : null));
|
|
public static T GetValue<T>(Metrics key) =>
|
|
(T)Registry.Values[$"{key.GetType().Name}.{key}"];
|
|
|
|
|
|
private static string GetDefaultLanguage()
|
|
{
|
|
if (CultureInfo.InstalledUICulture.TwoLetterISOLanguageName.Belongs("ua", "ru", "by", "kz", "kg", "md", "lv", "ee")) //Languages for Russian-speaking countries
|
|
return "ru-RU";
|
|
else
|
|
return "en-US";
|
|
}
|
|
|
|
public static async Task ResetStorage()
|
|
{
|
|
Registry.Values.Clear();
|
|
foreach (IStorageItem i in await Folder.GetItemsAsync())
|
|
await i.DeleteAsync();
|
|
}
|
|
}
|
|
} |