Files
crosslangdevstudio/ViewModels/ProjectConfigurationViewModel.cs
2025-10-22 17:31:32 -05:00

224 lines
5.9 KiB
C#

using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Platform.Storage;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using CrossLangDevStudio.Messages;
using CrossLangDevStudio.Views;
using Newtonsoft.Json;
namespace CrossLangDevStudio.ViewModels;
public partial class ProjectConfigurationViewModel : ViewModelBase, ISavable
{
/*
<ComboBoxItem>Console Application</ComboBoxItem>
<ComboBoxItem>Library</ComboBoxItem>
<ComboBoxItem>Web Application</ComboBoxItem>
<ComboBoxItem>Template</ComboBoxItem>
<ComboBoxItem>Compile Tool</ComboBoxItem>
<ComboBoxItem>Tool</ComboBoxItem>
<ComboBoxItem>Archive</ComboBoxItem>
*/
TabItemViewModel tab;
public string FilePath { get; }
bool _modified = false;
public bool Modified
{
get => _modified;
set
{
_modified = value;
if (value)
{
tab.Header = $"Project Configuration*";
}
else
{
tab.Header = "Project Configuration";
}
}
}
string _name;
string _version;
string _icon;
string _maintainer;
string _repository;
string _homepage;
string _license;
string _shortname;
string _shortnamepretty;
string _description;
int _type;
public string Name
{
get => _name;
set
{
Modified = true;
SetProperty(ref _name, value, nameof(Name));
}
}
public string Version
{
get => _version;
set
{
Modified = true;
SetProperty(ref _version, value, nameof(Version));
}
}
public string Icon
{
get => _icon;
set
{
Modified = true;
SetProperty(ref _icon, value, nameof(Icon));
}
}
public string Maintainer
{
get => _maintainer;
set
{
Modified = true;
SetProperty(ref _maintainer, value, nameof(Maintainer));
}
}
public string Homepage
{
get => _homepage;
set
{
Modified = true;
SetProperty(ref _homepage, value, nameof(Homepage));
}
}
public string Repository
{
get => _repository;
set
{
Modified = true;
SetProperty(ref _repository, value, nameof(Repository));
}
}
public string License
{
get => _license;
set
{
Modified = true;
SetProperty(ref _license, value, nameof(License));
}
}
public string ShortName
{
get => _shortname;
set
{
Modified = true;
SetProperty(ref _shortname, value, nameof(ShortName));
}
}
public string ShortNamePretty
{
get => _shortnamepretty;
set
{
Modified = true;
SetProperty(ref _shortnamepretty, value, nameof(ShortNamePretty));
}
}
public string Description
{
get => _description;
set
{
Modified = true;
SetProperty(ref _description, value, nameof(Description));
}
}
public int Type
{
get => _type;
set
{
Modified = true;
SetProperty(ref _type, value, nameof(Type));
}
}
public ProjectConfigurationViewModel(string configPath, TabItemViewModel tab)
{
FilePath = configPath;
this.tab = tab;
var config = JsonConvert.DeserializeObject<CrossLangConfig>(File.ReadAllText(configPath));
_name = config?.Name ?? "";
_version = config?.Version.ToString() ?? "1.0.0.0-dev";
_icon = config?.Icon ?? "";
_repository = config?.Info?.Repoository ?? "";
_homepage = config?.Info?.HomePage ?? "";
_maintainer = config?.Info?.Maintainer ?? "";
_license = config?.Info?.License ?? "";
_shortname = config?.Info?.ShortName ?? "";
_shortnamepretty = config?.Info?.ShortNamePretty ?? "";
_type = Array.IndexOf(CrossLangShell.ProjectTypes, config?.Info?.Type ?? "console");
_type = _type == -1 ? 0 : _type;
_description = config?.Info?.Description ?? "";
}
[RelayCommand]
public void Save()
{
SaveRecovery(FilePath);
Modified = false;
}
public void SaveRecovery(string path)
{
var config = JsonConvert.DeserializeObject<CrossLangConfig>(File.ReadAllText(FilePath)) ?? new CrossLangConfig();
config.Name = Name;
if (CrossLangVersion.TryParse(Version, out var vers))
{
config.Version = vers;
}
config.Icon = string.IsNullOrWhiteSpace(Icon) ? null : Icon;
config.Info.Description = string.IsNullOrWhiteSpace(Description) ? null : Description;
config.Info.HomePage = string.IsNullOrWhiteSpace(Homepage) ? null : Homepage;
config.Info.License = string.IsNullOrWhiteSpace(License) ? null : License;
config.Info.Maintainer = string.IsNullOrWhiteSpace(Maintainer) ? null : Maintainer;
config.Info.Repoository = string.IsNullOrWhiteSpace(Repository) ? null : Repository;
config.Info.ShortName = string.IsNullOrWhiteSpace(ShortName) ? null : ShortName;
config.Info.ShortNamePretty = string.IsNullOrWhiteSpace(ShortNamePretty) ? null : ShortNamePretty;
config.Info.Type = CrossLangShell.ProjectTypes[Type];
File.WriteAllText(path, JsonConvert.SerializeObject(config, Formatting.Indented, new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore
}));
}
}