DynamicTreeNodes.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Linq.Expressions;
  7. using InABox.Core;
  8. using Xamarin.Forms;
  9. namespace InABox.Mobile
  10. {
  11. public class DynamicTreeNode : INotifyPropertyChanged
  12. {
  13. private DynamicTreeNodes _owner;
  14. public ObservableCollection<DynamicTreeNode> Children => _owner.GetChilden(_id);
  15. private Guid _id;
  16. public Guid ID
  17. {
  18. get { return _id; }
  19. set
  20. {
  21. _id = value;
  22. RaisedOnPropertyChanged("ID");
  23. }
  24. }
  25. private Guid _parent;
  26. public Guid Parent
  27. {
  28. get { return _parent; }
  29. set
  30. {
  31. _parent = value;
  32. RaisedOnPropertyChanged("Parent");
  33. }
  34. }
  35. private string _description;
  36. public string Description
  37. {
  38. get { return _description; }
  39. set
  40. {
  41. _description = value;
  42. RaisedOnPropertyChanged("Description");
  43. }
  44. }
  45. private ImageSource? _image;
  46. public ImageSource? Image
  47. {
  48. get { return _image; }
  49. set
  50. {
  51. _image = value;
  52. RaisedOnPropertyChanged("Image");
  53. }
  54. }
  55. public event PropertyChangedEventHandler? PropertyChanged;
  56. public void RaisedOnPropertyChanged(string propertyName)
  57. {
  58. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  59. }
  60. public DynamicTreeNode(DynamicTreeNodes owner)
  61. {
  62. _owner = owner;
  63. _description = "";
  64. }
  65. public DynamicTreeNode(DynamicTreeNodes owner, Guid id, Guid parent) : this(owner)
  66. {
  67. _id = id;
  68. _parent = parent;
  69. }
  70. public DynamicTreeNode GetParent() => _owner.Nodes.FirstOrDefault(x => x.ID == _parent);
  71. public int Index()
  72. {
  73. var parent = GetParent();
  74. return parent != null
  75. ? parent.Children.IndexOf(this) + 1
  76. : _owner.Nodes.IndexOf(this) + 1;
  77. }
  78. public String Number
  79. {
  80. get
  81. {
  82. String result = Index().ToString();
  83. var parent = GetParent();
  84. while (parent != null)
  85. {
  86. int index = parent.Index();
  87. result = $"{index}.{result}";
  88. parent = parent.GetParent();
  89. }
  90. return result;
  91. }
  92. }
  93. }
  94. public class DynamicTreeNodes
  95. {
  96. private List<DynamicTreeNode> _nodes;
  97. public ObservableCollection<DynamicTreeNode> Nodes => new ObservableCollection<DynamicTreeNode>(_nodes.Where(x => x.Parent == Guid.Empty));
  98. public DynamicTreeNode? this[Guid id] => _nodes.FirstOrDefault(x => x.ID == id);
  99. public DynamicTreeNodes()
  100. {
  101. _nodes = new List<DynamicTreeNode>();
  102. }
  103. public DynamicTreeNode Add(Guid id, Guid parent)
  104. {
  105. var node = new DynamicTreeNode(this, id, parent);
  106. _nodes.Add(node);
  107. return node;
  108. }
  109. public DynamicTreeNode Find(Guid id) => _nodes.FirstOrDefault(x => x.ID == id);
  110. public void GetChildren(List<Guid> nodes, Guid id)
  111. {
  112. nodes.Add(id);
  113. var children = GetChilden(id);
  114. foreach (var child in children)
  115. GetChildren(nodes, child.ID);
  116. }
  117. public ObservableCollection<DynamicTreeNode> GetChilden(Guid id)
  118. {
  119. return new ObservableCollection<DynamicTreeNode>(_nodes.Where(x => x.Parent.Equals(id) && (x.ID != id)));
  120. }
  121. public void Load<T>(CoreTable table, Expression<Func<T, Guid>> id, Expression<Func<T, Guid>> parentid, Expression<Func<T, String>> description)
  122. {
  123. _nodes.Clear();
  124. foreach (var row in table.Rows)
  125. {
  126. Guid _id = row.Get<T, Guid>(id);
  127. Guid _parent = row.Get<T, Guid>(parentid);
  128. String _description = row.Get<T, String>(description);
  129. Add(_id, _parent).Description = _description;
  130. }
  131. }
  132. }
  133. }