DynamicTreeNodes.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. }
  71. public class DynamicTreeNodes
  72. {
  73. private List<DynamicTreeNode> _nodes;
  74. public ObservableCollection<DynamicTreeNode> Nodes => new ObservableCollection<DynamicTreeNode>(_nodes.Where(x => x.Parent == Guid.Empty));
  75. public DynamicTreeNode? this[Guid id] => _nodes.FirstOrDefault(x => x.ID == id);
  76. public DynamicTreeNodes()
  77. {
  78. _nodes = new List<DynamicTreeNode>();
  79. }
  80. public DynamicTreeNode Add(Guid id, Guid parent)
  81. {
  82. var node = new DynamicTreeNode(this, id, parent);
  83. _nodes.Add(node);
  84. return node;
  85. }
  86. public DynamicTreeNode Find(Guid id) => _nodes.FirstOrDefault(x => x.ID == id);
  87. public void GetChildren(List<Guid> nodes, Guid id)
  88. {
  89. nodes.Add(id);
  90. var children = GetChilden(id);
  91. foreach (var child in children)
  92. GetChildren(nodes, child.ID);
  93. }
  94. public ObservableCollection<DynamicTreeNode> GetChilden(Guid id)
  95. {
  96. return new ObservableCollection<DynamicTreeNode>(_nodes.Where(x => x.Parent.Equals(id) && (x.ID != id)));
  97. }
  98. public void Load<T>(CoreTable table, Expression<Func<T, Guid>> id, Expression<Func<T, Guid>> parentid, Expression<Func<T, String>> description)
  99. {
  100. _nodes.Clear();
  101. foreach (var row in table.Rows)
  102. {
  103. Guid _id = row.Get<T, Guid>(id);
  104. Guid _parent = row.Get<T, Guid>(parentid);
  105. String _description = row.Get<T, String>(description);
  106. Add(_id, _parent).Description = _description;
  107. }
  108. }
  109. }
  110. }