Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using BraccoApp.Controller;
- using BraccoApp.Model;
- using BraccoApp.Utils;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.Linq;
- using System.Runtime.CompilerServices;
- using System.Text;
- using System.Threading.Tasks;
- using Xamarin.Forms;
- using Xamarin.Forms.Xaml;
- namespace BraccoApp.View {
- [XamlCompilation(XamlCompilationOptions.Compile)]
- public partial class MenuView : ContentView {
- public static MenuView ctx;
- public static Menu Root;
- public static List<Menu> MenuList = new List<Menu>();
- public static int CurrentLevel;
- public static Menu CurrentMenu;
- public static double MenuWidth = 300;
- public static Button HomeButton;
- private static MenuItemHead currentSelected;
- private static bool tapped = false;
- public Dictionary<string, Menu> MenuMap = new Dictionary<string, Menu>();
- public MenuView() {
- InitializeComponent();
- ctx = this;
- //menuWrapper.IsClippedToBounds = true;
- if (App.Current.Properties.ContainsKey("user_logged")) {
- User user = (User)App.Current.Properties["user_logged"];
- if (user.CanTestMode) {
- switchContainer.IsVisible = true;
- this.testSwitch.IsToggled = ((bool)App.Current.Properties["test_mode"]);
- this.testSwitch.Toggled += async (object sender, ToggledEventArgs e) => {
- //if (App.Current.Properties.ContainsKey("test_mode")) {
- // App.Current.Properties["test_mode"] = !((bool)App.Current.Properties["test_mode"]);
- //} else
- App.Current.Properties["test_mode"] = e.Value;
- LoadMenusAsync();
- };
- }
- }
- var closeTapped = new TapGestureRecognizer();
- closeTapped.Tapped += (s, e) => {
- HomePage.HideMenu();
- };
- closeButton.GestureRecognizers.Add(closeTapped);
- backButton.Clicked += backEventHandler;
- HomeButton = homeButton;
- HomeButton.Clicked += homeEventHandler;
- /* MUST BE CALLED FROM OUTSIDE */
- //LoadMenusAsync();
- }
- /**
- * Metodo per popolare il menù
- */
- /* MUST BE CALLED FROM OUTSIDE */
- public static async Task LoadMenusAsync() {
- try {
- var docs = DocumentController.Documents;
- //var docs = DocumentController.GetDocumentsByUuidParent(null);
- //Menu root = new Menu("HOME", docs, "0");
- //ctx.MenuMap.Add("0", root);
- //List<Menu> menus = new List<Menu>();
- //List<Document> notOpen = docs.Where(d => d.IsFolder && !d.IsFolderOpen).ToList();
- //foreach (Document d in notOpen) {
- // menus.AddRange(GenMenus(d));
- //}
- await GenMenusPlain(docs);
- Menu root;
- if (!ctx.MenuMap.TryGetValue("0", out root)) {
- // the key isn't in the dictionary.
- return; // or whatever you want to do
- }
- ctx.menuContainer.Children.Clear();
- ctx.menuContainer.Children.Add(root,
- Constraint.Constant(0),
- Constraint.Constant(0),
- Constraint.RelativeToParent((parent) => {
- return (parent.Width);
- })
- );
- await root.LayoutTo(new Rectangle(0, 0, ctx.Width, ctx.Height), 500, Easing.CubicIn);
- ctx.docName.Text = root.Name;
- //ctx.backButton.GestureRecognizers.Clear();
- ctx.backButton.IsVisible = false;
- CurrentMenu = root;
- Root = root;
- //await SetRoot(new Menu("HOME", docs, null));
- //await DownloadMenuImageAndIcon();
- //StartFilesDownload();
- } catch (Exception e) {
- Utils.Utils.Log("Menuview.LoadMenusAsync(): " + e.Message);
- await LogController.AddErrorLog(e.Message, e.StackTrace);
- }
- }
- public static async Task GenMenusPlain(List<Document> documents) {
- try {
- Menu root = new Menu("HOME", documents.Where(d => d.Uuidp == null).ToList(), "0");
- ctx.MenuMap.TryAdd("0", root);
- IEnumerable<Document> notOpenFolders = documents.Where(d => d.IsFolder && !d.IsFolderOpen && d.HasChilds).Distinct(); //&& d.hasChilds
- foreach (Document folder in notOpenFolders) {
- IEnumerable<Document> children = documents.Where(d => d.Uuidp == folder.Uuid);
- if (children.Any()) {
- Menu menu = new Menu(folder.Code, children.ToList(), folder.Uuid);
- if (!ctx.MenuMap.TryAdd(folder.Uuid, menu)) continue;
- }
- }
- var map = ctx.MenuMap.ToList();
- } catch (Exception e) {
- Utils.Utils.Log("Menuview.GenMenusPlain(): " + e.Message);
- await LogController.AddErrorLog(e.Message, e.StackTrace);
- }
- }
- public static List<Menu> GenMenus(Document document) {
- List<Menu> menus = new List<Menu>();
- if (document.Documents != null && document.Documents.Any()) {
- Menu menu = new Menu(document.Code, document.Documents.ToList(), document.Uuid);
- ctx.MenuMap.Add(document.Uuid, menu);
- menus.Add(menu);
- foreach (Document doc in document.Documents.ToList().Where(d => d.IsFolder && !d.IsFolderOpen)) {
- List<Menu> children = GenMenus(doc);
- menus.AddRange(children);
- }
- }
- return menus;
- }
- public static async Task Selected(MenuItemHead head) {
- MenuItemHead current = currentSelected;
- if (current != null) current.BackgroundColor = Color.Transparent;
- head.BackgroundColor = Color.FromHex("#0055A1");
- currentSelected = head;
- }
- public static async Task SetRoot(Menu root) {
- ctx.menuContainer.Children.Clear();
- ctx.menuContainer.Children.Add(root,
- Constraint.Constant(0),
- Constraint.Constant(0),
- Constraint.RelativeToParent((parent) => {
- return (parent.Width);
- })
- );
- await root.LayoutTo(new Rectangle(0, 0, ctx.Width, ctx.Height), 500, Easing.CubicIn);
- ctx.docName.Text = root.Name;
- //ctx.backButton.GestureRecognizers.Clear();
- ctx.backButton.IsVisible = false;
- MenuList.Clear();
- MenuList.Add(root);
- Root = root;
- CurrentLevel = 0;
- CurrentMenu = Root;
- }
- /*
- public static async Task Push(Menu menu) {
- ctx.menuContainer.Children.Add(menu,
- Constraint.RelativeToParent((parent) => {
- return (parent.Width);
- }),
- Constraint.Constant(0),
- Constraint.RelativeToParent((parent) => {
- return (parent.Width);
- })
- );
- if (menu.Parent != null) {
- ctx.docName.Text = menu.Name.ToUpper();
- var homeTapped = new TapGestureRecognizer();
- homeTapped.Tapped += async (s, e) => {
- HomePage.ShowHome();
- await ToRoot();
- };
- ctx.homeButton.GestureRecognizers.Add(homeTapped);
- ctx.homeButton.IsVisible = true;
- var backTapped = new TapGestureRecognizer();
- backTapped.Tapped += async (s, e) => {
- await Pop();
- };
- ctx.backButton.GestureRecognizers.Add(backTapped);
- ctx.backButton.IsVisible = true;
- }
- Menu parentMenu = CurrentMenu;
- parentMenu.LayoutTo(new Rectangle(-ctx.Width, 0, ctx.Width, ctx.Height), 500, Easing.CubicIn);
- await menu.TranslateTo(-ctx.Width, 0, 500, Easing.CubicIn);
- MenuList.Add(menu);
- CurrentMenu = menu;
- CurrentLevel++;
- }*/
- public static async Task Push(string uuidParent) {
- try {
- Menu menu;
- if (!ctx.MenuMap.TryGetValue(uuidParent, out menu)) {
- // the key isn't in the dictionary.
- return; // or whatever you want to do
- }
- ctx.menuContainer.Children.Add(menu,
- Constraint.RelativeToParent((parent) => {
- return (parent.Width);
- }),
- Constraint.Constant(0),
- Constraint.RelativeToParent((parent) => {
- return (parent.Width);
- })
- );
- if (uuidParent != "0" && uuidParent != null && uuidParent != "null") {
- ctx.backButton.IsVisible = true;
- ctx.docName.Text = menu.Name.ToUpper();
- ctx.docName.IsVisible = true;
- }
- Menu current = CurrentMenu;
- //parentMenu.LayoutTo(new Rectangle(-300, 0, 300, ctx.Height), 500, Easing.CubicIn);
- //parentMenu.TranslateTo(-300, 0, 500, Easing.CubicIn);
- //await menu.LayoutTo(new Rectangle(300, 0, 300, ctx.Height), 1, Easing.CubicIn);
- ctx.menuWrapper.ScrollToAsync(0, 0, true);
- current.IsVisible = false;
- //await menu.TranslateTo(300, 0, 1, Easing.CubicIn);
- await menu.TranslateTo(-300, 0, 500, Easing.CubicIn);
- menu.IsVisible = true;
- CurrentMenu = menu;
- } catch (Exception e) {
- Utils.Utils.Log("MenuView.Push(): " + e.Message);
- await LogController.AddErrorLog(e.Message, e.StackTrace);
- }
- }
- public static async Task Pop2() {
- try {
- Menu current = CurrentMenu;
- Menu parentMenu;
- String uuidParentParent = DocumentController.GetDocumentByUuid(current.uuidParent).Uuidp;
- if (uuidParentParent == null) uuidParentParent = "0";
- Dictionary<string, Menu> ddd = ctx.MenuMap;
- if (!ctx.MenuMap.TryGetValue(uuidParentParent, out parentMenu)) {
- // the key isn't in the dictionary.
- return; // or whatever you want to do
- }
- ctx.menuWrapper.ScrollToAsync(0, 0, true);
- await current.TranslateTo(300, 0, 500, Easing.CubicIn);
- parentMenu.IsVisible = true;
- //parentMenu.TranslateTo(0, 0, 500, Easing.CubicIn);
- //parentMenu.LayoutTo(new Rectangle(0, 0, 300, ctx.Height), 500, Easing.CubicIn);
- //ctx.menuContainer.Children.Remove(CurrentMenu);
- CurrentMenu = parentMenu;
- if (CurrentMenu.uuidParent != null && CurrentMenu.uuidParent != "0" && CurrentMenu.uuidParent != "null") {
- ctx.docName.Text = CurrentMenu.Name.ToUpper();
- //var backTapped = new TapGestureRecognizer();
- //backTapped.Tapped += async (s, e) => {
- // await Pop2();
- //};
- //ctx.backButton.GestureRecognizers.Clear();
- //ctx.backButton.GestureRecognizers.Add(backTapped);
- ctx.backButton.IsVisible = true;
- } else {
- ctx.docName.Text = "HOME";
- //ctx.backButton.GestureRecognizers.Clear();
- ctx.backButton.IsVisible = false;
- }
- } catch (Exception e) {
- Utils.Utils.Log("MenuView.Pop(): " + e.Message);
- await LogController.AddErrorLog(e.Message, e.StackTrace);
- }
- }
- public static async Task ToRoot() {
- try {
- Menu current = CurrentMenu;
- Menu parentMenu;
- if (!ctx.MenuMap.TryGetValue("0", out parentMenu)) {
- // the key isn't in the dictionary.
- return; // or whatever you want to do
- }
- ctx.menuWrapper.ScrollToAsync(0, 0, true);
- await current.TranslateTo(300, 0, 500, Easing.CubicIn);
- parentMenu.IsVisible = true;
- CurrentMenu = parentMenu;
- ctx.docName.Text = "HOME";
- ctx.backButton.IsVisible = false;
- } catch (Exception e) {
- Utils.Utils.Log("MenuView.ToRoot(): " + e.Message);
- await LogController.AddErrorLog(e.Message, e.StackTrace);
- }
- }
- private static async void backEventHandler(object sender, EventArgs args) {
- try {
- if (!tapped) {
- tapped = true;
- await Pop2();
- tapped = false;
- }
- } catch (Exception e) {
- Utils.Utils.Log("MenuView.backEventHandler(): " + e.Message);
- await LogController.AddErrorLog(e.Message, e.StackTrace);
- }
- }
- private static async void homeEventHandler(object sender, EventArgs args) {
- try {
- if (!tapped) {
- tapped = true;
- Document home = DocumentController.GetDocumentHome();
- if (home != null)
- await HomePage.OpenDocument(DocumentController.GetDocumentHome().File.FilePath, false, false);
- if (ctx.docName.Text != "HOME") await ToRoot();
- tapped = false;
- }
- } catch (Exception e) {
- Utils.Utils.Log("MenuView.homeEventHandler(): " + e.Message);
- await LogController.AddErrorLog(e.Message, e.StackTrace);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement