Advertisement
MIkeKaye

NewPage1.xaml.cs

Jan 20th, 2025
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.81 KB | Source Code | 0 0
  1. using System.Collections.ObjectModel;
  2. using System.ComponentModel;
  3. using System.Diagnostics;
  4. using System.Runtime.CompilerServices;
  5. using System.Windows.Input;
  6. using CommunityToolkit.Mvvm.ComponentModel;
  7. using Microsoft.Maui.Platform;
  8. namespace MM2.Views;
  9. public partial class NewPage1 : ContentPage
  10. {
  11. public partial class item : ObservableObject
  12. {
  13. //[ObservableProperty]
  14. //public ObservableCollection<item> items;
  15.  
  16. //[ObservableProperty] public bool isSelected;
  17. //[ObservableProperty] public Color backgroundColor;
  18. [ObservableProperty] public string acct;
  19. [ObservableProperty] public decimal amount;
  20.  
  21. public int tag;
  22.  
  23. public event PropertyChangedEventHandler PropertyChanged;
  24. protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
  25. {
  26. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  27. }
  28. }
  29. public partial class MyViewModel : ObservableObject
  30. {
  31. //[ObservableProperty] public item? itemMaybe { get; set; }
  32. //[ObservableProperty] public DateOnly theDate { get; set; }
  33. // [ObservableProperty] int anInt;
  34.  
  35. item _SelectedItem;
  36. public item SelectedItem
  37. {
  38. get { return _SelectedItem; }
  39. set
  40. {
  41. if (_SelectedItem == value) return;
  42. _SelectedItem = value;
  43. //OnPropertyChanged();
  44. } // OnPropertyChanged needed/okay/bad?
  45. }
  46.  
  47. private DateTime _theDate;
  48. public DateTime theDate
  49. {
  50. get { return _theDate; }
  51. set
  52. {
  53. if (_theDate == value) return;
  54. _theDate = value;
  55. OnPropertyChanged();
  56. }
  57. }
  58. private int _anInt;
  59. public int anInt
  60. {
  61. get { return _anInt; }
  62. set
  63. {
  64. if (_anInt == value) return;
  65. _anInt = value;
  66. OnPropertyChanged();
  67. }
  68. }
  69.  
  70. private item _itemMaybe;
  71. public item itemMaybe
  72. {
  73. get { return _itemMaybe; }
  74. set
  75. {
  76. if (_itemMaybe == value) return;
  77. _itemMaybe = value;
  78. OnPropertyChanged();
  79. }
  80. }
  81. public event PropertyChangedEventHandler PropertyChanged;
  82. protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
  83. {
  84. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  85. }
  86. }
  87.  
  88. public ObservableCollection<item> itemsOC { get; set; }
  89. item OneItem { get; set; }
  90. item OneMini { get; set; }
  91. item itemMaybe { get; set; }
  92. item SelectedItem { get; set; }
  93. //public DateOnly theDate { get; set; }
  94. string Q;
  95. MyTable tbl;
  96. int MiniIndex = 0;
  97. MyViewModel vm = new();
  98.  
  99. public NewPage1()
  100. {
  101. InitializeComponent();
  102. BindingContext = vm;
  103. //theDate = DateTime.Today;
  104. //anInt = 1234;
  105. Setup();
  106. }
  107. void Setup()
  108. {
  109. Q = "SELECT AcctName, AcctBalance FROM Assets;";
  110. tbl = new(Q);
  111. int NumRows = tbl.numRows; //3
  112. int NumCols = tbl.numCols; //2
  113.  
  114. itemsOC = new();
  115. BindingContext = this;
  116. for (int i = 0; i < NumRows; i++)
  117. {
  118. OneItem = new();
  119. string[] aRow = tbl.GetRow(i);
  120. OneItem.acct = aRow[0];
  121. OneItem.amount = Convert.ToDecimal(aRow[1]);
  122. OneItem.tag = i;
  123. //zzz OneItem.isSelected = false; //Not useful??
  124. //OneItem.backgroundColor = Colors.Grey;
  125. //OneItem.backgroundColor = (Color)Resources["ColorUnSel"];
  126. itemsOC.Add(OneItem);
  127. }
  128. }
  129. private void SelectItemByIndex(object sender, EventArgs e)
  130. {
  131. // Not used?
  132. MiniView.SelectedItem = itemsOC[MiniIndex];
  133. }
  134. void DummyForNow(object o, EventArgs e)
  135. {
  136.  
  137. }
  138. // fake private item for itemsClicked()
  139. // No longer used: item PriorItem = null;
  140. private void itemsTapped(object s, EventArgs e)
  141. {
  142. // aparently s = ItemsSource
  143. if (SelectedItem is null)
  144. {
  145. return;
  146. }
  147. OneMini = itemMaybe;
  148.  
  149. int tag = itemMaybe.tag;
  150. int index = itemsOC.IndexOf(itemsOC.Where
  151. (X => X.tag == tag).FirstOrDefault());
  152. if (index == -1)
  153. {
  154. return; // trap this line for debug
  155. }
  156. // index is valid
  157. MiniIndex = 2;// = index;DEBUG!!
  158. //make it look selected
  159. //OneMini.backgroundColor = (Color)Resources["ColorSel"];
  160. //OneMini.isSelected = true;
  161.  
  162. //MiniView.SelectedItem = itemsOC[2];
  163.  
  164. #if false // make the prior one look unsected
  165. // try removing this
  166. if (PriorItem is not null)
  167. {
  168. PriorItem.backgroundColor =
  169. (Color)Resources["ColorUnSel"];
  170. PriorItem.isSelected = false;
  171. PriorItem = itemMaybe; // for next time
  172. }
  173. #endif
  174. SetButtonsVisible(); // up/down/remove
  175. silli.Text = "ABC";
  176. var zzz = silli.BackgroundColor;
  177. // both of these were null
  178. var q1 = silli.Command;
  179. var q2 = silli.CommandParameter;
  180. } // END itemsTapped(object s, EventArgs e)
  181. void click(object sender, EventArgs e)
  182. {
  183. int c = itemsOC.Count;
  184. if (c < 1) return;
  185. // swap bottom to top
  186. itemsOC.Move(c - 1, 0);
  187. // reverse the above
  188. itemsOC.Move(0, c - 1);
  189. }
  190. void SetButtonsVisible()
  191. {
  192. int max = itemsOC.Count - 1;
  193. if (max < 0)
  194. {
  195. bDel.IsVisible = false;
  196. return;
  197. }
  198. bTop.IsVisible = true;
  199. bDwn.IsVisible = true;
  200. bDel.IsVisible = true;
  201. bUp.IsVisible = true;
  202. if (MiniIndex == 0) bTop.IsVisible = false;
  203. if (MiniIndex == 0) bUp.IsVisible = false;
  204. if (MiniIndex >= max) bDwn.IsVisible = false;
  205. }
  206. void clickDown(object s, EventArgs e)
  207. {
  208. itemsOC.Move(MiniIndex, MiniIndex+1);
  209. MiniIndex++;
  210. MiniView.SelectedItem = itemsOC[MiniIndex];
  211. }
  212. void clickToTop(object s, EventArgs e)
  213. {
  214. itemsOC.Move(MiniIndex, 0);
  215. MiniIndex = 0;
  216. MiniView.SelectedItem = itemsOC[MiniIndex];
  217. }
  218. void clickDelete(object s, EventArgs e)
  219. {
  220. itemsOC.RemoveAt(MiniIndex);
  221. MiniIndex = -1;
  222. }
  223. void clickUp(object s, EventArgs e)
  224. {
  225. itemsOC.Move(MiniIndex, MiniIndex-1);
  226. MiniIndex--;
  227. MiniView.SelectedItem = itemsOC[MiniIndex];
  228. }
  229. } // END partial class NewPage1
  230.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement