Skip to content

Commit

Permalink
update edit mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusz-kierepka-hl committed Feb 8, 2025
1 parent 83a4294 commit 08e5ae7
Show file tree
Hide file tree
Showing 10 changed files with 679 additions and 614 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,5 @@
/ChatAAC/bin/Debug/net8.0/System.Speech.dll
/ChatAAC/bin/Debug/net8.0/Tmds.DBus.Protocol.dll
/.idea/.idea.ChatAAC/.idea/vcs.xml

.fake
1 change: 1 addition & 0 deletions .idea/.idea.ChatAAC/.idea/avalonia.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions ChatAAC/ChatAAC.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.2.3"/>
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.1.4"/>
<PackageReference Include="Avalonia.Xaml.Behaviors" Version="11.2.0.7" />
<PackageReference Include="Avalonia.Xaml.Interactivity" Version="11.2.0.7" />
<PackageReference Include="Avalonia.Xaml.Behaviors" Version="11.2.0.8" />
<PackageReference Include="Avalonia.Xaml.Interactivity" Version="11.2.0.8" />
<PackageReference Include="BinToss.GroupBox.Avalonia" Version="1.0.0"/>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0"/>
<PackageReference Include="DotNet.Bundle" Version="0.9.13" />
<PackageReference Include="MessageBox.Avalonia" Version="3.2.0" />
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="9.0.1" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.33.0" />
<PackageReference Include="OllamaSharp" Version="5.0.2" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.36.1" />
<PackageReference Include="OllamaSharp" Version="5.0.6" />
<PackageReference Include="ReactiveUI" Version="20.1.63"/>
<PackageReference Include="ReactiveUI.Fody" Version="19.5.41"/>
<PackageReference Include="RestSharp" Version="112.1.0"/>
Expand Down
18 changes: 18 additions & 0 deletions ChatAAC/Converters/NullToPlusConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Globalization;
using Avalonia.Data.Converters;

namespace ChatAAC.Converters;

public class NullToPlusConverter : IValueConverter
{
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
return value == null ? "+" : "";
}

public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
3 changes: 2 additions & 1 deletion ChatAAC/ViewModels/ButtonViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ namespace ChatAAC.ViewModels;

public class ButtonViewModel(Button button, int row, int column) : ReactiveObject
{
public Button Button { get; set; } = button;
public Button Button => button;

public int Row { get; set; } = row;
public int Column { get; set; } = column;
}
100 changes: 81 additions & 19 deletions ChatAAC/ViewModels/EditButtonViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Media;
using ChatAAC.Views;
using MsBox.Avalonia;


namespace ChatAAC.ViewModels;

