Files
crosslangdevstudio/ViewModels/ProjectConfigurationViewModel.cs
2025-09-06 11:14:02 -05:00

219 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;
private 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 _templatename;
string _templatenamepretty;
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 TemplateName
{
get => _templatename;
set
{
Modified = true;
SetProperty(ref _templatename, value, nameof(TemplateName));
}
}
public string TemplateNamePretty
{
get => _templatenamepretty;
set
{
Modified = true;
SetProperty(ref _templatenamepretty, value, nameof(TemplateNamePretty));
}
}
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 ?? "";
_templatename = config?.Info?.TemplateName ?? "";
_templatenamepretty = config?.Info?.TemplateNamePretty ?? "";
_type = Array.IndexOf(CrossLangShell.ProjectTypes, config?.Info?.Type ?? "console");
_type = _type == -1 ? 0 : _type;
_description = config?.Info?.Description ?? "";
}
[RelayCommand]
public void Save()
{
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.TemplateName = string.IsNullOrWhiteSpace(TemplateName) ? null : TemplateName;
config.Info.TemplateNamePretty = string.IsNullOrWhiteSpace(TemplateNamePretty) ? null : TemplateNamePretty;
config.Info.Type = CrossLangShell.ProjectTypes[Type];
File.WriteAllText(FilePath, JsonConvert.SerializeObject(config, Formatting.Indented, new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore
}));
Modified = false;
}
}