TestArrangementOfElementsViewModel.cs 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Collections.Generic;
  3. using Avalonia.Layout;
  4. using System.Collections.ObjectModel;
  5. using System.Diagnostics;
  6. using ReactiveUI;
  7. using Avalonia;
  8. using System.Linq;
  9. using EntranseTesting.Models;
  10. using System.Runtime.InteropServices;
  11. using CommunityToolkit.Mvvm.ComponentModel;
  12. namespace EntranseTesting.ViewModels
  13. {
  14. public partial class TestArrangementOfElementsViewModel : ObservableObject
  15. {
  16. EntranceTestingContext baseConnection = new EntranceTestingContext();
  17. public const string CustomFormat = "element-item-format";
  18. [ObservableProperty] int numberTask;
  19. [ObservableProperty] string question;
  20. [ObservableProperty] List<QuestionImage> qImage= new List<QuestionImage>();
  21. [ObservableProperty] Orientation stackLayoutOrientation;
  22. [ObservableProperty] private ElementOfArrangement? draggingElementItem;
  23. ObservableCollection<ElementOfArrangement> element = new ObservableCollection<ElementOfArrangement>();
  24. public ObservableCollection<ElementOfArrangement> Element { get => element; set => element = value; }
  25. public TestArrangementOfElementsViewModel(int numberTask, Orientation orientation)
  26. {
  27. this.numberTask = numberTask;
  28. Question = baseConnection.Questions.FirstOrDefault(tb => tb.Id == numberTask).Name;
  29. QImage = baseConnection.QuestionImages.Where(tb => tb.IdQuestion == numberTask).ToList();
  30. StackLayoutOrientation = orientation;
  31. int responseIndex = Response.IndexResponse(numberTask);
  32. if (Response.responseUsers[responseIndex].UserResponseArrangements.Count == 0)//åñëè ïîëüçîâàòåëü íå îòâå÷àë
  33. {
  34. //çàïîëíÿåì äàííûå
  35. List<ElementOfArrangement> _list = baseConnection.ElementOfArrangements.Where(tb => tb.IdQuestion == numberTask).ToList();
  36. foreach (ElementOfArrangement elem in _list)
  37. Element.Add(elem);
  38. int i = 1;
  39. //çàïèñûâàåì â øàáëîí îòâåòà
  40. foreach (ElementOfArrangement elem in _list)
  41. Response.responseUsers[responseIndex].UserResponseArrangements.Add(new UserResponseArrangement { IdElement = elem.Id, Position = i++ });
  42. }
  43. else//åñëè ïîëüçîâàòåëü îòâå÷àë
  44. {
  45. //çàïîëíÿåì äàííûå
  46. List<ElementOfArrangement> _list = baseConnection.ElementOfArrangements.Where(tb => tb.IdQuestion == numberTask).ToList();
  47. List<UserResponseArrangement> _response = Response.responseUsers[responseIndex].UserResponseArrangements.ToList();
  48. _response = _response.OrderBy(tb => tb.Position).ToList();
  49. foreach (UserResponseArrangement item in _response)
  50. {
  51. ElementOfArrangement elem = _list.FirstOrDefault(tb => tb.Id == item.IdElement);
  52. Element.Add(elem);
  53. }
  54. }
  55. }
  56. public void StartDrag(ElementOfArrangement elem)
  57. {
  58. DraggingElementItem = elem;
  59. }
  60. public void Drop(ElementOfArrangement elem, Point startPosition, Point endPosition)
  61. {
  62. ElementOfArrangement item = Element.FirstOrDefault(t => t.Name == elem.Name);
  63. if (item is null)
  64. {
  65. Debug.WriteLine($"Elem with id '{elem.Name}' not found");
  66. return;
  67. }
  68. int indexItem = Element.IndexOf(item);
  69. int difference = 0;
  70. if (StackLayoutOrientation == Orientation.Horizontal)
  71. difference = (int)Math.Round((endPosition.X - startPosition.X + 10) / DraggingElementItem.Width);
  72. else if (StackLayoutOrientation == Orientation.Vertical)
  73. difference = (int)Math.Round((endPosition.Y - startPosition.Y + 10) / DraggingElementItem.Height);
  74. if (difference != 0)
  75. {
  76. Element.Remove(item);
  77. difference = (difference + indexItem < 0) ? 0 : (difference + indexItem > Element.Count) ? Element.Count : difference + indexItem;
  78. Element.Insert(difference, item);
  79. }
  80. }
  81. }
  82. }