public class EditButtonViewModel : ReactiveObject
{
private readonly Button _originalButton;
private readonly IList<Image> _obfImages; // direct reference to the underlying ObfFile.Images
private ObfFile _obfData;

private string _id;
private string _label;
Expand All @@ -29,40 +32,42 @@ public class EditButtonViewModel : ReactiveObject
private Image? _selectedImage;

public bool IsConfirmed { get; private set; }
public bool IsExistingButton => _originalButton != null;

public ReactiveCommand<Unit, Unit> ConfirmCommand { get; }
public ReactiveCommand<Unit, Unit> CancelCommand { get; }
public ReactiveCommand<Unit, Unit> AddImageCommand { get; }

public EditButtonViewModel(Button button, IList<Image> obfImages)
public EditButtonViewModel(Button button, ObfFile? obfData)
{
_originalButton = button ?? throw new ArgumentNullException(nameof(button));
_obfImages = obfImages;
_obfImages = obfData?.Images ?? [];
_obfData = obfData ?? new ObfFile();

// Copy existing fields
_id = button.Id;
_label = button.Label;
_borderColor = button.BorderColor;
_backgroundColor = button.BackgroundColor;

// Convert any existing string to an Avalonia color
if (Color.TryParse(button.BorderColor, out var bc))
_borderColorAvalonia = bc;
if (Color.TryParse(button.BackgroundColor, out var bg))
_backgroundColorAvalonia = bg;


_vocalization = button.Vocalization;
_action = button.Action;
_loadBoardPath = button.LoadBoard?.Path;

// Try to find the currently selected image from the button’s ImageId
var found = obfImages.FirstOrDefault(img => img.Id == button.ImageId);
var found = obfData?.Images.FirstOrDefault(img => img.Id == button.ImageId);
_selectedImage = found;

ConfirmCommand = ReactiveCommand.Create(Confirm);
CancelCommand = ReactiveCommand.Create(Cancel);
AddImageCommand = ReactiveCommand.Create(AddImage);
AddImageCommand = ReactiveCommand.Create(AddImage);
}

#region Properties
Expand Down Expand Up @@ -156,31 +161,75 @@ public Image? SelectedImage

private void Confirm()
{
// Save changes back to the original button
_originalButton.Id = _id;
_originalButton.Label = _label;
_originalButton.BorderColor = _borderColorAvalonia.ToString();
_originalButton.BackgroundColor = _backgroundColorAvalonia.ToString();
_originalButton.Vocalization = _vocalization;
_originalButton.Action = _action;
// Check for duplicate ID before applying changes
if (IsDuplicateId(_id))
{
// Show error message using AvaloniaUI MessageBox
var messageBoxStandardWindow = MessageBoxManager.GetMessageBoxStandard(
"Error", "Duplicate ID. Please enter a unique ID.");
messageBoxStandardWindow.ShowWindowAsync();

// If an image is selected, store its ID
_originalButton.ImageId = _selectedImage?.Id ?? string.Empty;
return; // Stop execution if ID is duplicate
}

if (!string.IsNullOrEmpty(_loadBoardPath))
if (IsExistingButton)
{
_originalButton.LoadBoard ??= new LoadBoard();
_originalButton.LoadBoard.Path = _loadBoardPath;
// Save changes back to the original button
_originalButton.Id = _id;
_originalButton.Label = _label;
_originalButton.BorderColor = _borderColorAvalonia.ToString();
_originalButton.BackgroundColor = _backgroundColorAvalonia.ToString();
_originalButton.Vocalization = _vocalization;
_originalButton.Action = _action;

// If an image is selected, store its ID
_originalButton.ImageId = _selectedImage?.Id ?? string.Empty;

if (!string.IsNullOrEmpty(_loadBoardPath))
{
_originalButton.LoadBoard ??= new LoadBoard();
_originalButton.LoadBoard.Path = _loadBoardPath;
}
else
{
_originalButton.LoadBoard = null;
}
}
else
{
_originalButton.LoadBoard = null;
var newButton = new Button()
{
Id = _id,
Label = _label,
BorderColor = BorderColorAvalonia.ToString(),
BackgroundColor = _backgroundColorAvalonia.ToString(),
Vocalization = _vocalization,
Action = _action,
ImageId = _selectedImage?.Id ?? string.Empty
};
if (!string.IsNullOrEmpty(_loadBoardPath))
{
newButton.LoadBoard ??= new LoadBoard();
newButton.LoadBoard.Path = _loadBoardPath;
}
else
{
newButton.LoadBoard = null;
}


_obfData.Buttons.Add(newButton);
}

IsConfirmed = true;
CloseWindow();
}

private bool IsDuplicateId(string newId)
{
return _obfData.Buttons.Any(b => b.Id == newId && b != _originalButton);
}

private void Cancel()
{
IsConfirmed = false;
Expand Down Expand Up @@ -208,13 +257,26 @@ private void AddImage()

// user created a new image
var newImage = vm.CreateImage();
newImage.Id = GenerateUniqueId(); // Call the GenerateUniqueId function

// Add to the underlying list
_obfImages.Add(newImage);

// Now set the newly created image as selected
SelectedImage = newImage;
}

private string GenerateUniqueId()
{
string newId;
do
{
newId = Guid.NewGuid().ToString();
} while (_obfData.Images.Any(img => img.Id == newId) || _obfData.Buttons.Any(b => b.Id == newId));

return newId;
}

private void CloseWindow()
{
var lifetime = Application.Current?.ApplicationLifetime;
Expand Down
56 changes: 56 additions & 0 deletions ChatAAC/ViewModels/GridCellViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Reactive;
using ReactiveUI;
using ChatAAC.Models.Obf;

namespace ChatAAC.ViewModels
{
public class GridCellViewModel : ReactiveObject
{
public int Row { get; }
public int Column { get; }
private Button? _button;
public Button? Button
{
get => _button;
set => this.RaiseAndSetIfChanged(ref _button, value);
}

// Komenda edycji komórki – jeżeli w komórce nie ma przycisku, tworzy nowy, w przeciwnym razie edytuje istniejący
public ReactiveCommand<Unit, Unit> EditCellCommand { get; }

private readonly MainViewModel _parent;

public GridCellViewModel(int row, int column, Button? button, MainViewModel parent)
{
Row = row;
Column = column;
Button = button;
_parent = parent;

EditCellCommand = ReactiveCommand.Create(async () =>
{
if (Button == null)
{
// Utwórz nowy przycisk z domyślnymi wartościami
var newButton = new Button
{
Id = Guid.NewGuid().ToString(),
Label = "New",
BorderColor = "#FF000000",
BackgroundColor = "#FFFFFFFF",
Vocalization = "",
Action = ""
};
Button = newButton;
// Dodaj nowy przycisk do modelu
_parent.ObfData?.Buttons.Add(newButton);
// Zaktualizuj siatkę – przypisz do komórki nowy identyfikator
_parent.UpdateGridOrderForCell(Row, Column, newButton.Id);
}
// Otwórz okno edycji przycisku
await _parent.EditButtonAsync(Button);
});
}
}
}
Loading

0 comments on commit 08e5ae7

Please sign in to comment.