689450393b
- Migrate shared components from WPF to a new Core library. - Introduce GroupInfo, M3UInfo, and ObservableViewModelBase classes. - Implement M3UParser for downloading and parsing M3U files. - Add ProgramsData for managing program lists and guides. - Create SettingsModel for application settings management. - Update project references in solution files.
88 lines
3.1 KiB
C#
88 lines
3.1 KiB
C#
using Newtonsoft.Json;
|
|
using System.IO;
|
|
|
|
namespace TV_Player.ViewModels
|
|
{
|
|
public static class SettingsModel
|
|
{
|
|
private static readonly string AppDataFolder = Path.Combine(GetWritableAppDataFolder(), "TVPlayer");
|
|
private static readonly string SettingsFilePath = Path.Combine(AppDataFolder, "settings.json");
|
|
|
|
private static string GetWritableAppDataFolder()
|
|
{
|
|
var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
|
|
if (!string.IsNullOrWhiteSpace(localAppData))
|
|
{
|
|
return localAppData;
|
|
}
|
|
|
|
var appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
|
|
if (!string.IsNullOrWhiteSpace(appData))
|
|
{
|
|
return appData;
|
|
}
|
|
|
|
return AppContext.BaseDirectory;
|
|
}
|
|
|
|
public static Dictionary<string,string> Playlists { get; set; }
|
|
public static bool StartFullScreen { get; set; }
|
|
public static bool StartFromLastScreen { get; set; }
|
|
public static string LastScreen { get; set; }
|
|
public static GroupInfo Group { get; set; }
|
|
public static M3UInfo Program { get; set; }
|
|
public static string[] HiddenGroups { get; set; }
|
|
|
|
public static void SaveSetttings()
|
|
{
|
|
// Create an anonymous object to hold the properties
|
|
var dataToSerialize = new
|
|
{
|
|
Playlists,
|
|
StartFromLastScreen,
|
|
StartFullScreen,
|
|
LastScreen,
|
|
Group,
|
|
Program,
|
|
HiddenGroups,
|
|
};
|
|
|
|
// Serialize the object to JSON
|
|
string json = JsonConvert.SerializeObject(dataToSerialize, Formatting.Indented);
|
|
|
|
if (!Directory.Exists(AppDataFolder))
|
|
Directory.CreateDirectory(AppDataFolder);
|
|
// Save the JSON to a file
|
|
File.WriteAllText(SettingsFilePath, json);
|
|
}
|
|
|
|
public static void LoadSettings()
|
|
{
|
|
var loadedData = new
|
|
{
|
|
Playlists = default(Dictionary<string,string>),
|
|
LastScreen = default(string),
|
|
Group = default(GroupInfo),
|
|
Program = default(M3UInfo),
|
|
StartFromLastScreen = default(bool),
|
|
StartFullScreen = default(bool),
|
|
HiddenGroups = default(string[])
|
|
};
|
|
if (File.Exists(SettingsFilePath))
|
|
{
|
|
// Read the JSON content from the file
|
|
string json = File.ReadAllText(SettingsFilePath);
|
|
loadedData = JsonConvert.DeserializeAnonymousType(json, loadedData);
|
|
}
|
|
// Assign the values to the properties
|
|
Playlists = loadedData.Playlists;
|
|
LastScreen = loadedData.LastScreen;
|
|
Group = loadedData.Group;
|
|
Program = loadedData.Program;
|
|
StartFromLastScreen = loadedData.StartFromLastScreen;
|
|
StartFullScreen = loadedData.StartFullScreen;
|
|
HiddenGroups = loadedData.HiddenGroups;
|
|
}
|
|
}
|
|
}
|