Advertisement
Guest User

MenuView.cs

a guest
Jan 20th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.34 KB | None | 0 0
  1. using BraccoApp.Controller;
  2. using BraccoApp.Model;
  3. using BraccoApp.Utils;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Collections.ObjectModel;
  7. using System.ComponentModel;
  8. using System.Linq;
  9. using System.Runtime.CompilerServices;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12.  
  13. using Xamarin.Forms;
  14. using Xamarin.Forms.Xaml;
  15.  
  16. namespace BraccoApp.View {
  17. [XamlCompilation(XamlCompilationOptions.Compile)]
  18. public partial class MenuView : ContentView {
  19.  
  20. public static MenuView ctx;
  21. public static Menu Root;
  22. public static List<Menu> MenuList = new List<Menu>();
  23. public static int CurrentLevel;
  24. public static Menu CurrentMenu;
  25. public static double MenuWidth = 300;
  26. public static Button HomeButton;
  27.  
  28. private static MenuItemHead currentSelected;
  29. private static bool tapped = false;
  30.  
  31. public Dictionary<string, Menu> MenuMap = new Dictionary<string, Menu>();
  32.  
  33. public MenuView() {
  34. InitializeComponent();
  35.  
  36. ctx = this;
  37. //menuWrapper.IsClippedToBounds = true;
  38.  
  39. if (App.Current.Properties.ContainsKey("user_logged")) {
  40. User user = (User)App.Current.Properties["user_logged"];
  41. if (user.CanTestMode) {
  42. switchContainer.IsVisible = true;
  43.  
  44. this.testSwitch.IsToggled = ((bool)App.Current.Properties["test_mode"]);
  45. this.testSwitch.Toggled += async (object sender, ToggledEventArgs e) => {
  46. //if (App.Current.Properties.ContainsKey("test_mode")) {
  47. // App.Current.Properties["test_mode"] = !((bool)App.Current.Properties["test_mode"]);
  48. //} else
  49. App.Current.Properties["test_mode"] = e.Value;
  50. LoadMenusAsync();
  51. };
  52. }
  53. }
  54.  
  55. var closeTapped = new TapGestureRecognizer();
  56. closeTapped.Tapped += (s, e) => {
  57. HomePage.HideMenu();
  58. };
  59. closeButton.GestureRecognizers.Add(closeTapped);
  60.  
  61. backButton.Clicked += backEventHandler;
  62.  
  63. HomeButton = homeButton;
  64. HomeButton.Clicked += homeEventHandler;
  65.  
  66. /* MUST BE CALLED FROM OUTSIDE */
  67. //LoadMenusAsync();
  68. }
  69.  
  70. /**
  71. * Metodo per popolare il menù
  72. */
  73. /* MUST BE CALLED FROM OUTSIDE */
  74. public static async Task LoadMenusAsync() {
  75. try {
  76. var docs = DocumentController.Documents;
  77. //var docs = DocumentController.GetDocumentsByUuidParent(null);
  78.  
  79. //Menu root = new Menu("HOME", docs, "0");
  80. //ctx.MenuMap.Add("0", root);
  81.  
  82. //List<Menu> menus = new List<Menu>();
  83. //List<Document> notOpen = docs.Where(d => d.IsFolder && !d.IsFolderOpen).ToList();
  84. //foreach (Document d in notOpen) {
  85. // menus.AddRange(GenMenus(d));
  86. //}
  87.  
  88. await GenMenusPlain(docs);
  89.  
  90. Menu root;
  91. if (!ctx.MenuMap.TryGetValue("0", out root)) {
  92. // the key isn't in the dictionary.
  93. return; // or whatever you want to do
  94. }
  95.  
  96. ctx.menuContainer.Children.Clear();
  97. ctx.menuContainer.Children.Add(root,
  98. Constraint.Constant(0),
  99. Constraint.Constant(0),
  100. Constraint.RelativeToParent((parent) => {
  101. return (parent.Width);
  102. })
  103. );
  104. await root.LayoutTo(new Rectangle(0, 0, ctx.Width, ctx.Height), 500, Easing.CubicIn);
  105.  
  106. ctx.docName.Text = root.Name;
  107.  
  108. //ctx.backButton.GestureRecognizers.Clear();
  109. ctx.backButton.IsVisible = false;
  110.  
  111. CurrentMenu = root;
  112. Root = root;
  113.  
  114. //await SetRoot(new Menu("HOME", docs, null));
  115. //await DownloadMenuImageAndIcon();
  116. //StartFilesDownload();
  117. } catch (Exception e) {
  118. Utils.Utils.Log("Menuview.LoadMenusAsync(): " + e.Message);
  119. await LogController.AddErrorLog(e.Message, e.StackTrace);
  120. }
  121. }
  122.  
  123. public static async Task GenMenusPlain(List<Document> documents) {
  124. try {
  125. Menu root = new Menu("HOME", documents.Where(d => d.Uuidp == null).ToList(), "0");
  126. ctx.MenuMap.TryAdd("0", root);
  127.  
  128. IEnumerable<Document> notOpenFolders = documents.Where(d => d.IsFolder && !d.IsFolderOpen && d.HasChilds).Distinct(); //&& d.hasChilds
  129. foreach (Document folder in notOpenFolders) {
  130. IEnumerable<Document> children = documents.Where(d => d.Uuidp == folder.Uuid);
  131. if (children.Any()) {
  132. Menu menu = new Menu(folder.Code, children.ToList(), folder.Uuid);
  133. if (!ctx.MenuMap.TryAdd(folder.Uuid, menu)) continue;
  134. }
  135. }
  136. var map = ctx.MenuMap.ToList();
  137. } catch (Exception e) {
  138. Utils.Utils.Log("Menuview.GenMenusPlain(): " + e.Message);
  139. await LogController.AddErrorLog(e.Message, e.StackTrace);
  140. }
  141. }
  142.  
  143. public static List<Menu> GenMenus(Document document) {
  144.  
  145. List<Menu> menus = new List<Menu>();
  146.  
  147. if (document.Documents != null && document.Documents.Any()) {
  148. Menu menu = new Menu(document.Code, document.Documents.ToList(), document.Uuid);
  149. ctx.MenuMap.Add(document.Uuid, menu);
  150.  
  151. menus.Add(menu);
  152. foreach (Document doc in document.Documents.ToList().Where(d => d.IsFolder && !d.IsFolderOpen)) {
  153. List<Menu> children = GenMenus(doc);
  154. menus.AddRange(children);
  155. }
  156. }
  157.  
  158. return menus;
  159. }
  160.  
  161. public static async Task Selected(MenuItemHead head) {
  162. MenuItemHead current = currentSelected;
  163. if (current != null) current.BackgroundColor = Color.Transparent;
  164. head.BackgroundColor = Color.FromHex("#0055A1");
  165. currentSelected = head;
  166. }
  167.  
  168. public static async Task SetRoot(Menu root) {
  169. ctx.menuContainer.Children.Clear();
  170. ctx.menuContainer.Children.Add(root,
  171. Constraint.Constant(0),
  172. Constraint.Constant(0),
  173. Constraint.RelativeToParent((parent) => {
  174. return (parent.Width);
  175. })
  176. );
  177. await root.LayoutTo(new Rectangle(0, 0, ctx.Width, ctx.Height), 500, Easing.CubicIn);
  178.  
  179. ctx.docName.Text = root.Name;
  180.  
  181. //ctx.backButton.GestureRecognizers.Clear();
  182. ctx.backButton.IsVisible = false;
  183.  
  184. MenuList.Clear();
  185. MenuList.Add(root);
  186.  
  187. Root = root;
  188. CurrentLevel = 0;
  189. CurrentMenu = Root;
  190. }
  191.  
  192. /*
  193. public static async Task Push(Menu menu) {
  194. ctx.menuContainer.Children.Add(menu,
  195. Constraint.RelativeToParent((parent) => {
  196. return (parent.Width);
  197. }),
  198. Constraint.Constant(0),
  199. Constraint.RelativeToParent((parent) => {
  200. return (parent.Width);
  201. })
  202. );
  203.  
  204. if (menu.Parent != null) {
  205. ctx.docName.Text = menu.Name.ToUpper();
  206.  
  207.  
  208. var homeTapped = new TapGestureRecognizer();
  209. homeTapped.Tapped += async (s, e) => {
  210. HomePage.ShowHome();
  211. await ToRoot();
  212. };
  213. ctx.homeButton.GestureRecognizers.Add(homeTapped);
  214. ctx.homeButton.IsVisible = true;
  215.  
  216. var backTapped = new TapGestureRecognizer();
  217. backTapped.Tapped += async (s, e) => {
  218. await Pop();
  219. };
  220. ctx.backButton.GestureRecognizers.Add(backTapped);
  221. ctx.backButton.IsVisible = true;
  222. }
  223.  
  224. Menu parentMenu = CurrentMenu;
  225. parentMenu.LayoutTo(new Rectangle(-ctx.Width, 0, ctx.Width, ctx.Height), 500, Easing.CubicIn);
  226. await menu.TranslateTo(-ctx.Width, 0, 500, Easing.CubicIn);
  227.  
  228. MenuList.Add(menu);
  229. CurrentMenu = menu;
  230. CurrentLevel++;
  231. }*/
  232.  
  233. public static async Task Push(string uuidParent) {
  234. try {
  235. Menu menu;
  236. if (!ctx.MenuMap.TryGetValue(uuidParent, out menu)) {
  237. // the key isn't in the dictionary.
  238. return; // or whatever you want to do
  239. }
  240.  
  241. ctx.menuContainer.Children.Add(menu,
  242. Constraint.RelativeToParent((parent) => {
  243. return (parent.Width);
  244. }),
  245. Constraint.Constant(0),
  246. Constraint.RelativeToParent((parent) => {
  247. return (parent.Width);
  248. })
  249. );
  250.  
  251. if (uuidParent != "0" && uuidParent != null && uuidParent != "null") {
  252.  
  253. ctx.backButton.IsVisible = true;
  254.  
  255. ctx.docName.Text = menu.Name.ToUpper();
  256. ctx.docName.IsVisible = true;
  257. }
  258.  
  259. Menu current = CurrentMenu;
  260. //parentMenu.LayoutTo(new Rectangle(-300, 0, 300, ctx.Height), 500, Easing.CubicIn);
  261. //parentMenu.TranslateTo(-300, 0, 500, Easing.CubicIn);
  262. //await menu.LayoutTo(new Rectangle(300, 0, 300, ctx.Height), 1, Easing.CubicIn);
  263. ctx.menuWrapper.ScrollToAsync(0, 0, true);
  264.  
  265. current.IsVisible = false;
  266. //await menu.TranslateTo(300, 0, 1, Easing.CubicIn);
  267. await menu.TranslateTo(-300, 0, 500, Easing.CubicIn);
  268. menu.IsVisible = true;
  269.  
  270. CurrentMenu = menu;
  271. } catch (Exception e) {
  272. Utils.Utils.Log("MenuView.Push(): " + e.Message);
  273. await LogController.AddErrorLog(e.Message, e.StackTrace);
  274. }
  275. }
  276.  
  277. public static async Task Pop2() {
  278. try {
  279. Menu current = CurrentMenu;
  280.  
  281. Menu parentMenu;
  282. String uuidParentParent = DocumentController.GetDocumentByUuid(current.uuidParent).Uuidp;
  283. if (uuidParentParent == null) uuidParentParent = "0";
  284. Dictionary<string, Menu> ddd = ctx.MenuMap;
  285. if (!ctx.MenuMap.TryGetValue(uuidParentParent, out parentMenu)) {
  286. // the key isn't in the dictionary.
  287. return; // or whatever you want to do
  288. }
  289.  
  290. ctx.menuWrapper.ScrollToAsync(0, 0, true);
  291. await current.TranslateTo(300, 0, 500, Easing.CubicIn);
  292. parentMenu.IsVisible = true;
  293. //parentMenu.TranslateTo(0, 0, 500, Easing.CubicIn);
  294.  
  295. //parentMenu.LayoutTo(new Rectangle(0, 0, 300, ctx.Height), 500, Easing.CubicIn);
  296.  
  297. //ctx.menuContainer.Children.Remove(CurrentMenu);
  298. CurrentMenu = parentMenu;
  299.  
  300. if (CurrentMenu.uuidParent != null && CurrentMenu.uuidParent != "0" && CurrentMenu.uuidParent != "null") {
  301. ctx.docName.Text = CurrentMenu.Name.ToUpper();
  302.  
  303. //var backTapped = new TapGestureRecognizer();
  304. //backTapped.Tapped += async (s, e) => {
  305. // await Pop2();
  306. //};
  307. //ctx.backButton.GestureRecognizers.Clear();
  308. //ctx.backButton.GestureRecognizers.Add(backTapped);
  309. ctx.backButton.IsVisible = true;
  310. } else {
  311. ctx.docName.Text = "HOME";
  312.  
  313. //ctx.backButton.GestureRecognizers.Clear();
  314. ctx.backButton.IsVisible = false;
  315. }
  316. } catch (Exception e) {
  317. Utils.Utils.Log("MenuView.Pop(): " + e.Message);
  318. await LogController.AddErrorLog(e.Message, e.StackTrace);
  319. }
  320. }
  321.  
  322. public static async Task ToRoot() {
  323. try {
  324. Menu current = CurrentMenu;
  325.  
  326. Menu parentMenu;
  327.  
  328. if (!ctx.MenuMap.TryGetValue("0", out parentMenu)) {
  329. // the key isn't in the dictionary.
  330. return; // or whatever you want to do
  331. }
  332.  
  333. ctx.menuWrapper.ScrollToAsync(0, 0, true);
  334. await current.TranslateTo(300, 0, 500, Easing.CubicIn);
  335.  
  336. parentMenu.IsVisible = true;
  337.  
  338. CurrentMenu = parentMenu;
  339.  
  340. ctx.docName.Text = "HOME";
  341.  
  342. ctx.backButton.IsVisible = false;
  343.  
  344. } catch (Exception e) {
  345. Utils.Utils.Log("MenuView.ToRoot(): " + e.Message);
  346. await LogController.AddErrorLog(e.Message, e.StackTrace);
  347. }
  348. }
  349.  
  350. private static async void backEventHandler(object sender, EventArgs args) {
  351. try {
  352. if (!tapped) {
  353. tapped = true;
  354. await Pop2();
  355. tapped = false;
  356. }
  357. } catch (Exception e) {
  358. Utils.Utils.Log("MenuView.backEventHandler(): " + e.Message);
  359. await LogController.AddErrorLog(e.Message, e.StackTrace);
  360. }
  361. }
  362.  
  363. private static async void homeEventHandler(object sender, EventArgs args) {
  364. try {
  365. if (!tapped) {
  366. tapped = true;
  367. Document home = DocumentController.GetDocumentHome();
  368. if (home != null)
  369. await HomePage.OpenDocument(DocumentController.GetDocumentHome().File.FilePath, false, false);
  370. if (ctx.docName.Text != "HOME") await ToRoot();
  371. tapped = false;
  372. }
  373. } catch (Exception e) {
  374. Utils.Utils.Log("MenuView.homeEventHandler(): " + e.Message);
  375. await LogController.AddErrorLog(e.Message, e.StackTrace);
  376. }
  377. }
  378. }
  379. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement