Advertisement
Guest User

xamarin forms main page

a guest
Aug 30th, 2015
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 19.85 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using Xamarin.Forms;
  4. using ImageCircle.Forms.Plugin.Abstractions;
  5. using FormsApp.Controllers;
  6. using FormsApp.Model;
  7. using FormsApp.Views.Setup;
  8. using PanicModels;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Threading.Tasks;
  12. using Connectivity.Plugin;
  13. using FormsApp.Views.Setup.Tariff;
  14. using Newtonsoft.Json;
  15. using Geolocator.Plugin;
  16. using XLabs.Forms.Services;
  17. using XLabs.Ioc;
  18. using XLabs.Platform.Device;
  19.  
  20. namespace FormsApp.Views
  21. {
  22.  
  23.     public class MasterPage :  MasterDetailPage
  24.     {
  25.         private ListItemModel selected = null;
  26.         private Label header;
  27.         private RelativeLayout profileLayout;
  28.         private CircleImage profileImage;
  29.         private Image profileImageBackground;
  30.         public PanicPage detailPage;
  31.         private ContentPage masterPage;
  32.         private ListView listView;
  33.         private ProfilePage profilePage;
  34.         private TariffPage tariffPage;
  35.         private TermsPage termsPage;
  36.         private InstructionsPage instructionsPage;
  37.         private HelpPage helpPage;
  38.  
  39.         private ActivityIndicator busyIndicator;
  40.         public MasterPage(Profile profile)
  41.         {
  42.             var layout = new StackLayout
  43.             {
  44.                 VerticalOptions = LayoutOptions.FillAndExpand,
  45.                 HorizontalOptions = LayoutOptions.FillAndExpand,
  46.                 BackgroundColor = Color.FromRgb(242, 247, 252)
  47.             };
  48.             busyIndicator = new ActivityIndicator();
  49.             busyIndicator.VerticalOptions = LayoutOptions.Center;
  50.             busyIndicator.HorizontalOptions = LayoutOptions.Center;
  51.             layout.Children.Add(busyIndicator);
  52.             this.Master = new ContentPage
  53.             {
  54.                 Title = "Profile", // Title required!
  55.                 BackgroundColor = Color.FromRgb(242, 247, 252),
  56.                 Content = new StackLayout
  57.                 {
  58.                             BackgroundColor = Color.FromRgb(242, 247, 252),
  59.                     VerticalOptions = LayoutOptions.CenterAndExpand,
  60.                     HorizontalOptions = LayoutOptions.CenterAndExpand,
  61.                     Children =
  62.                         {
  63.                             layout
  64.                         }
  65.                 }
  66.             };
  67.             this.Detail = new ContentPage { BackgroundColor = Color.FromRgb(242, 247, 252), Content = layout };
  68.             LayoutHelper.Device = Resolver.Resolve<IDevice>();
  69.             LayoutHelper.FontManager = Resolver.Resolve<IFontManager>();
  70.             Load (profile);
  71.             busyIndicator.IsRunning = true;
  72.             busyIndicator.IsEnabled = true;
  73.         }
  74.  
  75.         private async Task Load(Profile profile)
  76.         {
  77.                 #region tab menu
  78.  
  79.  
  80.                 header = new Label
  81.                 {
  82.                     Text =
  83.                         !ProfileController.Instance.Logined
  84.                             ? "Вы не авторизованы"
  85.                             : (profile.profileModel.Name + "\n" + profile.profileModel.Phonenumber),
  86.                     Font = Font.SystemFontOfSize(LayoutHelper.GetHeaderFontSize(), FontAttributes.None),
  87.                     HorizontalOptions = LayoutOptions.Center,
  88.                     TextColor = Color.White,
  89.                     XAlign = TextAlignment.Center,
  90.                     YAlign = TextAlignment.Center,
  91.                     BackgroundColor = Color.Transparent,
  92.                     HeightRequest = LayoutHelper.GetHeaderHeight()
  93.                 };
  94.  
  95.                 profileImage = new CircleImage
  96.                 {
  97.                     HeightRequest = LayoutHelper.GetProfileImageSize(),
  98.                     WidthRequest = LayoutHelper.GetProfileImageSize(),
  99.                     HorizontalOptions = LayoutOptions.Fill,
  100.                     Aspect = Aspect.AspectFill,
  101.                     Source = ImageSource.FromFile("photoIcon.png"),
  102.                     BorderThickness = 2,
  103.                     BorderColor = Color.White,
  104.                     VerticalOptions = LayoutOptions.Center
  105.                 };
  106.  
  107.                 var profileButton = new Button
  108.                 {
  109.                     HeightRequest = LayoutHelper.GetProfileImageSize(),
  110.                     WidthRequest = LayoutHelper.GetProfileImageSize(),
  111.                     HorizontalOptions = LayoutOptions.Fill,
  112.                     VerticalOptions = LayoutOptions.Center,
  113.                     BackgroundColor = Color.Transparent,
  114.                 };
  115.  
  116.                 var profileButtonLayout = new RelativeLayout {HeightRequest = LayoutHelper.GetImageSideViewHeight()};
  117.                 profileButtonLayout.BackgroundColor = Color.Transparent;
  118.                 profileButtonLayout.Children.Add(profileImage,
  119.                     Constraint.Constant(0),
  120.                     Constraint.Constant(0));
  121.                 profileButtonLayout.Children.Add(profileButton,
  122.                     Constraint.Constant(0),
  123.                     Constraint.Constant(0));
  124.  
  125.                 profileLayout = new RelativeLayout {BackgroundColor = Color.FromRgb(149, 114, 236)};
  126.                 profileLayout.HeightRequest = LayoutHelper.GetImageSideViewHeight();
  127.                 profileImageBackground = new Image()
  128.                 {
  129.                     Source = "defaultBack.png"
  130.                 };
  131.  
  132.                 profileLayout.Children.Add(profileImageBackground,
  133.                     Constraint.Constant(0),
  134.                     Constraint.Constant(0),
  135.                     Constraint.RelativeToParent((parent) => { return parent.Width; }));
  136.  
  137.                 busyIndicator = null;
  138.                 busyIndicator = new ActivityIndicator();
  139.  
  140.  
  141.                 profileLayout.Children.Add(profileButtonLayout,
  142.                     Constraint.RelativeToParent(
  143.                         (parent) => { return (parent.Width - LayoutHelper.GetProfileImageSize())/2; }),
  144.                     Constraint.RelativeToParent(
  145.                         (parent) => { return (parent.Height - LayoutHelper.GetProfileImageSize())/2; }),
  146.                     Constraint.Constant(LayoutHelper.GetProfileImageSize()),
  147.                     Constraint.Constant(LayoutHelper.GetProfileImageSize()));
  148.  
  149.                 profileLayout.Children.Add(busyIndicator,
  150.                     Constraint.RelativeToParent(
  151.                         (parent) => { return (parent.Width - LayoutHelper.GetProfileImageSize()) / 2; }),
  152.                     Constraint.RelativeToParent(
  153.                         (parent) => { return (parent.Height - LayoutHelper.GetProfileImageSize()) / 2; }),
  154.                     Constraint.Constant(LayoutHelper.GetProfileImageSize()),
  155.                     Constraint.Constant(LayoutHelper.GetProfileImageSize()));
  156.  
  157.                 busyIndicator.IsRunning = false;
  158.                 busyIndicator.IsVisible = false;
  159.  
  160.                 profileLayout.Children.Add(header,
  161.                     Constraint.Constant(0),
  162.                     Constraint.RelativeToView(profileLayout, (layout, view) => { return view.HeightRequest - header.HeightRequest - 5; }),
  163.                     Constraint.RelativeToParent((parent) => { return parent.Width; }));
  164.  
  165.                 #endregion
  166.  
  167.                 // Assemble an array of NamedColor objects.
  168.  
  169.                 #region menu buttons
  170.  
  171.                 ListItemModel[] menuButtons =
  172.                 {
  173.                     new ListItemModel(@"AlarmMenu32.png", "Тревога"),
  174.                     new ListItemModel(@"Wallet32.png", "Тарифы"),
  175.                     new ListItemModel(@"UseInfo32.png", "Условия пользования"),
  176.                     new ListItemModel(@"InfoFilled32.png", "Инструкции"),
  177.                     new ListItemModel(@"Help32.png", "Помощь"),
  178.                     new ListItemModel(@"Exit25.png", "Выход")
  179.                 };
  180.  
  181.                 // Create ListView for the master page.
  182.                 listView = new ListView
  183.                 {
  184.                     ItemsSource = menuButtons,
  185.                     ItemTemplate = new DataTemplate(typeof (MenuButtonView))
  186.                 };
  187.  
  188.                 listView.RowHeight = LayoutHelper.GetMenuRowHeight();
  189.                 listView.BackgroundColor = Color.FromRgb(242, 247, 252);
  190.                 listView.SeparatorVisibility = SeparatorVisibility.None;
  191.                 listView.SeparatorColor = Color.Transparent;
  192.                 // Create the master page with the ListView.
  193.  
  194.                 #endregion
  195.  
  196.                 #region events
  197.  
  198.                 masterPage = new ContentPage
  199.                 {
  200.                     Title = "Profile", // Title required!
  201.                     BackgroundColor = Color.FromRgb(242, 247, 252),
  202.                     Content = new StackLayout
  203.                     {
  204.                         VerticalOptions = LayoutOptions.Start,
  205.                         Children =
  206.                         {
  207.                             profileLayout,
  208.                             listView
  209.                         }
  210.                     }
  211.                 };
  212.                 ProfileController.Instance.OnProfileUpdated += ProfileController_OnProfileUpdated;
  213.                 ProfileController.Instance.OnImageReceived += Profile_ImageUpdate;
  214.                 // Create the detail page using NamedColorPage
  215.                 detailPage = new PanicPage(this);
  216.                 profileButton.Clicked += async (sender, args) =>
  217.                 {
  218.                 try
  219.                 {
  220.                     if (selected != null)
  221.                     {
  222.                         selected.SetColors(false);
  223.                     }
  224.                     if (busyIndicator == null)
  225.                     {
  226.                         busyIndicator = new ActivityIndicator();
  227.                         profileLayout.Children.Add(busyIndicator,
  228.                             Constraint.RelativeToParent(
  229.                                 (parent) => { return (parent.Width - LayoutHelper.GetProfileImageSize()) / 2; }),
  230.                             Constraint.RelativeToParent(
  231.                                 (parent) => { return (parent.Height - LayoutHelper.GetProfileImageSize()) / 2; }),
  232.                             Constraint.Constant(LayoutHelper.GetProfileImageSize()),
  233.                             Constraint.Constant(LayoutHelper.GetProfileImageSize()));
  234.                     }
  235.                     if (ProfileController.Instance.Logined && !busyIndicator.IsRunning)
  236.                     {
  237.                         busyIndicator.IsRunning = true;
  238.                         busyIndicator.IsVisible = true;
  239.                         if (profilePage == null)
  240.                         {
  241.                             var lprofile = await ProfileController.Instance.GetProfile();
  242.                             // Set the BindingContext of the detail page.
  243.                             profilePage = new ProfilePage(lprofile);
  244.                             await Task.Delay(500);
  245.                             await profilePage.Load();
  246.                         }
  247.                         profilePage.UpdateImage();
  248.                         this.Detail = profilePage;
  249.                         // Show the detail page.
  250.                         this.IsPresented = true;
  251.                         await Task.Delay(500);
  252.                         busyIndicator.IsRunning = false;
  253.                         busyIndicator.IsVisible = false;
  254.                     }
  255.                     else if (!ProfileController.Instance.Logined)
  256.                     {
  257.                         Navigation.PushAsync(new LoginPage(detailPage.EnableService));
  258.                     }
  259.                 }
  260.                 catch(Exception ex)
  261.                 {
  262.                     string msg = ex.ToString();
  263.                 }
  264.                 };
  265.  
  266.                 // For Android & Windows Phone, provide a way to get back to the master page.я
  267.                 if (Device.OS != TargetPlatform.iOS)
  268.                 {
  269.                     TapGestureRecognizer tap = new TapGestureRecognizer();
  270.                     tap.Tapped += (sender, args) =>
  271.                     {
  272.                         this.IsPresented = true;
  273.                     };
  274.  
  275.                     detailPage.Content.GestureRecognizers.Add(tap);
  276.                 }
  277.  
  278.                 // Define a selected handler for the ListView.
  279.                 listView.ItemSelected += (sender, args) =>
  280.                 {
  281.                     try
  282.                     {
  283.  
  284.                         if (!this.IsPresented)
  285.                             return;
  286.                         this.IsPresented = false;
  287.                         if (selected != null)
  288.                         {
  289.                             selected.SetColors(false);
  290.                         }
  291.  
  292.                         // Select new
  293.                         selected = (listView.SelectedItem as ListItemModel);
  294.                         selected.SetColors(true);
  295.  
  296.                         if (selected.Name == "Тревога")
  297.                         {
  298.                             this.Detail = detailPage;
  299.                         }
  300.                         if (selected.Name == "Тарифы")
  301.                         {
  302.                             if (tariffPage == null)
  303.                                 tariffPage = new TariffPage(this);
  304.                             tariffPage.Load();
  305.                             this.Detail = tariffPage;
  306.                         }
  307.                         if (selected.Name == "Условия пользования")
  308.                         {
  309.                             if (termsPage == null) termsPage = new TermsPage(false);
  310.                             termsPage.Load();
  311.                             this.Detail = termsPage;
  312.                         }
  313.                         if (selected.Name == "Инструкции")
  314.                         {
  315.                             if (instructionsPage == null) instructionsPage = new InstructionsPage();
  316.                             instructionsPage.Load();
  317.                             this.Detail = instructionsPage;
  318.                         }
  319.                         if (selected.Name == "Помощь")
  320.                         {
  321.                             if (helpPage == null) helpPage = new HelpPage();
  322.                             this.Detail = helpPage;
  323.                         }
  324.                         if (selected.Name == "Выход")
  325.                         {
  326.                             Logout();
  327.                         }
  328.                         this.Detail.BindingContext = args.SelectedItem;
  329.  
  330.                         // Show the detail page.
  331.                         this.IsPresented = true;
  332.                     }
  333.                     catch (NullReferenceException exs)
  334.                     {
  335.                         string msg = exs.ToString();
  336.                     }
  337.                     catch (Exception ex)
  338.                     {
  339.                         string msg = ex.ToString();
  340.                     }
  341.                 };
  342.  
  343.                 // Initialize the ListView selection.
  344.                 listView.SelectedItem = menuButtons[0];
  345.  
  346.                 #endregion
  347.                 finished = false;
  348.                     Task.Factory.StartNew(async() =>
  349.                     {
  350.                         if (CrossConnectivity.Current.IsConnected && !ProfileController.Instance.Logined)
  351.                         {
  352.                             ProfileController.Instance.CheckSessionKey();
  353.                             if (ProfileController.Instance.Logined)
  354.                             {
  355.                                 await ProfileController.Instance.GetProfile();
  356.                                 if (ProfileController.Instance.IsActivated())
  357.                                     detailPage.EnableService(null, EventArgs.Empty);
  358.                                 else
  359.                                     FinishLoadPage();
  360.                             }
  361.                             else
  362.                             {
  363.                                 FinishLoadPage();
  364.                             }
  365.                         }
  366.                         else
  367.                         {
  368.                             FinishLoadPage();
  369.                         }
  370.                     });
  371.             /*
  372.             CrossGeolocator.Current.DesiredAccuracy = 50;
  373.             var position = await CrossGeolocator.Current.GetPositionAsync(timeout: 10000);
  374.             PanicController.OnGeoUpdated(position.Latitude, position.Longitude);*/
  375.         }
  376.  
  377.         private static bool finished = false;
  378.         public void FinishLoadPage()
  379.         {
  380.  
  381.             Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
  382.             {
  383.                 if (!finished)
  384.                 {
  385.                     this.Master = masterPage;
  386.                     this.Detail = detailPage;
  387.                     if (ProfileController.Instance.Logined)
  388.                         Profile_ImageUpdate(null, ProfileController.Instance.ProfileImage);
  389.  
  390.                 }
  391.                 finished = true;
  392.             });
  393.         }
  394.  
  395.         private void ProfileController_OnProfileUpdated(Profile profile)
  396.         {
  397.  
  398.             Xamarin.Forms.Device.BeginInvokeOnMainThread(async () =>
  399.             {
  400.                 if (ProfileController.Instance.Logined && profile != null)
  401.                 {
  402.                     header.Text = (profile.profileModel.Name + "\n" + profile.profileModel.Phonenumber);
  403.                     header.HeightRequest = LayoutHelper.GetHeaderHeight()*2;
  404.                     if (profile.profileModel.PaymentStatus == "[PUSH]")
  405.                     {
  406.                         var result = await DisplayAlert("ВНИМАНИЕ!",
  407.                             "На ваше счету недостаточно средств для вызова следующей тревоги, Ваш аккаунт будет деактивирован через час!",
  408.                                 "Оплатить", "ОК");
  409.                         if (result)
  410.                         {
  411.                             if (tariffPage == null)
  412.                                 tariffPage = new TariffPage(this);
  413.                             tariffPage.Load();
  414.                             this.Detail = tariffPage;
  415.                         }
  416.                     }
  417. /*               if (ProfileController.ProfileImage == null) return;
  418.                 profileImage.Source = null;
  419.                 profileImage.Source = ImageSource.FromStream(() => {return new MemoryStream(ProfileController.ProfileImage);});
  420.                 profileImageBackground.Source = null;
  421.                 profileImageBackground.Source = ImageSource.FromStream(() => {return new MemoryStream(ProfileController.ProfileImage);});
  422.                 profileLayout.HeightRequest = profileImageBackground.Width * 0.565656;*/
  423.                 }
  424.             });
  425.         }
  426.  
  427.         private void Profile_ImageUpdate(object o, ImageModel image)
  428.         {
  429.             if (finished)
  430.             {
  431.                 Xamarin.Forms.Device.BeginInvokeOnMainThread(async () =>
  432.                 {
  433.                     if (ProfileController.Instance.Logined && image != null)
  434.                     {
  435.                         profileImage.Source = null;
  436.                             profileImage.Source = ImageSource.FromStream(() => { return new MemoryStream(image.data); });
  437.                         profileImageBackground.Source = null;
  438.                             profileImageBackground.Source = ImageSource.FromStream(() => { return new MemoryStream(image.data); });
  439.                             profileLayout.HeightRequest = profileImageBackground.Width * 0.565656;
  440.                             if (!image.Horizontal) {
  441.                                 profileImage.Rotation = 90;
  442.                                 profileImageBackground.Rotation = 90;
  443.                             }
  444.                             else
  445.                             {
  446.                                 profileImage.Rotation = 0;
  447.                                 profileImageBackground.Rotation = 0;
  448.                             }
  449.                     }
  450.                 });
  451.             }
  452.         }
  453.  
  454.         public void LoginPage()
  455.         {
  456.             if (selected != null)
  457.             {
  458.                 selected.SetColors(false);
  459.             }
  460.  
  461.             // Select new
  462.             selected = (listView.ItemsSource as IEnumerable<ListItemModel>).First();
  463.             selected.SetColors(true);
  464.             listView.SelectedItem = selected;
  465.             this.Detail = detailPage;
  466.             Navigation.PushAsync(new LoginPage(detailPage.EnableService));
  467.         }
  468.  
  469.         public void Logout()
  470.         {
  471.             if (ProfileController.Instance.Logined)
  472.             {
  473.                 detailPage.DisableView();
  474.                 ProfileController.Instance.SessionToken = JsonConvert.SerializeObject(new LoginToken());
  475.                 ProfileController.Instance.Logout();
  476.                 ProfileController.Instance.Logined = false;
  477.                 profileImageBackground.Source = null;
  478.                 profileImageBackground.Source = ImageSource.FromFile("defaultBack.png");
  479.                 profileImage.Source = null;
  480.                 profileImage.Source = ImageSource.FromFile("photoIcon.png");
  481.                 header.Text = "Вы не авторизованы";
  482.                 this.Detail = detailPage;
  483.             }
  484.         }
  485.  
  486.         public static async Task ConnectionLoop()
  487.         {
  488.             while (!CrossConnectivity.Current.IsConnected)
  489.             {
  490.                 await Task.Delay(1500);
  491.             }
  492.         }
  493.     }
  494. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement