Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
kierepka committed Feb 20, 2025
1 parent 08e5ae7 commit d741bd2
Show file tree
Hide file tree
Showing 10 changed files with 705 additions and 758 deletions.
401 changes: 182 additions & 219 deletions ChatAAC/Services/BoardLoaderService.cs

Large diffs are not rendered by default.

122 changes: 56 additions & 66 deletions ChatAAC/ViewModels/EditGridViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,93 +1,83 @@
using ReactiveUI;
using System;
using System.Reactive;
using Avalonia;
using ChatAAC.Models.Obf;
using Avalonia;

namespace ChatAAC.ViewModels;

public class EditGridViewModel : ReactiveObject
namespace ChatAAC.ViewModels
{
private readonly Grid _gridData;
private int _rows;
private int _columns;
public bool IsConfirmed { get; private set; }

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

public EditGridViewModel(Grid gridData)
public class EditGridViewModel : ReactiveObject
{
_gridData = gridData ?? throw new ArgumentNullException(nameof(gridData));
_rows = gridData.Rows;
_columns = gridData.Columns;
private readonly Grid _gridData;
private int _rows;
private int _columns;
public bool IsConfirmed { get; private set; }

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

public int Rows
{
get => _rows;
set => this.RaiseAndSetIfChanged(ref _rows, value);
}
public ReactiveCommand<Unit, Unit> ConfirmCommand { get; }
public ReactiveCommand<Unit, Unit> CancelCommand { get; }

public int Columns
{
get => _columns;
set => this.RaiseAndSetIfChanged(ref _columns, value);
}
public EditGridViewModel(Grid gridData)
{
_gridData = gridData ?? throw new ArgumentNullException(nameof(gridData));
_rows = gridData.Rows;
_columns = gridData.Columns;
ConfirmCommand = ReactiveCommand.Create(Confirm);
CancelCommand = ReactiveCommand.Create(Cancel);
}

private void Confirm()
{
// Possibly check if rows/cols decreased, prompt user
if (_rows < _gridData.Rows || _columns < _gridData.Columns)
public int Rows
{
// Show a user prompt or automatically remove references
// For brevity, let's just do it automatically
TrimOrder();
get => _rows;
set => this.RaiseAndSetIfChanged(ref _rows, value);
}

_gridData.Rows = _rows;
_gridData.Columns = _columns;
public int Columns
{
get => _columns;
set => this.RaiseAndSetIfChanged(ref _columns, value);
}

IsConfirmed = true;
CloseWindow();
}
private void Confirm()
{
if (_rows < _gridData.Rows || _columns < _gridData.Columns)
{
TrimOrder();
}
_gridData.Rows = _rows;
_gridData.Columns = _columns;
IsConfirmed = true;
CloseWindow();
}

private void TrimOrder()
{
var newOrder = new string?[_rows][];
for (var r = 0; r < _rows; r++)
private void TrimOrder()
{
newOrder[r] = new string?[_columns];
for (var c = 0; c < _columns; c++)
var newOrder = new string?[_rows][];
for (int r = 0; r < _rows; r++)
{
if (r < _gridData.Order.Length && c < _gridData.Order[r].Length)
{
newOrder[r][c] = _gridData.Order[r][c];
}
else
newOrder[r] = new string?[_columns];
for (int c = 0; c < _columns; c++)
{
newOrder[r][c] = null;
if (r < _gridData.Order.Length && c < _gridData.Order[r].Length)
newOrder[r][c] = _gridData.Order[r][c];
else
newOrder[r][c] = null;
}
}
_gridData.Order = newOrder;
}

_gridData.Order = newOrder;
}

private void Cancel()
{
IsConfirmed = false;
CloseWindow();
}
private void Cancel()
{
IsConfirmed = false;
CloseWindow();
}

private void CloseWindow()
{
if (Application.Current?.ApplicationLifetime is Avalonia.Controls.ApplicationLifetimes.IClassicDesktopStyleApplicationLifetime desktop)
private void CloseWindow()
{
desktop.Windows[^1].Close();
if (Application.Current?.ApplicationLifetime is Avalonia.Controls.ApplicationLifetimes.IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.Windows[^1].Close();
}
}
}
}
17 changes: 8 additions & 9 deletions ChatAAC/ViewModels/GridCellViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Reactive;
using System.Threading.Tasks;
using ReactiveUI;
using ChatAAC.Models.Obf;

Expand All @@ -9,24 +10,23 @@ 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;
// Zmieniono typ na ReactiveCommand<Unit, Task>
public ReactiveCommand<Unit, Task> EditCellCommand { get; }

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

EditCellCommand = ReactiveCommand.Create(async () =>
{
Expand All @@ -44,12 +44,11 @@ public GridCellViewModel(int row, int column, Button? button, MainViewModel pare
};
Button = newButton;
// Dodaj nowy przycisk do modelu
_parent.ObfData?.Buttons.Add(newButton);
parent1.ObfData?.Buttons.Add(newButton);
// Zaktualizuj siatkę – przypisz do komórki nowy identyfikator
_parent.UpdateGridOrderForCell(Row, Column, newButton.Id);
parent1.UpdateGridOrderForCell(Row, Column, newButton.Id);
}
// Otwórz okno edycji przycisku
await _parent.EditButtonAsync(Button);
await parent1.EditButtonAsync(Button);
});
}
}
Expand Down
Loading

0 comments on commit d741bd2

Please sign in to comment.